refactor: improve apikey admin api and store the admin on db after message verification in UI

This commit is contained in:
Jeeiii
2024-03-27 13:06:47 +01:00
parent fa4792cbe0
commit e496fd6fd5
5 changed files with 91 additions and 41 deletions

View File

@@ -1,7 +1,7 @@
import { Body, Controller, Get, Param, Post, Put } from "@nestjs/common"
import { ApiCreatedResponse } from "@nestjs/swagger"
import { ApiKeyActions } from "@bandada/utils"
import { CreateAdminDTO } from "./dto/create-admin.dto"
import { UpdateApiKeyDTO } from "./dto/update-apikey.dto"
import { AdminsService } from "./admins.service"
import { Admin } from "./entities/admin.entity"
@@ -20,11 +20,11 @@ export class AdminsController {
return this.adminsService.findOne({ id: adminId })
}
@Put("update-apikey")
async updateApiKey(@Body() dto: UpdateApiKeyDTO): Promise<string> {
return this.adminsService.updateApiKey({
adminId: dto.adminId,
action: dto.action
})
@Put(":admin/apikey")
async updateApiKey(
@Param("admin") adminId: string,
@Body() action: ApiKeyActions
): Promise<string> {
return this.adminsService.updateApiKey(adminId, action)
}
}

View File

@@ -7,7 +7,6 @@ import { v4 } from "uuid"
import { ApiKeyActions } from "@bandada/utils"
import { CreateAdminDTO } from "./dto/create-admin.dto"
import { Admin } from "./entities/admin.entity"
import { UpdateApiKeyDTO } from "./dto/update-apikey.dto"
@Injectable()
export class AdminsService {
@@ -36,11 +35,15 @@ export class AdminsService {
/**
* Updates the API key for a given admin based on the specified actions.
*
* @param {UpdateApiKeyDTO} updateApiKeyDTO The DTO containing the admin ID and the action to be performed.
* @param adminId The identifier of the admin.
* @param action The action to be executed on the API key of the admin.
* @returns {Promise<string>} The API key of the admin after the update operation. If the API key is disabled, the return value might not be meaningful.
* @throws {BadRequestException} If the admin ID does not correspond to an existing admin, if the admin does not have an API key when trying to enable it, or if the action is unsupported.
*/
async updateApiKey({ adminId, action }: UpdateApiKeyDTO): Promise<string> {
async updateApiKey(
adminId: string,
action: ApiKeyActions
): Promise<string> {
const admin = await this.findOne({
id: adminId
})

View File

@@ -1,10 +0,0 @@
import { IsEnum, IsString } from "class-validator"
import { ApiKeyActions } from "@bandada/utils"
export class UpdateApiKeyDTO {
@IsString()
adminId: string
@IsEnum(ApiKeyActions)
action: ApiKeyActions
}

View File

@@ -1,6 +1,6 @@
import { request } from "@bandada/utils"
import { ApiKeyActions, request } from "@bandada/utils"
import { SiweMessage } from "siwe"
import { Group } from "../types"
import { Admin, Group } from "../types"
import createAlert from "../utils/createAlert"
const API_URL = import.meta.env.VITE_API_URL
@@ -106,6 +106,69 @@ export async function createGroup(
}
}
/**
* It creates a new admin.
* @param id The admin id.
* @param address The admin address.
* @returns The Admin.
*/
export async function createAdmin(
id: string,
address: string
): Promise<Admin | null> {
try {
return await request(`${API_URL}/admins`, {
method: "POST",
data: {
id,
address
}
})
} catch (error: any) {
console.error(error)
createAlert(error.response.data.message)
return null
}
}
/**
* It returns details of a specific admin.
* @param adminId The admin id.
* @returns The admin details.
*/
export async function getAdmin(adminId: string): Promise<Admin | null> {
try {
return await request(`${API_URL}/admins/${adminId}`)
} catch (error: any) {
console.error(error)
createAlert(error.response.data.message)
return null
}
}
/**
* It works with the Admin API key.
* @param adminId The admin id.
* @param action The action to carry on the API key.
*/
export async function updateApiKey(
adminId: string,
action: ApiKeyActions
): Promise<string | null> {
try {
return await request(`${API_URL}/admins/${adminId}/apikey`, {
method: "PUT",
data: {
action
}
})
} catch (error: any) {
console.error(error)
createAlert(error.response.data.message)
return null
}
}
/**
* It updates the detail of a group.
* @param group The group id.
@@ -130,23 +193,6 @@ export async function createGroup(
// }
// }
/**
* It generates a new API key.
* @param group The group id.
*/
// @todo needs refactoring to support the new logic.
// export async function generateApiKey(groupId: string): Promise<string | null> {
// try {
// return await request(`${API_URL}/groups/${groupId}/api-key`, {
// method: "PATCH"
// })
// } catch (error: any) {
// console.error(error)
// createAlert(error.response.data.message)
// return null
// }
// }
/**
* It removes a group.
* @param groupId The group id.

View File

@@ -17,7 +17,13 @@ import { SiweMessage } from "siwe"
import { configureChains, createClient, WagmiConfig } from "wagmi"
import { sepolia } from "wagmi/chains"
import { publicProvider } from "wagmi/providers/public"
import { getNonce, logOut, signIn } from "../api/bandadaAPI"
import {
createAdmin,
getAdmin,
getNonce,
logOut,
signIn
} from "../api/bandadaAPI"
import useSessionData from "../hooks/use-session-data"
import { Admin } from "../types"
@@ -59,7 +65,7 @@ export function AuthContextProvider({ children }: { children: ReactNode }) {
getMessageBody: ({ message }) => message.prepareMessage(),
verify: async ({ message, signature }) => {
const admin = await signIn({
const admin: Admin = await signIn({
message,
signature
})
@@ -67,6 +73,11 @@ export function AuthContextProvider({ children }: { children: ReactNode }) {
if (admin) {
saveAdmin(admin)
const alreadyCreated = await getAdmin(admin.id)
if (!alreadyCreated)
await createAdmin(admin.id, admin.address)
return true
}