From 43d4c2274f7ba239826c310ef9bcd2379efd979c Mon Sep 17 00:00:00 2001 From: goemen Date: Thu, 25 Apr 2024 13:53:10 -0700 Subject: [PATCH 1/2] changing the shape of the output of the external api --- .../src/v1/routes/pay-transparency-routes.ts | 6 -- .../routes/external-consumer-routes.spec.ts | 1 - .../src/v1/routes/external-consumer-routes.ts | 3 + .../external-consumer-service.spec.ts | 83 ++++++++++--------- .../v1/services/external-consumer-service.ts | 39 ++++++--- 5 files changed, 72 insertions(+), 60 deletions(-) diff --git a/backend-external/src/v1/routes/pay-transparency-routes.ts b/backend-external/src/v1/routes/pay-transparency-routes.ts index 302b35311..0756c1842 100644 --- a/backend-external/src/v1/routes/pay-transparency-routes.ts +++ b/backend-external/src/v1/routes/pay-transparency-routes.ts @@ -73,12 +73,6 @@ const router = express.Router(); * Report: * allOf: * - $ref: "#/components/schemas/ReportItem" - * - type: object - * properties: - * history: - * type: array - * items: - * $ref: "#/components/schemas/ReportItem" * * PaginatedReports: * type: object diff --git a/backend/src/v1/routes/external-consumer-routes.spec.ts b/backend/src/v1/routes/external-consumer-routes.spec.ts index 051196f67..90c6fe252 100644 --- a/backend/src/v1/routes/external-consumer-routes.spec.ts +++ b/backend/src/v1/routes/external-consumer-routes.spec.ts @@ -123,7 +123,6 @@ describe('external-consumer-routes', () => { revision: REPORT.revision, update_date: REPORT.update_date.toISOString(), user_comment: REPORT.user_comment, - history: [], }, ], totalRecords: 1, diff --git a/backend/src/v1/routes/external-consumer-routes.ts b/backend/src/v1/routes/external-consumer-routes.ts index fec03b851..0dbe98046 100644 --- a/backend/src/v1/routes/external-consumer-routes.ts +++ b/backend/src/v1/routes/external-consumer-routes.ts @@ -1,12 +1,15 @@ import express, { Request, Response } from 'express'; import { externalConsumerService } from '../services/external-consumer-service'; import { utils } from '../services/utils-service'; +import {} from 'lodash' const router = express.Router(); router.get( '/', utils.asyncHandler(async (req: Request, res: Response) => { + + try { const startDate = req.query.startDate?.toString(); const endDate = req.query.endDate?.toString(); diff --git a/backend/src/v1/services/external-consumer-service.spec.ts b/backend/src/v1/services/external-consumer-service.spec.ts index 9cdf46f8f..19357c0a0 100644 --- a/backend/src/v1/services/external-consumer-service.spec.ts +++ b/backend/src/v1/services/external-consumer-service.spec.ts @@ -143,49 +143,52 @@ describe('external-consumer-service', () => { revision: testData.revision, update_date: testData.update_date, user_comment: testData.user_comment, - history: [ + }); + + expect(results.records[1]).toStrictEqual({ + calculated_data: [ { - calculated_data: [ - { - is_suppressed: - testData.report_history[0].calculated_data_history[0].is_suppressed, - value: testData.report_history[0].calculated_data_history[0].value, - calculation_code: - testData.report_history[0].calculated_data_history[0].calculation_code - .calculation_code, - }, - ], - company_address_line1: - testData.report_history[0].pay_transparency_company.address_line1, - company_address_line2: - testData.report_history[0].pay_transparency_company.address_line2, - company_bceid_business_guid: - testData.report_history[0].pay_transparency_company.bceid_business_guid, - company_city: testData.report_history[0].pay_transparency_company.city, - company_country: testData.report_history[0].pay_transparency_company.country, - company_id: testData.report_history[0].company_id, - company_name: testData.report_history[0].pay_transparency_company.company_name, - company_postal_code: testData.report_history[0].pay_transparency_company.postal_code, - company_province: testData.report_history[0].pay_transparency_company.province, - create_date: testData.report_history[0].create_date, - data_constraints: testData.report_history[0].data_constraints, - employee_count_range: - testData.report_history[0].employee_count_range.employee_count_range, - naics_code: - testData.report_history[0].naics_code_report_history_naics_codeTonaics_code - .naics_code, - naics_code_label: - testData.report_history[0].naics_code_report_history_naics_codeTonaics_code - .naics_label, - report_end_date: testData.report_history[0].report_end_date, - report_id: testData.report_history[0].report_id, - report_start_date: testData.report_history[0].report_start_date, - report_status: testData.report_history[0].report_status, - revision: testData.report_history[0].revision, - update_date: testData.report_history[0].update_date, - user_comment: testData.report_history[0].user_comment, + is_suppressed: + testData.report_history[0].calculated_data_history[0].is_suppressed, + value: testData.report_history[0].calculated_data_history[0].value, + calculation_code: + testData.report_history[0].calculated_data_history[0] + .calculation_code.calculation_code, }, ], + company_address_line1: + testData.report_history[0].pay_transparency_company.address_line1, + company_address_line2: + testData.report_history[0].pay_transparency_company.address_line2, + company_bceid_business_guid: + testData.report_history[0].pay_transparency_company.bceid_business_guid, + company_city: testData.report_history[0].pay_transparency_company.city, + company_country: + testData.report_history[0].pay_transparency_company.country, + company_id: testData.report_history[0].company_id, + company_name: + testData.report_history[0].pay_transparency_company.company_name, + company_postal_code: + testData.report_history[0].pay_transparency_company.postal_code, + company_province: + testData.report_history[0].pay_transparency_company.province, + create_date: testData.report_history[0].create_date, + data_constraints: testData.report_history[0].data_constraints, + employee_count_range: + testData.report_history[0].employee_count_range.employee_count_range, + naics_code: + testData.report_history[0] + .naics_code_report_history_naics_codeTonaics_code.naics_code, + naics_code_label: + testData.report_history[0] + .naics_code_report_history_naics_codeTonaics_code.naics_label, + report_end_date: testData.report_history[0].report_end_date, + report_id: testData.report_history[0].report_id, + report_start_date: testData.report_history[0].report_start_date, + report_status: testData.report_history[0].report_status, + revision: testData.report_history[0].revision, + update_date: testData.report_history[0].update_date, + user_comment: testData.report_history[0].user_comment, }); }); diff --git a/backend/src/v1/services/external-consumer-service.ts b/backend/src/v1/services/external-consumer-service.ts index 0450c6a37..0b3b459e9 100644 --- a/backend/src/v1/services/external-consumer-service.ts +++ b/backend/src/v1/services/external-consumer-service.ts @@ -1,6 +1,7 @@ import prismaReadOnlyReplica from '../prisma/prisma-client-readonly-replica'; import { LocalDate, convert } from '@js-joda/core'; import pick from 'lodash/pick'; +import flatten from 'lodash/flatten'; import { PayTransparencyUserError } from './file-upload-service'; const denormalizeCompany = (company) => { @@ -25,6 +26,7 @@ const denormalizeReport = ( calculation_code: any; }[], ) => { + return { ...pick(report, [ 'report_id', @@ -143,6 +145,12 @@ const externalConsumerService = { }, pay_transparency_company: true, }, + where: { + create_date: { + gte: convert(startDt).toDate(), + lte: convert(endDt).toDate(), + }, + }, }, }, skip: offset, @@ -153,22 +161,27 @@ const externalConsumerService = { totalRecords: totalCount, page: offset, pageSize: limit, - records: results.map((report) => { - return { - ...denormalizeReport( - report, - (r) => r.naics_code_pay_transparency_report_naics_codeTonaics_code, - (r) => r.pay_transparency_calculated_data, - ), - history: report.report_history.map((report) => { - return denormalizeReport( + records: [ + ...results.map((report) => { + return { + ...denormalizeReport( + report, + (r) => + r.naics_code_pay_transparency_report_naics_codeTonaics_code, + (r) => r.pay_transparency_calculated_data, + ), + }; + }), + ...flatten(results.map((r) => r.report_history)).map((report) => { + return { + ...denormalizeReport( report, (r) => r.naics_code_report_history_naics_codeTonaics_code, (r) => r.calculated_data_history, - ); - }), - }; - }), + ), + }; + }), + ], }; }, }; From 43063dec13496c3a0ceb6b5e7b6a08b5b111fc9c Mon Sep 17 00:00:00 2001 From: goemen Date: Thu, 25 Apr 2024 13:56:09 -0700 Subject: [PATCH 2/2] remove unecessary changes --- backend/src/v1/routes/external-consumer-routes.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/backend/src/v1/routes/external-consumer-routes.ts b/backend/src/v1/routes/external-consumer-routes.ts index 0dbe98046..fec03b851 100644 --- a/backend/src/v1/routes/external-consumer-routes.ts +++ b/backend/src/v1/routes/external-consumer-routes.ts @@ -1,15 +1,12 @@ import express, { Request, Response } from 'express'; import { externalConsumerService } from '../services/external-consumer-service'; import { utils } from '../services/utils-service'; -import {} from 'lodash' const router = express.Router(); router.get( '/', utils.asyncHandler(async (req: Request, res: Response) => { - - try { const startDate = req.query.startDate?.toString(); const endDate = req.query.endDate?.toString();