Skip to content

Commit 4563002

Browse files
Merge pull request #799 from companieshouse/feature/siv-478-downstream-lb-changes-node-api-client
SIV-478 downstream lb changes node api client
2 parents 9de27a9 + 7c11c82 commit 4563002

File tree

3 files changed

+148
-64
lines changed

3 files changed

+148
-64
lines changed

src/services/associations/service.ts

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
InvitationList,
1010
NewAssociationResponse,
1111
PreviousStateList,
12-
QueryParameters
12+
QueryParameters,
13+
SearchForCompanyAssociationPostBody
1314
} from "./types";
1415
import Mapping from "../../mapping/mapping";
1516

@@ -31,23 +32,20 @@ export default class AssociationsService {
3132
* @param includeRemoved - a flag to get a list of companies where status is "removed". Default value: false.
3233
* @param pageIndex - a page number to be returned. Default value: 0.
3334
* @param itemsPerPage - a number of items to be returned per page. Default value: 15.
34-
* @param userId - a unique identifier of a user to check if associated with the company.
3535
* @returns a promise that resolves to the HTTP response from the server that includes the associations or errors object.
3636
*/
3737
public async getCompanyAssociations (
3838
companyNumber: string,
3939
includeRemoved?: boolean,
4040
pageIndex?: number,
41-
itemsPerPage?: number,
42-
userId?: string
41+
itemsPerPage?: number
4342
): Promise<Resource<AssociationList | Errors>> {
4443
let queryString: string = "";
45-
if (includeRemoved || pageIndex || itemsPerPage || userId) {
44+
if (includeRemoved || pageIndex || itemsPerPage) {
4645
const queryParameters: QueryParameters = {
4746
include_removed: includeRemoved || undefined,
4847
page_index: pageIndex || undefined,
49-
items_per_page: itemsPerPage || undefined,
50-
user_id: userId || undefined
48+
items_per_page: itemsPerPage || undefined
5149
};
5250
queryString = this.getQueryString(queryParameters);
5351
}
@@ -59,14 +57,31 @@ export default class AssociationsService {
5957
}
6058

6159
/**
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.
60+
* Searches for an association between a user and a company using either the user's email or user ID.
61+
* Only one of userEmail or userId should be provided.
62+
* Optionally filter by association status.
63+
*
64+
* @param companyNumber - The company number to search associations for.
65+
* @param userEmail - The user's email address (optional).
66+
* @param userId - The user's unique identifier (optional).
67+
* @param associationStatus - Array of association statuses to filter by (optional).
68+
* Available values: confirmed, awaiting-approval, removed, migrated, unauthorised.
69+
* Default: confirmed.
70+
* @returns Promise resolving to the association or errors object.
6671
*/
67-
public async getCompanyAssociationByUserEmail (companyNumber: string, userEmail: string): Promise<Resource<Association | Errors>> {
68-
const url = `/associations/companies/${companyNumber}`;
69-
const body = { user_email: userEmail }
72+
public async searchForCompanyAssociation (
73+
companyNumber: string,
74+
userEmail?: string,
75+
userId?: string,
76+
associationStatus?: AssociationStatus[]
77+
): Promise<Resource<Association | Errors>> {
78+
const url = `/associations/companies/${companyNumber}/search`;
79+
80+
const body: SearchForCompanyAssociationPostBody = {};
81+
if (userEmail) body.user_email = userEmail;
82+
if (userId) body.user_id = userId;
83+
if (associationStatus) body.status = associationStatus;
84+
7085
const response = await this.client.httpPost(url, body);
7186

7287
return this.getResource(response) as Resource<Association | Errors>;
@@ -101,21 +116,34 @@ export default class AssociationsService {
101116
}
102117

103118
/**
104-
* Creates a new association for a user in session.
105-
* @param companyNumber - a company number of the company with which a new association for the user will be created.
106-
* @param inviteeEmailAddress - an email address of the user invited to have an association with a company.
107-
* @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.
108123
*/
109124
public async createAssociation (
110125
companyNumber: string,
111-
inviteeEmailAddress?: string
126+
userId: string
112127
): Promise<Resource<NewAssociationResponse | Errors>> {
113-
const url = inviteeEmailAddress ? "/associations/invitations" : "/associations";
114-
const body = inviteeEmailAddress
115-
? { company_number: companyNumber, invitee_email_id: inviteeEmailAddress }
116-
: { company_number: companyNumber };
128+
const url = "/associations";
129+
const body = { company_number: companyNumber, user_id: userId };
117130
const response = await this.client.httpPost(url, body);
131+
return this.getResource(response) as Resource<NewAssociationResponse | Errors>;
132+
}
118133

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);
119147
return this.getResource(response) as Resource<NewAssociationResponse | Errors>;
120148
}
121149

src/services/associations/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,9 @@ export interface NewAssociationResponse {
208208
}
209209

210210
export type AssociationsResponse = AssociationList | Association | NewAssociationResponse | Errors;
211+
212+
export interface SearchForCompanyAssociationPostBody {
213+
user_email?: string;
214+
user_id?: string;
215+
status?: AssociationStatus[]
216+
}

0 commit comments

Comments
 (0)