diff --git a/api/src/services/uploads/uploads.scenarios.ts b/api/src/services/uploads/uploads.scenarios.ts index e4963130..6c857482 100644 --- a/api/src/services/uploads/uploads.scenarios.ts +++ b/api/src/services/uploads/uploads.scenarios.ts @@ -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', }, }, @@ -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: [ { @@ -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: [ { @@ -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: [ { @@ -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: [ { diff --git a/api/src/services/uploads/uploads.test.ts b/api/src/services/uploads/uploads.test.ts index cedb8c06..11a8d4a9 100644 --- a/api/src/services/uploads/uploads.test.ts +++ b/api/src/services/uploads/uploads.test.ts @@ -263,8 +263,10 @@ describeScenario( 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 () => { @@ -273,8 +275,10 @@ describeScenario( 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) }) } ) @@ -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}`, @@ -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 = { diff --git a/api/src/services/uploads/uploads.ts b/api/src/services/uploads/uploads.ts index 02a2f229..b60062e6 100644 --- a/api/src/services/uploads/uploads.ts +++ b/api/src/services/uploads/uploads.ts @@ -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, @@ -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' }, + } // Get the most recent upload for each expenditure category and agency and set the S3 Object key for (const upload of validUploadsInPeriod) { @@ -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