diff --git a/services/app-api/src/postgres/contractAndRates/insertContract.ts b/services/app-api/src/postgres/contractAndRates/insertContract.ts index c59fb8b14d..13ba1e1d18 100644 --- a/services/app-api/src/postgres/contractAndRates/insertContract.ts +++ b/services/app-api/src/postgres/contractAndRates/insertContract.ts @@ -80,13 +80,28 @@ async function insertDraftContract( contractType: contractType, contractExecutionStatus, contractDocuments: { - create: contractDocuments, + create: + contractDocuments && + contractDocuments.map((d, idx) => ({ + position: idx, + ...d, + })), }, supportingDocuments: { - create: supportingDocuments, + create: + supportingDocuments && + supportingDocuments.map((d, idx) => ({ + position: idx, + ...d, + })), }, stateContacts: { - create: stateContacts, + create: + stateContacts && + stateContacts.map((c, idx) => ({ + position: idx, + ...c, + })), }, contractDateStart, contractDateEnd, diff --git a/services/app-api/src/postgres/contractAndRates/insertRate.ts b/services/app-api/src/postgres/contractAndRates/insertRate.ts index 32f902e5c7..69a874cb5f 100644 --- a/services/app-api/src/postgres/contractAndRates/insertRate.ts +++ b/services/app-api/src/postgres/contractAndRates/insertRate.ts @@ -54,10 +54,20 @@ async function insertDraftRate( rateType, rateCapitationType: rateCapitationType, rateDocuments: { - create: rateDocuments, + create: + rateDocuments && + rateDocuments.map((d, idx) => ({ + position: idx, + ...d, + })), }, supportingDocuments: { - create: supportingDocuments, + create: + supportingDocuments && + supportingDocuments.map((d, idx) => ({ + position: idx, + ...d, + })), }, rateDateStart, rateDateEnd, @@ -67,10 +77,20 @@ async function insertDraftRate( rateProgramIDs, rateCertificationName, certifyingActuaryContacts: { - create: certifyingActuaryContacts, + create: + certifyingActuaryContacts && + certifyingActuaryContacts.map((c, idx) => ({ + position: idx, + ...c, + })), }, addtlActuaryContacts: { - create: addtlActuaryContacts, + create: + addtlActuaryContacts && + addtlActuaryContacts.map((c, idx) => ({ + position: idx, + ...c, + })), }, actuaryCommunicationPreference, }, diff --git a/services/app-api/src/postgres/contractAndRates/updateDraftContractWithRates.ts b/services/app-api/src/postgres/contractAndRates/updateDraftContractWithRates.ts index a60e6eaff2..a21a31b7a2 100644 --- a/services/app-api/src/postgres/contractAndRates/updateDraftContractWithRates.ts +++ b/services/app-api/src/postgres/contractAndRates/updateDraftContractWithRates.ts @@ -12,25 +12,7 @@ import { includeDraftRates } from './prismaDraftContractHelpers' import { rateRevisionToDomainModel } from './prismaSharedContractRateHelpers' import type { RateFormEditable } from './updateDraftRate' import { isEqualData } from '../../resolvers/healthPlanPackage/contractAndRates/resolverHelpers' - -// since prisma needs nulls to indicate "remove this field" instead of "ignore this field" -// this function translates undefineds into nulls -function nullify(field: T | undefined): T | null { - if (field === undefined) { - return null - } - - return field -} - -// since prisma needs nulls to indicate "remove this field" instead of "ignore this field" -// this function translates undefineds into empty arrays -function emptify(field: T[] | undefined): T[] { - if (field === undefined) { - return [] - } - return field -} +import { emptify, nullify } from '../prismaDomainAdaptors' type ContractFormEditable = Partial diff --git a/services/app-api/src/postgres/prismaDomainAdaptors.ts b/services/app-api/src/postgres/prismaDomainAdaptors.ts new file mode 100644 index 0000000000..b276f41cb2 --- /dev/null +++ b/services/app-api/src/postgres/prismaDomainAdaptors.ts @@ -0,0 +1,20 @@ +// since prisma needs nulls to indicate "remove this field" instead of "ignore this field" +// this function translates undefineds into nulls +function nullify(field: T | undefined): T | null { + if (field === undefined) { + return null + } + + return field +} + +// since prisma needs nulls to indicate "remove this field" instead of "ignore this field" +// this function translates undefineds into empty arrays +function emptify(field: T[] | undefined): T[] { + if (field === undefined) { + return [] + } + return field +} + +export { nullify, emptify } diff --git a/services/app-api/src/resolvers/healthPlanPackage/submitHealthPlanPackage.ts b/services/app-api/src/resolvers/healthPlanPackage/submitHealthPlanPackage.ts index 888a5d7367..a7a896a53f 100644 --- a/services/app-api/src/resolvers/healthPlanPackage/submitHealthPlanPackage.ts +++ b/services/app-api/src/resolvers/healthPlanPackage/submitHealthPlanPackage.ts @@ -46,6 +46,7 @@ import type { PackageStatusType, RateType, } from '../../domain-models/contractAndRates' +import type { RateFormEditable } from '../../postgres/contractAndRates/updateDraftRate' export const SubmissionErrorCodes = ['INCOMPLETE', 'INVALID'] as const type SubmissionErrorCode = (typeof SubmissionErrorCodes)[number] // iterable union type @@ -305,26 +306,6 @@ export function submitHealthPlanPackageResolver( initialFormData = conversionResult contractRevisionID = contractWithHistory.revisions[0].id - // If we are submitting a CONTRACT ONLY but it still has rates associated with it, we need to remove those draftRates now - if ( - initialFormData.submissionType === 'CONTRACT_ONLY' && - initialFormData.rateInfos.length > 0 - ) { - const rateRemovalResult = - await store.updateDraftContractWithRates({ - contractID: contractWithHistory.id, - formData: contractWithHistory.draftRevision.formData, - rateFormDatas: [], - }) - if (rateRemovalResult instanceof Error) { - const errMessage = - 'Failed to remove draft rates from a CONTRACT ONLY submission: ' + - rateRemovalResult.message - logError('submitHealthPlanPackage', errMessage) - setErrorAttributesOnActiveSpan(errMessage, span) - throw new Error(errMessage) - } - } // Final clean + check of data before submit - parse to state submission const maybeLocked = parseAndSubmit(initialFormData) @@ -338,6 +319,13 @@ export function submitHealthPlanPackageResolver( } // Since submit can change the form data, we have to save it again. + // if the rates were removed, we remove them. + let removeRateInfos: RateFormEditable[] | undefined = undefined + if (maybeLocked.rateInfos.length === 0) { + // undefined means ignore rates in updaterDraftContractWithRates, empty array means empty them. + removeRateInfos = [] + } + const updateResult = await store.updateDraftContractWithRates({ contractID: input.pkgID, formData: { @@ -364,6 +352,7 @@ export function submitHealthPlanPackageResolver( } ), }, + rateFormDatas: removeRateInfos, }) if (updateResult instanceof Error) { const errMessage = `Failed to update submitted contract info with ID: ${contractRevisionID}; ${updateResult.message}` @@ -378,7 +367,7 @@ export function submitHealthPlanPackageResolver( } // If there are rates, submit those first - if (initialFormData.rateInfos.length > 0) { + if (contractWithHistory.revisions[0].rateRevisions.length > 0) { const ratePromises: Promise[] = [] contractWithHistory.revisions[0].rateRevisions.forEach( (rateRev) => {