Skip to content

Commit

Permalink
create user via api
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 17, 2024
1 parent 74cb012 commit 051632b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 55 deletions.
19 changes: 1 addition & 18 deletions admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
},

Expand All @@ -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(
Expand Down
69 changes: 34 additions & 35 deletions admin/resources/users/user.actions.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -16,45 +10,50 @@ 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",
notice: {
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",
};
}
}
};
4 changes: 2 additions & 2 deletions api/src/modules/admin/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ControllerResponse> {
return tsRestHandler(adminContract.addUser, async ({ body }) => {
Expand Down

0 comments on commit 051632b

Please sign in to comment.