Skip to content

Commit

Permalink
feat: add missing APIs endpoints on ApiSdk
Browse files Browse the repository at this point in the history
  • Loading branch information
0xjei committed Mar 28, 2024
1 parent a1f1f3d commit 57fabe0
Show file tree
Hide file tree
Showing 7 changed files with 907 additions and 61 deletions.
178 changes: 158 additions & 20 deletions apps/api/src/app/groups/groups.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
import {
ApiBody,
ApiCreatedResponse,
ApiExcludeEndpoint,
ApiHeader,
ApiOperation,
ApiQuery,
Expand Down Expand Up @@ -64,37 +63,176 @@ export class GroupsController {

@Post()
@UseGuards(AuthGuard)
@ApiExcludeEndpoint()
async createGroup(@Req() req: Request, @Body() dto: CreateGroupDto) {
const group = await this.groupsService.createGroup(
dto,
req.session.adminId
)
const fingerprint = await this.groupsService.getFingerprint(group.id)
@ApiBody({ required: false, type: Array<CreateGroupDto> })
@ApiHeader({ name: "x-api-key", required: false })
@ApiCreatedResponse({ type: Group })
@ApiOperation({
description: "Create one or more groups using an API Key or manually."
})
async createGroups(
@Body() dtos: Array<CreateGroupDto>,
@Headers() headers: Headers,
@Req() req: Request
) {
let groups = []
const groupsToResponseDTO = []

return mapGroupToResponseDTO(group, fingerprint)
const apiKey = headers["x-api-key"] as string

if (apiKey) {
groups = await this.groupsService.createGroupsWithAPIKey(
dtos,
apiKey
)
}

if (req.session.adminId) {
groups = await this.groupsService.createGroupsManually(
dtos,
req.session.adminId
)
}

for await (const group of groups) {
const fingerprint = await this.groupsService.getFingerprint(
group.id
)

groupsToResponseDTO.push(mapGroupToResponseDTO(group, fingerprint))
}

return groupsToResponseDTO
}

@Delete()
@UseGuards(AuthGuard)
@ApiBody({ required: false, type: Array<string> })
@ApiHeader({ name: "x-api-key", required: false })
@ApiOperation({
description: "Remove one or more groups using an API Key or manually."
})
async removeGroups(
@Body() groupsIds: Array<string>,
@Headers() headers: Headers,
@Req() req: Request
) {
const apiKey = headers["x-api-key"] as string

if (apiKey) {
await this.groupsService.removeGroupsWithAPIKey(groupsIds, apiKey)
}

if (req.session.adminId) {
await this.groupsService.removeGroupsManually(
groupsIds,
req.session.adminId
)
}
}

@Delete(":group")
@UseGuards(AuthGuard)
@ApiExcludeEndpoint()
async removeGroup(@Req() req: Request, @Param("group") groupId: string) {
await this.groupsService.removeGroup(groupId, req.session.adminId)
@ApiHeader({ name: "x-api-key", required: false })
@ApiOperation({
description: "Remove a specific group using an API Key or manually"
})
async removeGroup(
@Param("group") groupId: string,
@Headers() headers: Headers,
@Req() req: Request
) {
const apiKey = headers["x-api-key"] as string

if (apiKey) {
await this.groupsService.removeGroupWithAPIKey(groupId, apiKey)
}

if (req.session.adminId) {
await this.groupsService.removeGroupManually(
groupId,
req.session.adminId
)
}
}

@Patch()
@UseGuards(AuthGuard)
@ApiBody({ required: false, type: Array<UpdateGroupDto> })
@ApiCreatedResponse({ type: Array<Group> })
@ApiHeader({ name: "x-api-key", required: false })
@ApiOperation({
description: "Update one or more groups using an API Key or manually."
})
async updateGroups(
@Headers() headers: Headers,
@Body() groupsIds: Array<string>,
@Body() dtos: Array<UpdateGroupDto>,
@Req() req: Request
) {
let groups = []
const groupsToResponseDTO = []

const apiKey = headers["x-api-key"] as string

if (apiKey) {
groups = await this.groupsService.updateGroupsWithApiKey(
groupsIds,
dtos,
apiKey
)
}

if (req.session.adminId) {
groups = await this.groupsService.updateGroupsManually(
groupsIds,
dtos,
req.session.adminId
)
}

for await (const group of groups) {
const fingerprint = await this.groupsService.getFingerprint(
group.id
)

groupsToResponseDTO.push(mapGroupToResponseDTO(group, fingerprint))
}

return groupsToResponseDTO
}

@Patch(":group")
@UseGuards(AuthGuard)
@ApiExcludeEndpoint()
@ApiHeader({ name: "x-api-key", required: false })
@ApiBody({ required: false, type: UpdateGroupDto })
@ApiCreatedResponse({ type: Group })
@ApiOperation({
description: "Update a specific group using an API Key or manually."
})
async updateGroup(
@Req() req: Request,
@Param("group") groupId: string,
@Body() dto: UpdateGroupDto
@Headers() headers: Headers,
@Body() dto: UpdateGroupDto,
@Req() req: Request
) {
const group = await this.groupsService.updateGroup(
groupId,
dto,
req.session.adminId
)
let group: any
const apiKey = headers["x-api-key"] as string

if (apiKey) {
group = await this.groupsService.updateGroupWithApiKey(
groupId,
dto,
apiKey
)
}

if (req.session.adminId) {
group = await this.groupsService.updateGroupManually(
groupId,
dto,
req.session.adminId
)
}

const fingerprint = await this.groupsService.getFingerprint(groupId)

Expand Down
44 changes: 11 additions & 33 deletions apps/dashboard/src/api/bandadaAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,20 @@ export async function createGroup(
credentials?: any
): Promise<Group | null> {
try {
const group = await request(`${API_URL}/groups`, {
const groups = await request(`${API_URL}/groups`, {
method: "POST",
data: {
name,
description,
treeDepth,
fingerprintDuration,
credentials: JSON.stringify(credentials)
}
data: [
{
name,
description,
treeDepth,
fingerprintDuration,
credentials: JSON.stringify(credentials)
}
]
})

return { ...group, type: "off-chain" }
return { ...groups.at(0), type: "off-chain" }
} catch (error: any) {
console.error(error)
createAlert(error.response.data.message)
Expand Down Expand Up @@ -169,30 +171,6 @@ export async function updateApiKey(
}
}

/**
* It updates the detail of a group.
* @param group The group id.
* @param memberId The group member id.
*/
// @todo needs refactoring to support the new logic.
// export async function updateGroup(
// groupId: string,
// { apiEnabled }: { apiEnabled: boolean }
// ): Promise<Group | null> {
// try {
// const group = await request(`${API_URL}/groups/${groupId}`, {
// method: "PATCH",
// data: { apiEnabled }
// })

// return { ...group, type: "off-chain" }
// } catch (error: any) {
// console.error(error)
// createAlert(error.response.data.message)
// return null
// }
// }

/**
* It removes a group.
* @param groupId The group id.
Expand Down
68 changes: 68 additions & 0 deletions libs/api-sdk/src/admins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { request } from "@bandada/utils"
import { AdminRequest, AdminResponse, AdminUpdateApiKeyRequest } from "./types"

const url = "/admins"

/**
* Create an admin with the provided details.
* @param dto Array of objects containing the details for the admin to be created.
* @returns Array of the created groups.
*/
export async function createAdmin(
config: object,
dto: AdminRequest
): Promise<AdminResponse> {
const newConfig: any = {
method: "post",
data: {
dto
},
...config
}

return request(url, newConfig)
}

/**
* Get an admin.
* @param adminId The admin id.
* @returns The admin with given id.
*/
export async function getAdmin(
config: object,
adminId: string
): Promise<AdminResponse> {
const requestUrl = `${url}/${adminId}`

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

return request(requestUrl, newConfig)
}

/**
* Update an admin API key.
* @param adminId The admin id.
* @param dto The action to be executed on the API key.
* @returns The updated API key.
*/
export async function updateApiKey(
config: object,
adminId: string,
dto: AdminUpdateApiKeyRequest
): Promise<string> {
const requestUrl = `${url}/${adminId}/apikey`

const newConfig: any = {
method: "put",
body: {
adminId,
dto
},
...config
}

return request(requestUrl, newConfig)
}
Loading

0 comments on commit 57fabe0

Please sign in to comment.