Skip to content

Commit f298e9e

Browse files
authored
Merge pull request #836 from companieshouse/SIV-940-update-association-requests-with-x-request-id
SIV-940 Update association requests to allow x-request-id headers
2 parents c0fa049 + 5a82498 commit f298e9e

File tree

2 files changed

+133
-24
lines changed

2 files changed

+133
-24
lines changed

src/services/associations/service.ts

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
SearchForCompanyAssociationPostBody
1414
} from "./types";
1515
import Mapping from "../../mapping/mapping";
16+
import { addRequestIdHeader } from "../../util";
1617

1718
/**
1819
* A service class for managing communications with the associations API.
@@ -38,7 +39,8 @@ export default class AssociationsService {
3839
companyNumber: string,
3940
includeRemoved?: boolean,
4041
pageIndex?: number,
41-
itemsPerPage?: number
42+
itemsPerPage?: number,
43+
requestId?: string
4244
): Promise<Resource<AssociationList | Errors>> {
4345
let queryString: string = "";
4446
if (includeRemoved || pageIndex || itemsPerPage) {
@@ -51,7 +53,8 @@ export default class AssociationsService {
5153
}
5254

5355
const url = `/associations/companies/${companyNumber}${queryString}`;
54-
const response = await this.client.httpGet(url);
56+
const headers = addRequestIdHeader(requestId);
57+
const response = await this.client.httpGet(url, headers);
5558

5659
return this.getResource(response) as Resource<AssociationList | Errors>;
5760
}
@@ -73,16 +76,17 @@ export default class AssociationsService {
7376
companyNumber: string,
7477
userEmail?: string,
7578
userId?: string,
76-
associationStatus?: AssociationStatus[]
79+
associationStatus?: AssociationStatus[],
80+
requestId?: string
7781
): Promise<Resource<Association | Errors>> {
7882
const url = `/associations/companies/${companyNumber}/search`;
7983

8084
const body: SearchForCompanyAssociationPostBody = {};
8185
if (userEmail) body.user_email = userEmail;
8286
if (userId) body.user_id = userId;
8387
if (associationStatus) body.status = associationStatus;
84-
85-
const response = await this.client.httpPost(url, body);
88+
const headers = addRequestIdHeader(requestId);
89+
const response = await this.client.httpPost(url, body, headers);
8690

8791
return this.getResource(response) as Resource<Association | Errors>;
8892
}
@@ -99,7 +103,8 @@ export default class AssociationsService {
99103
associationStatus: AssociationStatus[],
100104
pageIndex?: number,
101105
itemsPerPage?: number,
102-
companyNumber?: string
106+
companyNumber?: string,
107+
requestId?: string
103108
): Promise<Resource<AssociationList | Errors>> {
104109
const queryParameters: QueryParameters = {
105110
page_index: pageIndex || undefined,
@@ -110,7 +115,8 @@ export default class AssociationsService {
110115
const queryString = this.getQueryString(queryParameters);
111116

112117
const url = `/associations${queryString}`;
113-
const response = await this.client.httpGet(url);
118+
const headers = addRequestIdHeader(requestId);
119+
const response = await this.client.httpGet(url, headers);
114120

115121
return this.getResource(response) as Resource<AssociationList | Errors>;
116122
}
@@ -123,11 +129,13 @@ export default class AssociationsService {
123129
*/
124130
public async createAssociation (
125131
companyNumber: string,
126-
userId: string
132+
userId: string,
133+
requestId?: string
127134
): Promise<Resource<NewAssociationResponse | Errors>> {
128135
const url = "/associations";
129136
const body = { company_number: companyNumber, user_id: userId };
130-
const response = await this.client.httpPost(url, body);
137+
const headers = addRequestIdHeader(requestId);
138+
const response = await this.client.httpPost(url, body, headers);
131139
return this.getResource(response) as Resource<NewAssociationResponse | Errors>;
132140
}
133141

@@ -139,11 +147,13 @@ export default class AssociationsService {
139147
*/
140148
public async inviteUser (
141149
companyNumber: string,
142-
inviteeEmailAddress: string
150+
inviteeEmailAddress: string,
151+
requestId?: string
143152
): Promise<Resource<NewAssociationResponse | Errors>> {
144153
const url = "/associations/invitations";
145154
const body = { company_number: companyNumber, invitee_email_id: inviteeEmailAddress };
146-
const response = await this.client.httpPost(url, body);
155+
const headers = addRequestIdHeader(requestId);
156+
const response = await this.client.httpPost(url, body, headers);
147157
return this.getResource(response) as Resource<NewAssociationResponse | Errors>;
148158
}
149159

@@ -153,11 +163,12 @@ export default class AssociationsService {
153163
* @returns a promise that resolves to the HTTP response from the server that includes the association data or errors object.
154164
*/
155165
public async getAssociation (
156-
associationId: string
166+
associationId: string,
167+
requestId?: string
157168
): Promise<Resource<Association | Errors>> {
158169
const url = `/associations/${associationId}`;
159-
const response = await this.client.httpGet(url);
160-
170+
const headers = addRequestIdHeader(requestId);
171+
const response = await this.client.httpGet(url, headers);
161172
return this.getResource(response) as Resource<Association | Errors>;
162173
}
163174

@@ -169,12 +180,13 @@ export default class AssociationsService {
169180
*/
170181
public async updateAssociationStatus (
171182
associationId: string,
172-
status: AssociationStatus
183+
status: AssociationStatus,
184+
requestId?: string
173185
): Promise<Resource<undefined | Errors>> {
174186
const url = `/associations/${associationId}`;
175187
const body = { status: status };
176-
const response = await this.client.httpPatch(url, body);
177-
188+
const headers = addRequestIdHeader(requestId);
189+
const response = await this.client.httpPatch(url, body, headers);
178190
return this.getResource(response) as Resource<undefined | Errors>;
179191
}
180192

@@ -188,7 +200,8 @@ export default class AssociationsService {
188200
*/
189201
public async getInvitations (
190202
pageIndex?: number,
191-
itemsPerPage?: number
203+
itemsPerPage?: number,
204+
requestId?: string
192205
): Promise<Resource<InvitationList | Errors>> {
193206
const queryParameters: QueryParameters = {
194207
page_index: pageIndex || undefined,
@@ -197,7 +210,9 @@ export default class AssociationsService {
197210
const queryString = this.getQueryString(queryParameters);
198211

199212
const url = `/associations/invitations${queryString}`;
200-
const response = await this.client.httpGet(url);
213+
const headers = addRequestIdHeader(requestId);
214+
215+
const response = await this.client.httpGet(url, headers);
201216

202217
return this.getResource(response) as Resource<InvitationList | Errors>;
203218
}
@@ -210,15 +225,16 @@ export default class AssociationsService {
210225
*/
211226
public async postInvitation (
212227
companyNumber: string,
213-
inviteeEmailAddress: string
228+
inviteeEmailAddress: string,
229+
requestId?: string
214230
): Promise<Resource<NewAssociationResponse | Errors>> {
215231
const body = {
216232
company_number: companyNumber,
217233
invitee_email_id: inviteeEmailAddress
218234
};
219235
const url = `/associations/invitations`;
220-
221-
const response = await this.client.httpPost(url, body);
236+
const headers = addRequestIdHeader(requestId);
237+
const response = await this.client.httpPost(url, body, headers);
222238

223239
return this.getResource(response) as Resource<NewAssociationResponse | Errors>;
224240
}
@@ -233,7 +249,8 @@ export default class AssociationsService {
233249
public async getPreviousStates (
234250
associationID: string,
235251
pageIndex?: number,
236-
itemsPerPage?: number
252+
itemsPerPage?: number,
253+
requestId?: string
237254
): Promise<Resource<PreviousStateList | Errors>> {
238255
let queryString: string = "";
239256
if (pageIndex || itemsPerPage) {
@@ -245,7 +262,8 @@ export default class AssociationsService {
245262
}
246263

247264
const url = `/associations/${associationID}/previous-states${queryString}`;
248-
const response = await this.client.httpGet(url);
265+
const headers = addRequestIdHeader(requestId);
266+
const response = await this.client.httpGet(url, headers);
249267

250268
return this.getResource(response) as Resource<PreviousStateList | Errors>;
251269
}

test/services/associations/service.spec.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@ describe("AssociationsService", () => {
211211
expect(data.httpStatusCode).to.equal(500);
212212
});
213213
});
214+
215+
it("should include the x-request-id header when requestId is provided", async () => {
216+
const expectedHeader = { "X-Request-Id": "random-uuid" };
217+
const mockRequest = sinon.stub(requestClient, "httpGet").resolves(mockGetResponse[200]);
218+
await associationsService.getCompanyAssociations(companyNumber, undefined, undefined, undefined, "random-uuid")
219+
220+
expect(mockRequest.calledOnce).to.be.true;
221+
expect(mockRequest.firstCall.args[1]).to.deep.equal(expectedHeader);
222+
});
214223
});
215224

216225
describe("searchForCompanyAssociation", () => {
@@ -333,6 +342,14 @@ describe("AssociationsService", () => {
333342
expect(data.httpStatusCode).to.equal(500);
334343
});
335344
});
345+
346+
it("should include the x-request-id header when requestId is provided", async () => {
347+
const expectedHeader = { "X-Request-Id": "random-uuid" };
348+
const mockRequest = sinon.stub(requestClient, "httpPost").resolves(mockGetResponse[200]);
349+
await associationsService.searchForCompanyAssociation(companyNumber, undefined, undefined, undefined, "random-uuid")
350+
expect(mockRequest.calledOnce).to.be.true;
351+
expect(mockRequest.firstCall.args[2]).to.deep.equal(expectedHeader);
352+
});
336353
});
337354

338355
describe("searchAssociations", () => {
@@ -428,6 +445,16 @@ describe("AssociationsService", () => {
428445
expect(data.httpStatusCode).to.equal(500);
429446
});
430447
});
448+
449+
it("should include the x-request-id header when requestId is provided", async () => {
450+
const expectedHeader = { "X-Request-Id": "random-uuid" };
451+
const mockRequest = sinon.stub(requestClient, "httpGet").resolves(mockGetResponse[200])
452+
const status = [AssociationStatus.AWAITING_APPROVAL];
453+
454+
await associationsService.searchAssociations(status, undefined, undefined, undefined, "random-uuid")
455+
expect(mockRequest.calledOnce).to.be.true;
456+
expect(mockRequest.firstCall.args[1]).to.deep.equal(expectedHeader);
457+
});
431458
});
432459

433460
describe("createAssociation", () => {
@@ -488,6 +515,15 @@ describe("AssociationsService", () => {
488515
expect(data.httpStatusCode).to.equal(500);
489516
});
490517
});
518+
519+
it("should include the x-request-id header when requestId is provided", async () => {
520+
const expectedHeader = { "X-Request-Id": "random-uuid" };
521+
const mockRequest = sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[201]);
522+
523+
await associationsService.createAssociation(companyNumber, userId, "random-uuid")
524+
expect(mockRequest.calledOnce).to.be.true;
525+
expect(mockRequest.firstCall.args[2]).to.deep.equal(expectedHeader);
526+
});
491527
});
492528

493529
describe("inviteUser", () => {
@@ -540,6 +576,15 @@ describe("AssociationsService", () => {
540576
expect(data.httpStatusCode).to.equal(500);
541577
});
542578
});
579+
580+
it("should include the x-request-id header when requestId is provided", async () => {
581+
const expectedHeader = { "X-Request-Id": "random-uuid" };
582+
const mockRequest = sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[201]);
583+
584+
await associationsService.inviteUser(companyNumber, inviteeEmailAddress, "random-uuid")
585+
expect(mockRequest.calledOnce).to.be.true;
586+
expect(mockRequest.firstCall.args[2]).to.deep.equal(expectedHeader);
587+
});
543588
});
544589

545590
describe("getAssociation", () => {
@@ -625,6 +670,15 @@ describe("AssociationsService", () => {
625670
expect(data.httpStatusCode).to.equal(500);
626671
});
627672
});
673+
674+
it("should include the x-request-id header when requestId is provided", async () => {
675+
const expectedHeader = { "X-Request-Id": "random-uuid" };
676+
const mockRequest = sinon.stub(requestClient, "httpGet").resolves(mockGetResponse[200]);
677+
678+
await associationsService.getAssociation("", "random-uuid")
679+
expect(mockRequest.calledOnce).to.be.true;
680+
expect(mockRequest.firstCall.args[1]).to.deep.equal(expectedHeader);
681+
});
628682
});
629683

630684
describe("updateAssociationStatus", () => {
@@ -695,6 +749,16 @@ describe("AssociationsService", () => {
695749
expect(data.httpStatusCode).to.equal(500);
696750
});
697751
});
752+
753+
it("should include the x-request-id header when requestId is provided", async () => {
754+
const expectedHeader = { "X-Request-Id": "random-uuid" };
755+
const mockRequest = sinon.stub(requestClient, "httpPatch").resolves(mockGetResponse[200]);
756+
const status = AssociationStatus.REMOVED;
757+
758+
await associationsService.updateAssociationStatus("associationId", status, "random-uuid")
759+
expect(mockRequest.calledOnce).to.be.true;
760+
expect(mockRequest.firstCall.args[2]).to.deep.equal(expectedHeader);
761+
});
698762
});
699763

700764
describe("getInvitations", () => {
@@ -793,6 +857,15 @@ describe("AssociationsService", () => {
793857
expect(data.httpStatusCode).to.equal(500);
794858
});
795859
});
860+
861+
it("should include the x-request-id header when requestId is provided", async () => {
862+
const expectedHeader = { "X-Request-Id": "random-uuid" };
863+
const mockRequest = sinon.stub(requestClient, "httpGet").resolves(mockGetInvitationsResponse[200]);
864+
865+
await associationsService.getInvitations(undefined, undefined, "random-uuid")
866+
expect(mockRequest.calledOnce).to.be.true;
867+
expect(mockRequest.firstCall.args[1]).to.deep.equal(expectedHeader);
868+
});
796869
});
797870

798871
describe("postInvitation", () => {
@@ -845,6 +918,15 @@ describe("AssociationsService", () => {
845918
expect(data.httpStatusCode).to.equal(500);
846919
});
847920
});
921+
922+
it("should include the x-request-id header when requestId is provided", async () => {
923+
const expectedHeader = { "X-Request-Id": "random-uuid" };
924+
const mockRequest = sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[201]);
925+
926+
await associationsService.postInvitation(companyNumber, inviteeEmailAddress, "random-uuid")
927+
expect(mockRequest.calledOnce).to.be.true;
928+
expect(mockRequest.firstCall.args[2]).to.deep.equal(expectedHeader);
929+
});
848930
});
849931

850932
describe("getPreviousStates", () => {
@@ -941,5 +1023,14 @@ describe("AssociationsService", () => {
9411023
expect(data.httpStatusCode).to.equal(500);
9421024
});
9431025
});
1026+
1027+
it("should include the x-request-id header when requestId is provided", async () => {
1028+
const expectedHeader = { "X-Request-Id": "random-uuid" };
1029+
const mockRequest = sinon.stub(requestClient, "httpGet").resolves(mockGetInvitationsResponse[200]);
1030+
1031+
await associationsService.getPreviousStates("", undefined, undefined, "random-uuid")
1032+
expect(mockRequest.calledOnce).to.be.true;
1033+
expect(mockRequest.firstCall.args[1]).to.deep.equal(expectedHeader);
1034+
});
9441035
});
9451036
});

0 commit comments

Comments
 (0)