feat(services): update user service dynamic types and enhance error handling
- Refactored `editUserService` function to use `IUserUpdate` type for `inputData` instead of `IDbUser`. - Enhanced error handling in `editUserService` and `deleteUserService` by checking for affected rows in the returned result. - Updated `deleteUserService` from using MongoDB method to MySqlService method. Issue: #18 Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
parent
1cbc771251
commit
bdfc598218
@ -8,6 +8,7 @@ import MySqlService from "@services/mysql.service";
|
|||||||
import { isDebugMode } from "@utils/debugState";
|
import { isDebugMode } from "@utils/debugState";
|
||||||
import { Logger } from "tslog";
|
import { Logger } from "tslog";
|
||||||
import { v4 } from "uuid";
|
import { v4 } from "uuid";
|
||||||
|
import {IUserUpdate} from "@interfaces/services/IUserUpdate";
|
||||||
|
|
||||||
const logger = new Logger({
|
const logger = new Logger({
|
||||||
name: "UserService",
|
name: "UserService",
|
||||||
@ -294,8 +295,8 @@ async function getAllUsersService(): Promise<Array<IDbUser> | ISError> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function editUserService(
|
async function editUserService(
|
||||||
targetId,
|
targetId: string,
|
||||||
inputData: IDbUser,
|
inputData: IUserUpdate,
|
||||||
): Promise<ISError | boolean> {
|
): Promise<ISError | boolean> {
|
||||||
if (!targetId || targetId.length !== 36) {
|
if (!targetId || targetId.length !== 36) {
|
||||||
logger.info(`\n\n> Invalid ID (${targetId})\n`);
|
logger.info(`\n\n> Invalid ID (${targetId})\n`);
|
||||||
@ -305,18 +306,20 @@ async function editUserService(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
const dbUser = await MySqlService.User.getById(DbHandler, targetId);
|
const dbUser = await MySqlService.User.getById(DbHandler, targetId);
|
||||||
if (!dbUser.id) {
|
if (!dbUser[0] || !dbUser[0].id) {
|
||||||
return {
|
return {
|
||||||
error: ErrorType.NotFound,
|
error: ErrorType.NotFound,
|
||||||
message: "User not found.",
|
message: "User not found.",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const result = await MySqlService.User.update(DbHandler, {
|
const result = await MySqlService.User.update(DbHandler, inputData);
|
||||||
username: inputData.username,
|
if (result.affectedRows === 0) {
|
||||||
firstname: inputData.firstname,
|
return {
|
||||||
lastname: inputData.lastname,
|
error: ErrorType.DatabaseError,
|
||||||
dob: inputData.dob,
|
message: "An unknown error occurred.",
|
||||||
});
|
};
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -325,13 +328,12 @@ async function editUserService(
|
|||||||
* @param {string} targetId - The ID of the user to be deleted.
|
* @param {string} targetId - The ID of the user to be deleted.
|
||||||
* @return {Promise<boolean>} - A promise that resolves to true if the user is successfully deleted, or false if an error occurs.
|
* @return {Promise<boolean>} - A promise that resolves to true if the user is successfully deleted, or false if an error occurs.
|
||||||
*/
|
*/
|
||||||
async function deleteUserService(targetId) {
|
async function deleteUserService(targetId: string): Promise<boolean> {
|
||||||
logger.info(`Deleting user ${targetId}`);
|
logger.info(`Deleting user ${targetId}`);
|
||||||
try {
|
try {
|
||||||
await Db.collection("users").deleteOne({
|
const DeleteResult = await MySqlService.User.delete(DbHandler, targetId)
|
||||||
id: targetId,
|
if (DeleteResult.affectedRows !== 0) return true;
|
||||||
});
|
return false;
|
||||||
return true;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.warn(e);
|
logger.warn(e);
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user