diff --git a/.changeset/thick-buttons-rush.md b/.changeset/thick-buttons-rush.md new file mode 100644 index 0000000..fbb37fa --- /dev/null +++ b/.changeset/thick-buttons-rush.md @@ -0,0 +1,5 @@ +--- +"@absmach/magistrala-sdk": minor +--- + +Remove domain id in invitations file diff --git a/examples/domains.ts b/examples/domains.ts index 91eaa72..79eedcc 100644 --- a/examples/domains.ts +++ b/examples/domains.ts @@ -8,10 +8,9 @@ const mySdk = new SDK({ }) const token = '' - mySdk.domains .CreateDomain( - { name: '' }, + { name: '', alias: '' }, token ) .then((response: any) => { diff --git a/examples/invitations.ts b/examples/invitations.ts index 3fcf14e..28f5e28 100644 --- a/examples/invitations.ts +++ b/examples/invitations.ts @@ -15,7 +15,7 @@ mySdk.invitations.SendInvitation( { user_id: '', domain_id: domainId, - relation: 'administrator' + relation: 'contributor' }, token ) @@ -28,7 +28,7 @@ mySdk.invitations.SendInvitation( mySdk.invitations.Invitation( '', - '', + domainId, token ) .then((response: any) => { @@ -43,7 +43,6 @@ mySdk.invitations.Invitations( limit: 10, offset: 0 }, - domainId, token ) .then((response: any) => { @@ -54,7 +53,18 @@ mySdk.invitations.Invitations( }) mySdk.invitations.AcceptInvitation( - '', + domainId, + token +) + .then((response: any) => { + console.log('response: ', response) + }) + .catch((error) => { + console.log(error) + }) + +mySdk.invitations.RejectInvitation( + domainId, token ) .then((response: any) => { @@ -65,8 +75,8 @@ mySdk.invitations.AcceptInvitation( }) mySdk.invitations.DeleteInvitation( - '', '', + '', token ) .then((response: any) => { diff --git a/src/invitations.ts b/src/invitations.ts index 419418b..b62c829 100644 --- a/src/invitations.ts +++ b/src/invitations.ts @@ -51,9 +51,10 @@ export default class Invitations { try { const response = await fetch( - new URL(`${invitation.domain_id as string}/${this.invitationsEndpoint}`, this.invitationsUrl).toString(), + new URL(`${this.invitationsEndpoint}`, this.invitationsUrl).toString(), options ) + console.log('url', response.url) if (!response.ok) { const errorRes = await response.json() throw this.invitationError.HandleError(errorRes.message, response.status) @@ -85,7 +86,7 @@ export default class Invitations { try { const response = await fetch( - new URL(`${domainId}/${this.invitationsEndpoint}/${userId}`, this.invitationsUrl).toString(), + new URL(`${this.invitationsEndpoint}/${userId}/${domainId}`, this.invitationsUrl).toString(), options ) if (!response.ok) { @@ -99,12 +100,11 @@ export default class Invitations { } } - public async Invitations (queryParams: PageMetadata, domainId: string, token: string): Promise { + public async Invitations (queryParams: PageMetadata, token: string): Promise { // Invitations returns a list of invitations. /** * @method Invitations - returns a list of invitations. * @param {Object} queryParams - The query parameters such as limit and offset. - * @param {string} domainId - The Domain ID. * @param {string} token - The user's access token. * @returns {Object} - The invitations page object that has a list of invitations and pagination information. */ @@ -123,7 +123,7 @@ export default class Invitations { try { const response = await fetch( new URL( - `${domainId}/${this.invitationsEndpoint}?${new URLSearchParams(stringParams).toString()}`, + `${this.invitationsEndpoint}?${new URLSearchParams(stringParams).toString()}`, this.invitationsUrl ).toString(), options @@ -144,7 +144,7 @@ export default class Invitations { /** * @method AcceptInvitation - accepts an invitation by adding the user to the domain that they were invited to. * @param {String} domainId - The Domain ID. - * @param {string} token - The user's access token. + * @param {string} token - The invited user's access token. * @returns {Object} - The response object which has a status and a message. */ const options: RequestInit = { @@ -152,12 +152,13 @@ export default class Invitations { headers: { 'Content-Type': this.contentType, Authorization: `Bearer ${token}` - } + }, + body: JSON.stringify({ domain_id: domainId }) } try { const response = await fetch( - new URL(`${domainId}/${this.invitationsEndpoint}/accept`, this.invitationsUrl).toString(), + new URL(`${this.invitationsEndpoint}/accept`, this.invitationsUrl).toString(), options ) if (!response.ok) { @@ -171,6 +172,39 @@ export default class Invitations { } } + public async RejectInvitation (domainId: string, token: string): Promise { + // RejectInvitation rejects an invitation by declining an invitation a user was sent to join a domain. + /** + * @method RejectInvitation - rejects an invitation by declining an invitation a user was sent to join a domain. + * @param {String} domainId - The Domain ID. + * @param {string} token - The invited user's access token. + * @returns {Object} - The response object which has a status and a message. + */ + const options: RequestInit = { + method: 'POST', + headers: { + 'Content-Type': this.contentType, + Authorization: `Bearer ${token}` + }, + body: JSON.stringify({ domain_id: domainId }) + } + + try { + const response = await fetch( + new URL(`${this.invitationsEndpoint}/reject`, this.invitationsUrl).toString(), + options + ) + if (!response.ok) { + const errorRes = await response.json() + throw this.invitationError.HandleError(errorRes.message, response.status) + } + const inviteResponse: Response = { status: response.status, message: 'Invitation rejected successfully' } + return inviteResponse + } catch (error) { + throw error + } + } + public async DeleteInvitation (userId: string, domainId: string, token: string): Promise { // DeleteInvitation deletes an invitation. /** @@ -190,7 +224,7 @@ export default class Invitations { try { const response = await fetch( - new URL(`${domainId}/${this.invitationsEndpoint}/${userId}`, this.invitationsUrl).toString(), + new URL(`${this.invitationsEndpoint}/${userId}/${domainId}`, this.invitationsUrl).toString(), options ) if (!response.ok) { diff --git a/tests/invitations.test.ts b/tests/invitations.test.ts index cde8562..fe42edf 100644 --- a/tests/invitations.test.ts +++ b/tests/invitations.test.ts @@ -60,7 +60,7 @@ describe('Invitations', () => { test('invitations should return a list of invitations and return success', async () => { fetchMock.mockResponseOnce(JSON.stringify(invitationsPage)) - const response = await sdk.invitations.Invitations(queryParams, domainId, token) + const response = await sdk.invitations.Invitations(queryParams, token) expect(response).toEqual(invitationsPage) }) @@ -75,6 +75,17 @@ describe('Invitations', () => { expect(response).toEqual(AcceptInvitationResponse) }) + test('reject invitation should reject an invitation and return success', async () => { + const RejectInvitationResponse = { + status: 200, + message: 'Invitation rejected successfully' + } + fetchMock.mockResponseOnce(JSON.stringify(RejectInvitationResponse)) + + const response = await sdk.invitations.RejectInvitation(domainId, token) + expect(response).toEqual(RejectInvitationResponse) + }) + test('delete invitation should delete an invitation and return success', async () => { const DeleteInvitationResponse = { status: 200,