Skip to content

Commit

Permalink
Do not update submitted rates, only connect to draft contract revision.
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLin0991 committed Sep 12, 2023
1 parent e50bf1c commit 7f6b00b
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContractFormDataType>

Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -271,7 +293,7 @@ async function updateDraftContract(
rateRevision.actuaryCommunicationPreference,
},
},
},
} : undefined,
draftContractRevisions: {
connect: {
id: currentRev.id,
Expand Down

0 comments on commit 7f6b00b

Please sign in to comment.