✨ (roles) create endpoint for assignment
new endpoint for user to assign with existed role. NEED FIX!!
This commit is contained in:
@ -1,16 +1,39 @@
|
||||
import { Context } from "elysia";
|
||||
import { UserRoleAssignmentSchema } from "../schemas/UserRoleAssignment.schema";
|
||||
import { returnErrorResponse } from "../../../helpers/callback/httpResponse";
|
||||
import { userRoleAssignmentSchema } from "../schemas/userRoleAssignment.schema";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import { userRoleAssignmentService } from "../services/userRoleAssignment.service";
|
||||
import {
|
||||
returnErrorResponse,
|
||||
returnWriteResponse,
|
||||
} from "../../../helpers/callback/httpResponse";
|
||||
|
||||
export const UserRoleAssignmentController = async (ctx: Context) => {
|
||||
const validation = UserRoleAssignmentSchema.safeParse(ctx.body);
|
||||
// if (!validation.success)
|
||||
// return returnErrorResponse(
|
||||
// ctx.set,
|
||||
// 400,
|
||||
// "Validation error",
|
||||
// validation.error
|
||||
// );
|
||||
export const userRoleAssignmentController = async (ctx: Context) => {
|
||||
// Validate form input using zod schema
|
||||
const validation = userRoleAssignmentSchema.safeParse(ctx.body);
|
||||
if (!validation.success)
|
||||
return returnErrorResponse(
|
||||
ctx.set,
|
||||
400,
|
||||
"Validation error",
|
||||
validation.error
|
||||
);
|
||||
|
||||
return ctx.body;
|
||||
try {
|
||||
// Store the userId and roleId from the validated data
|
||||
const payload = {
|
||||
userId: validation.data.userId,
|
||||
roleId: validation.data.roleId,
|
||||
};
|
||||
|
||||
// Call the service to assign the user role
|
||||
const AssignUserRole = await userRoleAssignmentService(payload);
|
||||
return returnWriteResponse(
|
||||
ctx.set,
|
||||
201,
|
||||
"User role assignment successfully",
|
||||
AssignUserRole
|
||||
);
|
||||
} catch (error) {
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
import Elysia from "elysia";
|
||||
import { createUserRoleController } from "./controller/createUserRole.controller";
|
||||
import { unautenticatedMiddleware } from "../../middleware/auth/unauthenticated.middleware";
|
||||
import { UserRoleAssignmentController } from "./controller/UserRoleAssignment.controller";
|
||||
import { userRoleAssignmentController } from "./controller/userRoleAssignment.controller";
|
||||
|
||||
export const userRoleModule = new Elysia({ prefix: "/roles" })
|
||||
.get("/", () => "Hello User Role Module", {
|
||||
beforeHandle: unautenticatedMiddleware,
|
||||
})
|
||||
.post("/", createUserRoleController)
|
||||
.post("/assign", UserRoleAssignmentController); // need fix and it just for development only!
|
||||
.post("/assign", userRoleAssignmentController); // need fix and it just for development only!
|
||||
|
||||
@ -0,0 +1,22 @@
|
||||
import { userModel } from "../../user/user.model";
|
||||
import { UserRoleAssignment } from "../userRole.types";
|
||||
|
||||
export const userRoleAssignmentRepository = async ({
|
||||
userId,
|
||||
roleId,
|
||||
}: UserRoleAssignment) => {
|
||||
const userAssigned = await userModel.update({
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
data: {
|
||||
roles: {
|
||||
connect: {
|
||||
id: roleId,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return userAssigned;
|
||||
};
|
||||
@ -1,6 +1,6 @@
|
||||
import z from "zod";
|
||||
|
||||
export const UserRoleAssignmentSchema = z.object({
|
||||
export const userRoleAssignmentSchema = z.object({
|
||||
userId: z.string(),
|
||||
roleId: z.string(),
|
||||
});
|
||||
|
||||
18
src/modules/userRole/services/userRoleAssignment.service.ts
Normal file
18
src/modules/userRole/services/userRoleAssignment.service.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
import { userRoleAssignmentRepository } from "../repositories/userRoleAssignment.repository";
|
||||
import { UserRoleAssignment } from "../userRole.types";
|
||||
|
||||
export const userRoleAssignmentService = async ({
|
||||
userId,
|
||||
roleId,
|
||||
}: UserRoleAssignment) => {
|
||||
try {
|
||||
const assignRoleToUser = await userRoleAssignmentRepository({
|
||||
userId,
|
||||
roleId,
|
||||
});
|
||||
return assignRoleToUser;
|
||||
} catch (error) {
|
||||
ErrorForwarder(error);
|
||||
}
|
||||
};
|
||||
@ -0,0 +1,4 @@
|
||||
export interface UserRoleAssignment {
|
||||
userId: string;
|
||||
roleId: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user