diff --git a/admin/index.ts b/admin/index.ts index c757ee2e..dee457e0 100644 --- a/admin/index.ts +++ b/admin/index.ts @@ -48,22 +48,6 @@ const start = async () => { options: { parent: databaseNavigation, icon: "Globe", - actions: { - new: { - handler: async (request, response, context) => { - const { resource, h, record, records } = context; - const { payload } = request; - return { - record: {}, - notice: { - message: "Action completed", - type: "success", - }, - }; - }, - }, - //actionType: "record", - }, }, }, @@ -83,8 +67,7 @@ const start = async () => { cookiePassword: "some-secret", }); - const adminRouterWithAuth = AdminJSExpress.buildRouter(admin); - app.use(admin.options.rootPath, adminRouterWithAuth); + app.use(admin.options.rootPath, adminRouter); app.listen(PORT, () => { console.log( diff --git a/admin/resources/users/user.actions.ts b/admin/resources/users/user.actions.ts index 3bb8fea7..3601c354 100644 --- a/admin/resources/users/user.actions.ts +++ b/admin/resources/users/user.actions.ts @@ -1,10 +1,4 @@ -import { - ActionContext, - ActionRequest, - ActionResponse, - populator, -} from "adminjs"; -import { getAuthUserFromContext } from "../../utils/get-auth-user-from-context.js"; +import { ActionContext, ActionRequest, ActionResponse } from "adminjs"; import { CreateUserDto } from "@shared/dtos/users/create-user.dto.js"; const API_URL = process.env.API_URL || "http://localhost:4000"; @@ -16,32 +10,31 @@ export const createUserAction = async ( ) => { if (request.method === "post") { const { resource, currentAdmin, records } = context; - const { email, password, role, name } = request.payload as CreateUserDto; - let record = resource.build({ email, password, role, name }); - const test2 = await record.create(context); - - const test = 111; - // const [populatedRecord] = await populator([record], context); - + const { email, role, name, partnerName } = request.payload as CreateUserDto; + const record = resource.build({ email, role, name }); + const accessToken = currentAdmin?.accessToken; try { - // const apiResponse = await fetch(`${API_URL}/admin/users`, { - // method: "POST", - // headers: { - // "Content-Type": "application/json", - // Authorization: `Bearer ${accessToken}`, - // Origin: context.req.headers.origin, - // }, - // body: JSON.stringify(request.payload), - // }); - // - // if (!apiResponse.ok) { - // throw new Error("Failed to create user"); - // } + const apiResponse = await fetch(`${API_URL}/admin/users`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${accessToken}`, + Origin: response.req.headers.origin, + }, + body: JSON.stringify(request.payload), + }); - // const resources = await context.resource.findMany({}); - console.log("RECORDS", context.records); - console.log("SINGLE RECORD", context.record); - const json = test2.toJSON(); + if (!apiResponse.ok) { + const res = await apiResponse.json(); + return { + record, + redirectUrl: "/admin/resources/User", + notice: { + message: JSON.stringify(res.errors), + type: "error", + }, + }; + } return { redirectUrl: "/admin/resources/User", @@ -49,12 +42,18 @@ export const createUserAction = async ( message: "User created successfully", type: "success", }, - record: json, + record, }; } catch (error) { - const testerror = error; - console.error(error); - throw error; + console.error("Error creating user", error); + return { + record, + notice: { + message: "Error creating user: Contact administrator", + type: "error", + }, + redirectUrl: "/admin/resources/User", + }; } } }; diff --git a/api/src/modules/admin/admin.controller.ts b/api/src/modules/admin/admin.controller.ts index 5a833cc7..e89f9094 100644 --- a/api/src/modules/admin/admin.controller.ts +++ b/api/src/modules/admin/admin.controller.ts @@ -10,12 +10,12 @@ import { ROLES } from '@shared/entities/users/roles.enum'; @Controller() @UseGuards(JwtAuthGuard, RolesGuard) +@RequiredRoles(ROLES.ADMIN) export class AdminController { constructor(private readonly auth: AuthenticationService) {} - @RequiredRoles(ROLES.ADMIN) @TsRestHandler(adminContract.addUser) - async createUser( + async addUser( @Headers('origin') origin: string, ): Promise { return tsRestHandler(adminContract.addUser, async ({ body }) => {