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(DTFS2-7203): fix bug where /companies returns 500 for companies w/o SIC codes #932

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 42 additions & 0 deletions src/modules/companies/companies.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { resetAllWhenMocks, when } from 'jest-when';

import { SectorIndustriesService } from '../sector-industries/sector-industries.service';
import { CompaniesService } from './companies.service';
import { GetCompanyResponse } from './dto/get-company-response.dto';
import { CompaniesOverseasCompanyException } from './exception/companies-overseas-company-exception.exception';

describe('CompaniesService', () => {
Expand Down Expand Up @@ -80,6 +81,47 @@ describe('CompaniesService', () => {
expect(response).toEqual(getCompanyResponse);
});

it('returns a mapped form of the company returned by the CompaniesHouseService when it has no type', async () => {
const { type: _removed, ...getCompanyCompaniesHouseResponseNoType } = getCompanyCompaniesHouseResponse;

when(companiesHouseServiceGetCompanyByRegistrationNumber).calledWith(testRegistrationNumber).mockReturnValueOnce(getCompanyCompaniesHouseResponseNoType);
when(sectorIndustriesServiceFind).calledWith(null, null).mockReturnValueOnce(findSectorIndustriesResponse);

const response = await service.getCompanyByRegistrationNumber(testRegistrationNumber);

expect(response).toEqual(getCompanyResponse);
});

it('returns a mapped form of the company returned by the CompaniesHouseService when it has no SIC codes', async () => {
const { sic_codes: _removed, ...getCompanyCompaniesHouseResponseNoSicCodes } = getCompanyCompaniesHouseResponse;
const { industries: _removed2, ...getCompanyResponseNoIndustries } = getCompanyResponse;
(getCompanyResponseNoIndustries as GetCompanyResponse).industries = [];

when(companiesHouseServiceGetCompanyByRegistrationNumber)
.calledWith(testRegistrationNumber)
.mockReturnValueOnce(getCompanyCompaniesHouseResponseNoSicCodes);
when(sectorIndustriesServiceFind).calledWith(null, null).mockReturnValueOnce(findSectorIndustriesResponse);

const response = await service.getCompanyByRegistrationNumber(testRegistrationNumber);

expect(response).toEqual(getCompanyResponseNoIndustries);
});

it('returns a mapped form of the company returned by the CompaniesHouseService when it has no registered office address', async () => {
const { registered_office_address: _removed, ...getCompanyCompaniesHouseResponseNoRegisteredOfficeAddress } = getCompanyCompaniesHouseResponse;
const { registeredAddress: _removed2, ...getCompanyResponseNoRegisteredAddress } = getCompanyResponse;
(getCompanyResponseNoRegisteredAddress as GetCompanyResponse).registeredAddress = {};

when(companiesHouseServiceGetCompanyByRegistrationNumber)
.calledWith(testRegistrationNumber)
.mockReturnValueOnce(getCompanyCompaniesHouseResponseNoRegisteredOfficeAddress);
when(sectorIndustriesServiceFind).calledWith(null, null).mockReturnValueOnce(findSectorIndustriesResponse);

const response = await service.getCompanyByRegistrationNumber(testRegistrationNumber);

expect(response).toEqual(getCompanyResponseNoRegisteredAddress);
});

it('throws a NotFoundException if the call to getCompanyByRegistrationNumber on the CompaniesHouseService throws a CompaniesHouseNotFoundException', async () => {
const companiesHouseNotFoundException = new CompaniesHouseNotFoundException(`Company with registration number ${testRegistrationNumber} was not found.`);
when(companiesHouseServiceGetCompanyByRegistrationNumber).calledWith(testRegistrationNumber).mockRejectedValueOnce(companiesHouseNotFoundException);
Expand Down
18 changes: 9 additions & 9 deletions src/modules/companies/companies.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class CompaniesService {
}

private validateCompanyIsUkCompany(company: GetCompanyCompaniesHouseResponse, registrationNumber: string): never | undefined {
if (company.type.includes('oversea')) {
if (company.type?.includes('oversea')) {
throw new CompaniesOverseasCompanyException(
`Company with registration number ${registrationNumber} is an overseas company. UKEF can only process applications from companies based in the UK.`,
);
Expand All @@ -53,13 +53,13 @@ export class CompaniesService {
companiesHouseRegistrationNumber: company.company_number,
companyName: company.company_name,
registeredAddress: {
organisationName: address.organisation_name,
addressLine1: address.address_line_1,
addressLine2: address.address_line_2,
addressLine3: address.address_line_3,
locality: address.locality,
postalCode: address.postal_code,
country: address.country,
organisationName: address?.organisation_name,
addressLine1: address?.address_line_1,
addressLine2: address?.address_line_2,
addressLine3: address?.address_line_3,
locality: address?.locality,
postalCode: address?.postal_code,
country: address?.country,
},
industries: this.mapSicCodes(company.sic_codes, industryClasses),
};
Expand All @@ -68,7 +68,7 @@ export class CompaniesService {
private mapSicCodes(sicCodes: string[], industryClasses: SectorIndustryEntity[]): Industry[] {
const industries = [];

sicCodes.forEach((sicCode) => {
sicCodes?.forEach((sicCode) => {
industryClasses.forEach((industryClass) => {
if (sicCode === industryClass.ukefIndustryId) {
industries.push({
Expand Down
8 changes: 4 additions & 4 deletions src/modules/companies/dto/get-company-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ export class GetCompanyResponse {
companyName: string;
registeredAddress: {
organisationName?: string;
addressLine1: string;
addressLine1?: string;
addressLine2?: string;
addressLine3?: string;
locality: string;
postalCode: string;
country: string;
locality?: string;
postalCode?: string;
country?: string;
};
industries: Industry[];
}
Expand Down
Loading