Skip to content

Commit

Permalink
Merge pull request #86 from wambui-pixel/85-fixinvitations
Browse files Browse the repository at this point in the history
JSSDK - 85 - Update invitations files
  • Loading branch information
ianmuchyri authored Nov 9, 2024
2 parents fb606b7 + 7f68722 commit 4f2787d
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-buttons-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@absmach/magistrala-sdk": patch
---

Remove domain id in invitations file
3 changes: 1 addition & 2 deletions examples/domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ const mySdk = new SDK({
})

const token = '<token>'

mySdk.domains
.CreateDomain(
{ name: '<domainName>' },
{ name: '<domainName>', alias: '<domainAlias>' },
token
)
.then((response: any) => {
Expand Down
18 changes: 14 additions & 4 deletions examples/invitations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mySdk.invitations.SendInvitation(

mySdk.invitations.Invitation(
'<userID>',
'<domainID>',
domainId,
token
)
.then((response: any) => {
Expand All @@ -43,7 +43,6 @@ mySdk.invitations.Invitations(
limit: 10,
offset: 0
},
domainId,
token
)
.then((response: any) => {
Expand All @@ -54,7 +53,18 @@ mySdk.invitations.Invitations(
})

mySdk.invitations.AcceptInvitation(
'<domainID>',
domainId,
token
)
.then((response: any) => {
console.log('response: ', response)
})
.catch((error) => {
console.log(error)
})

mySdk.invitations.RejectInvitation(
domainId,
token
)
.then((response: any) => {
Expand All @@ -65,8 +75,8 @@ mySdk.invitations.AcceptInvitation(
})

mySdk.invitations.DeleteInvitation(
'<domainID>',
'<userID>',
domainId,
token
)
.then((response: any) => {
Expand Down
1 change: 1 addition & 0 deletions src/defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ export interface PageMetadata {
state?: string
list_perms?: boolean
invited_by?: string
domain_id?: string
user_id?: string
relation?: string
from?: number
Expand Down
51 changes: 42 additions & 9 deletions src/invitations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ 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
)
if (!response.ok) {
Expand Down Expand Up @@ -85,7 +85,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) {
Expand All @@ -99,12 +99,11 @@ export default class Invitations {
}
}

public async Invitations (queryParams: PageMetadata, domainId: string, token: string): Promise<InvitationsPage> {
public async Invitations (queryParams: PageMetadata, token: string): Promise<InvitationsPage> {
// 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.
*/
Expand All @@ -123,7 +122,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
Expand All @@ -144,20 +143,21 @@ 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 = {
method: 'POST',
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) {
Expand All @@ -171,6 +171,39 @@ export default class Invitations {
}
}

public async RejectInvitation (domainId: string, token: string): Promise<Response> {
// 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<Response> {
// DeleteInvitation deletes an invitation.
/**
Expand All @@ -190,7 +223,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) {
Expand Down
13 changes: 12 additions & 1 deletion tests/invitations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand All @@ -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,
Expand Down

0 comments on commit 4f2787d

Please sign in to comment.