Skip to content

Commit

Permalink
move read operations outside of transaction, increase timeout to 10s
Browse files Browse the repository at this point in the history
  • Loading branch information
Vikariusu committed Oct 2, 2024
1 parent f4b689c commit 7364f50
Showing 1 changed file with 36 additions and 35 deletions.
71 changes: 36 additions & 35 deletions api/src/services/reportingPeriods/reportingPeriods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,43 +117,44 @@ export const certifyReportingPeriodAndOpenNextPeriod = async ({
}

try {
const newReportingPeriod = await db.$transaction(async (prisma) => {
const currentReportingPeriod = await db.reportingPeriod.findUnique({
where: { id: reportingPeriodId },
})
if (!currentReportingPeriod) {
throw new Error(
`Reporting period with id ${reportingPeriodId} not found`
)
}

// Find the next reporting period
const nextReportingPeriod = await db.reportingPeriod.findFirst({
where: { startDate: { gt: currentReportingPeriod.startDate } },
orderBy: { startDate: 'asc' },
})
if (!nextReportingPeriod) {
throw new Error('No next reporting period found')
}

await prisma.reportingPeriodCertification.create({
data: {
reportingPeriodId,
organizationId,
certifiedById: currentUser.id,
},
})

const preferences = {
current_reporting_period_id: nextReportingPeriod.id,
}
await db.organization.update({
data: { preferences },
where: { id: organizationId },
})
const currentReportingPeriod = await reportingPeriod({
id: reportingPeriodId,
})
if (!currentReportingPeriod) {
throw new Error(`Reporting period with id ${reportingPeriodId} not found`)
}

return nextReportingPeriod
const nextReportingPeriod = await db.reportingPeriod.findFirst({
where: { startDate: { gt: currentReportingPeriod.startDate } },
orderBy: { startDate: 'asc' },
})
if (!nextReportingPeriod) {
throw new Error('No next reporting period found')
}

const newReportingPeriod = await db.$transaction(
async (prisma) => {
await prisma.reportingPeriodCertification.create({
data: {
reportingPeriodId,
organizationId,
certifiedById: currentUser.id,
},
})

await prisma.organization.update({
where: { id: organizationId },
data: {
preferences: {
current_reporting_period_id: nextReportingPeriod.id,
},
},
})

return nextReportingPeriod
},
{ timeout: 10000 }
)

return newReportingPeriod
} catch (err) {
Expand Down

0 comments on commit 7364f50

Please sign in to comment.