refactor(services): simplify function calls and logging statements in user.service.ts
This commit simplifies multi-line function calls and logging statements in `user.service.ts` to be just single-line. This change enhances the readability and maintainability of the code by reducing unnecessary lines and making the function calls and logging statements more straightforward. Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
parent
a6593cb76f
commit
19d265a0e6
@ -18,13 +18,8 @@ const DbHandler = new MySqlService.Handler("UserService");
|
||||
* @param {string} username - The username of the user to retrieve.
|
||||
* @returns {Promise<Object | null>} - The user object if found, or null if not found.
|
||||
*/
|
||||
async function getUserFromUsername(
|
||||
username: string,
|
||||
): Promise<object | null> {
|
||||
const dbUser = await MySqlService.User.getByUsername(
|
||||
DbHandler,
|
||||
username,
|
||||
);
|
||||
async function getUserFromUsername(username: string): Promise<object | null> {
|
||||
const dbUser = await MySqlService.User.getByUsername(DbHandler, username);
|
||||
if (dbUser === undefined) return null;
|
||||
return dbUser;
|
||||
}
|
||||
@ -37,22 +32,16 @@ async function getUserFromIdService(id: string | undefined) {
|
||||
|
||||
async function register(ReqData: IReqRegister) {
|
||||
if (ReqData.password.length < 6) {
|
||||
logger.info(
|
||||
`REGISTER :> Invalid password (${ReqData.username})`,
|
||||
);
|
||||
logger.info(`REGISTER :> Invalid password (${ReqData.username})`);
|
||||
return {
|
||||
error: "invalidPassword",
|
||||
};
|
||||
}
|
||||
const passwordHash = await CredentialService.hash(
|
||||
ReqData.password,
|
||||
);
|
||||
const passwordHash = await CredentialService.hash(ReqData.password);
|
||||
|
||||
// Does the new user has accepted GDPR ?
|
||||
if (ReqData.gdpr !== true) {
|
||||
logger.info(
|
||||
`REGISTER :> GDPR not validated (${ReqData.username})`,
|
||||
);
|
||||
logger.info(`REGISTER :> GDPR not validated (${ReqData.username})`);
|
||||
return {
|
||||
error: "gdprNotApproved",
|
||||
};
|
||||
@ -60,9 +49,7 @@ async function register(ReqData: IReqRegister) {
|
||||
|
||||
// Check if exist and return
|
||||
|
||||
const dbUserIfExist = await getUserFromUsername(
|
||||
ReqData.username,
|
||||
);
|
||||
const dbUserIfExist = await getUserFromUsername(ReqData.username);
|
||||
if (dbUserIfExist) {
|
||||
logger.info(
|
||||
`REGISTER :> User exist (${dbUserIfExist.username})\n ID:${dbUserIfExist.id}`,
|
||||
@ -110,9 +97,7 @@ async function register(ReqData: IReqRegister) {
|
||||
};
|
||||
logger.info(userData);
|
||||
await Db.collection("users").insertOne(NewUser);
|
||||
logger.info(
|
||||
`REGISTER :> Inserted new user (${NewUser.username})`,
|
||||
);
|
||||
logger.info(`REGISTER :> Inserted new user (${NewUser.username})`);
|
||||
return userData;
|
||||
}
|
||||
|
||||
@ -123,18 +108,14 @@ async function login(ReqData: IReqLogin) {
|
||||
ReqData.username,
|
||||
);
|
||||
if (!dbUser) {
|
||||
console.log(
|
||||
`LoginService :> User does not exist (${ReqData.username})`,
|
||||
);
|
||||
console.log(`LoginService :> User does not exist (${ReqData.username})`);
|
||||
return {
|
||||
error: "userNotFound",
|
||||
};
|
||||
}
|
||||
if (ReqData.password.length < 6) {
|
||||
console.log("X");
|
||||
console.log(
|
||||
`LoginService :> Invalid password (${ReqData.username})`,
|
||||
);
|
||||
console.log(`LoginService :> Invalid password (${ReqData.username})`);
|
||||
return {
|
||||
error: "invalidPassword",
|
||||
};
|
||||
@ -145,9 +126,7 @@ async function login(ReqData: IReqLogin) {
|
||||
);
|
||||
if (!isPasswordValid) {
|
||||
console.log(isPasswordValid);
|
||||
console.log(
|
||||
`LoginService :> Invalid password (${ReqData.username})`,
|
||||
);
|
||||
console.log(`LoginService :> Invalid password (${ReqData.username})`);
|
||||
return {
|
||||
error: "invalidPassword",
|
||||
};
|
||||
@ -213,16 +192,12 @@ async function getAllUsersService() {
|
||||
*/
|
||||
async function editUserService(targetId, sanitizedData) {
|
||||
if (sanitizedData.password) {
|
||||
const passwordHash = await getHashFromPassword(
|
||||
sanitizedData.password,
|
||||
);
|
||||
const passwordHash = await getHashFromPassword(sanitizedData.password);
|
||||
delete sanitizedData.password;
|
||||
logger.info(`Changing password for user "${targetId}"`);
|
||||
sanitizedData.passwordHash = passwordHash;
|
||||
}
|
||||
const updatedUserResult = await Db.collection(
|
||||
"users",
|
||||
).updateOne(
|
||||
const updatedUserResult = await Db.collection("users").updateOne(
|
||||
{
|
||||
id: targetId,
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user