Skip to content

Commit

Permalink
Merge branch 'main' into wml-insert-rate-scratch
Browse files Browse the repository at this point in the history
  • Loading branch information
macrael committed Aug 3, 2023
2 parents 2d07f76 + a05eaf3 commit 0d23a7f
Show file tree
Hide file tree
Showing 30 changed files with 482 additions and 531 deletions.
11 changes: 5 additions & 6 deletions services/app-api/src/launchDarkly/launchDarkly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ import { logError } from '../logger'
import { setErrorAttributesOnActiveSpan } from '../resolvers/attributeHelper'

//Set up default feature flag values used to returned data
const defaultFeatureFlags: FeatureFlagSettings = featureFlagKeys.reduce(
(a, c) => {
const defaultFeatureFlags = (): FeatureFlagSettings =>
featureFlagKeys.reduce((a, c) => {
const flag = featureFlags[c].flag
const defaultValue = featureFlags[c].defaultValue
return Object.assign(a, { [flag]: defaultValue })
},
{} as FeatureFlagSettings
)
}, {} as FeatureFlagSettings)

type LDService = {
getFeatureFlag: (
Expand Down Expand Up @@ -50,7 +48,8 @@ function offlineLDService(): LDService {
`No connection to LaunchDarkly, fallback to offlineLDService with default value for ${flag}`,
context.span
)
return defaultFeatureFlags[flag]
const featureFlags = defaultFeatureFlags()
return featureFlags[flag]
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ describe('findContract', () => {
must(await submitRate(client, rate3.id, stateUser.id, '3.0 create'))

// Now, find that contract and assert the history is what we expected
const threeContract = await findContractWithHistory(
client,
contractA.id
const threeContract = must(
await findContractWithHistory(client, contractA.id)
)
if (threeContract instanceof Error) {
throw threeContract
Expand All @@ -114,7 +113,9 @@ describe('findContract', () => {
must(await submitRate(client, rate2.id, stateUser.id, '2.1 remove'))

// Now, find that contract and assert the history is what we expected
const twoContract = await findContractWithHistory(client, contractA.id)
const twoContract = must(
await findContractWithHistory(client, contractA.id)
)
if (twoContract instanceof Error) {
throw twoContract
}
Expand All @@ -131,9 +132,8 @@ describe('findContract', () => {
must(await submitRate(client, rate1.id, stateUser.id, '1.1 new name'))

// Now, find that contract and assert the history is what we expected
const backAgainContract = await findContractWithHistory(
client,
contractA.id
const backAgainContract = must(
await findContractWithHistory(client, contractA.id)
)
if (backAgainContract instanceof Error) {
throw backAgainContract
Expand All @@ -159,9 +159,8 @@ describe('findContract', () => {
)

// Now, find that contract and assert the history is what we expected
let testingContract = await findContractWithHistory(
client,
contractA.id
let testingContract = must(
await findContractWithHistory(client, contractA.id)
)
if (testingContract instanceof Error) {
throw testingContract
Expand Down Expand Up @@ -202,16 +201,17 @@ describe('findContract', () => {
)

// Now, find that contract and assert the history is what we expected
testingContract = await findContractWithHistory(client, contractA.id)
testingContract = must(
await findContractWithHistory(client, contractA.id)
)
if (testingContract instanceof Error) {
throw testingContract
}
expect(testingContract.revisions).toHaveLength(8)

// Now, find that contract and assert the history is what we expected
const resultingContract = await findContractWithHistory(
client,
contractA.id
const resultingContract = must(
await findContractWithHistory(client, contractA.id)
)
if (resultingContract instanceof Error) {
throw resultingContract
Expand Down Expand Up @@ -453,9 +453,8 @@ describe('findContract', () => {
)

// Now, find that contract and assert the history is what we expected
const resultingContract = await findContractWithHistory(
client,
contractA.id
const resultingContract = must(
await findContractWithHistory(client, contractA.id)
)
if (resultingContract instanceof Error) {
throw resultingContract
Expand Down Expand Up @@ -628,11 +627,7 @@ describe('findContract', () => {
throw new Error('Should be impossible to submit twice in a row.')
}

const res = await findContractWithHistory(client, contractA.id)

if (res instanceof Error) {
throw res
}
const res = must(await findContractWithHistory(client, contractA.id))

const revisions = res.revisions.reverse()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrismaTransactionType } from '../prismaTypes'
import { ContractType } from '../../domain-models/contractAndRates/contractAndRatesZodSchema'
import { updateInfoIncludeUpdater } from '../prismaHelpers'
import { parseContractWithHistory } from './prismaContractWithHistoryConverter'
import { NotFoundError } from '../storeError'

// findContractWithHistory returns a ContractType with a full set of
// ContractRevisions in reverse chronological order. Each revision is a change to this
Expand All @@ -10,7 +10,7 @@ import { parseContractWithHistory } from './prismaContractWithHistoryConverter'
async function findContractWithHistory(
client: PrismaTransactionType,
contractID: string
): Promise<ContractType | Error> {
): Promise<ContractType | NotFoundError | Error> {
try {
const contract = await client.contractTable.findFirst({
where: {
Expand Down Expand Up @@ -48,7 +48,7 @@ async function findContractWithHistory(
if (!contract) {
const err = `PRISMA ERROR: Cannot find contract with id: ${contractID}`
console.error(err)
return new Error(err)
return new NotFoundError(err)
}

return parseContractWithHistory(contract)
Expand Down
31 changes: 31 additions & 0 deletions services/app-api/src/postgres/contractAndRates/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { insertDraftContract, InsertContractArgsType } from './insertContract'
import { findContractWithHistory } from './findContractWithHistory'
import { findDraftContract } from './findDraftContract'
import {
contractFormDataToDomainModel,
convertUpdateInfoToDomainModel,
draftContractRevToDomainModel,
draftContractToDomainModel,
contractRevToDomainModel,
draftRatesToDomainModel,
ratesRevisionsToDomainModel,
contractWithHistoryToDomainModel,
getContractStatus,
} from './prismaToDomainModel'

export {
insertDraftContract,
findContractWithHistory,
findDraftContract,
contractFormDataToDomainModel,
convertUpdateInfoToDomainModel,
draftContractRevToDomainModel,
draftContractToDomainModel,
contractRevToDomainModel,
draftRatesToDomainModel,
ratesRevisionsToDomainModel,
contractWithHistoryToDomainModel,
getContractStatus,
}

export type { InsertContractArgsType }
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ function contractWithHistoryToDomainModel(
continue
}

// This initial entry is the first history record of this contract revision.
// Then the for loop with it's rateRevisions are additional history records for each change in rate revisions.
// This is why allRevisionSets could have more entries than contract revisions.
const initialEntry: ContractRevisionSet = {
contractRev,
submitInfo: contractRev.submitInfo,
Expand All @@ -97,6 +100,7 @@ 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.
for (const rateRev of contractRev.rateRevisions) {
Expand Down Expand Up @@ -142,11 +146,9 @@ function contractWithHistoryToDomainModel(

const revisions = contractRevToDomainModel(allRevisionSets).reverse()

const contractStatus = getContractStatus(contract.revisions)

return {
id: contract.id,
status: contractStatus,
status: getContractStatus(contract.revisions),
stateCode: contract.stateCode,
stateNumber: contract.stateNumber,
revisions: revisions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { insertDraftRate } from './insertRate'
import { submitRate } from './submitRate'
import { updateDraftRate } from './updateDraftRate'
import { must, createInsertContractData } from '../../testHelpers'
import { NotFoundError } from '../storeError'

describe('submitContract', () => {
it('creates a submission from a draft', async () => {
Expand All @@ -23,9 +24,13 @@ describe('submitContract', () => {
})

// submitting before there's a draft should be an error
expect(
await submitContract(client, '1111', '1111', 'failed submit')
).toBeInstanceOf(Error)
const submitError = await submitContract(
client,
'1111',
'1111',
'failed submit'
)
expect(submitError).toBeInstanceOf(NotFoundError)

// create a draft contract
const draftContractData = createInsertContractData({
Expand Down Expand Up @@ -61,15 +66,15 @@ describe('submitContract', () => {
})
)

// resubmitting should be an error
expect(
await submitContract(
client,
contractA.id,
stateUser.id,
'initial submit'
)
).toBeInstanceOf(Error)
const resubmitStoreError = await submitContract(
client,
contractA.id,
stateUser.id,
'initial submit'
)

// resubmitting should be a store error
expect(resubmitStoreError).toBeInstanceOf(NotFoundError)
})

it('invalidates old revisions when new revisions are submitted', async () => {
Expand Down
11 changes: 7 additions & 4 deletions services/app-api/src/postgres/contractAndRates/submitContract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PrismaClient } from '@prisma/client'
import { ContractType } from '../../domain-models/contractAndRates/contractAndRatesZodSchema'
import { findContractWithHistory } from './findContractWithHistory'
import { NotFoundError } from '../storeError'

// Update the given revision
// * invalidate relationships of previous revision
Expand All @@ -10,7 +11,7 @@ async function submitContract(
contractID: string,
submittedByUserID: string,
submitReason: string
): Promise<ContractType | Error> {
): Promise<ContractType | NotFoundError | Error> {
const groupTime = new Date()

try {
Expand Down Expand Up @@ -38,9 +39,11 @@ async function submitContract(
},
},
})

if (!currentRev) {
console.error('No Unsubmitted Rev!')
return new Error('cant find the current rev to submit')
const err = `PRISMA ERROR: Cannot find the current rev to submit with contract id: ${contractID}`
console.error(err)
return new NotFoundError(err)
}

const submittedRateRevisions = currentRev.draftRates.map(
Expand Down Expand Up @@ -149,7 +152,7 @@ async function submitContract(
return await findContractWithHistory(tx, contractID)
})
} catch (err) {
console.error('SUBMITeeee PRISMA CONTRACT ERR', err)
console.error('SUBMIT PRISMA CONTRACT ERR', err)
return err
}
}
Expand Down
14 changes: 6 additions & 8 deletions services/app-api/src/postgres/contractAndRates/unlockContract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PrismaClient } from '@prisma/client'
import { ContractType } from '../../domain-models/contractAndRates/contractAndRatesZodSchema'
import { findContractWithHistory } from './findContractWithHistory'
import { NotFoundError } from '../storeError'

// Unlock the given contract
// * copy form data
Expand All @@ -10,7 +11,7 @@ async function unlockContract(
contractID: string,
unlockedByUserID: string,
unlockReason: string
): Promise<ContractType | Error> {
): Promise<ContractType | NotFoundError | Error> {
const groupTime = new Date()

try {
Expand All @@ -36,12 +37,9 @@ async function unlockContract(
},
})
if (!currentRev) {
console.error(
'Programming Error: cannot find the current revision to submit'
)
return new Error(
'Programming Error: cannot find the current revision to submit'
)
const err = `PRISMA ERROR: Cannot find the current revision to unlock with contract id: ${contractID}`
console.error(err)
return new NotFoundError(err)
}

if (!currentRev.submitInfoID) {
Expand Down Expand Up @@ -95,7 +93,7 @@ async function unlockContract(
return findContractWithHistory(tx, contractID)
})
} catch (err) {
console.error('SUBMIT PRISMA CONTRACT ERR', err)
console.error('UNLOCK PRISMA CONTRACT ERR', err)
return err
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '@prisma/client'
import { ContractType } from '../../domain-models/contractAndRates/contractAndRatesZodSchema'
import { findContractWithHistory } from './findContractWithHistory'
import { NotFoundError } from '../storeError'

type UpdateContractArgsType = {
populationCovered: PopulationCoverageType
Expand All @@ -24,7 +25,7 @@ async function updateDraftContract(
contractID: string,
formData: UpdateContractArgsType,
rateIDs: string[]
): Promise<ContractType | Error> {
): Promise<ContractType | NotFoundError | Error> {
try {
// Given all the Rates associated with this draft, find the most recent submitted
// rateRevision to update.
Expand All @@ -35,8 +36,9 @@ async function updateDraftContract(
},
})
if (!currentRev) {
console.error('No Draft Rev!')
return new Error('cant find a draft rev to submit')
const err = `PRISMA ERROR: Cannot find the current rev to update with contract id: ${contractID}`
console.error(err)
return new NotFoundError(err)
}

await client.contractRevisionTable.update({
Expand Down Expand Up @@ -67,7 +69,7 @@ async function updateDraftContract(

return findContractWithHistory(client, contractID)
} catch (err) {
console.error('SUBMIT PRISMA CONTRACT ERR', err)
console.error('UPDATE PRISMA CONTRACT ERR', err)
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion services/app-api/src/postgres/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export { InsertHealthPlanPackageArgsType } from './healthPlanPackage'
export { InsertUserArgsType } from './user'
export { NewPostgresStore, Store } from './postgresStore'
export { NewPrismaClient } from './prismaClient'
export { isStoreError, StoreError } from './storeError'
export { isStoreError, StoreError, NotFoundError } from './storeError'
export { findStatePrograms } from './state/findStatePrograms'
Loading

0 comments on commit 0d23a7f

Please sign in to comment.