Skip to content

Commit

Permalink
include city and province in the company address shown in reports
Browse files Browse the repository at this point in the history
  • Loading branch information
banders committed May 7, 2024
1 parent 28228f9 commit 3236f68
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 7 deletions.
54 changes: 51 additions & 3 deletions backend/src/v1/services/report-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,52 @@ describe('getReportData', () => {
});
});

describe('formatCompanyAddress', () => {
describe('when the company object provides values for all address-related properties', () => {
it('the formatted address includes all address-related properties in the correct order', () => {
const mockCompany = {
address_line1: 'A1',
address_line2: 'A2',
city: 'C',
province: 'P',
};
const address = reportServicePrivate.formatCompanyAddress(mockCompany);
expect(address).toBe('A1 A2, C, P');
});
});
describe('when the company object is null', () => {
it('the formatted address is an empty string', () => {
const mockCompany = null;
const address = reportServicePrivate.formatCompanyAddress(mockCompany);
expect(address).toBe('');
});
});
describe('when the company contains values for all properties except addres_line2', () => {
it('the formatted address omits address_line2', () => {
const mockCompany = {
address_line1: 'A1',
address_line2: null,
city: 'C',
province: 'P',
};
const address = reportServicePrivate.formatCompanyAddress(mockCompany);
expect(address).toBe('A1, C, P');
});
});
describe("when the company's address-related properties start or end with extra whitespace", () => {
it('the extra whitespace is trimmed from the formatted address', () => {
const mockCompany = {
address_line1: ' A1',
address_line2: 'A2 ',
city: 'C ',
province: ' P',
};
const address = reportServicePrivate.formatCompanyAddress(mockCompany);
expect(address).toBe('A1 A2, C, P');
});
});
});

describe('getReportHtml', () => {
describe('when a valid report id is provided', () => {
it('returns an HTML string of the report', async () => {
Expand Down Expand Up @@ -862,9 +908,11 @@ describe('publishReport', () => {

await expect(
reportService.publishReport(mockDraftReportInApi),
).rejects.toEqual(new Error(
'A report for this time period already exists and cannot be updated.',
));
).rejects.toEqual(
new Error(
'A report for this time period already exists and cannot be updated.',
),
);
});
});
});
Expand Down
45 changes: 41 additions & 4 deletions backend/src/v1/services/report-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,42 @@ const reportServicePrivate = {

return report;
},

/**
*
* @param report Combines several address-related properties from
* the pay_transparency_company object (such as address_line1, address_line2,
* city, and province) into a full civic address string. If all address-related
* properties are missing, returns an empty string.
* @returns a formatted civic address string
*/
formatCompanyAddress(pay_transparency_company: any): string {
if (!pay_transparency_company) {
return '';
}
let streetAddressLines: string[] = [];
if (pay_transparency_company?.address_line1) {
streetAddressLines.push(pay_transparency_company?.address_line1.trim());
}
if (pay_transparency_company?.address_line2) {
streetAddressLines.push(pay_transparency_company?.address_line2.trim());
}
var streetAddress = streetAddressLines.join(' ').trim();

let fullAddressParts: string[] = [];
if (streetAddress) {
fullAddressParts.push(streetAddress);
}
if (pay_transparency_company?.city) {
fullAddressParts.push(pay_transparency_company?.city.trim());
}
if (pay_transparency_company?.province) {
fullAddressParts.push(pay_transparency_company?.province.trim());
}

let fullAddress = fullAddressParts.join(', ').trim();
return fullAddress;
},
};

const reportService = {
Expand Down Expand Up @@ -1023,8 +1059,9 @@ const reportService = {

const reportData = {
companyName: report.pay_transparency_company.company_name,
companyAddress:
`${report.pay_transparency_company.address_line1} ${report.pay_transparency_company.address_line2}`.trim(),
companyAddress: reportServicePrivate.formatCompanyAddress(
report?.pay_transparency_company,
),
reportingYear: report.reporting_year,
reportStartDate: LocalDate.from(
nativeJs(report.report_start_date, ZoneId.UTC),
Expand Down Expand Up @@ -1186,8 +1223,8 @@ const reportService = {

if (existing_published_report && !existing_published_report.is_unlocked) {
throw new Error(
'A report for this time period already exists and cannot be updated.',
);
'A report for this time period already exists and cannot be updated.',
);
}

// If there is an existing Published report, move it into
Expand Down

0 comments on commit 3236f68

Please sign in to comment.