Skip to content

Commit

Permalink
Work with @macrael @JasonLin0991 on types clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
haworku committed Aug 2, 2023
1 parent 09a2c03 commit 96d5702
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ const contractFormDataSchema = z.object({

const contractRevisionSchema = z.object({
id: z.string().uuid(),
submitInfo: updateInfoSchema,
unlockInfo: updateInfoSchema,
submitInfo: updateInfoSchema.optional(),
unlockInfo: updateInfoSchema.optional(),
createdAt: z.date(),
updatedAt: z.date(),
formData: contractFormDataSchema,
Expand All @@ -58,8 +58,8 @@ const contractRevisionSchema = z.object({
// For full rate revision schema see rateRevisionsSChema
const attachedRatesSchema = z.object({
id: z.string().uuid(),
submitInfo: updateInfoSchema,
unlockInfo: updateInfoSchema,
submitInfo: updateInfoSchema.optional(),
unlockInfo: updateInfoSchema.optional(),
revisionFormData: rateFormDataSchema,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
draftContractRevToDomainModel,
contractWithHistoryToDomainModel,
draftRatesToDomainModel,
draftRateRevToDomainModel,
} from '../../postgres/contractAndRates/prismaToDomainModel'

// CONTRACT PARSERS
Expand Down Expand Up @@ -87,7 +88,7 @@ function parseContractWithHistory(
function parseDraftRateRevision(
revision: DraftRateRevisionTableWithRelations
): RateRevisionType | Error {
const draftRateRevision = draftRatesToDomainModel(revision)
const draftRateRevision = draftRateRevToDomainModel(revision)
const parseDraft = contractRevisionZodSchema.safeParse(draftRateRevision)

if (!parseDraft.success) {
Expand All @@ -101,10 +102,9 @@ function parseDraftRateRevision(
}

function parseDraftRate(
rates: DraftRateTableWithRelations[]
rate: DraftRateTableWithRelations
): RateType | Error {
const draftRate = rates[0]
const draftRateDomain = draftRatesToDomainModel(draftRate)
const draftRateDomain = draftRatesToDomainModel(rate)

const parseDraft = draftRateZodSchema.safeParse(draftRateDomain)

Expand Down
17 changes: 14 additions & 3 deletions services/app-api/src/postgres/contractAndRates/insertContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
SubmissionType,
ContractType as PrismaContractType,
} from '@prisma/client'
import { ContractType } from '../../domain-models/contractAndRates/contractZodSchema'
import { parseDraftContract } from '../../domain-models/contractAndRates/parseDomainData'
import { ContractType, draftContractZodSchema } from '../../domain-models/contractAndRates/contractZodSchema'
import { draftContractRevisionsWithDraftRates } from '../prismaHelpers'
import { draftContractToDomainModel } from './prismaToDomainModel'

type InsertContractArgsType = {
stateCode: string
Expand Down Expand Up @@ -58,7 +58,18 @@ async function insertDraftContract(
},
})

return parseDraftContract(contract)
const draftContract = draftContractToDomainModel(contract)

const parseDraft = draftContractZodSchema.safeParse(draftContract)

if (!parseDraft.success) {
console.warn(
`ERROR: attempting to parse prisma draft contract failed: ${parseDraft.error}`
)
return parseDraft.error
}

return parseDraft.data
})
} catch (err) {
console.error('CONTRACT PRISMA ERR', err)
Expand Down
162 changes: 81 additions & 81 deletions services/app-api/src/postgres/contractAndRates/insertRate.test.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,83 @@
import { must } from '../../testHelpers'
import { getStateRecord } from '../../testHelpers/contractAndRates'
import { mockInsertDraftRateArgs} from '../../testHelpers/contractAndRates/rateHelpers'
import { sharedTestPrismaClient } from '../../testHelpers/storeHelpers'
import { InsertRateArgsType, insertDraftRate } from './insertRate'
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'

describe('insertRate', () => {
it('creates a new draft rate', async ()=>{
const client = await sharedTestPrismaClient()

// create a draft rate
const draftRateData = mockInsertDraftRateArgs()
const draftRate = must(
await insertDraftRate(client, draftRateData)
)

// Expect a single rate revision
expect(draftRate.revisions).toHaveLength(1)
})

it('increments state number count', async () => {
const client = await sharedTestPrismaClient()
const stateCode = 'OH'
const initialState = await getStateRecord(client, stateCode)

const contractA = mockInsertDraftRateArgs({
stateCode,
})
const contractB = mockInsertDraftRateArgs({
stateCode,
})

const submittedRateA = must(
await insertDraftRate(client, contractA)
)

// Expect state record count to be incremented by 1
expect(submittedRateA.stateNumber).toEqual(
initialState.latestStateSubmissionNumber + 1
)

const submittedRateB = must(
await insertDraftRate(client, contractB)
)

// Expect state record count to be incremented by 2
expect(submittedRateB.stateNumber).toEqual(
initialState.latestStateSubmissionNumber + 2
)
})

it('returns an error when invalid state code is provided', async () => {
const client = await sharedTestPrismaClient()

const draftRateData = mockInsertDraftRateArgs({
stateCode: 'CANADA',
})
const draftRate = await insertDraftRate(
client,
draftRateData
)

// Expect a prisma error
console.log(draftRate)
expect(draftRate).toBeInstanceOf(PrismaClientKnownRequestError)
})
it('returns an database error when zod validation fails', async () => {
const client = await sharedTestPrismaClient()

// create a draft rate
const draftRateData = mockInsertDraftRateArgs({rateType: null} as unknown as Partial<InsertRateArgsType>)
const draftRate = must(
await insertDraftRate(client, draftRateData)
)

// Expect a single rate revision
console.log(draftRate)
expect(draftRate).toBeInstanceOf(PrismaClientKnownRequestError)
})
// import { must } from '../../testHelpers'
// import { getStateRecord } from '../../testHelpers/contractAndRates'
// import { mockInsertDraftRateArgs} from '../../testHelpers/contractAndRates/rateHelpers'
// import { sharedTestPrismaClient } from '../../testHelpers/storeHelpers'
// import { InsertRateArgsType, insertDraftRate } from './insertRate'
// import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'

// describe('insertRate', () => {
// it('creates a new draft rate', async ()=>{
// const client = await sharedTestPrismaClient()

// // create a draft rate
// const draftRateData = mockInsertDraftRateArgs()
// const draftRate = must(
// await insertDraftRate(client, draftRateData)
// )

// // Expect a single rate revision
// expect(draftRate.revisions).toHaveLength(1)
// })

// it('increments state number count', async () => {
// const client = await sharedTestPrismaClient()
// const stateCode = 'OH'
// const initialState = await getStateRecord(client, stateCode)

// const contractA = mockInsertDraftRateArgs({
// stateCode,
// })
// const contractB = mockInsertDraftRateArgs({
// stateCode,
// })

// const submittedRateA = must(
// await insertDraftRate(client, contractA)
// )

// // Expect state record count to be incremented by 1
// expect(submittedRateA.stateNumber).toEqual(
// initialState.latestStateSubmissionNumber + 1
// )

// const submittedRateB = must(
// await insertDraftRate(client, contractB)
// )

// // Expect state record count to be incremented by 2
// expect(submittedRateB.stateNumber).toEqual(
// initialState.latestStateSubmissionNumber + 2
// )
// })

// it('returns an error when invalid state code is provided', async () => {
// const client = await sharedTestPrismaClient()

// const draftRateData = mockInsertDraftRateArgs({
// stateCode: 'CANADA',
// })
// const draftRate = await insertDraftRate(
// client,
// draftRateData
// )

// // Expect a prisma error
// console.log(draftRate)
// expect(draftRate).toBeInstanceOf(PrismaClientKnownRequestError)
// })
// it('returns an database error when zod validation fails', async () => {
// const client = await sharedTestPrismaClient()

// // create a draft rate
// const draftRateData = mockInsertDraftRateArgs({rateType: null} as unknown as Partial<InsertRateArgsType>)
// const draftRate = must(
// await insertDraftRate(client, draftRateData)
// )

// // Expect a single rate revision
// console.log(draftRate)
// expect(draftRate).toBeInstanceOf(PrismaClientKnownRequestError)
// })


// it('actuary contacts must link to either rate revision certifying actuary or to a rate revision additional actuary', async() =>{
Expand Down Expand Up @@ -179,4 +179,4 @@ describe('insertRate', () => {
// // Expect a single rate revision
// expect(draftRate.revisions).toHaveLength(1)
// })
})
// })
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,39 @@ function draftContractRevToDomainModel(
id: revision.id,
createdAt: revision.createdAt,
updatedAt: revision.updatedAt,
submitInfo: convertUpdateInfoToDomainModel(revision.submitInfo),
unlockInfo: convertUpdateInfoToDomainModel(revision.submitInfo),
formData: contractFormDataToDomainModel(revision),
rateRevisions: draftRatesToDomainModel(revision.draftRates),
rateRevisions: revision.draftRates.map ( (rate) => {
return {
id: rate.revisions[0].id,
submitInfo: convertUpdateInfoToDomainModel(rate.revisions[0].submitInfo),
unlockInfo: convertUpdateInfoToDomainModel(rate.revisions[0].unlockInfo),
revisionFormData: rateFormDataToDomainModel(rate.revisions[0]),
}
})
}
}

function contractRevToDomainModel(
contractRevisions: ContractRevisionTableWithRelations[]
revisions: ContractRevisionSet[]
): ContractRevisionType[] {
const domainContractRevisions = contractRevisions.map((contractRev) => ({
id: contractRev.id,
submitInfo: convertUpdateInfoToDomainModel(contractRev.submitInfo),
unlockInfo: contractRev.unlockInfo
? convertUpdateInfoToDomainModel(contractRev.unlockInfo)
const contractRevisions = revisions.map((entry) => ({
id: entry.contractRev.id,
submitInfo: convertUpdateInfoToDomainModel(entry.submitInfo),
unlockInfo: entry.unlockInfo
? convertUpdateInfoToDomainModel(entry.unlockInfo)
: undefined,
createdAt: contractRev.createdAt,
updatedAt: contractRev.updatedAt,
formData: contractFormDataToDomainModel(contractRev),
rateRevisions: ratesRevisionsToDomainModel(contractRev.rateRevisions),
createdAt: entry.contractRev.createdAt,
updatedAt: entry.contractRev.updatedAt,
formData: contractFormDataToDomainModel(entry.contractRev),
rateRevisions: entry.rateRevisions.map((rrev) => ({
id: rrev.id,
formData: rateFormDataToDomainModel(rrev),
})),
}))

return domainContractRevisions
return contractRevisions
}

function draftContractToDomainModel(
Expand Down Expand Up @@ -331,6 +343,7 @@ const rateFormDataToDomainModel = (
}
}


function draftRatesToDomainModel(
rate: DraftRateTableWithRelations
): RateType[] {
Expand All @@ -349,6 +362,19 @@ function draftRatesToDomainModel(
}
}

// returns thin/attached rate revision
function latestRevisionFromRate(
rate: RateType
): ContractRevisionType['rateRevisions'][0] {
return {
id: rate.revisions[0].id,
submitInfo: rate.revisions[0].submitInfo,
unlockInfo: rate.revisions[0].unlockInfo,
revisionFormData: rate.revisions[0].formData,
}

}

function draftRateRevToDomainModel(
revision: DraftRateRevisionTableWithRelations
): RateRevisionType {
Expand Down
1 change: 1 addition & 0 deletions services/app-api/src/postgres/prismaHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const draftContractRevisionsWithDraftRates = {
addtlActuaryContacts: true,
submitInfo: updateInfoIncludeUpdater,
unlockInfo: updateInfoIncludeUpdater,
draftContracts: true
},
where: {
submitInfoID: { not: null },
Expand Down

0 comments on commit 96d5702

Please sign in to comment.