Skip to content

Commit

Permalink
add tx for updates
Browse files Browse the repository at this point in the history
  • Loading branch information
macrael committed Mar 7, 2024
1 parent 18f3605 commit e6e139a
Showing 1 changed file with 99 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PrismaClient } from '@prisma/client'
import type { ContractType } from '../../domain-models/contractAndRates'
import { NotFoundError } from '../postgresErrors'
import { emptify, nullify } from '../prismaDomainAdaptors'
import type { PrismaTransactionType } from '../prismaTypes'
import { findContractWithHistory } from './findContractWithHistory'
import type { RateFormEditable } from './updateDraftRate'

Expand Down Expand Up @@ -134,120 +134,124 @@ function prismaUpdateRateFormDataFromDomain(rateFormData: RateFormEditable) {
}

async function updateDraftContractRates(
client: PrismaTransactionType,
client: PrismaClient,
args: UpdateDraftContractRatesArgsType
): Promise<ContractType | Error> {
try {
// for now, get the latest contract revision, eventually we'll have rate revisions directly on this
const contract = await client.contractTable.findUnique({
where: {
id: args.contractID,
},
include: {
revisions: {
take: 1,
orderBy: {
createdAt: 'desc',
return await client.$transaction(async (tx) => {
// for now, get the latest contract revision, eventually we'll have rate revisions directly on this
const contract = await tx.contractTable.findUnique({
where: {
id: args.contractID,
},
include: {
revisions: {
take: 1,
orderBy: {
createdAt: 'desc',
},
},
},
},
})
})

if (!contract) {
return new NotFoundError(
'contract not found with ID: ' + args.contractID
)
}
if (!contract) {
return new NotFoundError(
'contract not found with ID: ' + args.contractID
)
}

const draftRevision = contract.revisions[0]
if (!draftRevision) {
return new Error(
'PROGRAMMER ERROR: This draft contract has no draft revision'
)
}
const draftRevision = contract.revisions[0]
if (!draftRevision) {
return new Error(
'PROGRAMMER ERROR: This draft contract has no draft revision'
)
}

// figure out the rate number range for created rates.
const state = await client.state.findUnique({
where: { stateCode: contract.stateCode },
})
// figure out the rate number range for created rates.
const state = await tx.state.findUnique({
where: { stateCode: contract.stateCode },
})

if (!state) {
return new Error(
'PROGRAMER ERROR: No state found with code: ' +
contract.stateCode
)
}
if (!state) {
return new Error(
'PROGRAMER ERROR: No state found with code: ' +
contract.stateCode
)
}

let nextRateNumber = state.latestStateRateCertNumber
let nextRateNumber = state.latestStateRateCertNumber

// create new rates with new revisions
const ratesToCreate = args.rateUpdates.create.map((ru) => {
const rateFormDataem = ru.formData
const thisRateNumber = nextRateNumber
nextRateNumber++
return {
stateCode: contract.stateCode,
stateNumber: thisRateNumber,
revisions: {
create: prismaRateCreateFormDataFromDomain(rateFormDataem),
},
}
})
// create new rates with new revisions
const ratesToCreate = args.rateUpdates.create.map((ru) => {
const rateFormDataem = ru.formData
const thisRateNumber = nextRateNumber
nextRateNumber++
return {
stateCode: contract.stateCode,
stateNumber: thisRateNumber,
revisions: {
create: prismaRateCreateFormDataFromDomain(
rateFormDataem
),
},
}
})

// to delete draft rates, we need to delete their revisions first
await client.rateRevisionTable.deleteMany({
where: {
rateID: {
in: args.rateUpdates.delete.map((ru) => ru.rateID),
// to delete draft rates, we need to delete their revisions first
await tx.rateRevisionTable.deleteMany({
where: {
rateID: {
in: args.rateUpdates.delete.map((ru) => ru.rateID),
},
},
},
})
})

// create new rates and link and unlink others
await client.contractRevisionTable.update({
where: { id: draftRevision.id },
data: {
draftRates: {
create: ratesToCreate,
connect: args.rateUpdates.link.map((ru) => ({
id: ru.rateID,
})),
disconnect: args.rateUpdates.unlink.map((ru) => ({
id: ru.rateID,
})),
delete: args.rateUpdates.delete.map((ru) => ({
id: ru.rateID,
})),
// create new rates and link and unlink others
await tx.contractRevisionTable.update({
where: { id: draftRevision.id },
data: {
draftRates: {
create: ratesToCreate,
connect: args.rateUpdates.link.map((ru) => ({
id: ru.rateID,
})),
disconnect: args.rateUpdates.unlink.map((ru) => ({
id: ru.rateID,
})),
delete: args.rateUpdates.delete.map((ru) => ({
id: ru.rateID,
})),
},
},
},
include: {
draftRates: true,
},
})

// update existing rates
for (const ru of args.rateUpdates.update) {
const draftRev = await client.rateRevisionTable.findFirst({
where: {
rateID: ru.rateID,
submitInfoID: null,
include: {
draftRates: true,
},
})

if (!draftRev) {
return new Error(
'attempting to update a rate that is not editable: ' +
ru.rateID
)
}
// update existing rates
for (const ru of args.rateUpdates.update) {
const draftRev = await tx.rateRevisionTable.findFirst({
where: {
rateID: ru.rateID,
submitInfoID: null,
},
})

await client.rateRevisionTable.update({
where: { id: draftRev.id },
data: prismaUpdateRateFormDataFromDomain(ru.formData),
})
}
if (!draftRev) {
return new Error(
'attempting to update a rate that is not editable: ' +
ru.rateID
)
}

await tx.rateRevisionTable.update({
where: { id: draftRev.id },
data: prismaUpdateRateFormDataFromDomain(ru.formData),
})
}

return findContractWithHistory(client, args.contractID)
return findContractWithHistory(tx, args.contractID)
})
} catch (err) {
console.error('PRISMA ERR', err)
return err
Expand Down

0 comments on commit e6e139a

Please sign in to comment.