🚩 (role) create user role assignment module
create module for assign to user and unassign role from user
This commit is contained in:
@ -1,39 +0,0 @@
|
||||
import { Context } from "elysia";
|
||||
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) => {
|
||||
// Validate form input using zod schema
|
||||
const validation = userRoleAssignmentSchema.safeParse(ctx.body);
|
||||
if (!validation.success)
|
||||
return returnErrorResponse(
|
||||
ctx.set,
|
||||
400,
|
||||
"Validation error",
|
||||
validation.error
|
||||
);
|
||||
|
||||
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,9 @@
|
||||
import Elysia from "elysia";
|
||||
import { createUserRoleController } from "./controller/createUserRole.controller";
|
||||
import { unautenticatedMiddleware } from "../../middleware/auth/unauthenticated.middleware";
|
||||
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("/", createUserRoleController);
|
||||
|
||||
@ -1,22 +0,0 @@
|
||||
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,18 +0,0 @@
|
||||
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);
|
||||
}
|
||||
};
|
||||
@ -1,4 +0,0 @@
|
||||
export interface UserRoleAssignment {
|
||||
userId: string;
|
||||
roleId: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user