Files
formbricks/packages/lib/responses.ts
T
Dhruwang Jariwala 00beeb0501 fix: recall parsing in emails and improvements in response pipeline (#2187)
Co-authored-by: Matti Nannt <mail@matthiasnannt.com>
Co-authored-by: Shubham Palriwala <spalriwalau@gmail.com>
2024-03-21 13:03:13 +00:00

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;
};