mirror of
https://github.com/formbricks/formbricks.git
synced 2026-01-30 10:49:55 -06:00
Co-authored-by: Johannes <johannes@formbricks.com> Co-authored-by: Johannes <72809645+jobenjada@users.noreply.github.com> Co-authored-by: Dhruwang Jariwala <67850763+Dhruwang@users.noreply.github.com> Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com> Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
import { TResponse } from "@formbricks/types/responses";
|
|
import { TSurvey, TSurveyQuestionType } from "@formbricks/types/surveys";
|
|
|
|
import { getLocalizedValue } from "./i18n/utils";
|
|
|
|
export const getQuestionResponseMapping = (
|
|
survey: TSurvey,
|
|
response: TResponse
|
|
): { question: string; answer: string | string[]; type: TSurveyQuestionType }[] => {
|
|
const questionResponseMapping: {
|
|
question: string;
|
|
answer: string | string[];
|
|
type: TSurveyQuestionType;
|
|
}[] = [];
|
|
|
|
for (const question of survey.questions) {
|
|
const answer = response.data[question.id];
|
|
|
|
const getAnswer = () => {
|
|
if (!answer) return "";
|
|
else {
|
|
if (question.type === "fileUpload") {
|
|
if (typeof answer === "string") {
|
|
return [answer];
|
|
} else {
|
|
return answer as string[];
|
|
// as array
|
|
}
|
|
} else {
|
|
return processResponseData(answer);
|
|
}
|
|
}
|
|
};
|
|
|
|
questionResponseMapping.push({
|
|
question: getLocalizedValue(question.headline, "default"),
|
|
answer: getAnswer(),
|
|
type: question.type,
|
|
});
|
|
}
|
|
|
|
return questionResponseMapping;
|
|
};
|
|
|
|
export const processResponseData = (
|
|
responseData: string | number | string[] | Record<string, string>
|
|
): string => {
|
|
if (!responseData) return "";
|
|
|
|
switch (typeof responseData) {
|
|
case "string":
|
|
return responseData;
|
|
|
|
case "number":
|
|
return responseData.toString();
|
|
|
|
case "object":
|
|
if (Array.isArray(responseData)) {
|
|
responseData = responseData
|
|
.filter((item) => item !== null && item !== undefined && item !== "")
|
|
.join(", ");
|
|
return responseData;
|
|
} else {
|
|
const formattedString = Object.entries(responseData)
|
|
.map(([key, value]) => `${key}: ${value}`)
|
|
.join("\n");
|
|
return formattedString;
|
|
}
|
|
|
|
default:
|
|
return "";
|
|
}
|
|
};
|