From 7ec6637e617f743952b1072f6d0a890b46aab095 Mon Sep 17 00:00:00 2001 From: ali ebrahimi Date: Mon, 5 Aug 2024 04:19:35 +0330 Subject: [PATCH 1/2] edit admin user by address --- src/server/adminJs/tabs/projectsTab.ts | 36 +++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/server/adminJs/tabs/projectsTab.ts b/src/server/adminJs/tabs/projectsTab.ts index f0af31aad..6a7596a73 100644 --- a/src/server/adminJs/tabs/projectsTab.ts +++ b/src/server/adminJs/tabs/projectsTab.ts @@ -1,12 +1,13 @@ import fs from 'fs'; import path from 'path'; -import adminJs from 'adminjs'; +import adminJs, { ValidationError } from 'adminjs'; import { SelectQueryBuilder } from 'typeorm'; import { ActionResponse, After, } from 'adminjs/src/backend/actions/action.interface'; import { RecordJSON } from 'adminjs/src/frontend/interfaces/record-json.interface'; +import { ethers } from 'ethers'; import { Project, ProjectUpdate, @@ -726,7 +727,7 @@ export const projectsTab = { filter: false, list: false, show: true, - edit: false, + edit: true, new: false, }, position: 2, @@ -968,7 +969,7 @@ export const projectsTab = { canAccessProjectAction({ currentAdmin }, ResourceActions.EDIT), before: async (request: AdminJsRequestInterface) => { - const { verified, reviewStatus } = request.payload; + const { verified, reviewStatus, adminUserAddress } = request.payload; const statusChanges: string[] = []; if (request?.payload?.id) { // remove addresses from payload to avoid updating them @@ -1037,6 +1038,33 @@ export const projectsTab = { request.payload.adminChanged = true; request.payload.newAdminId = newID; } + if (adminUserAddress) { + if (!ethers.utils.isAddress(adminUserAddress)) { + throw new ValidationError({ + adminUserAddress: { + message: + 'The address is malformed or not a valid Ethereum address', + }, + }); + } + + const newAdminUser = await User.findOne({ + where: { walletAddress: adminUserAddress }, + }); + if (!newAdminUser) { + throw new ValidationError({ + adminUserAddress: { + message: + 'The address does not correspond to a valid user profile', + }, + }); + } + + if (newAdminUser.id !== project?.adminUserId) { + request.payload.adminChanged = true; + request.payload.newAdminId = newAdminUser.id; + } + } // We put these status changes in payload, so in after hook we would know to send notification for users request.payload.statusChanges = statusChanges.join(','); @@ -1057,7 +1085,7 @@ export const projectsTab = { const adminUser = await User.findOne({ where: { id: request?.record?.params?.newAdminId }, }); - const previousAdminAddress = project.adminUser.walletAddress; + const previousAdminAddress = project.adminUser?.walletAddress; if (previousAdminAddress) { if (project.adminAddressHistory) { project.adminAddressHistory.push(previousAdminAddress); From cead1f9a7e09493c953a84b775237db849801a32 Mon Sep 17 00:00:00 2001 From: ali ebrahimi Date: Tue, 6 Aug 2024 04:34:01 +0330 Subject: [PATCH 2/2] add validation for new admin user to have a complete profile --- src/server/adminJs/tabs/projectsTab.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/server/adminJs/tabs/projectsTab.ts b/src/server/adminJs/tabs/projectsTab.ts index 6a7596a73..e9b4d9c17 100644 --- a/src/server/adminJs/tabs/projectsTab.ts +++ b/src/server/adminJs/tabs/projectsTab.ts @@ -1060,6 +1060,14 @@ export const projectsTab = { }); } + if (!newAdminUser.name || !newAdminUser.email) { + throw new ValidationError({ + adminUserAddress: { + message: 'The user profile is not completed', + }, + }); + } + if (newAdminUser.id !== project?.adminUserId) { request.payload.adminChanged = true; request.payload.newAdminId = newAdminUser.id;