From 7f6b00ba6993064246a1c1d12a0db492df2f9583 Mon Sep 17 00:00:00 2001 From: Jason Lin Date: Tue, 12 Sep 2023 16:41:00 -0400 Subject: [PATCH] Do not update submitted rates, only connect to draft contract revision. --- .../updateDraftContract.test.ts | 100 ++++++++++++++++++ .../contractAndRates/updateDraftContract.ts | 54 +++++++--- 2 files changed, 138 insertions(+), 16 deletions(-) diff --git a/services/app-api/src/postgres/contractAndRates/updateDraftContract.test.ts b/services/app-api/src/postgres/contractAndRates/updateDraftContract.test.ts index 1751abda3c..881d86619e 100644 --- a/services/app-api/src/postgres/contractAndRates/updateDraftContract.test.ts +++ b/services/app-api/src/postgres/contractAndRates/updateDraftContract.test.ts @@ -10,6 +10,8 @@ import { createInsertRateData } from '../../testHelpers/contractAndRates/rateHel import { v4 as uuidv4 } from 'uuid' import type { RateFormEditable } from './updateDraftRate' import { insertDraftRate } from './insertRate' +import { submitRate } from './submitRate'; +import {unlockRate} from './unlockRate'; describe('updateDraftContract', () => { afterEach(() => { @@ -616,4 +618,102 @@ describe('updateDraftContract', () => { 2 ) }) + + it('errors when trying to update a submitted rate', async () => { + const client = await sharedTestPrismaClient() + + const stateUser = await client.user.create({ + data: { + id: uuidv4(), + givenName: 'Aang', + familyName: 'Avatar', + email: 'aang@example.com', + role: 'STATE_USER', + stateCode: 'NM', + }, + }) + + const draftContractFormData = createInsertContractData({}) + const draftContract = must( + await insertDraftContract(client, draftContractFormData) + ) + + // new rate + const newRate = createInsertRateData({ + id: uuidv4(), + rateType: 'NEW', + }) + + // Update contract with new rates + const updatedContractWithNewRates = must( + await updateDraftContract(client, { + contractID: draftContract.id, + formData: {}, + rateFormDatas: [newRate], + }) + ) + + if (!updatedContractWithNewRates.draftRevision) { + throw new Error( + 'Unexpected error: draft rate is missing a draftRevision.' + ) + } + + const newlyCreatedRates = + updatedContractWithNewRates.draftRevision.rateRevisions + + // lets make sure we have rate ids + if (!newlyCreatedRates[0].formData.rateID) { + throw new Error( + 'Unexpected error. Rate revisions did not contain rate IDs' + ) + } + + // expect 1 rates + expect(newlyCreatedRates).toHaveLength(1) + + // submit rate + const submittedExistingRate = must( + await submitRate( + client, + newlyCreatedRates[0].formData.rateID, + stateUser.id, + 'Rate submit' + ) + ) + + if (!submittedExistingRate.revisions[0].formData) { + throw new Error( + 'Unexpected error. Rate revisions did not contain rate IDs' + ) + } + + // Update contract with submitted rate and try to update the submitted rate revision + const attemptToUpdateSubmittedRate = must(await updateDraftContract( + client, + { + contractID: updatedContractWithNewRates.id, + formData: {}, + rateFormDatas: [ + // attempt to update the revision data of a submitted rate. + { + ...submittedExistingRate.revisions[0].formData, + rateType: 'AMENDMENT', + }, + ], + } + )) + + if (!attemptToUpdateSubmittedRate.draftRevision) { + throw new Error( + 'Unexpected error: draft rate is missing a draftRevision.' + ) + } + + // We still expect 1 connected rate + expect(attemptToUpdateSubmittedRate.draftRevision.rateRevisions).toHaveLength(1) + + // We expect the rate type to not be changed to 'AMENDMENT' + expect(attemptToUpdateSubmittedRate.draftRevision.rateRevisions[0].formData.rateType).toBe('NEW') + }) }) diff --git a/services/app-api/src/postgres/contractAndRates/updateDraftContract.ts b/services/app-api/src/postgres/contractAndRates/updateDraftContract.ts index 8118f3c8d1..4c5df62020 100644 --- a/services/app-api/src/postgres/contractAndRates/updateDraftContract.ts +++ b/services/app-api/src/postgres/contractAndRates/updateDraftContract.ts @@ -12,6 +12,7 @@ import { includeDraftRates } from './prismaDraftContractHelpers' import { rateRevisionToDomainModel } from './prismaSharedContractRateHelpers' import type { RateFormEditable } from './updateDraftRate' import { isEqualData } from '../../resolvers/healthPlanPackage/contractAndRates/resolverHelpers' +import {includeFullRate} from './prismaSubmittedRateHelpers'; type ContractFormEditable = Partial @@ -151,20 +152,35 @@ async function updateDraftContract( if (updateRates) { for (const rateRevision of updateRates.upsertRates) { - // Check if the rate revision exists - // - We don't know if the rate exists in the DB we just know it's not connected to the contract. - // - toProtoBuffer gives every rate revision a UUID if there isn't one, so we cannot rely on revision - // id to know if it exists in the DB. - const currentRateRev = rateRevision.id - ? await tx.rateRevisionTable.findFirst({ - where: { - id: rateRevision.id, - }, - }) + // Check if the rate exists + // - We don't know if the rate revision exists in the DB we just know it's not connected to the contract. + // - toProtoBuffer gives every rate revision a UUID if there isn't one, so we cannot rely on revision id. + // - We can use this revision id to check if a rate and revision exists. + + // Find the rate of the revision with only one draft revision + const currentRate = rateRevision.id + ? await tx.rateTable.findFirst({ + where: { + revisions: { + some: { + id: rateRevision.id + } + } + }, + include: { + // include the single most recent revision that is not submitted + revisions: { + where: { + submitInfoID: null + }, + take: 1 + } + }, + }) : undefined - // If rate revision does not exist, we need to create a new rate. - if (!currentRateRev) { + // If rate does not exist, we need to create a new rate. + if (!currentRate) { const { latestStateRateCertNumber } = await client.state.update({ data: { @@ -223,15 +239,21 @@ async function updateDraftContract( }, }) } else { + // If the current rate has no draft revisions, based form our find with revision with no submitInfoID + // then this is a submitted rate + const isSubmitted = currentRate.revisions.length === 0 + await tx.rateTable.update({ where: { - id: currentRateRev.rateID, + id: currentRate.id, }, data: { - revisions: { + // if rate is not submitted, we update the revision data, otherwise we only make the + // connection to the draft contract revision. + revisions: !isSubmitted ? { update: { where: { - id: currentRateRev.id, + id: rateRevision.id, }, data: { rateType: rateRevision.rateType, @@ -271,7 +293,7 @@ async function updateDraftContract( rateRevision.actuaryCommunicationPreference, }, }, - }, + } : undefined, draftContractRevisions: { connect: { id: currentRev.id,