Skip to content

Commit

Permalink
feat(DTFS2-7121): add SIC code mapping to /companies endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar-richardson-softwire committed May 15, 2024
1 parent 76f6f1a commit da0260f
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"links": {
"filing_history": "/company/00000001/filing-history",
"self": "/company/00000001",
"persons_with_significant_control": "/company/00000001/persons-with-significant-control",
"officers": "/company/00000001/officers"
},
"accounts": {
"last_accounts": {
"period_end_on": "2022-12-31",
"type": "micro-entity",
"made_up_to": "2022-12-31",
"period_start_on": "2022-01-01"
},
"accounting_reference_date": {
"month": "12",
"day": "31"
},
"overdue": false,
"next_made_up_to": "2023-12-31",
"next_due": "2024-09-30",
"next_accounts": {
"period_start_on": "2023-01-01",
"due_on": "2024-09-30",
"period_end_on": "2023-12-31",
"overdue": false
}
},
"company_name": "TEST COMPANY LTD",
"company_number": "00000001",
"company_status": "active",
"confirmation_statement": {
"next_made_up_to": "2025-01-20",
"next_due": "2025-02-03",
"overdue": false,
"last_made_up_to": "2024-01-20"
},
"date_of_creation": "2020-01-21",
"etag": "4f776861e3dfca11b773ff496c55999b7ec6a1cc",
"has_charges": false,
"has_insolvency_history": false,
"jurisdiction": "england-wales",
"registered_office_address": {
"address_line_1": "1 Test Street",
"locality": "Test City",
"postal_code": "A1 2BC",
"country": "United Kingdom"
},
"registered_office_is_in_dispute": false,
"sic_codes": [
"59112",
"62012",
"62020",
"62090"
],
"type": "ltd",
"undeliverable_registered_office_address": false,
"has_super_secure_pscs": false,
"can_file": true
}
2 changes: 1 addition & 1 deletion src/modules/companies/companies.controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('CompaniesController', () => {
beforeEach(() => {
resetAllWhenMocks();

const companiesService = new CompaniesService(null);
const companiesService = new CompaniesService(null, null);
companiesServiceGetCompanyByRegistrationNumber = jest.fn();
companiesService.getCompanyByRegistrationNumber = companiesServiceGetCompanyByRegistrationNumber;

Expand Down
3 changes: 2 additions & 1 deletion src/modules/companies/companies.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Module } from '@nestjs/common';
import { CompaniesHouseModule } from '@ukef/helper-modules/companies-house/companies-house.module';

import { SectorIndustriesModule } from '../sector-industries/sector-industries.module';
import { CompaniesController } from './companies.controller';
import { CompaniesService } from './companies.service';

@Module({
imports: [CompaniesHouseModule],
imports: [CompaniesHouseModule, SectorIndustriesModule],
controllers: [CompaniesController],
providers: [CompaniesService],
})
Expand Down
24 changes: 21 additions & 3 deletions src/modules/companies/companies.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import { GetCompanyGenerator } from '@ukef-test/support/generator/get-company-ge
import { RandomValueGenerator } from '@ukef-test/support/generator/random-value-generator';
import { resetAllWhenMocks, when } from 'jest-when';

import { SectorIndustriesService } from '../sector-industries/sector-industries.service';
import { CompaniesService } from './companies.service';

describe('CompaniesService', () => {
let configServiceGet: jest.Mock;
let companiesHouseServiceGetCompanyByRegistrationNumber: jest.Mock;
let sectorIndustriesServiceFind: jest.Mock;
let service: CompaniesService;

const valueGenerator = new RandomValueGenerator();

const testRegistrationNumber = '00000001';

const { getCompanyCompaniesHouseResponse, getCompanyResponse } = new GetCompanyGenerator(valueGenerator).generate({
const { getCompanyCompaniesHouseResponse, findSectorIndustriesResponse, getCompanyResponse } = new GetCompanyGenerator(valueGenerator).generate({
numberToGenerate: 1,
registrationNumber: testRegistrationNumber,
});
Expand All @@ -31,21 +33,37 @@ describe('CompaniesService', () => {
const companiesHouseService = new CompaniesHouseService(null, configService);
companiesHouseService.getCompanyByRegistrationNumber = companiesHouseServiceGetCompanyByRegistrationNumber;

service = new CompaniesService(companiesHouseService);
sectorIndustriesServiceFind = jest.fn();
const sectorIndustriesService = new SectorIndustriesService(null, null);
sectorIndustriesService.find = sectorIndustriesServiceFind;

service = new CompaniesService(companiesHouseService, sectorIndustriesService);
});

describe('getCompanyByRegistrationNumber', () => {
it('calls getCompanyByRegistrationNumber on the CompaniesHouseService with the registration number', async () => {
when(companiesHouseServiceGetCompanyByRegistrationNumber).calledWith(testRegistrationNumber).mockReturnValueOnce(getCompanyCompaniesHouseResponse);
when(sectorIndustriesServiceFind).calledWith(null, null).mockReturnValueOnce(findSectorIndustriesResponse);

await service.getCompanyByRegistrationNumber(testRegistrationNumber);

expect(companiesHouseServiceGetCompanyByRegistrationNumber).toHaveBeenCalledTimes(1);
expect(companiesHouseServiceGetCompanyByRegistrationNumber).toHaveBeenCalledWith(testRegistrationNumber);
});

it('returns a reduced form of the company returned by the CompaniesHouseService, with fewer fields', async () => {
it(`calls find on the SectorIndustriesService with both arguments as 'null'`, async () => {
when(companiesHouseServiceGetCompanyByRegistrationNumber).calledWith(testRegistrationNumber).mockReturnValueOnce(getCompanyCompaniesHouseResponse);
when(sectorIndustriesServiceFind).calledWith(null, null).mockReturnValueOnce(findSectorIndustriesResponse);

await service.getCompanyByRegistrationNumber(testRegistrationNumber);

expect(sectorIndustriesServiceFind).toHaveBeenCalledTimes(1);
expect(sectorIndustriesServiceFind).toHaveBeenCalledWith(null, null);
});

it('returns a mapped form of the company returned by the CompaniesHouseService', async () => {
when(companiesHouseServiceGetCompanyByRegistrationNumber).calledWith(testRegistrationNumber).mockReturnValueOnce(getCompanyCompaniesHouseResponse);
when(sectorIndustriesServiceFind).calledWith(null, null).mockReturnValueOnce(findSectorIndustriesResponse);

const response = await service.getCompanyByRegistrationNumber(testRegistrationNumber);

Expand Down
41 changes: 35 additions & 6 deletions src/modules/companies/companies.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ import { Injectable } from '@nestjs/common';
import { CompaniesHouseService } from '@ukef/helper-modules/companies-house/companies-house.service';
import { GetCompanyCompaniesHouseResponse } from '@ukef/helper-modules/companies-house/dto/get-company-companies-house-response.dto';

import { GetCompanyResponse } from './dto/get-company-response.dto';
import { SectorIndustryEntity } from '../sector-industries/entities/sector-industry.entity';
import { SectorIndustriesService } from '../sector-industries/sector-industries.service';
import { GetCompanyResponse, Industry } from './dto/get-company-response.dto';

@Injectable()
export class CompaniesService {
constructor(private readonly companiesHouseService: CompaniesHouseService) {}
constructor(
private readonly companiesHouseService: CompaniesHouseService,
private readonly sectorIndustriesService: SectorIndustriesService,
) {}

async getCompanyByRegistrationNumber(registrationNumber: string): Promise<GetCompanyResponse> {
const company: GetCompanyCompaniesHouseResponse = await this.companiesHouseService.getCompanyByRegistrationNumber(registrationNumber);
const industryClasses: SectorIndustryEntity[] = await this.sectorIndustriesService.find(null, null);

const reducedCompany = this.reduceCompany(company);
const mappedCompany = this.mapCompany(company, industryClasses);

return reducedCompany;
return mappedCompany;
}

private reduceCompany(company: GetCompanyCompaniesHouseResponse): GetCompanyResponse {
private mapCompany(company: GetCompanyCompaniesHouseResponse, industryClasses: SectorIndustryEntity[]): GetCompanyResponse {
const address = company.registered_office_address;

return {
Expand All @@ -31,7 +37,30 @@ export class CompaniesService {
postalCode: address.postal_code,
country: address.country,
},
sicCodes: company.sic_codes,
industries: this.mapSicCodes(company.sic_codes, industryClasses),
};
}

private mapSicCodes(sicCodes: string[], industryClasses: SectorIndustryEntity[]): Industry[] {
const industries = [];

sicCodes.forEach((sicCode) => {
industryClasses.forEach((industryClass) => {
if (sicCode === industryClass.ukefIndustryId) {
industries.push({
sector: {
code: industryClass.ukefSectorId.toString(),
name: industryClass.ukefSectorName,
},
class: {
code: industryClass.ukefIndustryId,
name: industryClass.ukefIndustryName,
},
});
}
});
});

return industries;
}
}
13 changes: 12 additions & 1 deletion src/modules/companies/dto/get-company-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,16 @@ export class GetCompanyResponse {
postalCode: string;
country: string;
};
sicCodes: string[];
industries: Industry[];
}

export class Industry {
sector: {
code: string;
name: string;
};
class: {
code: string;
name: string;
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"companiesHouseRegistrationNumber": "00000001",
"companyName": "TEST COMPANY LTD",
"registeredAddress": {
"addressLine1": "1 Test Street",
"locality": "Test City",
"postalCode": "A1 2BC",
"country": "United Kingdom"
},
"industries": [
{
"class": {
"code": "59112",
"name": "Video production activities"
},
"sector": {
"code": "1009",
"name": "Information and communication"
}
},
{
"class": {
"code": "62012",
"name": "Business and domestic software development"
},
"sector": {
"code": "1009",
"name": "Information and communication"
}
},
{
"class": {
"code": "62020",
"name": "Information technology consultancy activities"
},
"sector": {
"code": "1009",
"name": "Information and communication"
}
},
{
"class": {
"code": "62090",
"name": "Other information technology service activities"
},
"sector": {
"code": "1009",
"name": "Information and communication"
}
}
]
}
1 change: 1 addition & 0 deletions src/modules/sector-industries/sector-industries.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ import { SectorIndustriesService } from './sector-industries.service';
imports: [TypeOrmModule.forFeature([SectorIndustryEntity], DATABASE.MDM)],
controllers: [SectorIndustriesController],
providers: [SectorIndustriesService],
exports: [SectorIndustriesService],
})
export class SectorIndustriesModule {}
4 changes: 2 additions & 2 deletions test/companies/get-company-by-registration-number.api-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { ENVIRONMENT_VARIABLES, TIME_EXCEEDING_COMPANIES_HOUSE_TIMEOUT } from '@
import { GetCompanyGenerator } from '@ukef-test/support/generator/get-company-generator';
import { RandomValueGenerator } from '@ukef-test/support/generator/random-value-generator';
import nock from 'nock';
import getCompanyCompaniesHouseResponse = require('@ukef/helper-modules/companies-house/examples/example-response-for-get-company-by-registration-number.json');
import getCompanyResponse = require('@ukef/modules/companies/examples/example-response-for-get-company-by-registration-number.json');

describe('GET /companies?registrationNumber=', () => {
let api: Api;
Expand All @@ -13,11 +15,9 @@ describe('GET /companies?registrationNumber=', () => {
const {
companiesHousePath,
mdmPath,
getCompanyCompaniesHouseResponse,
getCompanyCompaniesHouseMalformedAuthorizationHeaderResponse,
getCompanyCompaniesHouseInvalidAuthorizationResponse,
getCompanyCompaniesHouseNotFoundResponse,
getCompanyResponse,
} = new GetCompanyGenerator(valueGenerator).generate({
numberToGenerate: 1,
registrationNumber: '00000001',
Expand Down
Loading

0 comments on commit da0260f

Please sign in to comment.