Skip to content

Commit addcd0f

Browse files
Merge pull request #139 from companieshouse/feature/faml-746-psc-discrepancies
Feature/faml 746 psc discrepancies
2 parents b668da3 + 32d348f commit addcd0f

File tree

10 files changed

+871
-0
lines changed

10 files changed

+871
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./types";
2+
export { default as PSCDiscrepanciesReportService } from "./service";
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { IHttpClient } from "../../http";
2+
import { PSCDiscrepancyReport } from "./types"
3+
import Util from "./util"
4+
import { Result } from "services/result";
5+
import { ApiResponse, ApiErrorResponse } from "services/resource";
6+
7+
const PSC_DISCREPANCY_API_URL = "/psc-discrepancy-reports";
8+
9+
type PromisedReportResult= Promise<Result<ApiResponse<PSCDiscrepancyReport>, ApiErrorResponse>>;
10+
11+
export default class {
12+
utility: Util;
13+
constructor (private readonly client: IHttpClient) { this.utility = new Util() }
14+
15+
public async getReport (reportId: string): PromisedReportResult {
16+
return this.getReportBySelfLink(this.buildSelfLink(reportId));
17+
}
18+
19+
public async getReportBySelfLink (selfLink: string): PromisedReportResult {
20+
const resp = await this.client.httpGet(`${selfLink}`);
21+
return this.utility.processResponse(resp);
22+
}
23+
24+
public async createNewReport (obligedEntityType: String): PromisedReportResult {
25+
const resp = await this.client.httpPost(
26+
PSC_DISCREPANCY_API_URL,
27+
{
28+
obligedEntityType: obligedEntityType,
29+
status: "INCOMPLETE"
30+
});
31+
return this.utility.processResponse(resp);
32+
}
33+
34+
public async updateReport (reportId: string, report: PSCDiscrepancyReport): PromisedReportResult {
35+
return this.updateReportBySelfLink(this.buildSelfLink(reportId), report);
36+
}
37+
38+
public async updateReportBySelfLink (selfLink: string, report: PSCDiscrepancyReport): PromisedReportResult {
39+
const resp = await this.client.httpPut(selfLink, report);
40+
return this.utility.processResponse(resp);
41+
}
42+
43+
private buildSelfLink (reportId: string): string {
44+
return `${PSC_DISCREPANCY_API_URL}/${reportId}`;
45+
}
46+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export interface PSCDiscrepancyReport {
2+
obliged_entity_organisation_name: string;
3+
obliged_entity_name: string;
4+
obliged_entity_contact_name: string;
5+
obliged_entity_email: string;
6+
obliged_entity_telephone_number: string;
7+
obliged_entity_type: string;
8+
company_number: string;
9+
submission_reference: string;
10+
status: string;
11+
etag: string;
12+
kind: string;
13+
links: LinksResource;
14+
}
15+
16+
export interface LinksResource {
17+
self: string;
18+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { HttpResponse } from "../../http";
2+
import Resource, { ApiErrorResponse, ApiError, ApiResponse } from "../../services/resource";
3+
import { failure, success, Result, Success, Failure } from "../result";
4+
import Mapping from "../../mapping/mapping";
5+
6+
type NestedErrors = { errors: ApiError[] }
7+
8+
export default class {
9+
constructor () { }
10+
11+
processResponse (resp: HttpResponse): Result<ApiResponse<any>, ApiErrorResponse> {
12+
if (resp.error) {
13+
const error: ApiErrorResponse = {
14+
httpStatusCode: resp.status,
15+
errors: this.buildErrors(resp.error)
16+
};
17+
return failure(error);
18+
} else {
19+
return success({
20+
httpStatusCode: resp.status,
21+
headers: resp.headers,
22+
resource: resp.body
23+
})
24+
}
25+
}
26+
27+
private buildErrors (errors: string | NestedErrors): ApiError[] {
28+
if (typeof errors === "string") {
29+
const ret: ApiError = {
30+
error: errors
31+
};
32+
return [ret];
33+
} else if ((errors as NestedErrors).errors) {
34+
return (errors as NestedErrors).errors.reduce<ApiError[]>((previousValue, currentValue) => {
35+
return [...previousValue, Mapping.camelCaseKeys(currentValue)]
36+
}, []);
37+
} else {
38+
return [];
39+
}
40+
}
41+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./types";
2+
export { default as PSCDiscrepanciesService } from "./service";
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { IHttpClient } from "../../http";
2+
import { ApiErrorResponse, ApiResponse } from "../../services/resource";
3+
import { PSCDiscrepancy } from "./types"
4+
import { Result } from "../result";
5+
import Util from "../psc-discrepancies-report/util"
6+
7+
type PromisedDiscrepancyResult = Promise<Result<ApiResponse<PSCDiscrepancy>, ApiErrorResponse>>;
8+
type PromisedDiscrepanciesResult = Promise<Result<ApiResponse<PSCDiscrepancy[]>, ApiErrorResponse>>;
9+
10+
export default class {
11+
utility: Util;
12+
constructor (private readonly client: IHttpClient) { this.utility = new Util() }
13+
14+
public async getPscDiscrepanciesForReport (reportSelfLink: string): PromisedDiscrepanciesResult {
15+
const resp = await this.client.httpGet(this.buildBaseURL(reportSelfLink));
16+
17+
return this.utility.processResponse(resp);
18+
}
19+
20+
public async getPscDiscrepancy (selfLink: string): PromisedDiscrepancyResult {
21+
const resp = await this.client.httpGet(selfLink);
22+
return this.utility.processResponse(resp);
23+
}
24+
25+
public async createPscDiscrepancy (reportSelfLink: string, discrepancy:PSCDiscrepancy): PromisedDiscrepancyResult {
26+
const resp = await this.client.httpPost(this.buildBaseURL(reportSelfLink), discrepancy);
27+
return this.utility.processResponse(resp);
28+
}
29+
30+
private buildBaseURL (reportSelfLink: string): string {
31+
return `${reportSelfLink}/discrepancies`;
32+
}
33+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export interface PSCDiscrepancy {
2+
links: LinksResource,
3+
etag: string,
4+
kind: string,
5+
details: string,
6+
psc_name: string,
7+
psc_date_of_birth: string
8+
}
9+
export interface LinksResource {
10+
self: string;
11+
psc_discrepancy_report: string;
12+
}

0 commit comments

Comments
 (0)