99 InvitationList ,
1010 NewAssociationResponse ,
1111 PreviousStateList ,
12- QueryParameters
12+ QueryParameters ,
13+ SearchForCompanyAssociationPostBody
1314} from "./types" ;
1415import 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
0 commit comments