From e6e139a82dd001558f1b91a78a866daadc233c3f Mon Sep 17 00:00:00 2001 From: MacRae Linton Date: Wed, 6 Mar 2024 17:58:23 -0800 Subject: [PATCH] add tx for updates --- .../updateDraftContractRates.ts | 194 +++++++++--------- 1 file changed, 99 insertions(+), 95 deletions(-) diff --git a/services/app-api/src/postgres/contractAndRates/updateDraftContractRates.ts b/services/app-api/src/postgres/contractAndRates/updateDraftContractRates.ts index 43a67b8b62..d689d88b44 100644 --- a/services/app-api/src/postgres/contractAndRates/updateDraftContractRates.ts +++ b/services/app-api/src/postgres/contractAndRates/updateDraftContractRates.ts @@ -1,7 +1,7 @@ +import type { PrismaClient } from '@prisma/client' import type { ContractType } from '../../domain-models/contractAndRates' import { NotFoundError } from '../postgresErrors' import { emptify, nullify } from '../prismaDomainAdaptors' -import type { PrismaTransactionType } from '../prismaTypes' import { findContractWithHistory } from './findContractWithHistory' import type { RateFormEditable } from './updateDraftRate' @@ -134,120 +134,124 @@ function prismaUpdateRateFormDataFromDomain(rateFormData: RateFormEditable) { } async function updateDraftContractRates( - client: PrismaTransactionType, + client: PrismaClient, args: UpdateDraftContractRatesArgsType ): Promise { try { - // for now, get the latest contract revision, eventually we'll have rate revisions directly on this - const contract = await client.contractTable.findUnique({ - where: { - id: args.contractID, - }, - include: { - revisions: { - take: 1, - orderBy: { - createdAt: 'desc', + return await client.$transaction(async (tx) => { + // for now, get the latest contract revision, eventually we'll have rate revisions directly on this + const contract = await tx.contractTable.findUnique({ + where: { + id: args.contractID, + }, + include: { + revisions: { + take: 1, + orderBy: { + createdAt: 'desc', + }, }, }, - }, - }) + }) - if (!contract) { - return new NotFoundError( - 'contract not found with ID: ' + args.contractID - ) - } + if (!contract) { + return new NotFoundError( + 'contract not found with ID: ' + args.contractID + ) + } - const draftRevision = contract.revisions[0] - if (!draftRevision) { - return new Error( - 'PROGRAMMER ERROR: This draft contract has no draft revision' - ) - } + const draftRevision = contract.revisions[0] + if (!draftRevision) { + return new Error( + 'PROGRAMMER ERROR: This draft contract has no draft revision' + ) + } - // figure out the rate number range for created rates. - const state = await client.state.findUnique({ - where: { stateCode: contract.stateCode }, - }) + // figure out the rate number range for created rates. + const state = await tx.state.findUnique({ + where: { stateCode: contract.stateCode }, + }) - if (!state) { - return new Error( - 'PROGRAMER ERROR: No state found with code: ' + - contract.stateCode - ) - } + if (!state) { + return new Error( + 'PROGRAMER ERROR: No state found with code: ' + + contract.stateCode + ) + } - let nextRateNumber = state.latestStateRateCertNumber + let nextRateNumber = state.latestStateRateCertNumber - // create new rates with new revisions - const ratesToCreate = args.rateUpdates.create.map((ru) => { - const rateFormDataem = ru.formData - const thisRateNumber = nextRateNumber - nextRateNumber++ - return { - stateCode: contract.stateCode, - stateNumber: thisRateNumber, - revisions: { - create: prismaRateCreateFormDataFromDomain(rateFormDataem), - }, - } - }) + // create new rates with new revisions + const ratesToCreate = args.rateUpdates.create.map((ru) => { + const rateFormDataem = ru.formData + const thisRateNumber = nextRateNumber + nextRateNumber++ + return { + stateCode: contract.stateCode, + stateNumber: thisRateNumber, + revisions: { + create: prismaRateCreateFormDataFromDomain( + rateFormDataem + ), + }, + } + }) - // to delete draft rates, we need to delete their revisions first - await client.rateRevisionTable.deleteMany({ - where: { - rateID: { - in: args.rateUpdates.delete.map((ru) => ru.rateID), + // to delete draft rates, we need to delete their revisions first + await tx.rateRevisionTable.deleteMany({ + where: { + rateID: { + in: args.rateUpdates.delete.map((ru) => ru.rateID), + }, }, - }, - }) + }) - // create new rates and link and unlink others - await client.contractRevisionTable.update({ - where: { id: draftRevision.id }, - data: { - draftRates: { - create: ratesToCreate, - connect: args.rateUpdates.link.map((ru) => ({ - id: ru.rateID, - })), - disconnect: args.rateUpdates.unlink.map((ru) => ({ - id: ru.rateID, - })), - delete: args.rateUpdates.delete.map((ru) => ({ - id: ru.rateID, - })), + // create new rates and link and unlink others + await tx.contractRevisionTable.update({ + where: { id: draftRevision.id }, + data: { + draftRates: { + create: ratesToCreate, + connect: args.rateUpdates.link.map((ru) => ({ + id: ru.rateID, + })), + disconnect: args.rateUpdates.unlink.map((ru) => ({ + id: ru.rateID, + })), + delete: args.rateUpdates.delete.map((ru) => ({ + id: ru.rateID, + })), + }, }, - }, - include: { - draftRates: true, - }, - }) - - // update existing rates - for (const ru of args.rateUpdates.update) { - const draftRev = await client.rateRevisionTable.findFirst({ - where: { - rateID: ru.rateID, - submitInfoID: null, + include: { + draftRates: true, }, }) - if (!draftRev) { - return new Error( - 'attempting to update a rate that is not editable: ' + - ru.rateID - ) - } + // update existing rates + for (const ru of args.rateUpdates.update) { + const draftRev = await tx.rateRevisionTable.findFirst({ + where: { + rateID: ru.rateID, + submitInfoID: null, + }, + }) - await client.rateRevisionTable.update({ - where: { id: draftRev.id }, - data: prismaUpdateRateFormDataFromDomain(ru.formData), - }) - } + if (!draftRev) { + return new Error( + 'attempting to update a rate that is not editable: ' + + ru.rateID + ) + } + + await tx.rateRevisionTable.update({ + where: { id: draftRev.id }, + data: prismaUpdateRateFormDataFromDomain(ru.formData), + }) + } - return findContractWithHistory(client, args.contractID) + return findContractWithHistory(tx, args.contractID) + }) } catch (err) { console.error('PRISMA ERR', err) return err