Skip to content

Commit 7c11c82

Browse files
SIV-478 The function for creating an association and invitation split into two independent functions
1 parent 1e6e8ac commit 7c11c82

File tree

2 files changed

+72
-41
lines changed

2 files changed

+72
-41
lines changed

src/services/associations/service.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,34 @@ export default class AssociationsService {
116116
}
117117

118118
/**
119-
* Creates a new association for a user in session.
120-
* @param companyNumber - a company number of the company with which a new association for the user will be created.
121-
* @param inviteeEmailAddress - an email address of the user invited to have an association with a company.
122-
* @returns a promise that resolves to the HTTP response from the server that includes the new association's link (it contains the association identifier) or errors object.
119+
* Creates a new association for a user with provided userId.
120+
* @param companyNumber - The company number for the new association.
121+
* @param userId - The user's unique identifier.
122+
* @returns A promise resolving to the new association's link or errors object.
123123
*/
124124
public async createAssociation (
125125
companyNumber: string,
126-
inviteeEmailAddress?: string
126+
userId: string
127127
): Promise<Resource<NewAssociationResponse | Errors>> {
128-
const url = inviteeEmailAddress ? "/associations/invitations" : "/associations";
129-
const body = inviteeEmailAddress
130-
? { company_number: companyNumber, invitee_email_id: inviteeEmailAddress }
131-
: { company_number: companyNumber };
128+
const url = "/associations";
129+
const body = { company_number: companyNumber, user_id: userId };
132130
const response = await this.client.httpPost(url, body);
131+
return this.getResource(response) as Resource<NewAssociationResponse | Errors>;
132+
}
133133

134+
/**
135+
* Invites a user with the provided email address to a company.
136+
* @param companyNumber - The company number.
137+
* @param inviteeEmailAddress - The email address of the user to invite.
138+
* @returns A promise resolving to the new association's link or errors object.
139+
*/
140+
public async inviteUser (
141+
companyNumber: string,
142+
inviteeEmailAddress: string
143+
): Promise<Resource<NewAssociationResponse | Errors>> {
144+
const url = "/associations/invitations";
145+
const body = { company_number: companyNumber, invitee_email_id: inviteeEmailAddress };
146+
const response = await this.client.httpPost(url, body);
134147
return this.getResource(response) as Resource<NewAssociationResponse | Errors>;
135148
}
136149

test/services/associations/service.spec.ts

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ describe("AssociationsService", () => {
432432

433433
describe("createAssociation", () => {
434434
let associationsService: AssociationsService;
435-
const inviteeEmailAddress = "[email protected]";
435+
const userId = "1234567890";
436436

437437
beforeEach(() => {
438438
sinon.reset();
@@ -442,17 +442,7 @@ describe("AssociationsService", () => {
442442

443443
it("should return 201 response if a new association created", async () => {
444444
sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[201]);
445-
await associationsService.createAssociation(companyNumber).then((data) => {
446-
expect(data.httpStatusCode).to.equal(201);
447-
const castedData: Resource<NewAssociationResponse> = data as Resource<NewAssociationResponse>;
448-
expect(castedData).to.exist;
449-
expect(castedData.resource?.associationLink).to.equal("/associations/123456");
450-
});
451-
});
452-
453-
it("should return 201 response if a new association created for invited user", async () => {
454-
sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[201]);
455-
await associationsService.createAssociation(companyNumber, inviteeEmailAddress).then((data) => {
445+
await associationsService.createAssociation(companyNumber, userId).then((data) => {
456446
expect(data.httpStatusCode).to.equal(201);
457447
const castedData: Resource<NewAssociationResponse> = data as Resource<NewAssociationResponse>;
458448
expect(castedData).to.exist;
@@ -462,62 +452,90 @@ describe("AssociationsService", () => {
462452

463453
it("should return 400 response", async () => {
464454
sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[400]);
465-
await associationsService.createAssociation(companyNumber)
466-
.then((data) => {
467-
expect(data.httpStatusCode).to.equal(400);
468-
});
469-
});
470-
471-
it("should return 400 response for invited user", async () => {
472-
sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[400]);
473-
await associationsService.createAssociation(companyNumber, inviteeEmailAddress)
455+
await associationsService.createAssociation(companyNumber, userId)
474456
.then((data) => {
475457
expect(data.httpStatusCode).to.equal(400);
476458
});
477459
});
478460

479461
it("should return 401 response", async () => {
480462
sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[401]);
481-
await associationsService.createAssociation(companyNumber)
463+
await associationsService.createAssociation(companyNumber, userId)
482464
.then((data) => {
483465
expect(data.httpStatusCode).to.equal(401);
484466
});
485467
});
486468

487469
it("should return 401 response for invited user", async () => {
488470
sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[401]);
489-
await associationsService.createAssociation(companyNumber, inviteeEmailAddress).then((data) => {
471+
await associationsService.createAssociation(companyNumber, userId).then((data) => {
490472
expect(data.httpStatusCode).to.equal(401);
491473
});
492474
});
493475

494476
it("should return 403 response", async () => {
495477
sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[403]);
496-
await associationsService.createAssociation(companyNumber)
478+
await associationsService.createAssociation(companyNumber, userId)
497479
.then((data) => {
498480
expect(data.httpStatusCode).to.equal(403);
499481
});
500482
});
501483

502-
it("should return 403 response for invited user", async () => {
503-
sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[403]);
484+
it("should return 500 response", async () => {
485+
sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[500]);
486+
await associationsService.createAssociation(companyNumber, userId)
487+
.then((data) => {
488+
expect(data.httpStatusCode).to.equal(500);
489+
});
490+
});
491+
});
492+
493+
describe("inviteUser", () => {
494+
let associationsService: AssociationsService;
495+
const inviteeEmailAddress = "[email protected]";
496+
497+
beforeEach(() => {
498+
sinon.reset();
499+
sinon.restore();
500+
associationsService = new AssociationsService(requestClient);
501+
});
502+
503+
it("should return 201 response if a new association created for invited user", async () => {
504+
sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[201]);
505+
await associationsService.inviteUser(companyNumber, inviteeEmailAddress).then((data) => {
506+
expect(data.httpStatusCode).to.equal(201);
507+
const castedData: Resource<NewAssociationResponse> = data as Resource<NewAssociationResponse>;
508+
expect(castedData).to.exist;
509+
expect(castedData.resource?.associationLink).to.equal("/associations/123456");
510+
});
511+
});
512+
513+
it("should return 400 response for invited user", async () => {
514+
sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[400]);
504515
await associationsService.createAssociation(companyNumber, inviteeEmailAddress)
505516
.then((data) => {
506-
expect(data.httpStatusCode).to.equal(403);
517+
expect(data.httpStatusCode).to.equal(400);
507518
});
508519
});
509520

510-
it("should return 500 response", async () => {
511-
sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[500]);
512-
await associationsService.createAssociation(companyNumber)
521+
it("should return 401 response for invited user", async () => {
522+
sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[401]);
523+
await associationsService.inviteUser(companyNumber, inviteeEmailAddress).then((data) => {
524+
expect(data.httpStatusCode).to.equal(401);
525+
});
526+
});
527+
528+
it("should return 403 response for invited user", async () => {
529+
sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[403]);
530+
await associationsService.inviteUser(companyNumber, inviteeEmailAddress)
513531
.then((data) => {
514-
expect(data.httpStatusCode).to.equal(500);
532+
expect(data.httpStatusCode).to.equal(403);
515533
});
516534
});
517535

518536
it("should return 500 response for invited user", async () => {
519537
sinon.stub(requestClient, "httpPost").resolves(mockPostResponse[500]);
520-
await associationsService.createAssociation(companyNumber, inviteeEmailAddress)
538+
await associationsService.inviteUser(companyNumber, inviteeEmailAddress)
521539
.then((data) => {
522540
expect(data.httpStatusCode).to.equal(500);
523541
});

0 commit comments

Comments
 (0)