Skip to content

Commit

Permalink
MR-3545, MR-2370: Fix change history, add cypress tests, and turn on …
Browse files Browse the repository at this point in the history
…remaining API Unit tests (#1959)

* populating

* keep incoming

* remove default name. not sure why it's here?

* use the right deploy script

* move the migration into a func

* return errors in prepopulate updateinfo

* use error returns on insertContractId

* return the error. no throws here for try/catch

* clean up error messaging

* move contract ops to migrateContract()

* don't forget the associations error handling

* the migrate results type

* more tests

* make it a helper func

* rate revision test

* check that we can get the migrated revision back

* record the exception no need to return a packageId

* fix test

* more logging

* clearAllMocks

* doesnt' test anything now

* some debug

* keep track of migrations that did not complete

* crashing on the user find

* log out some stats

* need to continue

* just use the client we already get

* remove some debug

* all the contract ammendment info

* get unlockInfoID

* rate revision as well

* testing file

* fix up test some more

* Adding test fixes and fixes from test

* fixes in migrate. add DB reset at init.

* use the prisma types in the test

* finish adding contract docs

* adds the rate docs

* add another fake doc

* contract/rate doc migration moved.

* move join table work to the create phase

* cleanup all the things

* format on save?

* enable remaining jest tests

* fix up compare

* Enable `rates-db-refactor` and `supporting-docs-by-rate` flag for cypress tests.

* Skip Q&A tests, resolver not modified for refactor yet.

* logging data objects out for compare

* refactor comparison

* more cleaning on test

* fix the map to flatmap

* fix up which we compare with

* error check

* Add extra test for change history order and count.

* Remove rate history from contract history.

* Add a check for which query to wait for.

* Apply suggestions from code review

Co-authored-by: Jason Lin <98117700+JasonLin0991@users.noreply.github.com>

* some fixes to the migrator

* fix ordering of contract revisions

* update yarn.lock

* Fix contractWithHistoryToDomainModel.

* debug helper.

* Update tests

* Longer timeout looking for unlock button.

* Update services/app-api/src/resolvers/healthPlanPackage/indexHealthPlanPackages.test.ts

* Update services/app-api/src/resolvers/healthPlanPackage/indexHealthPlanPackages.test.ts

* cypress re-run

* add position to actuary contacts

* Increase timeouts for more docs.

* tests are suspiciously fixed

* Skip tests for rate history.

* test and fix how update contract with rates reads rateInfo.id

* update migrators handling of submittedAt and unlocked rates

* pass tx into findContractWithHistory. variable name changes for clarity.

* Change setting id from revision id to rate id.

* Fix tests for rate formData.id change.

* connect related contract

* fix tests proactively

---------

Co-authored-by: Mazdak Atighi <maz@truss.works>
Co-authored-by: Mojo Talantikite <mojo.talantikite@gmail.com>
Co-authored-by: MacRae Linton <macrael@truss.works>
Co-authored-by: MacRae Linton <55759+macrael@users.noreply.github.com>
  • Loading branch information
5 people authored and haworku committed Oct 16, 2023
1 parent 7153a99 commit 00e00cb
Show file tree
Hide file tree
Showing 23 changed files with 1,649 additions and 939 deletions.
439 changes: 437 additions & 2 deletions services/app-api/src/handlers/proto_to_db.test.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion services/app-api/src/handlers/proto_to_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export const main: Handler = async (): Promise<APIGatewayProxyResultV2> => {
}

console.info(
`Migrated HealthPlanRevision ${revision.pkgID} successfully...`
`Migrated HealthPlanRevision ${revision.id} successfully...`
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { findRateWithHistory } from './findRateWithHistory'
import { must, createInsertContractData } from '../../testHelpers'
import { createInsertRateData } from '../../testHelpers/contractAndRates/rateHelpers'

describe('findContract', () => {
// TODO: Enable these tests again after reimplementing rate change history that was in contractWithHistoryToDomainModel
describe.skip('findContract', () => {
it('finds a stripped down contract with history', async () => {
const client = await sharedTestPrismaClient()

Expand Down Expand Up @@ -240,7 +241,7 @@ describe('findContract', () => {
(rateRevision) =>
rateRevision.formData.rateID !== rate1.id &&
rateRevision.formData.rateID !== rate2.id
),
).map(rate => rate.formData),
})
)
must(
Expand Down Expand Up @@ -549,7 +550,7 @@ describe('findContract', () => {
(rateRevision) =>
rateRevision.formData.rateID !== rate1.id &&
rateRevision.formData.rateID !== rate2.id
),
).map(rate => rate.formData),
})
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import { findContractWithHistory } from './findContractWithHistory'
import type { DraftContractType } from '../../domain-models/contractAndRates/contractTypes'

describe('findRate', () => {
it('finds a stripped down rate with history', async () => {
// TODO: Enable this tests again after reimplementing rate change history that was in contractWithHistoryToDomainModel
it.skip('finds a stripped down rate with history', async () => {
const client = await sharedTestPrismaClient()

const stateUser = await client.user.create({
Expand Down Expand Up @@ -561,6 +562,7 @@ describe('findRate', () => {
unlockReason: 'unlocking A.1',
})
)
// Save update to get latest rate data
const updatedDraftContractA = must(
await updateDraftContractWithRates(client, {
contractID: contractA.id,
Expand All @@ -583,12 +585,12 @@ describe('findRate', () => {
updatedDraftContractA.draftRevision?.rateRevisions.filter(
(rateRevision) =>
rateRevision.formData.rateID !== rate1.id
),
).map(rate => rate.formData),
})
)
must(
await submitContract(client, {
contractID: contractA.id,
contractID: updatedDraftContractA.id,
submittedByUserID: stateUser.id,
submitReason: 'Submitting A.2',
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,56 @@ function contractWithHistoryToDomainModel(

allRevisionSets.push(initialEntry)

let lastEntry = initialEntry
// Now we construct a revision history for each change in rate revisions.
// go through every rate revision in the join table in time order and construct a revisionSet
// with (or without) the new rate revision in it.
// This code below was used to construct rate change history and add into our contract revision history by pushing
// new contract revisions into the array. This however caused issues with the frontend apollo cache because we
// used duplicate contract revision ids to create new revisions for rate changes.
// For now, we are commenting out the code until we are ready for this feature and leaving it intact.

// let lastEntry = initialEntry
// // Now we construct a revision history for each change in rate revisions.
// // go through every rate revision in the join table in time order and construct a revisionSet
// // with (or without) the new rate revision in it.
// for (const rateRev of contractRev.rateRevisions) {
// if (!rateRev.rateRevision.submitInfo) {
// return new Error(
// 'Programming Error: a contract is associated with an unsubmitted rate'
// )
// }
//
// // if it's from before this contract was submitted, it's there at the beginning.
// if (
// rateRev.rateRevision.submitInfo.updatedAt <=
// contractRev.submitInfo.updatedAt
// ) {
// if (!rateRev.isRemoval) {
// initialEntry.rateRevisions.push(rateRev.rateRevision)
// }
// } else {
// // if after, then it's always a new entry in the list
// let lastRates = [...lastEntry.rateRevisions]
//
// // take out the previous rate revision this revision supersedes
// lastRates = lastRates.filter(
// (r) => r.rateID !== rateRev.rateRevision.rateID
// )
// // an isRemoval entry indicates that this rate was removed from this contract.
// if (!rateRev.isRemoval) {
// lastRates.push(rateRev.rateRevision)
// }
//
// const newRev: ContractRevisionSet = {
// contractRev,
// submitInfo: rateRev.rateRevision.submitInfo,
// unlockInfo: rateRev.rateRevision.unlockInfo || undefined,
// rateRevisions: lastRates,
// }
//
// lastEntry = newRev
// allRevisionSets.push(newRev)
// }
// }

// Basically the same as above, except we do not create new contract revisions for rate changes.
for (const rateRev of contractRev.rateRevisions) {
if (!rateRev.rateRevision.submitInfo) {
return new Error(
Expand All @@ -167,7 +213,7 @@ function contractWithHistoryToDomainModel(
}
} else {
// if after, then it's always a new entry in the list
let lastRates = [...lastEntry.rateRevisions]
let lastRates = [...initialEntry.rateRevisions]

// take out the previous rate revision this revision supersedes
lastRates = lastRates.filter(
Expand All @@ -178,15 +224,7 @@ function contractWithHistoryToDomainModel(
lastRates.push(rateRev.rateRevision)
}

const newRev: ContractRevisionSet = {
contractRev,
submitInfo: rateRev.rateRevision.submitInfo,
unlockInfo: rateRev.rateRevision.unlockInfo || undefined,
rateRevisions: lastRates,
}

lastEntry = newRev
allRevisionSets.push(newRev)
initialEntry.rateRevisions = lastRates
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function rateFormDataToDomainModel(
}

return {
id: rateRevision.id,
id: rateRevision.rateID,
rateID: rateRevision.rateID,
rateType: rateRevision.rateType ?? undefined,
rateCapitationType: rateRevision.rateCapitationType ?? undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,21 @@ async function migrateContractRevision(
}

// Add the unlocked info to the table if it exists
if (formData.status === 'SUBMITTED' && revision.unlockedBy) {
if (
revision.unlockedAt &&
revision.unlockedBy &&
revision.unlockedReason
) {
const user = await client.user.findFirst({
where: { email: revision.unlockedBy },
})

if (user) {
createDataObject.unlockInfo = {
create: {
updatedAt: revision.unlockedAt ?? new Date(),
updatedAt: revision.unlockedAt,
updatedByID: user.id,
updatedReason:
revision.unlockedReason ??
'Migrated from previous system',
updatedReason: revision.unlockedReason,
},
}
} else {
Expand All @@ -129,14 +131,14 @@ async function migrateContractRevision(
}

// add the submit info to the table if it exists
if (formData.status === 'SUBMITTED' && revision.submittedBy) {
if (revision.submittedAt && revision.submittedBy) {
const user = await client.user.findFirst({
where: { email: revision.submittedBy },
})
if (user) {
createDataObject.submitInfo = {
create: {
updatedAt: formData.updatedAt,
updatedAt: revision.submittedAt,
updatedByID: user.id,
updatedReason:
revision.submittedReason ??
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,33 @@ export async function migrateRateInfo(
create: rateRevDocsArray,
}

// rate revisions on contract revisions join table
dataToCopy.contractRevisions = {
create: {
contractRevisionID: contractRevision.id,
validAfter: new Date(),
},
// Connect to shared contracts
if (rateInfo.packagesWithSharedRateCerts) {
const sharedContractIDs = rateInfo.packagesWithSharedRateCerts.map(
(pkg) => pkg.packageId
)
dataToCopy.contractsWithSharedRateRevision = {
connect: sharedContractIDs.map((cid) => ({ id: cid })),
}
}

// if this package is unlocked, so are the rates and contracts
// the only connection should be draftRates/draftContracts
if (!revision.submittedAt) {
dataToCopy.draftContracts = {
connect: {
id: contractRevision.contractID,
},
}
} else {
// if this package has been submitted, then we set the revision join table.
// rate revisions on contract revisions join table
dataToCopy.contractRevisions = {
create: {
contractRevisionID: contractRevision.id,
validAfter: new Date(),
},
}
}

// each rate revision data here belongs to a different Rate, so we need
Expand Down Expand Up @@ -250,4 +271,24 @@ export async function migrateRateInfo(
}
})
}

// and finally, if this is an unlocked contract, set the draftRates.
if (!revision.submittedAt) {
const rateIDs = formData.rateInfos.map((r) => r.id)

await client.contractRevisionTable.update({
where: { id: contractRevision.id },
data: {
updatedAt:
formData.updatedAt > revision.createdAt
? formData.updatedAt
: revision.createdAt,
draftRates: {
connect: rateIDs.map((rid) => ({
id: rid,
})),
},
},
})
}
}
Loading

0 comments on commit 00e00cb

Please sign in to comment.