👔 add create user role
This commit is contained in:
@ -0,0 +1,18 @@
|
||||
import { Context } from "elysia";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import { createUserRoleWithAdminSchema } from "../schemas/createUserRoleWithAdmin.schema";
|
||||
import { returnWriteResponse } from "../../../helpers/callback/httpResponse";
|
||||
import { createUserRoleWIthAdminService } from "../services/http/createUserRoleWIthAdmin.service";
|
||||
|
||||
export const createUserRoleWithAdminController = async (ctx: Context) => {
|
||||
try {
|
||||
const body = createUserRoleWithAdminSchema.parse(ctx.body);
|
||||
const createUserRole = createUserRoleWIthAdminService({
|
||||
...body,
|
||||
createdBy: "787",
|
||||
});
|
||||
return returnWriteResponse(ctx.set, 201, "User role created successfully");
|
||||
} catch (error) {
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
}
|
||||
};
|
||||
@ -1,7 +1,9 @@
|
||||
import Elysia from "elysia";
|
||||
import { getUserRoleByNameController } from "./controllers/getUserRoleByName.controller";
|
||||
import { getUserRoleByIdController } from "./controllers/getUserRoleById.controller";
|
||||
import { createUserRoleWithAdminController } from "./controllers/createUserRoleWithAdmin.controller";
|
||||
|
||||
export const userRoleModule = new Elysia({ prefix: "/users/roles" })
|
||||
.get("/n/:name", getUserRoleByNameController)
|
||||
.get("/id/:id", getUserRoleByIdController);
|
||||
.get("/id/:id", getUserRoleByIdController)
|
||||
.post("/", createUserRoleWithAdminController);
|
||||
|
||||
@ -0,0 +1,10 @@
|
||||
import { userRoleModel } from "../userRole.model";
|
||||
import { createUserRoleRepositoryPayload } from "../userRole.types";
|
||||
|
||||
export const createUserRoleRepository = async (
|
||||
payload: createUserRoleRepositoryPayload
|
||||
) => {
|
||||
return await userRoleModel.create({
|
||||
data: payload,
|
||||
});
|
||||
};
|
||||
@ -0,0 +1,55 @@
|
||||
import z from "zod";
|
||||
|
||||
const MAX_FILE_SIZE = 2 * 1024 * 1024; // 2MB
|
||||
const ACCEPTED_IMAGE_TYPES = ["image/jpeg", "image/png", "image/webp"];
|
||||
|
||||
export const createUserRoleWithAdminSchema = z.object({
|
||||
name: z.string(),
|
||||
primaryColor: z
|
||||
.string()
|
||||
.regex(
|
||||
/^#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{3})$/,
|
||||
"primaryColor: Invalid hex code"
|
||||
)
|
||||
.optional(),
|
||||
secondaryColor: z
|
||||
.string()
|
||||
.regex(
|
||||
/^#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{3})$/,
|
||||
"secondaryColor: Invalid hex code"
|
||||
)
|
||||
.optional(),
|
||||
pictureImage: z
|
||||
.instanceof(File)
|
||||
.refine(
|
||||
(file) => file.size <= MAX_FILE_SIZE,
|
||||
"pictureImage: File reached the maximum limit"
|
||||
)
|
||||
.refine(
|
||||
(file) => ACCEPTED_IMAGE_TYPES.includes(file.type),
|
||||
"pictureImage: File format not supported"
|
||||
)
|
||||
.optional(),
|
||||
badgeImage: z
|
||||
.instanceof(File)
|
||||
.refine(
|
||||
(file) => file.size <= MAX_FILE_SIZE,
|
||||
"badgeImage: File reached the maximum limit"
|
||||
)
|
||||
.refine(
|
||||
(file) => ACCEPTED_IMAGE_TYPES.includes(file.type),
|
||||
"badgeImage: File format not supported"
|
||||
)
|
||||
.optional(),
|
||||
isSuperadmin: z.boolean(),
|
||||
canEditMedia: z.boolean(),
|
||||
canManageMedia: z.boolean(),
|
||||
canEditEpisodes: z.boolean(),
|
||||
canManageEpisodes: z.boolean(),
|
||||
canEditComment: z.boolean(),
|
||||
canManageComment: z.boolean(),
|
||||
canEditUser: z.boolean(),
|
||||
canManageUser: z.boolean(),
|
||||
canEditSystem: z.boolean(),
|
||||
canManageSystem: z.boolean(),
|
||||
});
|
||||
@ -0,0 +1,16 @@
|
||||
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||
import { createUserRoleServicePayload } from "../../userRole.types";
|
||||
import { createUserRoleService } from "../internal/createUserRole.service";
|
||||
|
||||
export const createUserRoleWIthAdminService = async (
|
||||
payload: createUserRoleServicePayload["formInput"]
|
||||
) => {
|
||||
try {
|
||||
return await createUserRoleService({
|
||||
formInput: payload,
|
||||
queryTarget: "withAdmin",
|
||||
});
|
||||
} catch (error) {
|
||||
ErrorForwarder(error);
|
||||
}
|
||||
};
|
||||
@ -0,0 +1,33 @@
|
||||
import { AppError } from "../../../../helpers/error/instances/app";
|
||||
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||
import { createUserRoleRepository } from "../../repositories/createUserRole.repository";
|
||||
import {
|
||||
createUserRoleRepositoryPayload,
|
||||
createUserRoleServicePayload,
|
||||
} from "../../userRole.types";
|
||||
|
||||
export const createUserRoleService = async (
|
||||
payload: createUserRoleServicePayload
|
||||
) => {
|
||||
try {
|
||||
const repositoryMap = {
|
||||
withAdmin: createUserRoleRepository,
|
||||
};
|
||||
|
||||
const repoFn = repositoryMap[payload.queryTarget];
|
||||
if (!repoFn) throw new AppError(503, "Repository handler not found");
|
||||
|
||||
/**
|
||||
* TO-DO!!
|
||||
*
|
||||
* Create a function to handle storing image in here!
|
||||
*/
|
||||
|
||||
const userRoleData = await repoFn(
|
||||
payload.formInput as createUserRoleRepositoryPayload // CHANGE THIS AFTER CREATE STORE IMAGE FUNCTION
|
||||
);
|
||||
return userRoleData;
|
||||
} catch (error) {
|
||||
ErrorForwarder(error);
|
||||
}
|
||||
};
|
||||
@ -2,3 +2,51 @@ export interface findUserRolePayload {
|
||||
identifier: string;
|
||||
query_target: "id" | "name";
|
||||
}
|
||||
|
||||
export interface createUserRoleServicePayload {
|
||||
formInput: {
|
||||
name: string;
|
||||
primaryColor?: string | null;
|
||||
secondaryColor?: string | null;
|
||||
pictureImage?: File | null;
|
||||
badgeImage?: File | null;
|
||||
isSuperadmin: boolean;
|
||||
canEditMedia: boolean;
|
||||
canManageMedia: boolean;
|
||||
canEditEpisodes: boolean;
|
||||
canManageEpisodes: boolean;
|
||||
canEditComment: boolean;
|
||||
canManageComment: boolean;
|
||||
canEditUser: boolean;
|
||||
canManageUser: boolean;
|
||||
canEditSystem: boolean;
|
||||
canManageSystem: boolean;
|
||||
createdBy: string;
|
||||
deletedAt?: Date | string | null;
|
||||
createdAt?: Date | string;
|
||||
updatedAt?: Date | string;
|
||||
};
|
||||
queryTarget: "withAdmin";
|
||||
}
|
||||
export interface createUserRoleRepositoryPayload {
|
||||
name: string;
|
||||
primaryColor?: string | null;
|
||||
secondaryColor?: string | null;
|
||||
pictureImage?: string | null;
|
||||
badgeImage?: string | null;
|
||||
isSuperadmin: boolean;
|
||||
canEditMedia: boolean;
|
||||
canManageMedia: boolean;
|
||||
canEditEpisodes: boolean;
|
||||
canManageEpisodes: boolean;
|
||||
canEditComment: boolean;
|
||||
canManageComment: boolean;
|
||||
canEditUser: boolean;
|
||||
canManageUser: boolean;
|
||||
canEditSystem: boolean;
|
||||
canManageSystem: boolean;
|
||||
createdBy: string;
|
||||
deletedAt?: Date | string | null;
|
||||
createdAt?: Date | string;
|
||||
updatedAt?: Date | string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user