From 17444988b5c468099b9948527fddd40d56301df5 Mon Sep 17 00:00:00 2001 From: Hana Worku Date: Fri, 3 Nov 2023 11:16:25 -0500 Subject: [PATCH] Add as dataMigration --- .../20231026124562_clean_empty_rates.ts | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 services/app-api/src/dataMigrations/migrations/20231026124562_clean_empty_rates.ts diff --git a/services/app-api/src/dataMigrations/migrations/20231026124562_clean_empty_rates.ts b/services/app-api/src/dataMigrations/migrations/20231026124562_clean_empty_rates.ts new file mode 100644 index 0000000000..80e59423c5 --- /dev/null +++ b/services/app-api/src/dataMigrations/migrations/20231026124562_clean_empty_rates.ts @@ -0,0 +1,42 @@ +import type { PrismaTransactionType } from '../../postgres/prismaTypes' + +export const migrateToDeleteContractOnlyRates = async ( + client: PrismaTransactionType +): Promise => { + try { + // find those empty submitted rates by their revision + const badRateRevisions = await client.rateRevisionTable.findMany({ + where: { + rateType: undefined, + submitInfo: { + isNot: null, + }, + unlockInfo: { + is: null, // has to be submitted revision that is not yet unlocked + }, + }, + }) + console.info( + `Found ${badRateRevisions.length} rate revisions to delete` + ) + + await client.rateRevisionTable.deleteMany({ + where: { + id: { + in: badRateRevisions.map((rev) => rev.id), + }, + }, + }) + + // delete any empty rates + await client.rateTable.deleteMany({ + where: { + revisions: { + none: {}, + }, + }, + }) + } catch (error) { + console.error(`Data migration ERROR: ${error}`) + } +}