Skip to content

Commit

Permalink
POC for CRUD handling with tables
Browse files Browse the repository at this point in the history
  • Loading branch information
haworku committed Aug 29, 2023
1 parent b65d6f3 commit d3f8c70
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 15 deletions.
27 changes: 27 additions & 0 deletions services/app-api/src/postgres/contractAndRates/listHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const filterRemovals = (prevList: unknown[] = [], currentList: unknown[] = []) =>{
if (prevList.length === 0) return []
return prevList.filter( (item) => !currentList?.includes(item))
}
const filterAdditions = (prevList: unknown[] = [], currentList: unknown[] = []) => {
if (currentList.length === 0) return []
return currentList.filter( (item) => !prevList?.includes(item))
}
const filterUpdates = (prevList: unknown[] = [], currentList: unknown[] = []) => {
return currentList.filter( (item) => prevList?.includes(item))
}

const filterListIDsTuple = (prevList: unknown[] = [], currentList: unknown[] = []): [ [], [] , [] ] =>{
const removals = filterRemovals(prevList, currentList).map((item) => item.id)
const additions = filterAdditions(prevList,currentList).map((item) => item.id)
const updates = filterUpdates(prevList,currentList).map((item) => item.id)
return = {
removals,
additions,
updates
}
}
export {
filterRemovals,
filterAdditions,
filterUpdates,
}
51 changes: 36 additions & 15 deletions services/app-api/src/postgres/contractAndRates/updateDraftRate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ type UpdateRateArgsType = {
contractIDs: string[]
}

/*
updateDraftRate
This function calls two sequential rate revision updates in a transaciton
The first deletes related resources from the revision entirely
The second updates the Rate and re-creates/links for related resources (things like contacts and documents)
This approach was used for following reasons at the time of writing:
- Prisma has no native upsertMany functionality. Looping through each related resource to upsert felt overkill
- No need for version history, preserving dates on related resources in draft form
*/
async function updateDraftRate(
client: PrismaClient,
args: UpdateRateArgsType
Expand Down Expand Up @@ -55,11 +44,27 @@ async function updateDraftRate(
rateID: rateID,
submitInfoID: null,
},
include: {
certifyingActuaryContacts: true,
addtlActuaryContacts: true,
rateDocuments: true,
supportingDocuments: true
}
})
if (!currentRev) {
console.error('No Draft Rev!')
return new Error('cant find a draft rev to submit')
}

// Prepare to create/update/delete relationship

// const removedCertifyingActuaries = findRemovals(currentRev.certifyingActuaryContacts, certifyingActuaryContacts).map(contract =>contract.id)
// const removedAddtlActuaries = findRemovals(currentRev.addtlActuaryContacts, addtlActuaryContacts).map(contract =>contract.id)
// const removedRateDocs = findRemovals(currentRev.rateDocuments, rateDocuments ).map(doc =>doc.id)
// const removedSupportingDocs = findRemovals(currentRev.supportingDocuments, supportingDocuments ).map(doc =>doc.id)



// Clear all related resources on the revision
// Then update resource, adjusting all simple fields and creating new linked resources for fields holding relationships to other day
await tx.rateRevisionTable.update({
Expand All @@ -71,19 +76,35 @@ async function updateDraftRate(
rateCapitationType,

rateDocuments: {
deleteMany: {},
deleteMany: {
// where: {
// id: { in: removedRateDocs},
// },
},
create: rateDocuments,
},
supportingDocuments: {
deleteMany: {},
deleteMany: {
// where: {
// id: { in: removedSupportingDocs},
// },
},
create: supportingDocuments,
},
certifyingActuaryContacts: {
deleteMany: {},
deleteMany: {
// where: {
// id: { in: removedRateDocs},
// },
},
create: certifyingActuaryContacts,
},
addtlActuaryContacts: {
deleteMany: {},
deleteMany: {
// where: {
// id: { in: removedAddtlActuaries},
// },
},
create: addtlActuaryContacts,
},
rateDateStart,
Expand Down

0 comments on commit d3f8c70

Please sign in to comment.