Skip to content

Commit

Permalink
feat(api,api-sdk): add get group by name
Browse files Browse the repository at this point in the history
  • Loading branch information
waddaboo committed Dec 5, 2024
1 parent c299b60 commit 896f725
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 3 deletions.
10 changes: 8 additions & 2 deletions apps/api/src/app/groups/groups.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ export class GroupsController {
@Get()
@ApiQuery({ name: "adminId", required: false, type: String })
@ApiQuery({ name: "memberId", required: false, type: String })
@ApiQuery({ name: "name", required: false, type: String })
@ApiOperation({ description: "Returns the list of groups." })
@ApiCreatedResponse({ type: Group, isArray: true })
async getGroups(
@Query("adminId") adminId: string,
@Query("memberId") memberId: string
@Query("memberId") memberId: string,
@Query("name") name: string
) {
const groups = await this.groupsService.getGroups({ adminId, memberId })
const groups = await this.groupsService.getGroups({
adminId,
memberId,
name
})
const groupIds = groups.map((group) => group.id)
const fingerprints = await this.groupsService.getFingerprints(groupIds)

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

expect(result).toHaveLength(1)
})

it("Should return a list of groups by group name", async () => {
await groupsService.createGroup(
{
name: "OnChainGroup",
description: "This is a description",
treeDepth: 16,
fingerprintDuration: 3600
},
"admin02"
)

const result = await groupsService.getGroups({
name: "OnChainGroup"
})

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

describe("# getGroup", () => {
Expand Down
8 changes: 8 additions & 0 deletions apps/api/src/app/groups/groups.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ export class GroupsService {
async getGroups(filters?: {
adminId?: string
memberId?: string
name?: string
}): Promise<Group[]> {
let where = {}

Expand All @@ -838,6 +839,13 @@ export class GroupsService {
}
}

if (filters?.name) {
where = {
name: filters.name,
...where
}
}

return this.groupRepository.find({
relations: { members: true },
where,
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 @@ -25,7 +25,8 @@ import {
getGroupsByAdminId,
getGroupsByMemberId,
getCredentialGroupJoinUrl,
getMultipleCredentialsGroupJoinUrl
getMultipleCredentialsGroupJoinUrl,
getGroupsByName
} from "./groups"
import { createInvite, getInvite } from "./invites"

Expand Down Expand Up @@ -112,6 +113,17 @@ export default class ApiSdk {
return groups
}

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

return groups
}

/**
* Creates a group using the API key.
* @param groupCreationDetails Data to create the group.
Expand Down
31 changes: 31 additions & 0 deletions libs/api-sdk/src/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,37 @@ export async function getGroupsByMemberId(
return groups
}

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

let groups = await request(requestUrl, config)

groups = groups.map((group: any) => {
let credentials

try {
credentials = JSON.parse(group.credentials)
} catch (error) {
credentials = null
}

return {
...group,
credentials
}
})

return groups
}

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

const name = "Group1"

const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const groups: Group[] = await apiSdk.getGroupsByName(name)
expect(groups).toHaveLength(1)
})
it("Should return all groups by name and null in the credentials that don't have a valid JSON string", async () => {
requestMocked.mockImplementationOnce(() =>
Promise.resolve([
{
id: "10402173435763029700781503965100",
name: "Group1",
description: "This is a new group",
type: "off-chain",
admin: "0xdf558148e66850ac48dbe2c8119b0eefa7d08bfd19c997c90a142eb97916b847",
treeDepth: 16,
fingerprintDuration: 3600,
createdAt: "2023-07-15T08:21:05.000Z",
members: ["1"],
credentials: {}
}
])
)

const name = "Group1"

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

0 comments on commit 896f725

Please sign in to comment.