add 1 step find user

This commit is contained in:
rafiarrafif
2025-05-10 01:00:00 +07:00
parent 87cf7fa56c
commit 83f30bd36c
5 changed files with 58 additions and 5 deletions

View File

@ -11,6 +11,10 @@ export const loginWithPassword = async (
if (error || !ctx.body) if (error || !ctx.body)
return returnErrorResponse(ctx.set, 400, "Invalid user input", error); return returnErrorResponse(ctx.set, 400, "Invalid user input", error);
try {
const result = await loginWithPasswordService(ctx.body); const result = await loginWithPasswordService(ctx.body);
return result; return result;
} catch (error) {
return error;
}
}; };

View File

@ -1,9 +1,13 @@
import { findUserByEmailOrUsernameService } from "../../user/services/findUserByEmailOrUsername.service";
import { LoginWithPasswordRequest } from "../auth.types"; import { LoginWithPasswordRequest } from "../auth.types";
export const loginWithPasswordService = async ( export const loginWithPasswordService = async (
data: LoginWithPasswordRequest data: LoginWithPasswordRequest
) => { ) => {
return `Login with password service called with data: ${JSON.stringify( try {
data const userData = await findUserByEmailOrUsernameService(data.identifier);
)}`; return userData;
} catch (error) {
throw error;
}
}; };

View File

@ -0,0 +1,35 @@
import { userModel } from "../user.model";
export const findUserByEmailOrUsernameRepo = async (identifier: string) => {
const userData =
(await userModel.findUnique({
where: { email: identifier },
include: {
roles: {
omit: {
createdBy: true,
createdAt: true,
updatedAt: true,
deletedAt: true,
},
},
},
})) ||
(await userModel.findUnique({
where: { username: identifier },
include: {
roles: {
omit: {
createdBy: true,
createdAt: true,
updatedAt: true,
deletedAt: true,
},
},
},
}));
if (!userData) throw "User not found";
return userData;
};

View File

@ -0,0 +1,10 @@
import { findUserByEmailOrUsernameRepo } from "../repositories/findUserByEmailOrUsername.repository";
export const findUserByEmailOrUsernameService = async (identifier: string) => {
try {
const userData = await findUserByEmailOrUsernameRepo(identifier);
return userData;
} catch (error) {
throw error;
}
};