mirror of
https://github.com/formbricks/formbricks.git
synced 2026-05-05 19:30:48 -05:00
fix: Let CTA and consent question store nothing if dismissed (#2977)
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
This commit is contained in:
+105
@@ -0,0 +1,105 @@
|
||||
/* eslint-disable no-console -- logging is allowed in migration scripts */
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import { type TSurveyQuestion, TSurveyQuestionTypeEnum } from "@formbricks/types/surveys/types";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function runMigration(): Promise<void> {
|
||||
await prisma.$transaction(
|
||||
async (tx) => {
|
||||
const startTime = Date.now();
|
||||
console.log("Starting data migration...");
|
||||
|
||||
// Get all surveys with status not in draft and questions containing cta or consent
|
||||
const relevantSurveys = await tx.survey.findMany({
|
||||
where: {
|
||||
status: {
|
||||
notIn: ["draft"],
|
||||
},
|
||||
OR: [
|
||||
{
|
||||
questions: {
|
||||
array_contains: [{ type: "cta" }],
|
||||
},
|
||||
},
|
||||
{
|
||||
questions: {
|
||||
array_contains: [{ type: "consent" }],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
questions: true,
|
||||
},
|
||||
});
|
||||
|
||||
// Process each survey
|
||||
const migrationPromises = relevantSurveys.map(async (survey) => {
|
||||
const ctaOrConsentQuestionIds = survey.questions
|
||||
.filter(
|
||||
(ques: TSurveyQuestion) =>
|
||||
ques.type === TSurveyQuestionTypeEnum.CTA || ques.type === TSurveyQuestionTypeEnum.Consent
|
||||
)
|
||||
.map((ques: TSurveyQuestion) => ques.id);
|
||||
|
||||
// Get all responses for this survey
|
||||
const responses = await prisma.response.findMany({
|
||||
where: {
|
||||
surveyId: survey.id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
data: true,
|
||||
},
|
||||
});
|
||||
|
||||
// Update each response
|
||||
return responses.map(async (response) => {
|
||||
const updatedData = { ...response.data };
|
||||
|
||||
ctaOrConsentQuestionIds.forEach((questionId: string) => {
|
||||
if (updatedData[questionId] && updatedData[questionId] === "dismissed") {
|
||||
updatedData[questionId] = "";
|
||||
}
|
||||
});
|
||||
|
||||
return prisma.response.update({
|
||||
where: { id: response.id },
|
||||
data: { data: updatedData },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const migrationPromisesFlat = migrationPromises.flat();
|
||||
await Promise.all(migrationPromisesFlat);
|
||||
|
||||
const endTime = Date.now();
|
||||
console.log(`Data migration completed. Total time: ${((endTime - startTime) / 1000).toString()}s`);
|
||||
},
|
||||
{
|
||||
timeout: 180000, // 3 minutes
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function handleError(error: unknown): void {
|
||||
console.error("An error occurred during migration:", error);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
function handleDisconnectError(): void {
|
||||
console.error("Failed to disconnect Prisma client");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
function main(): void {
|
||||
runMigration()
|
||||
.catch(handleError)
|
||||
.finally(() => {
|
||||
prisma.$disconnect().catch(handleDisconnectError);
|
||||
});
|
||||
}
|
||||
|
||||
main();
|
||||
@@ -45,7 +45,8 @@
|
||||
"data-migration:multiple-endings": "ts-node ./data-migrations/20240801120500_thankYouCard_to_endings/data-migration.ts",
|
||||
"data-migration:simplified-email-verification": "ts-node ./data-migrations/20240726124100_replace_verifyEmail_with_isVerifyEmailEnabled/data-migration.ts",
|
||||
"data-migration:fix-logic-end-destination": "ts-node ./data-migrations/20240806120500_fix-logic-end-destination/data-migration.ts",
|
||||
"data-migration:v2.4": "pnpm data-migration:segments-cleanup && pnpm data-migration:multiple-endings && pnpm data-migration:simplified-email-verification && pnpm data-migration:fix-logic-end-destination"
|
||||
"data-migration:v2.4": "pnpm data-migration:segments-cleanup && pnpm data-migration:multiple-endings && pnpm data-migration:simplified-email-verification && pnpm data-migration:fix-logic-end-destination",
|
||||
"data-migration:remove-dismissed-value-inconsistency": "ts-node ./data-migrations/20240807120500_cta_consent_dismissed_inconsistency/data-migration.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@prisma/client": "^5.18.0",
|
||||
|
||||
Reference in New Issue
Block a user