Skip to content

Commit

Permalink
Merge pull request #524 from waddaboo/dev-493
Browse files Browse the repository at this point in the history
feat(api,api-sdk): add getGroupsByAdminId() and getGroupsByMemberId()
  • Loading branch information
vplasencia authored Jun 10, 2024
2 parents 4af550d + 7c763f3 commit dd5e640
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 6 deletions.
8 changes: 6 additions & 2 deletions apps/api/src/app/groups/groups.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ export class GroupsController {

@Get()
@ApiQuery({ name: "adminId", required: false, type: String })
@ApiQuery({ name: "memberId", required: false, type: String })
@ApiOperation({ description: "Returns the list of groups." })
@ApiCreatedResponse({ type: Group, isArray: true })
async getGroups(@Query("adminId") adminId: string) {
const groups = await this.groupsService.getGroups({ adminId })
async getGroups(
@Query("adminId") adminId: string,
@Query("memberId") memberId: string
) {
const groups = await this.groupsService.getGroups({ adminId, memberId })
const groupIds = groups.map((group) => group.id)
const fingerprints = await this.groupsService.getFingerprints(groupIds)

Expand Down
27 changes: 27 additions & 0 deletions apps/api/src/app/groups/groups.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,33 @@ describe("GroupsService", () => {

expect(result).toHaveLength(1)
})

it("Should return a list of groups by member", async () => {
const { id: _groupId } = await groupsService.createGroup(
{
name: "MemberGroup",
description: "This is a description",
treeDepth: 16,
fingerprintDuration: 3600
},
"admin"
)

const invite = await invitesService.createInvite(
{ groupId: _groupId },
"admin"
)

await groupsService.joinGroup(_groupId, "123456", {
inviteCode: invite.code
})

const result = await groupsService.getGroups({
memberId: "123456"
})

expect(result).toHaveLength(1)
})
})

describe("# getGroup", () => {
Expand Down
20 changes: 17 additions & 3 deletions apps/api/src/app/groups/groups.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -811,11 +811,25 @@ export class GroupsService {
* Returns a list of groups.
* @returns List of existing groups.
*/
async getGroups(filters?: { adminId: string }): Promise<Group[]> {
const where = []
async getGroups(filters?: {
adminId?: string
memberId?: string
}): Promise<Group[]> {
let where = {}

if (filters?.adminId) {
where.push({ adminId: filters.adminId })
where = {
adminId: filters.adminId
}
}

if (filters?.memberId) {
where = {
members: {
id: filters.memberId
},
...where
}
}

return this.groupRepository.find({
Expand Down
23 changes: 23 additions & 0 deletions apps/docs/docs/api-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,29 @@ Returns the list of groups.
const groups = await apiSdk.getGroups()
```

## Get groups by admin id

\# **getGroupsByAdminId**(): _Promise\<Group[]>_

Returns the list of groups by admin id.

```ts
const adminId =
"0xdf558148e66850ac48dbe2c8119b0eefa7d08bfd19c997c90a142eb97916b847"
const groups = await apiSdk.getGroupsByAdminId(adminId)
```

## Get groups by member id

\# **getGroupsByMemberId**(): _Promise\<Group[]>_

Returns the list of groups by member id.

```ts
const memberId = "1"
const groups = await apiSdk.getGroupsByMemberId(memberId)
```

## Is group member

\# **isGroupMember**(): _Promise\<boolean>_
Expand Down
23 changes: 23 additions & 0 deletions libs/api-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,29 @@ Returns the list of groups.
const groups = await apiSdk.getGroups()
```

## Get groups by admin id

\# **getGroupsByAdminId**(): _Promise\<Group[]>_

Returns the list of groups by admin id.

```ts
const adminId =
"0xdf558148e66850ac48dbe2c8119b0eefa7d08bfd19c997c90a142eb97916b847"
const groups = await apiSdk.getGroupsByAdminId(adminId)
```

## Get groups by member id

\# **getGroupsByMemberId**(): _Promise\<Group[]>_

Returns the list of groups by member id.

```ts
const memberId = "1"
const groups = await apiSdk.getGroupsByMemberId(memberId)
```

## Is group member

\# **isGroupMember**(): _Promise\<boolean>_
Expand Down
26 changes: 25 additions & 1 deletion libs/api-sdk/src/apiSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import {
addMembersByApiKey,
addMemberByInviteCode,
removeMemberByApiKey,
removeMembersByApiKey
removeMembersByApiKey,
getGroupsByAdminId,
getGroupsByMemberId
} from "./groups"
import { createInvite, getInvite } from "./invites"

Expand Down Expand Up @@ -85,6 +87,28 @@ export default class ApiSdk {
return groups
}

/**
* Returns the list of groups by admin id.
* @param adminId Admin id.
* @returns List of groups by admin id.
*/
async getGroupsByAdminId(adminId: string): Promise<Group[]> {
const groups = await getGroupsByAdminId(this._config, adminId)

return groups
}

/**
* Returns the list of groups by member id.
* @param memberId Member id.
* @returns List of groups by member id.
*/
async getGroupsByMemberId(memberId: string): Promise<Group[]> {
const groups = await getGroupsByMemberId(this._config, memberId)

return groups
}

/**
* Creates a group using the API key.
* @param groupCreationDetails Data to create the group.
Expand Down
42 changes: 42 additions & 0 deletions libs/api-sdk/src/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,48 @@ export async function getGroups(config: object): Promise<Group[]> {
return groups
}

/**
* Returns the list of groups by admin id.
* @param adminId Admin id.
* @returns List of groups by admin id.
*/
export async function getGroupsByAdminId(
config: object,
adminId: string
): Promise<Group[]> {
const requestUrl = `${url}?adminId=${adminId}`

const groups = await request(requestUrl, config)

groups.map((group: any) => ({
...group,
credentials: JSON.parse(group.credentials)
}))

return groups
}

/**
* Returns the list of groups by member id.
* @param memberId Member id.
* @returns List of groups by member id.
*/
export async function getGroupsByMemberId(
config: object,
memberId: string
): Promise<Group[]> {
const requestUrl = `${url}?memberId=${memberId}`

const groups = await request(requestUrl, config)

groups.map((group: any) => ({
...group,
credentials: JSON.parse(group.credentials)
}))

return groups
}

/**
* Creates one or more groups with the provided details.
* @param groupsCreationDetails Data to create the groups.
Expand Down
56 changes: 56 additions & 0 deletions libs/api-sdk/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,62 @@ describe("Bandada API SDK", () => {
expect(groups).toHaveLength(1)
})
})
describe("#getGroupsByAdminId", () => {
it("Should return all groups by admin id", async () => {
requestMocked.mockImplementationOnce(() =>
Promise.resolve([
{
id: "10402173435763029700781503965100",
name: "Group1",
description: "This is a new group",
admin: "0xdf558148e66850ac48dbe2c8119b0eefa7d08bfd19c997c90a142eb97916b847",
treeDepth: 16,
fingerprintDuration: 3600,
createdAt: "2023-07-15T08:21:05.000Z",
members: [],
credentials: null
}
])
)

const adminId =
"0xdf558148e66850ac48dbe2c8119b0eefa7d08bfd19c997c90a142eb97916b847"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const groups: Group[] = await apiSdk.getGroupsByAdminId(adminId)
expect(groups).toHaveLength(1)
groups.forEach((group: Group) => {
expect(group.admin).toBe(adminId)
})
})
})
describe("#getGroupsByMemberId", () => {
it("Should return all groups by member id", async () => {
requestMocked.mockImplementationOnce(() =>
Promise.resolve([
{
id: "10402173435763029700781503965100",
name: "Group1",
description: "This is a new group",
admin: "0xdf558148e66850ac48dbe2c8119b0eefa7d08bfd19c997c90a142eb97916b847",
treeDepth: 16,
fingerprintDuration: 3600,
createdAt: "2023-07-15T08:21:05.000Z",
members: ["1"],
credentials: null
}
])
)

const memberId = "1"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const groups: Group[] = await apiSdk.getGroupsByMemberId(
memberId
)
expect(groups).toHaveLength(1)
})
})
describe("#getGroup", () => {
it("Should return a group", async () => {
requestMocked.mockImplementationOnce(() =>
Expand Down

0 comments on commit dd5e640

Please sign in to comment.