From aa0429c291f4b7bcdc6be401b1b3182804258e7f Mon Sep 17 00:00:00 2001 From: aditya Date: Thu, 24 Oct 2024 11:02:44 -0500 Subject: [PATCH] feat: add front end button to upload subrecipients (#498) * fix: update to reduce dependency on reportingPeriodId * feat: add button to kick-off manual treasury generation --- api/src/graphql/subrecipients.sdl.ts | 13 ++++++ .../services/subrecipients/subrecipients.ts | 19 +++----- .../DownloadTreasuryFiles.tsx | 45 +++++++++++++++++++ 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/api/src/graphql/subrecipients.sdl.ts b/api/src/graphql/subrecipients.sdl.ts index e8f9f920..657301d6 100644 --- a/api/src/graphql/subrecipients.sdl.ts +++ b/api/src/graphql/subrecipients.sdl.ts @@ -34,6 +34,16 @@ export const schema = gql` organizationId: Int } + input UploadSubrecipientsInput { + organizationId: Int! + } + + type UploadSubrecipientsOutput { + success: Boolean! + message: String + countSubrecipients: Int! + } + type Mutation { createSubrecipient(input: CreateSubrecipientInput!): Subrecipient! @requireAuth @@ -42,5 +52,8 @@ export const schema = gql` input: UpdateSubrecipientInput! ): Subrecipient! @requireAuth deleteSubrecipient(id: Int!): Subrecipient! @requireAuth + uploadSubrecipients( + input: UploadSubrecipientsInput! + ): UploadSubrecipientsOutput! @requireAuth } ` diff --git a/api/src/services/subrecipients/subrecipients.ts b/api/src/services/subrecipients/subrecipients.ts index 10c03501..b331c3f1 100644 --- a/api/src/services/subrecipients/subrecipients.ts +++ b/api/src/services/subrecipients/subrecipients.ts @@ -52,27 +52,19 @@ export const deleteSubrecipient: MutationResolvers['deleteSubrecipient'] = ({ export const uploadSubrecipients: MutationResolvers['uploadSubrecipients'] = async ({ input }) => { - const { organizationId, reportingPeriodId } = input + const { organizationId } = input const organization = await db.organization.findUnique({ where: { id: organizationId }, }) const reportingPeriod = await db.reportingPeriod.findFirst({ - where: { id: reportingPeriodId }, + where: { id: organization.preferences?.current_reporting_period_id }, }) if (!organization || !reportingPeriod) { throw new Error('Organization or reporting period not found') } - if ( - organization.preferences?.current_reporting_period_id !== - reportingPeriod.id - ) { - throw new Error( - 'Reporting period does not match current reporting period' - ) - } try { - const subrecipientKey = `treasuryreports/${organizationId}/${reportingPeriodId}/subrecipients.json` + const subrecipientKey = `treasuryreports/${organization.id}/${reportingPeriod.id}/subrecipients.json` const startDate = new Date( reportingPeriod.endDate.getFullYear(), @@ -86,7 +78,10 @@ export const uploadSubrecipients: MutationResolvers['uploadSubrecipients'] = ) const subrecipientsWithUploads = await db.subrecipient.findMany({ - where: { createdAt: { lte: endDate, gte: startDate }, organizationId }, + where: { + createdAt: { lte: endDate, gte: startDate }, + organizationId: organization.id, + }, include: { subrecipientUploads: true }, }) const subrecipients = { diff --git a/web/src/components/TreasuryGeneration/DownloadTreasuryFiles/DownloadTreasuryFiles.tsx b/web/src/components/TreasuryGeneration/DownloadTreasuryFiles/DownloadTreasuryFiles.tsx index 7034a0e8..a6ed5415 100644 --- a/web/src/components/TreasuryGeneration/DownloadTreasuryFiles/DownloadTreasuryFiles.tsx +++ b/web/src/components/TreasuryGeneration/DownloadTreasuryFiles/DownloadTreasuryFiles.tsx @@ -1,4 +1,5 @@ import Button from 'react-bootstrap/Button' +import { useAuth } from 'web/src/auth' import { useMutation } from '@redwoodjs/web' import { toast } from '@redwoodjs/web/toast' @@ -11,7 +12,18 @@ const DOWNLOAD_TREASURY_FILE = gql` } ` +const UPLOAD_SUBRECIPIENTS = gql` + mutation UploadSubrecipients($input: UploadSubrecipientsInput!) { + uploadSubrecipients(input: $input) { + success + message + countSubrecipients + } + } +` + const DownloadTreasuryFiles = () => { + const { currentUser } = useAuth() const [downloadTreasuryFile] = useMutation(DOWNLOAD_TREASURY_FILE, { onCompleted: ({ downloadTreasuryFile }) => { const { fileLink } = downloadTreasuryFile @@ -21,12 +33,38 @@ const DownloadTreasuryFiles = () => { toast.error(error.message) }, }) + const onClick = (event) => { downloadTreasuryFile({ variables: { input: { fileType: event.target.getAttribute('name') } }, }) } + const [uploadSubrecipients] = useMutation(UPLOAD_SUBRECIPIENTS, { + onCompleted: ({ uploadSubrecipients }) => { + const { success, message, countSubrecipients } = uploadSubrecipients + if (success) { + toast.success( + `${message} - ${countSubrecipients} subrecipients uploaded` + ) + } else { + toast.error(message) + } + }, + onError: (error) => { + toast.error(error.message) + }, + }) + const onSubrecipientClick = () => { + uploadSubrecipients({ + variables: { + input: { + organizationId: currentUser.agency.organizationId, + }, + }, + }) + } + return (
@@ -42,6 +80,13 @@ const DownloadTreasuryFiles = () => { +
)