Skip to content

Commit da5e795

Browse files
authored
Merge pull request #214 from companieshouse/ncs-578-add-psc-statements-to-sdk
Add psc statements service to sdk
2 parents a12c32e + 1ac0bcd commit da5e795

File tree

5 files changed

+200
-1
lines changed

5 files changed

+200
-1
lines changed

src/client.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import EnhancedSearchService from "./services/search/enhanced-search/service";
1313
import PSCDiscrepancyService from "./services/psc-discrepancies/service";
1414
import PSCDiscrepanciesReportService from "./services/psc-discrepancies-report/service";
1515
import TransactionService from "./services/transaction/service";
16+
import CompanyPscStatementsService from "./services/company-psc-statements/service";
1617

1718
/**
1819
* ApiClient is the class that all service objects hang off.
@@ -22,7 +23,8 @@ export default class ApiClient {
2223
public readonly companyOfficers: CompanyOfficersService;
2324
public readonly companyFilingHistory: CompanyFilingHistoryService;
2425
public readonly companyProfile: CompanyProfileService;
25-
public readonly companyPsc : CompanyPscService ;
26+
public readonly companyPsc: CompanyPscService;
27+
public readonly companyPscStatements: CompanyPscStatementsService;
2628
public readonly certificate: CertificateService;
2729
public readonly certifiedCopies: CertifiedCopiesService;
2830
public readonly basket: BasketService;
@@ -44,6 +46,7 @@ export default class ApiClient {
4446
this.companyFilingHistory = new CompanyFilingHistoryService(apiClient);
4547
this.companyProfile = new CompanyProfileService(apiClient);
4648
this.companyPsc = new CompanyPscService(apiClient);
49+
this.companyPscStatements = new CompanyPscStatementsService(apiClient);
4750
this.certificate = new CertificateService(apiClient);
4851
this.certifiedCopies = new CertifiedCopiesService(apiClient);
4952
this.basket = new BasketService(apiClient);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./types";
2+
export * from "./service";
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { IHttpClient } from "../../http";
2+
import Resource, { ApiErrorResponse } from "../resource";
3+
import Mapping from "../../mapping/mapping";
4+
import {
5+
CompanyPersonsWithSignificantControlStatements,
6+
CompanyPersonsWithSignificantControlStatementsResource
7+
} from "./types";
8+
9+
/**
10+
* https://developer-specs.company-information.service.gov.uk/companies-house-public-data-api/reference/persons-with-significant-control/list-statements
11+
*/
12+
export default class CompanyPscStatementsService {
13+
constructor (private readonly client: IHttpClient) { }
14+
15+
/**
16+
* Get the PSC statements for a company.
17+
*
18+
* @param companyNumber the company number to look up
19+
* @param pageSize the size of the page to return
20+
* @param pageIndex the index of the page to return
21+
* @param registerView Display register specific information.
22+
*/
23+
public async getCompanyPscStatements (companyNumber: string, pageSize: number, pageIndex: number, registerView: boolean = false): Promise<Resource<CompanyPersonsWithSignificantControlStatements> | ApiErrorResponse> {
24+
let url = `/company/${companyNumber}/persons-with-significant-control-statements`;
25+
url = url.concat("?",
26+
`page_size=${pageSize}`,
27+
"&",
28+
`page_index=${pageIndex}`,
29+
"&",
30+
`register_view=${registerView}`);
31+
32+
const resp = await this.client.httpGet(url);
33+
34+
if (resp.status !== 200) {
35+
return {
36+
httpStatusCode: resp.status,
37+
errors: resp.error
38+
}
39+
}
40+
41+
const sdkResponse: Resource<CompanyPersonsWithSignificantControlStatements> = {
42+
httpStatusCode: resp.status
43+
};
44+
45+
const body = resp.body as CompanyPersonsWithSignificantControlStatementsResource;
46+
47+
sdkResponse.resource = Mapping.camelCaseKeys<CompanyPersonsWithSignificantControlStatements>(body);
48+
49+
return sdkResponse;
50+
}
51+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* CompanyPersonsWithSignificantControlStatementsResource is what is returned from the api.
3+
*/
4+
export interface CompanyPersonsWithSignificantControlStatementsResource {
5+
active_count: string,
6+
ceased_count: string,
7+
items: CompanyPersonWithSignificantControlStatementResource[],
8+
items_per_page?: string,
9+
links: { self : string },
10+
start_index?: string,
11+
total_results: string
12+
}
13+
14+
export interface CompanyPersonWithSignificantControlStatementResource {
15+
ceased_on?: string,
16+
etag: string,
17+
kind: string,
18+
linked_psc_name?: string,
19+
links: {
20+
person_with_significant_control?: string,
21+
self: string
22+
},
23+
notified_on: string,
24+
restrictions_notice_withdrawal_reason?: string,
25+
statement: string
26+
}
27+
28+
/**
29+
* CompanyPersonsWithSignificantControlStatements is what is returned from the sdk.
30+
*/
31+
export interface CompanyPersonsWithSignificantControlStatements {
32+
activeCount: string,
33+
ceasedCount: string,
34+
items: CompanyPersonWithSignificantControlStatement[],
35+
itemsPerPage?: string,
36+
links: { self : string },
37+
startIndex?: string,
38+
totalResults: string
39+
}
40+
41+
export interface CompanyPersonWithSignificantControlStatement {
42+
ceasedOn?: string,
43+
etag: string,
44+
kind: string,
45+
linkedPscName?: string,
46+
links: {
47+
personWithSignificantControl?: string,
48+
self: string
49+
},
50+
notifiedOn: string,
51+
restrictionsNoticeWithdrawalReason?: string,
52+
statement: string
53+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import chai from "chai";
2+
import sinon from "sinon";
3+
4+
import { RequestClient } from "../../../src/http";
5+
import CompanyPscStatementsService from "../../../src/services/company-psc-statements/service";
6+
import {
7+
CompanyPersonsWithSignificantControlStatements,
8+
CompanyPersonsWithSignificantControlStatementsResource
9+
} from "../../../src/services/company-psc-statements";
10+
import Resource from "../../../src/services/resource";
11+
const expect = chai.expect;
12+
13+
const requestClient = new RequestClient({ baseUrl: "URL-NOT-USED", oauthToken: "TOKEN-NOT-USED" });
14+
15+
describe("company-psc-statements", () => {
16+
beforeEach(() => {
17+
sinon.reset();
18+
sinon.restore();
19+
});
20+
21+
afterEach(done => {
22+
sinon.reset();
23+
sinon.restore();
24+
done();
25+
});
26+
27+
it("returns an error response on failure", async () => {
28+
const mockGetResponse = {
29+
status: 401,
30+
error: "An error occurred"
31+
};
32+
sinon.stub(requestClient, "httpGet").resolves(mockGetResponse);
33+
const companyPscStatement : CompanyPscStatementsService = new CompanyPscStatementsService(requestClient);
34+
const data = await companyPscStatement.getCompanyPscStatements("NUMBER-NOT-IMPORTANT", 2, 1);
35+
36+
expect(data.httpStatusCode).to.equal(401);
37+
});
38+
39+
it("maps the psc statement data items correctly", async () => {
40+
const mockResponseBody : CompanyPersonsWithSignificantControlStatementsResource = ({
41+
active_count: "1",
42+
ceased_count: "0",
43+
items_per_page: "1",
44+
start_index: "0",
45+
total_results: "1",
46+
links: {
47+
self: "statementsSelfLink"
48+
},
49+
items: [
50+
{
51+
ceased_on: "ceased",
52+
etag: "etag",
53+
kind: "kind",
54+
links: { self: "statementSelfLink" },
55+
notified_on: "notifiedOn",
56+
statement: "statement"
57+
}
58+
]
59+
});
60+
61+
const mockGetResponse = {
62+
status: 200,
63+
body: mockResponseBody
64+
};
65+
66+
sinon.stub(requestClient, "httpGet").resolves(mockGetResponse);
67+
const companyPscStatementService : CompanyPscStatementsService = new CompanyPscStatementsService(requestClient);
68+
const data = await companyPscStatementService.getCompanyPscStatements("NUMBER-NOT-IMPORTANT", 2, 1);
69+
70+
expect(data.httpStatusCode).to.equal(200);
71+
72+
const castedResponse: Resource<CompanyPersonsWithSignificantControlStatements> = data as Resource<CompanyPersonsWithSignificantControlStatements>
73+
74+
expect(castedResponse.resource.activeCount).to.equal(mockResponseBody.active_count);
75+
expect(castedResponse.resource.ceasedCount).to.equal(mockResponseBody.ceased_count);
76+
expect(castedResponse.resource.itemsPerPage).to.equal(mockResponseBody.items_per_page);
77+
expect(castedResponse.resource.startIndex).to.equal(mockResponseBody.start_index);
78+
expect(castedResponse.resource.totalResults).to.equal(mockResponseBody.total_results);
79+
80+
expect(castedResponse.resource.links.self).to.equal(mockResponseBody.links.self);
81+
82+
expect(castedResponse.resource.items.length).to.equal(mockResponseBody.items.length);
83+
84+
expect(castedResponse.resource.items[0].ceasedOn).to.equal(mockResponseBody.items[0].ceased_on);
85+
expect(castedResponse.resource.items[0].etag).to.equal(mockResponseBody.items[0].etag);
86+
expect(castedResponse.resource.items[0].kind).to.equal(mockResponseBody.items[0].kind);
87+
expect(castedResponse.resource.items[0].notifiedOn).to.equal(mockResponseBody.items[0].notified_on);
88+
expect(castedResponse.resource.items[0].statement).to.equal(mockResponseBody.items[0].statement);
89+
});
90+
});

0 commit comments

Comments
 (0)