add first operation in user role
This commit is contained in:
@ -1,5 +1,80 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Context } from "elysia";
|
||||
import {
|
||||
returnErrorResponse,
|
||||
returnWriteResponse,
|
||||
} from "../../../helpers/callback/httpResponse";
|
||||
import { handlePrismaError } from "../../../utils/databases/prisma/error/handler";
|
||||
import { createUserRoleSchema } from "../userRole.schema";
|
||||
import { JWTDecodeToken } from "../../../helpers/jwt/decodeToken";
|
||||
import { prisma } from "../../../utils/databases/prisma/connection";
|
||||
import { createUserRoleService } from "../services/createUserRole.service";
|
||||
|
||||
export const createUserRole = (ctx: Context) => {
|
||||
return "Hello User Role Module";
|
||||
/**
|
||||
* @function createUserRole
|
||||
* @description Creates a new user role in the database.
|
||||
*
|
||||
* @param {Context & { body: UserRole }} ctx - The context object containing the request body.
|
||||
* @param {UserRole} ctx.body - The user role data to be created.
|
||||
*
|
||||
* @returns {Promise<Object>} A response object indicating success or failure.
|
||||
* @throws {Object} An error response object if validation fails or an error occurs during role creation.
|
||||
*
|
||||
* @example
|
||||
* Request route: POST /roles
|
||||
* Request body:
|
||||
* {
|
||||
* "userID": "e31668e6-c261-4a7e-9469-ffad734cf2dd",
|
||||
* "name": "Admin",
|
||||
* "primaryColor": "#D9D9D9",
|
||||
* "secondaryColor": "#FFFFFF",
|
||||
* "pictureImage": "https://example.com/picture.jpg",
|
||||
* "badgeImage": "https://example.com/badge.jpg",
|
||||
* "isSuperadmin": false,
|
||||
* "canEditMedia": false,
|
||||
* "canManageMedia": false,
|
||||
* "canEditEpisodes": false,
|
||||
* "canManageEpisodes": false,
|
||||
* "canEditComment": false,
|
||||
* "canManageComment": false,
|
||||
* "canEditUser": false,
|
||||
* "canManageUser": false,
|
||||
* "canEditSystem": false,
|
||||
* "canManageSystem": false
|
||||
* }
|
||||
*/
|
||||
export const createUserRole = async (
|
||||
ctx: Context & { body: Prisma.UserRoleUncheckedCreateInput }
|
||||
) => {
|
||||
const { error } = createUserRoleSchema.validate(ctx.body);
|
||||
if (error)
|
||||
return returnErrorResponse(ctx.set, 400, "Invalid user input", error);
|
||||
|
||||
const formData: Prisma.UserRoleUncheckedCreateInput = { ...ctx.body };
|
||||
const userCreator = JWTDecodeToken(ctx);
|
||||
|
||||
const dataPayload = {
|
||||
...formData,
|
||||
isSuperadmin: Boolean(formData.isSuperadmin),
|
||||
canEditMedia: Boolean(formData.canEditMedia),
|
||||
canManageMedia: Boolean(formData.canManageMedia),
|
||||
canEditEpisodes: Boolean(formData.canEditEpisodes),
|
||||
canManageEpisodes: Boolean(formData.canManageEpisodes),
|
||||
canEditComment: Boolean(formData.canEditComment),
|
||||
canManageComment: Boolean(formData.canManageComment),
|
||||
canEditUser: Boolean(formData.canEditUser),
|
||||
canManageUser: Boolean(formData.canManageUser),
|
||||
canEditSystem: Boolean(formData.canEditSystem),
|
||||
canManageSystem: Boolean(formData.canManageSystem),
|
||||
createdBy: userCreator.user.id,
|
||||
deletedAt: null,
|
||||
};
|
||||
|
||||
createUserRoleService(dataPayload)
|
||||
.then((result) =>
|
||||
returnWriteResponse(ctx.set, 201, "User role created", result)
|
||||
)
|
||||
.catch((error) =>
|
||||
returnErrorResponse(ctx.set, 500, "Internal Server Error", error)
|
||||
);
|
||||
};
|
||||
|
||||
@ -0,0 +1,20 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { userRoleModel } from "../userRole.model";
|
||||
import { handlePrismaError } from "../../../utils/databases/prisma/error/handler";
|
||||
import { returnErrorResponse } from "../../../helpers/callback/httpResponse";
|
||||
import { Context } from "elysia";
|
||||
|
||||
export const createUserRoleService = async (
|
||||
ctx: Context,
|
||||
userRoleData: Prisma.UserRoleUncheckedCreateInput
|
||||
) => {
|
||||
try {
|
||||
const newUserRole = await userRoleModel.create({
|
||||
data: userRoleData,
|
||||
});
|
||||
return newUserRole;
|
||||
} catch (error) {
|
||||
const { status, message, details } = handlePrismaError(error);
|
||||
throw returnErrorResponse(ctx.set, status, message, details);
|
||||
}
|
||||
};
|
||||
|
||||
@ -0,0 +1,3 @@
|
||||
import { prisma } from "../../utils/databases/prisma/connection";
|
||||
|
||||
export const userRoleModel = prisma.userRole;
|
||||
|
||||
@ -0,0 +1,28 @@
|
||||
import Joi from "joi";
|
||||
|
||||
export const createUserRoleSchema = Joi.object({
|
||||
name: Joi.string().min(4).max(255).required(),
|
||||
primaryColor: Joi.string()
|
||||
.pattern(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
|
||||
.optional(),
|
||||
secondaryColor: Joi.string()
|
||||
.pattern(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
|
||||
.optional(),
|
||||
pictureImage: Joi.string()
|
||||
.uri({ scheme: ["http", "https"] })
|
||||
.optional(),
|
||||
badgeImage: Joi.string()
|
||||
.uri({ scheme: ["http", "https"] })
|
||||
.optional(),
|
||||
isSuperadmin: Joi.boolean().required(),
|
||||
canEditMedia: Joi.boolean().required(),
|
||||
canManageMedia: Joi.boolean().required(),
|
||||
canEditEpisodes: Joi.boolean().required(),
|
||||
canManageEpisodes: Joi.boolean().required(),
|
||||
canEditComment: Joi.boolean().required(),
|
||||
canManageComment: Joi.boolean().required(),
|
||||
canEditUser: Joi.boolean().required(),
|
||||
canManageUser: Joi.boolean().required(),
|
||||
canEditSystem: Joi.boolean().required(),
|
||||
canManageSystem: Joi.boolean().required(),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user