Skip to content

Commit 3394c87

Browse files
authored
Merge pull request #185 from companieshouse/ncs-449-add-transaction-to-sdk
NCS-449 adding transaction to SDK
2 parents 0dab735 + 0163d9a commit 3394c87

File tree

5 files changed

+162
-1
lines changed

5 files changed

+162
-1
lines changed

src/client.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import AlphabeticalSearchService from "./services/search/alphabetical-search/ser
1111
import DissolvedSearchService from "./services/search/dissolved-search/service";
1212
import PSCDiscrepancyService from "./services/psc-discrepancies/service";
1313
import PSCDiscrepanciesReportService from "./services/psc-discrepancies-report/service";
14+
import TransactionService from "./services/transaction/service";
1415

1516
/**
1617
* ApiClient is the class that all service objects hang off.
@@ -31,7 +32,8 @@ export default class ApiClient {
3132
public readonly alphabeticalSearch: AlphabeticalSearchService;
3233
public readonly dissolvedSearch: DissolvedSearchService;
3334
public readonly pscDiscrepancies: PSCDiscrepancyService;
34-
public readonly pscDiscrepancyReport:PSCDiscrepanciesReportService;
35+
public readonly pscDiscrepancyReport: PSCDiscrepanciesReportService;
36+
public readonly transaction: TransactionService;
3537

3638
constructor (readonly apiClient: IHttpClient, readonly accountClient: IHttpClient) {
3739
// services on the api domain using the apiClient
@@ -53,5 +55,6 @@ export default class ApiClient {
5355
this.refreshToken = new RefreshTokenService(accountClient);
5456
this.pscDiscrepancies = new PSCDiscrepancyService(apiClient);
5557
this.pscDiscrepancyReport = new PSCDiscrepanciesReportService(apiClient);
58+
this.transaction = new TransactionService(apiClient);
5659
}
5760
}

src/services/transaction/index.ts

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 {
3+
Transaction, TransactionResource
4+
5+
} from "./types";
6+
import Resource from "../resource";
7+
8+
export default class TransactionService {
9+
constructor (private readonly client: IHttpClient) { }
10+
11+
/**
12+
* Post a transaction.
13+
*
14+
* @param transaction the transaction to create
15+
*/
16+
public async postTransaction (transaction: Transaction): Promise<Resource<Transaction>> {
17+
let url = "/transactions"
18+
if (transaction.id) {
19+
url += "/" + transaction.id
20+
}
21+
22+
const resp = await this.client.httpPost(url);
23+
24+
const resource: Resource<Transaction> = {
25+
httpStatusCode: resp.status
26+
};
27+
28+
if (resp.error) {
29+
return resource;
30+
}
31+
32+
// cast the response body to the expected type
33+
const body = resp.body as TransactionResource;
34+
35+
resource.resource = {
36+
id: body.id,
37+
etag: body.etag,
38+
links: body.links,
39+
reference: body.reference,
40+
status: body.status,
41+
kind: body.kind,
42+
companyName: body.company_name,
43+
companyNumber: body.company_number,
44+
createdAt: body.created_at,
45+
createdBy: body.created_by,
46+
updatedAt: body.updated_at,
47+
description: body.description
48+
}
49+
return resource;
50+
}
51+
}

src/services/transaction/types.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* TransactionResource is what is returned from the api.
3+
*/
4+
export interface TransactionResource {
5+
id?: string,
6+
etag?: string,
7+
links?: {self: string},
8+
reference: string,
9+
status?: string,
10+
kind?: string,
11+
company_name?: string,
12+
company_number: string,
13+
created_at?: string,
14+
created_by?: {
15+
language: string,
16+
id: string,
17+
email: string
18+
},
19+
updated_at?: string,
20+
description: string
21+
}
22+
23+
export interface Transaction {
24+
id?: string,
25+
etag?: string,
26+
links?: {self: string},
27+
reference: string,
28+
status?: string,
29+
kind?: string,
30+
companyName?: string,
31+
companyNumber: string,
32+
createdAt?: string,
33+
createdBy?: {
34+
language: string,
35+
id: string,
36+
email: string
37+
},
38+
updatedAt?: string,
39+
description: string
40+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import chai from "chai";
2+
import sinon from "sinon";
3+
4+
import TransactionService from "../../../src/services/transaction/service";
5+
import { RequestClient } from "../../../src/http";
6+
import { Transaction, TransactionResource } from "../../../src/services/transaction";
7+
const expect = chai.expect;
8+
9+
const requestClient = new RequestClient({ baseUrl: "URL-NOT-USED", oauthToken: "TOKEN-NOT-USED" });
10+
11+
describe("transaction", () => {
12+
beforeEach(() => {
13+
sinon.reset();
14+
sinon.restore();
15+
});
16+
17+
afterEach(done => {
18+
sinon.reset();
19+
sinon.restore();
20+
done();
21+
});
22+
23+
it("returns an error response on failure", async () => {
24+
const mockPostResponse = {
25+
status: 401,
26+
error: "An error occurred"
27+
};
28+
29+
const mockRequest = sinon.stub(requestClient, "httpPost").resolves(mockPostResponse);
30+
const transaction : TransactionService = new TransactionService(requestClient);
31+
const data = await transaction.postTransaction({} as Transaction);
32+
33+
expect(data.httpStatusCode).to.equal(401);
34+
expect(data.resource).to.be.undefined;
35+
});
36+
37+
it("maps the company field data items correctly", async () => {
38+
const mockResponseBody : TransactionResource = ({
39+
id: "12345678",
40+
company_name: "HELLO LTD",
41+
company_number: "88",
42+
links: {
43+
self: "/self"
44+
},
45+
reference: "ref",
46+
description: "desc"
47+
});
48+
49+
const mockPostResponse = {
50+
status: 200,
51+
body: mockResponseBody
52+
};
53+
54+
const mockRequest = sinon.stub(requestClient, "httpPost").resolves(mockPostResponse);
55+
const transaction : TransactionService = new TransactionService(requestClient);
56+
const data = await transaction.postTransaction({} as Transaction);
57+
58+
expect(data.httpStatusCode).to.equal(200);
59+
expect(data.resource.companyName).to.equal(mockResponseBody.company_name);
60+
expect(data.resource.companyNumber).to.equal(mockResponseBody.company_number);
61+
expect(data.resource.links.self).to.equal(mockResponseBody.links.self);
62+
expect(data.resource.reference).to.equal(mockResponseBody.reference);
63+
expect(data.resource.description).to.equal(mockResponseBody.description);
64+
});
65+
});

0 commit comments

Comments
 (0)