🚚 create backup folder
create backup folder for archive the old modules
This commit is contained in:
@ -0,0 +1,81 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Context } from "elysia";
|
||||
import {
|
||||
returnErrorResponse,
|
||||
returnWriteResponse,
|
||||
} from "../../../helpers/callback/httpResponse";
|
||||
import { createUserRoleService } from "../services/createUserRole.service";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import { createUserRoleSchema } from "../schemas/createUserRole.schema";
|
||||
import { getCookie } from "../../../helpers/http/userHeader/cookies/getCookies";
|
||||
import { jwtDecode } from "../../../helpers/http/jwt/decode";
|
||||
|
||||
/**
|
||||
* @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 createUserRoleController = async (
|
||||
ctx: Context & { body: Prisma.UserRoleUncheckedCreateInput }
|
||||
) => {
|
||||
// Validation input form with schema
|
||||
const { error } = createUserRoleSchema.validate(ctx.body);
|
||||
if (error)
|
||||
return returnErrorResponse(ctx.set, 400, "Invalid user input", error);
|
||||
|
||||
// Delete this, use middleware instead!!!
|
||||
const cookie = getCookie(ctx);
|
||||
if (!cookie.auth_token)
|
||||
return returnErrorResponse(
|
||||
ctx.set,
|
||||
403,
|
||||
"Forbidden, You don't have access to this resouce"
|
||||
);
|
||||
|
||||
const jwtSession = jwtDecode(cookie.auth_token);
|
||||
|
||||
const formData: Prisma.UserRoleUncheckedCreateInput = {
|
||||
...ctx.body,
|
||||
createdBy: jwtSession.userId,
|
||||
};
|
||||
|
||||
try {
|
||||
const newUserRole = await createUserRoleService(formData);
|
||||
return returnWriteResponse(
|
||||
ctx.set,
|
||||
201,
|
||||
"User role created successfully",
|
||||
newUserRole
|
||||
);
|
||||
} catch (error) {
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
}
|
||||
};
|
||||
9
backup/modules/userRole/index.ts
Normal file
9
backup/modules/userRole/index.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import Elysia from "elysia";
|
||||
import { createUserRoleController } from "./controller/createUserRole.controller";
|
||||
import { unautenticatedMiddleware } from "../../middleware/auth/unauthenticated.middleware";
|
||||
|
||||
export const userRoleModule = new Elysia({ prefix: "/roles" })
|
||||
.get("/", () => "Hello User Role Module", {
|
||||
beforeHandle: unautenticatedMiddleware,
|
||||
})
|
||||
.post("/", createUserRoleController);
|
||||
@ -0,0 +1,11 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { userRoleModel } from "../userRole.model";
|
||||
|
||||
export const createUserRoleRepo = async (
|
||||
data: Prisma.UserRoleUncheckedCreateInput
|
||||
) => {
|
||||
const newUserRole = await userRoleModel.create({
|
||||
data,
|
||||
});
|
||||
return newUserRole;
|
||||
};
|
||||
28
backup/modules/userRole/schemas/createUserRole.schema.ts
Normal file
28
backup/modules/userRole/schemas/createUserRole.schema.ts
Normal file
@ -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(),
|
||||
});
|
||||
@ -0,0 +1,6 @@
|
||||
import z from "zod";
|
||||
|
||||
export const userRoleAssignmentSchema = z.object({
|
||||
userId: z.string(),
|
||||
roleId: z.string(),
|
||||
});
|
||||
29
backup/modules/userRole/services/createUserRole.service.ts
Normal file
29
backup/modules/userRole/services/createUserRole.service.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { createUserRoleRepo } from "../repositories/createUserRole.repository";
|
||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
|
||||
export const createUserRoleService = async (
|
||||
userRoleData: Prisma.UserRoleUncheckedCreateInput
|
||||
) => {
|
||||
try {
|
||||
const dataPayload = {
|
||||
...userRoleData,
|
||||
isSuperadmin: Boolean(userRoleData.isSuperadmin),
|
||||
canEditMedia: Boolean(userRoleData.canEditMedia),
|
||||
canManageMedia: Boolean(userRoleData.canManageMedia),
|
||||
canEditEpisodes: Boolean(userRoleData.canEditEpisodes),
|
||||
canManageEpisodes: Boolean(userRoleData.canManageEpisodes),
|
||||
canEditComment: Boolean(userRoleData.canEditComment),
|
||||
canManageComment: Boolean(userRoleData.canManageComment),
|
||||
canEditUser: Boolean(userRoleData.canEditUser),
|
||||
canManageUser: Boolean(userRoleData.canManageUser),
|
||||
canEditSystem: Boolean(userRoleData.canEditSystem),
|
||||
canManageSystem: Boolean(userRoleData.canManageSystem),
|
||||
deletedAt: null,
|
||||
};
|
||||
const newUserRole = await createUserRoleRepo(dataPayload);
|
||||
return newUserRole;
|
||||
} catch (error) {
|
||||
ErrorForwarder(error);
|
||||
}
|
||||
};
|
||||
3
backup/modules/userRole/userRole.model.ts
Normal file
3
backup/modules/userRole/userRole.model.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { prisma } from "../../utils/databases/prisma/connection";
|
||||
|
||||
export const userRoleModel = prisma.userRole;
|
||||
0
backup/modules/userRole/userRole.types.ts
Normal file
0
backup/modules/userRole/userRole.types.ts
Normal file
Reference in New Issue
Block a user