Skip to content

Commit

Permalink
feat: add front end button to upload subrecipients (#498)
Browse files Browse the repository at this point in the history
* fix: update to reduce dependency on reportingPeriodId

* feat: add button to kick-off manual treasury generation
  • Loading branch information
as1729 authored Oct 24, 2024
1 parent 25a4836 commit aa0429c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
13 changes: 13 additions & 0 deletions api/src/graphql/subrecipients.sdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,5 +52,8 @@ export const schema = gql`
input: UpdateSubrecipientInput!
): Subrecipient! @requireAuth
deleteSubrecipient(id: Int!): Subrecipient! @requireAuth
uploadSubrecipients(
input: UploadSubrecipientsInput!
): UploadSubrecipientsOutput! @requireAuth
}
`
19 changes: 7 additions & 12 deletions api/src/services/subrecipients/subrecipients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand All @@ -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 (
<div className="rw-segment">
<div className="rw-button-group">
Expand All @@ -42,6 +80,13 @@ const DownloadTreasuryFiles = () => {
<Button name="Subrecipient" onClick={onClick} className="rw-button">
Download Subrecipient file
</Button>
<Button
name="Subrecipient"
onClick={onSubrecipientClick}
className="rw-button"
>
Upload new Subrecipient file
</Button>
</div>
</div>
)
Expand Down

0 comments on commit aa0429c

Please sign in to comment.