✨ (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 { Context } from "elysia";
|
||||||
import { UserRoleAssignmentSchema } from "../schemas/UserRoleAssignment.schema";
|
import { userRoleAssignmentSchema } from "../schemas/userRoleAssignment.schema";
|
||||||
import { returnErrorResponse } from "../../../helpers/callback/httpResponse";
|
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) => {
|
export const userRoleAssignmentController = async (ctx: Context) => {
|
||||||
const validation = UserRoleAssignmentSchema.safeParse(ctx.body);
|
// Validate form input using zod schema
|
||||||
// if (!validation.success)
|
const validation = userRoleAssignmentSchema.safeParse(ctx.body);
|
||||||
// return returnErrorResponse(
|
if (!validation.success)
|
||||||
// ctx.set,
|
return returnErrorResponse(
|
||||||
// 400,
|
ctx.set,
|
||||||
// "Validation error",
|
400,
|
||||||
// validation.error
|
"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 Elysia from "elysia";
|
||||||
import { createUserRoleController } from "./controller/createUserRole.controller";
|
import { createUserRoleController } from "./controller/createUserRole.controller";
|
||||||
import { unautenticatedMiddleware } from "../../middleware/auth/unauthenticated.middleware";
|
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" })
|
export const userRoleModule = new Elysia({ prefix: "/roles" })
|
||||||
.get("/", () => "Hello User Role Module", {
|
.get("/", () => "Hello User Role Module", {
|
||||||
beforeHandle: unautenticatedMiddleware,
|
beforeHandle: unautenticatedMiddleware,
|
||||||
})
|
})
|
||||||
.post("/", createUserRoleController)
|
.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";
|
import z from "zod";
|
||||||
|
|
||||||
export const UserRoleAssignmentSchema = z.object({
|
export const userRoleAssignmentSchema = z.object({
|
||||||
userId: z.string(),
|
userId: z.string(),
|
||||||
roleId: 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