Skip to content

Commit 9de27a9

Browse files
Merge pull request #798 from companieshouse/feature/siv-461-amendments-for-associations-companies-company-number
SIV-461 amendments for associations companies company number
2 parents 1af09a7 + 3a7de30 commit 9de27a9

File tree

2 files changed

+111
-14
lines changed

2 files changed

+111
-14
lines changed

src/services/associations/service.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export default class AssociationsService {
3131
* @param includeRemoved - a flag to get a list of companies where status is "removed". Default value: false.
3232
* @param pageIndex - a page number to be returned. Default value: 0.
3333
* @param itemsPerPage - a number of items to be returned per page. Default value: 15.
34-
* @param userEmail - an email address of a user to check if associated with the company.
3534
* @param userId - a unique identifier of a user to check if associated with the company.
3635
* @returns a promise that resolves to the HTTP response from the server that includes the associations or errors object.
3736
*/
@@ -40,7 +39,6 @@ export default class AssociationsService {
4039
includeRemoved?: boolean,
4140
pageIndex?: number,
4241
itemsPerPage?: number,
43-
userEmail?: string,
4442
userId?: string
4543
): Promise<Resource<AssociationList | Errors>> {
4644
let queryString: string = "";
@@ -54,25 +52,34 @@ export default class AssociationsService {
5452
queryString = this.getQueryString(queryParameters);
5553
}
5654

57-
let headers: Headers;
58-
if (userEmail) {
59-
headers = { user_email: userEmail };
60-
}
61-
6255
const url = `/associations/companies/${companyNumber}${queryString}`;
63-
const response = await this.client.httpGet(url, headers);
56+
const response = await this.client.httpGet(url);
6457

6558
return this.getResource(response) as Resource<AssociationList | Errors>;
6659
}
6760

6861
/**
69-
* Initiates an HTTP GET request to retrieve the associations searched based on association status.
70-
* @param associationStatus - an association status used to filter associations. This parameter is required. Available values: confirmed, awaiting-approval, removed. Default value: confirmed.
71-
* @param pageIndex - a page to be returned. Default value: 0.
72-
* @param itemsPerPage - a number of items returned per page. Default value: 15.
73-
* @param companyNumber - a filter based on company number.
74-
* @returns a promise that resolves to the HTTP response from the server that includes the associations or errors object.
62+
* Initiates an HTTP GET request to retrieve the association for a company for the provided user email.
63+
* @param companyNumber - a company number of the company for which the associations should be retrieved.
64+
* @param userEmail - an email address of a user to check if associated with the company.
65+
* @returns a promise that resolves to the HTTP response from the server that includes the association or errors object.
7566
*/
67+
public async getCompanyAssociationByUserEmail (companyNumber: string, userEmail: string): Promise<Resource<Association | Errors>> {
68+
const url = `/associations/companies/${companyNumber}`;
69+
const body = { user_email: userEmail }
70+
const response = await this.client.httpPost(url, body);
71+
72+
return this.getResource(response) as Resource<Association | Errors>;
73+
}
74+
75+
/**
76+
* Initiates an HTTP GET request to retrieve the associations searched based on association status.
77+
* @param associationStatus - an association status used to filter associations. This parameter is required. Available values: confirmed, awaiting-approval, removed. Default value: confirmed.
78+
* @param pageIndex - a page to be returned. Default value: 0.
79+
* @param itemsPerPage - a number of items returned per page. Default value: 15.
80+
* @param companyNumber - a filter based on company number.
81+
* @returns a promise that resolves to the HTTP response from the server that includes the associations or errors object.
82+
*/
7683
public async searchAssociations (
7784
associationStatus: AssociationStatus[],
7885
pageIndex?: number,

test/services/associations/service.spec.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,96 @@ describe("AssociationsService", () => {
213213
});
214214
});
215215

216+
describe("getCompanyAssociationByUserEmail", () => {
217+
let associationsService: AssociationsService;
218+
219+
beforeEach(() => {
220+
sinon.reset();
221+
sinon.restore();
222+
associationsService = new AssociationsService(requestClient);
223+
});
224+
225+
it("should return 200 response with the company association", async () => {
226+
sinon.stub(requestClient, "httpPost").resolves({ status: 200, body: mockAssociationResource });
227+
const companyNumber = mockAssociationResource.company_number;
228+
const userEmail = mockAssociationResource.user_email;
229+
await associationsService.getCompanyAssociationByUserEmail(companyNumber, userEmail)
230+
.then((data) => {
231+
expect(data.httpStatusCode).to.equal(200);
232+
233+
const castedData: Resource<Association> = data as Resource<Association>;
234+
expect(castedData).to.exist;
235+
const association = castedData.resource as Association;
236+
expect(association).to.exist;
237+
expect(association.etag).to.equal("ABC");
238+
expect(association.id).to.equal("0123456789");
239+
expect(association.userId).to.equal("9876543210");
240+
expect(association.userEmail).to.equal("[email protected]");
241+
expect(association.displayName).to.equal("John Doe");
242+
expect(association.companyNumber).to.equal("AB123456");
243+
expect(association.companyName).to.equal("Company Ltd.");
244+
expect(association.status).to.equal(AssociationStatus.AWAITING_APPROVAL);
245+
expect(association.createdAt).to.equal("2022-03-05T11:41:09.568+00:00 UTC");
246+
expect(association.approvedAt).to.equal("");
247+
expect(association.removedAt).to.equal("");
248+
expect(association.kind).to.equal("association");
249+
expect(association.approvalRoute).to.equal(ApprovalRoute.INVITATION);
250+
expect(association.approvalExpiryAt).to.equal("2022-05-05T11:41:09.568+00:00 UTC");
251+
expect(association.links.self).to.equal("/12345");
252+
});
253+
});
254+
255+
it("should return 400 response", async () => {
256+
sinon.stub(requestClient, "httpPost").resolves(mockGetResponse[400]);
257+
const companyNumber = mockAssociationResource.company_number;
258+
const userEmail = mockAssociationResource.user_email;
259+
await associationsService.getCompanyAssociationByUserEmail(companyNumber, userEmail)
260+
.then((data) => {
261+
expect(data.httpStatusCode).to.equal(400);
262+
});
263+
});
264+
265+
it("should return 401 response", async () => {
266+
sinon.stub(requestClient, "httpPost").resolves(mockGetResponse[401]);
267+
const companyNumber = mockAssociationResource.company_number;
268+
const userEmail = mockAssociationResource.user_email;
269+
await associationsService.getCompanyAssociationByUserEmail(companyNumber, userEmail)
270+
.then((data) => {
271+
expect(data.httpStatusCode).to.equal(401);
272+
});
273+
});
274+
275+
it("should return 403 response", async () => {
276+
sinon.stub(requestClient, "httpPost").resolves(mockGetResponse[403]);
277+
const companyNumber = mockAssociationResource.company_number;
278+
const userEmail = mockAssociationResource.user_email;
279+
await associationsService.getCompanyAssociationByUserEmail(companyNumber, userEmail)
280+
.then((data) => {
281+
expect(data.httpStatusCode).to.equal(403);
282+
});
283+
});
284+
285+
it("should return 404 response", async () => {
286+
sinon.stub(requestClient, "httpPost").resolves(mockGetResponse[404]);
287+
const companyNumber = mockAssociationResource.company_number;
288+
const userEmail = mockAssociationResource.user_email;
289+
await associationsService.getCompanyAssociationByUserEmail(companyNumber, userEmail)
290+
.then((data) => {
291+
expect(data.httpStatusCode).to.equal(404);
292+
});
293+
});
294+
295+
it("should return 500 response", async () => {
296+
sinon.stub(requestClient, "httpPost").resolves(mockGetResponse[500]);
297+
const companyNumber = mockAssociationResource.company_number;
298+
const userEmail = mockAssociationResource.user_email;
299+
await associationsService.getCompanyAssociationByUserEmail(companyNumber, userEmail)
300+
.then((data) => {
301+
expect(data.httpStatusCode).to.equal(500);
302+
});
303+
});
304+
});
305+
216306
describe("searchAssociations", () => {
217307
let associationsService: AssociationsService;
218308

0 commit comments

Comments
 (0)