mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-24 11:39:22 -05:00
00beeb0501
Co-authored-by: Matti Nannt <mail@matthiasnannt.com> Co-authored-by: Shubham Palriwala <spalriwalau@gmail.com>
44 lines
1.1 KiB
TypeScript
44 lines
1.1 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 answer.toString();
|
|
}
|
|
}
|
|
};
|
|
|
|
questionResponseMapping.push({
|
|
question: getLocalizedValue(question.headline, "default"),
|
|
answer: getAnswer(),
|
|
type: question.type,
|
|
});
|
|
}
|
|
|
|
return questionResponseMapping;
|
|
};
|