Skip to content

Commit

Permalink
refactor: logic flow for saving and maintaining subrecipient state (#502
Browse files Browse the repository at this point in the history
)

* refactor: logic flow for saving and maintaining subrecipient state

* fix: remove unnecessary import
  • Loading branch information
as1729 authored Oct 31, 2024
1 parent aa0429c commit 20460e9
Showing 1 changed file with 77 additions and 55 deletions.
132 changes: 77 additions & 55 deletions api/src/functions/processValidationJson/processValidationJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,61 +198,7 @@ export const processRecord = async (

// If we passed validation, we will save the subrecipient info into our DB
if (passed && result.subrecipients?.length) {
const organizationId = extractOrganizationIdFromKey(key)
result.subrecipients.forEach((subrecipient) =>
saveSubrecipientInfo(
subrecipient,
key,
uploadId,
result.versionString,
organizationId
)
)

let reportingPeriod
try {
reportingPeriod = (
await db.upload.findUnique({
where: { id: uploadId },
include: { reportingPeriod: true },
})
).reportingPeriod
} catch (err) {
logger.error(`Could not find reporting period for upload ${uploadId}`)
throw new Error('Error determining reporting period for upload')
}

try {
const subrecipientKey = `treasuryreports/${organizationId}/${reportingPeriod.id}/subrecipients.json`
const startDate = new Date(
reportingPeriod.endDate.getFullYear(),
reportingPeriod.endDate.getMonth() + 1,
1
)
const endDate = new Date(
reportingPeriod.endDate.getFullYear(),
reportingPeriod.endDate.getMonth() + 2,
0
)
const subrecipientsWithUploads = await db.subrecipient.findMany({
where: {
createdAt: { lte: endDate, gte: startDate },
organizationId,
},
include: { subrecipientUploads: true },
})
const subrecipients = {
subrecipients: subrecipientsWithUploads,
}
await sendPutObjectToS3Bucket(
bucket,
subrecipientKey,
JSON.stringify(subrecipients)
)
} catch (err) {
logger.error(`Error saving subrecipients JSON file to S3: ${err}`)
throw new Error('Error saving subrecipient info to S3')
}
await handleSubrecipientUploads(result, key, uploadId, bucket)
}

// Delete the errors.json file from S3
Expand All @@ -272,6 +218,82 @@ export const processRecord = async (
}
}

async function handleSubrecipientUploads(result, key, uploadId, bucket) {
const organizationId = extractOrganizationIdFromKey(key)
for (const subrecipient of result.subrecipients) {
await saveSubrecipientInfo(
subrecipient,
key,
uploadId,
result.versionString,
organizationId
)
}

let reportingPeriod
try {
reportingPeriod = (
await db.upload.findUnique({
where: { id: uploadId },
include: { reportingPeriod: true },
})
).reportingPeriod
} catch (err) {
logger.error(`Could not find reporting period for upload ${uploadId}`)
throw new Error('Error determining reporting period for upload')
}

try {
const subrecipientsWithUploads = await getNewlyCreatedSubrecipients(
reportingPeriod,
organizationId
)
const subrecipients = {
subrecipients: subrecipientsWithUploads,
}
const subrecipientKey = `treasuryreports/${organizationId}/${reportingPeriod.id}/subrecipients.json`
await sendPutObjectToS3Bucket(
bucket,
subrecipientKey,
JSON.stringify(subrecipients)
)
logger.info(
`Successfully saved subrecipients to S3: ${subrecipientsWithUploads.length}`
)
} catch (err) {
logger.error(`Error saving subrecipients JSON file to S3: ${err}`)
throw new Error('Error saving subrecipient info to S3')
}
}

async function getNewlyCreatedSubrecipients(reportingPeriod, organizationId) {
// Get all subrecipients for the current reporting period
// These subrecipients are considered for the report if they were created
// during the month following the end of the reporting period
// For example: if the reporting period ends on 2022-03-31,
// any subrecipients created between 2022-04-01 and 2022-04-30 are new

const startDate = new Date(
reportingPeriod.endDate.getFullYear(),
reportingPeriod.endDate.getMonth() + 1,
1
)
const endDate = new Date(
reportingPeriod.endDate.getFullYear(),
reportingPeriod.endDate.getMonth() + 2,
0
)
const subrecipientsWithUploads = await db.subrecipient.findMany({
where: {
createdAt: { lte: endDate, gte: startDate },
organizationId,
},
include: { subrecipientUploads: true },
})

return subrecipientsWithUploads
}

async function saveSubrecipientInfo(
subrecipientInput: Subrecipient,
key: string,
Expand Down

0 comments on commit 20460e9

Please sign in to comment.