Skip to content

Commit 89bdb94

Browse files
authored
Merge pull request #222 from companieshouse/ncs-634-adding-get-transaction-call-to-transaction-service
NCS-634 Adding get transaction call to transaction service
2 parents 2202186 + 16cf72d commit 89bdb94

File tree

3 files changed

+88
-4
lines changed

3 files changed

+88
-4
lines changed

package-lock.json

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/services/transaction/service.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,46 @@ export default class TransactionService {
9494
};
9595
}
9696

97+
/**
98+
* Get a transaction.
99+
*
100+
* @param transactionId the id of the transaction to retrieve
101+
*/
102+
public async getTransaction (transactionId: string): Promise<Resource<Transaction>|ApiErrorResponse> {
103+
const url = "/transactions/" + transactionId
104+
const resp = await this.client.httpGet(url);
105+
106+
if (resp.error) {
107+
return {
108+
httpStatusCode: resp.status,
109+
errors: [resp.error]
110+
};
111+
}
112+
113+
const resource: Resource<Transaction> = {
114+
httpStatusCode: resp.status
115+
};
116+
117+
// cast the response body to the expected type
118+
const body = resp.body as TransactionResource;
119+
120+
resource.resource = {
121+
id: body.id,
122+
etag: body.etag,
123+
links: body.links,
124+
reference: body.reference,
125+
status: body.status,
126+
kind: body.kind,
127+
companyName: body.company_name,
128+
companyNumber: body.company_number,
129+
createdAt: body.created_at,
130+
createdBy: body.created_by,
131+
updatedAt: body.updated_at,
132+
description: body.description
133+
}
134+
return resource;
135+
}
136+
97137
private mapToResource (transaction:Transaction):TransactionResource {
98138
return {
99139
company_name: transaction.companyName,

test/services/transaction/service.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,51 @@ describe("transaction", () => {
6767
expect(castedData.resource.description).to.equal(mockResponseBody.description);
6868
});
6969

70+
it("get returns an error response on failure", async () => {
71+
const mockGetResponse = {
72+
status: 401,
73+
error: "An error occurred"
74+
};
75+
76+
const mockRequest = sinon.stub(requestClient, "httpGet").resolves(mockGetResponse);
77+
const transaction : TransactionService = new TransactionService(requestClient);
78+
const data = await transaction.getTransaction({} as string);
79+
80+
expect(data.httpStatusCode).to.equal(401);
81+
const castedData: ApiErrorResponse = data;
82+
expect(castedData.errors[0]).to.equal("An error occurred");
83+
});
84+
85+
it("get maps the company field data items correctly", async () => {
86+
const mockResponseBody : TransactionResource = ({
87+
id: "12345678",
88+
company_name: "HELLO LTD",
89+
company_number: "88",
90+
links: {
91+
self: "/self"
92+
},
93+
reference: "ref",
94+
description: "desc"
95+
});
96+
97+
const mockGetResponse = {
98+
status: 200,
99+
body: mockResponseBody
100+
};
101+
102+
const mockRequest = sinon.stub(requestClient, "httpGet").resolves(mockGetResponse);
103+
const transaction : TransactionService = new TransactionService(requestClient);
104+
const data = await transaction.getTransaction({} as string);
105+
106+
expect(data.httpStatusCode).to.equal(200);
107+
const castedData: Resource<Transaction> = data as Resource<Transaction>;
108+
expect(castedData.resource.companyName).to.equal(mockResponseBody.company_name);
109+
expect(castedData.resource.companyNumber).to.equal(mockResponseBody.company_number);
110+
expect(castedData.resource.links.self).to.equal(mockResponseBody.links.self);
111+
expect(castedData.resource.reference).to.equal(mockResponseBody.reference);
112+
expect(castedData.resource.description).to.equal(mockResponseBody.description);
113+
});
114+
70115
it("put returns successful response", async () => {
71116
const mockResponseBody : TransactionResource = ({
72117
id: "12345678",

0 commit comments

Comments
 (0)