feat: wrap article and user service functions with TypedResult for improved error handling

This commit is contained in:
2026-04-11 01:48:09 -03:00
parent 94e8058880
commit af17b6dc5a
10 changed files with 363 additions and 240 deletions

View File

@@ -6,6 +6,7 @@ import {
UpdateUserModel,
UserModel,
} from '@/lib/feature/user/user.model';
import { TypedResult, wrap } from '@/utils/types/results';
export const userEntityToModel = (userEntity: UserEntity): UserModel => {
return {
@@ -17,14 +18,7 @@ export const userEntityToModel = (userEntity: UserEntity): UserModel => {
};
};
/**
* Retrieves a user by their email address.
* @param email - The email address of the user to retrieve.
* @returns {Promise<UserModel | null>} The user model if found, otherwise null.
*/
export const getUserByEmail = async (
email: string
): Promise<UserModel | null> => {
const _getUserByEmail = async (email: string): Promise<UserModel | null> => {
const userRepository = await getRepository(UserEntity);
const userEntity = await userRepository.findOneBy({ email });
@@ -36,13 +30,7 @@ export const getUserByEmail = async (
return userEntityToModel(userEntity);
};
/**
* Saves a new user to the database.
* @param user - The user data to save.
* @returns {Promise<UserModel>} The saved user model.
* @throws {Error} If a user with the same email already exists.
*/
export const saveUser = async (user: CreateUserModel): Promise<UserModel> => {
const _saveUser = async (user: CreateUserModel): Promise<UserModel> => {
const userRepository = await getRepository(UserEntity);
if (!!(await userRepository.findOneBy({ email: user.email }))) {
@@ -53,14 +41,7 @@ export const saveUser = async (user: CreateUserModel): Promise<UserModel> => {
return userEntityToModel(await userRepository.save(newUser));
};
/**
* Updates an existing user in the database.
* @param userId - The ID of the user to update.
* @param user - The new user data.
* @returns {Promise<UserModel>} The updated user model.
* @throws {Error} If the user with the given ID does not exist.
*/
export const updateUser = async (
const _updateUser = async (
userId: string,
user: UpdateUserModel
): Promise<UserModel> => {
@@ -78,33 +59,55 @@ export const updateUser = async (
return userEntityToModel(await userRepository.save(existingUser));
};
/**
* Synchronizes a user with the database.
* If the user already exists, it skips saving and returns the existing user.
* If the user does not exist, it creates a new user record.
* @returns {Promise<UserModel>} The synchronized user model.
* @throws {Error} If the user email is not provided or if there is an issue
* saving the user.
* @param sessionClaims Session Claims from the Auth Provider
*/
export const syncUser = async (
sessionClaims: SessionClaims
): Promise<UserModel> => {
const _syncUser = async (sessionClaims: SessionClaims): Promise<UserModel> => {
const { full_name, email } = sessionClaims.user;
const role = sessionClaims.user.public_metadata.role;
const existingUser = await getUserByEmail(email);
const existingUser = await _getUserByEmail(email);
if (!existingUser) {
return await saveUser({
return await _saveUser({
name: full_name,
email: email,
role: role,
});
}
return await updateUser(existingUser.id, {
return await _updateUser(existingUser.id, {
name: full_name,
email: existingUser.email,
role: role,
});
};
/**
* Retrieves a user by their email address.
* @param email - The email address of the user to retrieve.
* @returns {Promise<TypedResult<UserModel | null>>} The user model if found, otherwise null.
*/
export const getUserByEmail = wrap(_getUserByEmail);
/**
* Saves a new user to the database.
* @param user - The user data to save.
* @returns {Promise<TypedResult<UserModel>>} The saved user model.
*/
export const saveUser = wrap(_saveUser);
/**
* Updates an existing user in the database.
* @param userId - The ID of the user to update.
* @param user - The new user data.
* @returns {Promise<TypedResult<UserModel>>} The updated user model.
*/
export const updateUser = wrap(_updateUser);
/**
* Synchronizes a user with the database.
* If the user already exists, updates it; if not, creates a new record.
* @param sessionClaims Session Claims from the Auth Provider
* @returns {Promise<TypedResult<UserModel>>} The synchronized user model.
*/
export const syncUser = wrap(_syncUser);
// Explicit re-export for TypeScript consumers who need the result type
export type { TypedResult };