fixing in user and authentication

This commit is contained in:
rafiarrafif
2025-05-11 01:41:05 +07:00
parent 954d40df3b
commit 307c5e2d68
15 changed files with 122 additions and 96 deletions

View File

@ -2,11 +2,11 @@ import { Prisma } from "@prisma/client";
import { Context } from "elysia";
import { createUserSchema } from "../user.schema";
import { createUserService } from "../services/createUser.service";
import { handlePrismaError } from "../../../utils/databases/prisma/error/handler";
import {
returnErrorResponse,
returnWriteResponse,
} from "../../../helpers/callback/httpResponse";
import { mainErrorHandler } from "../../../helpers/error/handler";
/**
* @function createUser
@ -45,8 +45,6 @@ export const createUser = async (
newUser
);
} catch (error) {
// Handle any errors that occur during user creation
const { status, message, details } = handlePrismaError(error);
return returnErrorResponse(ctx.set, status, message, details);
return mainErrorHandler(ctx.set, error);
}
};

View File

@ -1,10 +1,10 @@
import { handlePrismaError } from "../../../utils/databases/prisma/error/handler";
import {
returnErrorResponse,
returnReadResponse,
} from "../../../helpers/callback/httpResponse";
import { Context } from "elysia";
import { getAllUsersService } from "../services/getAllUser.service";
import { mainErrorHandler } from "../../../helpers/error/handler";
export const getAllUser = async (ctx: Context) => {
try {
@ -16,7 +16,6 @@ export const getAllUser = async (ctx: Context) => {
allUser
);
} catch (error) {
const { status, message, details } = handlePrismaError(error);
return returnErrorResponse(ctx.set, status, message, details);
return mainErrorHandler(ctx.set, error);
}
};

View File

@ -0,0 +1,17 @@
import { Prisma } from "@prisma/client";
import { userModel } from "../user.model";
export const createUserRepo = async (data: Prisma.UserCreateInput) => {
try {
const userData = await userModel.create({
data,
omit: {
password: true,
},
});
return userData;
} catch (error) {
throw error;
}
};

View File

@ -1,4 +1,4 @@
import { AppError } from "../../../helpers/error/handler";
import { AppError } from "../../../helpers/error/instances/app";
import { userModel } from "../user.model";
export const findUserByEmailOrUsernameRepo = async (identifier: string) => {

View File

@ -0,0 +1,15 @@
import { userModel } from "../user.model";
export const getAllUserRepo = async () => {
try {
const data = await userModel.findMany({
where: {
deletedAt: null,
},
});
return data;
} catch (error) {
throw error;
}
};

View File

@ -1,18 +1,15 @@
import { Prisma } from "@prisma/client";
import { hashPassword } from "../../../helpers/security/password/hash";
import { userModel } from "../user.model";
import { createUserRepo } from "../repositories/createUser.repository";
export const createUserService = async (userData: Prisma.UserCreateInput) => {
const { password, ...rest } = userData; // Destructure the password and the rest of the user data
const hashedPassword = await hashPassword(password); // Hash the password before saving to the database
// Create the user in the database using Prisma
try {
const newUser = await userModel.create({
data: {
...rest,
password: hashedPassword,
},
const newUser = await createUserRepo({
...rest,
password: hashedPassword,
});
return newUser;
} catch (error) {

View File

@ -1,12 +1,8 @@
import { userModel } from "../user.model";
import { getAllUserRepo } from "../repositories/getAllUser.repository";
export const getAllUsersService = async () => {
try {
const allUser = await userModel.findMany({
where: {
deletedAt: null,
},
});
const allUser = await getAllUserRepo();
return allUser;
} catch (error) {
throw error;