✨ (user) create validation in user update data
This commit is contained in:
12
prisma/migrations/20250627173346_initial/migration.sql
Normal file
12
prisma/migrations/20250627173346_initial/migration.sql
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
/*
|
||||||
|
Warnings:
|
||||||
|
|
||||||
|
- You are about to drop the column `commentPicture` on the `users` table. All the data in the column will be lost.
|
||||||
|
- You are about to drop the column `profilePicture` on the `users` table. All the data in the column will be lost.
|
||||||
|
|
||||||
|
*/
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "users" DROP COLUMN "commentPicture",
|
||||||
|
DROP COLUMN "profilePicture",
|
||||||
|
ADD COLUMN "avatar" TEXT,
|
||||||
|
ADD COLUMN "commentBackground" TEXT;
|
||||||
@ -2,6 +2,10 @@ import { saveFile } from "..";
|
|||||||
import { AppError } from "../../../error/instances/app";
|
import { AppError } from "../../../error/instances/app";
|
||||||
|
|
||||||
export const saveAvatar = async (file: File): Promise<string> => {
|
export const saveAvatar = async (file: File): Promise<string> => {
|
||||||
|
if (Array.isArray(file)) {
|
||||||
|
throw new AppError(415, "Can't upload more than 1 file");
|
||||||
|
}
|
||||||
|
|
||||||
const allowedTypes = ["image/png", "image/jpeg", "image/webp"];
|
const allowedTypes = ["image/png", "image/jpeg", "image/webp"];
|
||||||
if (!allowedTypes.includes(file.type)) {
|
if (!allowedTypes.includes(file.type)) {
|
||||||
throw new AppError(
|
throw new AppError(
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
import { Context } from "elysia";
|
import { Context } from "elysia";
|
||||||
import { returnWriteResponse } from "../../../helpers/callback/httpResponse";
|
import {
|
||||||
|
returnErrorResponse,
|
||||||
|
returnWriteResponse,
|
||||||
|
} from "../../../helpers/callback/httpResponse";
|
||||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||||
import { Prisma } from "@prisma/client";
|
import { Prisma } from "@prisma/client";
|
||||||
import { editUserService } from "../services/editUser.service";
|
import { editUserService } from "../services/editUser.service";
|
||||||
@ -8,6 +11,7 @@ import { getUserHeaderInformation } from "../../../helpers/http/userHeader/getUs
|
|||||||
import { setCookie } from "../../../helpers/http/userHeader/cookies/setCookies";
|
import { setCookie } from "../../../helpers/http/userHeader/cookies/setCookies";
|
||||||
import { COOKIE_KEYS } from "../../../constants/cookie.keys";
|
import { COOKIE_KEYS } from "../../../constants/cookie.keys";
|
||||||
import { jwtEncode } from "../../../helpers/http/jwt/encode";
|
import { jwtEncode } from "../../../helpers/http/jwt/encode";
|
||||||
|
import { editUserSchema } from "../schemas/editUser.schema";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @function editUserController
|
* @function editUserController
|
||||||
@ -36,8 +40,8 @@ import { jwtEncode } from "../../../helpers/http/jwt/encode";
|
|||||||
* "phoneCC": 62,
|
* "phoneCC": 62,
|
||||||
* "phoneNumber": 81234567890,
|
* "phoneNumber": 81234567890,
|
||||||
* "bioProfile": "Updated bio",
|
* "bioProfile": "Updated bio",
|
||||||
* "profilePicture": "https://example.com/new-profile.jpg",
|
* "profilePicture": JPG/PNG/JPEG File,
|
||||||
* "commentPicture": "https://example.com/new-comment.jpg",
|
* "commentPicture": JPG/PNG/JPEG File,
|
||||||
* "deletedAt": null
|
* "deletedAt": null
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
@ -58,6 +62,11 @@ export const editUserController = async (
|
|||||||
body: Prisma.UserUncheckedCreateInput;
|
body: Prisma.UserUncheckedCreateInput;
|
||||||
}
|
}
|
||||||
) => {
|
) => {
|
||||||
|
// Validate the request body against the edit user schema
|
||||||
|
const { error } = editUserSchema.validate(ctx.body);
|
||||||
|
if (error)
|
||||||
|
return returnErrorResponse(ctx.set, 422, "Invalid form input", error);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Get the user JWT token from cookies, if the token is not found, return an error response
|
// Get the user JWT token from cookies, if the token is not found, return an error response
|
||||||
const userCookie = getCookie(ctx);
|
const userCookie = getCookie(ctx);
|
||||||
|
|||||||
@ -1,15 +1,19 @@
|
|||||||
import Joi from "joi";
|
import Joi from "joi";
|
||||||
|
|
||||||
export const createUserSchema = Joi.object({
|
export const createUserSchema = Joi.object({
|
||||||
name: Joi.string().min(4).max(255).required(),
|
name: Joi.string()
|
||||||
username: Joi.string().min(4).max(255).required(),
|
.min(4)
|
||||||
email: Joi.string().email().required(),
|
.max(255)
|
||||||
password: Joi.string().min(8).max(255).required(),
|
.required(),
|
||||||
birthdate: Joi.date(),
|
username: Joi.string()
|
||||||
gender: Joi.string().valid("male", "female"),
|
.min(4)
|
||||||
phoneCC: Joi.string().min(2).max(2),
|
.max(255)
|
||||||
phoneNumber: Joi.string().min(7).max(15),
|
.required(),
|
||||||
bioProfile: Joi.string().max(300),
|
email: Joi.string()
|
||||||
avatar: Joi.string().uri(),
|
.email()
|
||||||
commentBackground: Joi.string().uri(),
|
.required(),
|
||||||
|
password: Joi.string()
|
||||||
|
.min(8)
|
||||||
|
.max(255)
|
||||||
|
.required(),
|
||||||
});
|
});
|
||||||
|
|||||||
22
src/modules/user/schemas/editUser.schema.ts
Normal file
22
src/modules/user/schemas/editUser.schema.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import Joi from "joi";
|
||||||
|
|
||||||
|
export const editUserSchema = Joi.object({
|
||||||
|
name: Joi.string()
|
||||||
|
.min(4)
|
||||||
|
.max(255),
|
||||||
|
username: Joi.string()
|
||||||
|
.min(4)
|
||||||
|
.max(255),
|
||||||
|
birthdate: Joi.date(),
|
||||||
|
gender: Joi.string().valid("male", "female"),
|
||||||
|
phoneCC: Joi.string()
|
||||||
|
.min(2)
|
||||||
|
.max(2),
|
||||||
|
phoneNumber: Joi.string()
|
||||||
|
.min(7)
|
||||||
|
.max(15),
|
||||||
|
bioProfile: Joi.string().max(300),
|
||||||
|
avatar: Joi.string().uri(),
|
||||||
|
commentBackground: Joi.string().uri(),
|
||||||
|
deletedAt: Joi.date(),
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user