added data-migration2

This commit is contained in:
Dhruwang
2024-03-20 11:48:39 +05:30
parent f27f55a655
commit c148e3bd74
4 changed files with 61 additions and 26 deletions
@@ -0,0 +1,49 @@
// migration script to translate surveys where thankYouCard buttonLabel is a string or question subheaders are strings
import { PrismaClient } from "@prisma/client";
import { hasStringSubheaders, translateSurvey } from "./lib/i18n";
const prisma = new PrismaClient();
async function main() {
await prisma.$transaction(
async (tx) => {
// Translate Surveys
const surveys = await tx.survey.findMany({
select: {
id: true,
questions: true,
thankYouCard: true,
welcomeCard: true,
},
});
if (!surveys) {
// stop the migration if there are no surveys
return;
}
for (const survey of surveys) {
if (typeof survey.thankYouCard.buttonLabel === "string" || hasStringSubheaders(survey.questions)) {
const translatedSurvey = translateSurvey(survey, []);
// Save the translated survey
await tx.survey.update({
where: { id: survey.id },
data: { ...translatedSurvey },
});
}
}
},
{
timeout: 50000,
}
);
}
main()
.catch(async (e) => {
console.error(e);
process.exit(1);
})
.finally(async () => await prisma.$disconnect());
@@ -13,6 +13,7 @@ import {
TSurveyMultipleChoiceSingleQuestion,
TSurveyNPSQuestion,
TSurveyOpenTextQuestion,
TSurveyQuestions,
TSurveyRatingQuestion,
TSurveyThankYouCard,
TSurveyWelcomeCard,
@@ -266,3 +267,12 @@ export const translateSurvey = (
thankYouCard: translatedThankYouCard,
});
};
export const hasStringSubheaders = (questions: TSurveyQuestions): boolean => {
for (const question of questions) {
if (typeof question.subheader === "string") {
return true;
}
}
return false;
};
+2 -1
View File
@@ -24,7 +24,8 @@
"post-install": "pnpm generate",
"predev": "pnpm generate",
"data-migration:v1.6": "ts-node ./migrations/20240207041922_advanced_targeting/data-migration.ts",
"data-migration:mls": "ts-node ./migrations/20240318050527_add_languages_and_survey_languages/data-migration.ts"
"data-migration:mls": "ts-node ./migrations/20240318050527_add_languages_and_survey_languages/data-migration.ts",
"data-migration2:mls": "ts-node ./migrations/20240318050527_add_languages_and_survey_languages/data-migration2.ts"
},
"dependencies": {
"@prisma/client": "^5.11.0",
-25
View File
@@ -19,7 +19,6 @@ import { ITEMS_PER_PAGE, SERVICES_REVALIDATION_INTERVAL } from "../constants";
import { displayCache } from "../display/cache";
import { getDisplaysByPersonId } from "../display/service";
import { reverseTranslateSurvey } from "../i18n/reverseTranslation";
import { translateSurvey } from "../i18n/utils";
import { personCache } from "../person/cache";
import { getPerson } from "../person/service";
import { productCache } from "../product/cache";
@@ -366,30 +365,6 @@ export const transformToLegacySurvey = async (
return formatDateFields(transformedSurvey, ZLegacySurvey);
};
export const transformSurveyToSpecificLanguage = async (
survey: TSurvey,
targetLanguageCode?: string
): Promise<TSurvey> => {
// if target language code is not available, it will be transformed to default language
const transformedSurvey = await unstable_cache(
async () => {
if (!survey.languages || survey.languages.length === 0) {
//survey do not have any translations
return survey;
}
return translateSurvey(survey, [], targetLanguageCode);
},
[`transformSurveyToSpecificLanguage-${survey}-${targetLanguageCode}`],
{
tags: [surveyCache.tag.byId(survey.id)],
revalidate: SERVICES_REVALIDATION_INTERVAL,
}
)();
// since the unstable_cache function does not support deserialization of dates, we need to manually deserialize them
// https://github.com/vercel/next.js/issues/51613
return formatDateFields(transformedSurvey, ZSurvey);
};
export const getSurveyCount = async (environmentId: string): Promise<number> => {
const count = await unstable_cache(
async () => {