Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure treasury generation payload has information for all projects #490

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions api/src/services/uploads/uploads.scenarios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,17 +410,24 @@ export const uploadCheck = defineScenario<
}),
},
expenditureCategory: {
one: {
oneA: {
data: {
name: '1A',
code: '1A',
updatedAt: '2024-01-26T15:11:27.000Z',
},
},
two: {
oneB: {
data: {
name: '2A',
code: '2A',
name: '1B',
code: '1B',
updatedAt: '2024-01-26T15:11:27.000Z',
},
},
oneC: {
data: {
name: '1C',
code: '1C',
updatedAt: '2024-01-26T15:11:27.000Z',
},
},
Expand All @@ -432,7 +439,7 @@ export const uploadCheck = defineScenario<
uploadedById: scenario.user.one.id,
agencyId: scenario.agency.one.id,
reportingPeriodId: scenario.reportingPeriod.one.id,
expenditureCategoryId: scenario.expenditureCategory.one.id,
expenditureCategoryId: scenario.expenditureCategory.oneA.id,
validations: {
create: [
{
Expand All @@ -459,7 +466,7 @@ export const uploadCheck = defineScenario<
uploadedById: scenario.user.two.id,
agencyId: scenario.agency.one.id,
reportingPeriodId: scenario.reportingPeriod.one.id,
expenditureCategoryId: scenario.expenditureCategory.one.id,
expenditureCategoryId: scenario.expenditureCategory.oneA.id,
validations: {
create: [
{
Expand All @@ -486,7 +493,7 @@ export const uploadCheck = defineScenario<
uploadedById: scenario.user.three.id,
agencyId: scenario.agency.two.id,
reportingPeriodId: scenario.reportingPeriod.one.id,
expenditureCategoryId: scenario.expenditureCategory.one.id,
expenditureCategoryId: scenario.expenditureCategory.oneA.id,
validations: {
create: [
{
Expand Down Expand Up @@ -517,7 +524,7 @@ export const uploadCheck = defineScenario<
uploadedById: scenario.user.four.id,
agencyId: scenario.agency.two.id,
reportingPeriodId: scenario.reportingPeriod.one.id,
expenditureCategoryId: scenario.expenditureCategory.two.id,
expenditureCategoryId: scenario.expenditureCategory.oneB.id,
validations: {
create: [
{
Expand Down
46 changes: 41 additions & 5 deletions api/src/services/uploads/uploads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,10 @@ describeScenario<StandardScenario>(
scenario.organization.one,
scenario.reportingPeriod.one
)
expect(Object.keys(result).length).toEqual(1)
expect(Object.keys(result)).toEqual(['1A'])
expect(Object.keys(result).length).toEqual(3)
expect(Object.keys(result['1A'].uploadsToAdd).length).toEqual(1)
expect(Object.keys(result['1B'].uploadsToAdd).length).toEqual(0)
expect(Object.keys(result['1C'].uploadsToAdd).length).toEqual(0)
})

it('returns two uploads of different categories', async () => {
Expand All @@ -273,8 +275,10 @@ describeScenario<StandardScenario>(
scenario.organization.two,
scenario.reportingPeriod.one
)
expect(Object.keys(result).length).toEqual(2)
expect(Object.keys(result).sort()).toEqual(['2A', '1A'].sort())
expect(Object.keys(result).length).toEqual(3)
expect(Object.keys(result['1A'].uploadsToAdd).length).toEqual(1)
expect(Object.keys(result['1B'].uploadsToAdd).length).toEqual(1)
expect(Object.keys(result['1C'].uploadsToAdd).length).toEqual(0)
})
}
)
Expand Down Expand Up @@ -399,7 +403,6 @@ describe('treasury report', () => {
id: mockUser.id,
},
outputTemplateId: mockReportingPeriod.outputTemplateId,
ProjectType: '1A',
uploadsToAdd: {
[mockUpload.agencyId]: {
objectKey: `uploads/${mockOrganization.id}/${mockUpload.agencyId}/${mockReportingPeriod.id}/${mockUpload.id}/${mockUpload.filename}`,
Expand All @@ -408,6 +411,39 @@ describe('treasury report', () => {
},
},
uploadsToRemove: {},
ProjectType: '1A',
},
'1B': {
organization: {
id: mockOrganization.id,
preferences: {
current_reporting_period_id: mockReportingPeriod.id,
},
},
user: {
email: mockUser.email,
id: mockUser.id,
},
outputTemplateId: mockReportingPeriod.outputTemplateId,
uploadsToAdd: {},
uploadsToRemove: {},
ProjectType: '1B',
},
'1C': {
organization: {
id: mockOrganization.id,
preferences: {
current_reporting_period_id: mockReportingPeriod.id,
},
},
user: {
email: mockUser.email,
id: mockUser.id,
},
outputTemplateId: mockReportingPeriod.outputTemplateId,
uploadsToAdd: {},
uploadsToRemove: {},
ProjectType: '1C',
},
}
const subrecipientPayload: SubrecipientLambdaPayload = {
Expand Down
52 changes: 23 additions & 29 deletions api/src/services/uploads/uploads.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Prisma } from '@prisma/client'
import type { Organization, ReportingPeriod } from '@prisma/client'
import cloneDeep from 'lodash/cloneDeep'
import type {
QueryResolvers,
MutationResolvers,
Expand Down Expand Up @@ -219,7 +220,28 @@ export const getUploadsByExpenditureCategory = async (
const validUploadsInPeriod: UploadsWithValidationsAndExpenditureCategory[] =
await getValidUploadsInCurrentPeriod(organization, reportingPeriod)

const uploadsByEC: ProjectLambdaPayload = {}
const commonData = {
organization: {
id: organization.id,
preferences: {
current_reporting_period_id:
organization.preferences['current_reporting_period_id'],
},
},
user: {
email: context.currentUser.email,
id: context.currentUser.id,
},
outputTemplateId: reportingPeriod.outputTemplateId,
uploadsToAdd: {},
uploadsToRemove: {},
}

const uploadsByEC: ProjectLambdaPayload = {
'1A': { ...cloneDeep(commonData), ProjectType: '1A' },
'1B': { ...cloneDeep(commonData), ProjectType: '1B' },
'1C': { ...cloneDeep(commonData), ProjectType: '1C' },
}
Comment on lines +223 to +244
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A factory function or something like the following might help avoid cloneDeep (assuming it would be preferable to avoid – if not, feel free to ignore).

const uploadsByEC = {};
for (const ec of ['1A', '1B', '1C']) {
  uploadsByEC[ec] = {
    organization: {
      id: organization.id,
      preferences: {
        current_reporting_period_id:
          organization.preferences['current_reporting_period_id'],
      },
    },
    user: {
      email: context.currentUser.email,
      id: context.currentUser.id,
    },
    outputTemplateId: reportingPeriod.outputTemplateId,
    uploadsToAdd: {},
    uploadsToRemove: {},
    ProjectType: ec,
  },
}


// Get the most recent upload for each expenditure category and agency and set the S3 Object key
for (const upload of validUploadsInPeriod) {
Expand All @@ -229,34 +251,6 @@ export const getUploadsByExpenditureCategory = async (
filename: upload.filename,
}

if (!uploadsByEC[upload.expenditureCategory.code]) {
// The EC code was never added. This is the time to initialize it.
uploadsByEC[upload.expenditureCategory.code] = {
organization: {
id: organization.id,
preferences: {
current_reporting_period_id:
organization.preferences['current_reporting_period_id'],
},
},
user: {
email: context.currentUser.email,
id: context.currentUser.id,
},
outputTemplateId: reportingPeriod.outputTemplateId,
ProjectType: upload.expenditureCategory.code,
uploadsToAdd: {},
uploadsToRemove: {},
}

// Set the upload to add for this agency
uploadsByEC[upload.expenditureCategory.code].uploadsToAdd[
upload.agencyId
] = uploadPayload

continue
}

if (
!uploadsByEC[upload.expenditureCategory.code].uploadsToAdd[
upload.agencyId
Expand Down
Loading