Skip to content

Commit

Permalink
feat(api-sdk): add redeemInvite
Browse files Browse the repository at this point in the history
  • Loading branch information
waddaboo committed Oct 31, 2024
1 parent 41a2797 commit 924b483
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
13 changes: 13 additions & 0 deletions libs/api-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,19 @@ const apiKey = "70f07d0d-6aa2-4fe1-b4b9-06c271a641dc"
const invite = await apiSdk.getInvite(inviteCode)
```

## Redeem invite

\# **redeemInvite**(): _Promise\<Invite>_

Redeems a specific invite.

```ts
const groupId = "10402173435763029700781503965100"
const inviteCode = "C5VAG4HD"

const invite = await apiSdk.redeemInvite(inviteCode, groupId)
```

## Get credential group join URL

\# **getCredentialGroupJoinUrl**(): _string_
Expand Down
14 changes: 13 additions & 1 deletion libs/api-sdk/src/apiSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
getGroupsByType,
createAssociatedGroup
} from "./groups"
import { createInvite, getInvite } from "./invites"
import { createInvite, getInvite, redeemInvite } from "./invites"

export default class ApiSdk {
private _url: string
Expand Down Expand Up @@ -419,6 +419,18 @@ export default class ApiSdk {
return invite
}

/**
* Redeems a specific invite.
* @param inviteCode Invite code.
* @param groupId Group id.
* @returns The updated invite.
*/
async redeemInvite(inviteCode: string, groupId: string): Promise<Invite> {
const invite = await redeemInvite(this._config, inviteCode, groupId)

return invite
}

/**
* Generate a custom url for joining a credential group.
* @param dashboardUrl Dashboard base url.
Expand Down
39 changes: 39 additions & 0 deletions libs/api-sdk/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,45 @@ describe("Bandada API SDK", () => {
expect(invite.group).toStrictEqual(group)
})
})

describe("# redeemInvite", () => {
it("Should redeem an invite", async () => {
const groupId = "95633257675970239314311768035433"
const groupName = "Group 1"
const group = {
id: groupId,
name: groupName,
description: "This is Group 1",
type: "off-chain",
adminId:
"0x63229164c457584616006e31d1e171e6cdd4163695bc9c4bf0227095998ffa4c",
treeDepth: 16,
fingerprintDuration: 3600,
credentials: null,
apiEnabled: false,
apiKey: null,
createdAt: "2023-08-09T18:09:53.000Z",
updatedAt: "2023-08-09T18:09:53.000Z"
}
const inviteCode = "C5VAG4HD"
const inviteCreatedAt = "2023-08-09T18:10:02.000Z"

requestMocked.mockImplementationOnce(() =>
Promise.resolve({
code: inviteCode,
isRedeemed: true,
createdAt: inviteCreatedAt,
group
})
)

const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const invite = await apiSdk.redeemInvite(inviteCode, groupId)

expect(invite.code).toBe(inviteCode)
expect(invite.isRedeemed).toBe(true)
})
})
})
describe("Check Parameter", () => {
describe("Should not throw an error if the parameter has the expected type", () => {
Expand Down
17 changes: 17 additions & 0 deletions libs/api-sdk/src/invites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,20 @@ export async function createInvite(

return req
}

export async function redeemInvite(
config: object,
inviteCode: string,
groupId: string
): Promise<Invite> {
const requestUrl = `${url}/redeem/${inviteCode}/group/${groupId}`

const newConfig: any = {
method: "post",
...config
}

const req = await request(requestUrl, newConfig)

return req
}

0 comments on commit 924b483

Please sign in to comment.