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.
|
* @param {string} username - The username of the user to retrieve.
|
||||||
* @returns {Promise<Object | null>} - The user object if found, or null if not found.
|
* @returns {Promise<Object | null>} - The user object if found, or null if not found.
|
||||||
*/
|
*/
|
||||||
async function getUserFromUsername(
|
async function getUserFromUsername(username: string): Promise<object | null> {
|
||||||
username: string,
|
const dbUser = await MySqlService.User.getByUsername(DbHandler, username);
|
||||||
): Promise<object | null> {
|
|
||||||
const dbUser = await MySqlService.User.getByUsername(
|
|
||||||
DbHandler,
|
|
||||||
username,
|
|
||||||
);
|
|
||||||
if (dbUser === undefined) return null;
|
if (dbUser === undefined) return null;
|
||||||
return dbUser;
|
return dbUser;
|
||||||
}
|
}
|
||||||
@ -37,22 +32,16 @@ async function getUserFromIdService(id: string | undefined) {
|
|||||||
|
|
||||||
async function register(ReqData: IReqRegister) {
|
async function register(ReqData: IReqRegister) {
|
||||||
if (ReqData.password.length < 6) {
|
if (ReqData.password.length < 6) {
|
||||||
logger.info(
|
logger.info(`REGISTER :> Invalid password (${ReqData.username})`);
|
||||||
`REGISTER :> Invalid password (${ReqData.username})`,
|
|
||||||
);
|
|
||||||
return {
|
return {
|
||||||
error: "invalidPassword",
|
error: "invalidPassword",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const passwordHash = await CredentialService.hash(
|
const passwordHash = await CredentialService.hash(ReqData.password);
|
||||||
ReqData.password,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Does the new user has accepted GDPR ?
|
// Does the new user has accepted GDPR ?
|
||||||
if (ReqData.gdpr !== true) {
|
if (ReqData.gdpr !== true) {
|
||||||
logger.info(
|
logger.info(`REGISTER :> GDPR not validated (${ReqData.username})`);
|
||||||
`REGISTER :> GDPR not validated (${ReqData.username})`,
|
|
||||||
);
|
|
||||||
return {
|
return {
|
||||||
error: "gdprNotApproved",
|
error: "gdprNotApproved",
|
||||||
};
|
};
|
||||||
@ -60,9 +49,7 @@ async function register(ReqData: IReqRegister) {
|
|||||||
|
|
||||||
// Check if exist and return
|
// Check if exist and return
|
||||||
|
|
||||||
const dbUserIfExist = await getUserFromUsername(
|
const dbUserIfExist = await getUserFromUsername(ReqData.username);
|
||||||
ReqData.username,
|
|
||||||
);
|
|
||||||
if (dbUserIfExist) {
|
if (dbUserIfExist) {
|
||||||
logger.info(
|
logger.info(
|
||||||
`REGISTER :> User exist (${dbUserIfExist.username})\n ID:${dbUserIfExist.id}`,
|
`REGISTER :> User exist (${dbUserIfExist.username})\n ID:${dbUserIfExist.id}`,
|
||||||
@ -110,9 +97,7 @@ async function register(ReqData: IReqRegister) {
|
|||||||
};
|
};
|
||||||
logger.info(userData);
|
logger.info(userData);
|
||||||
await Db.collection("users").insertOne(NewUser);
|
await Db.collection("users").insertOne(NewUser);
|
||||||
logger.info(
|
logger.info(`REGISTER :> Inserted new user (${NewUser.username})`);
|
||||||
`REGISTER :> Inserted new user (${NewUser.username})`,
|
|
||||||
);
|
|
||||||
return userData;
|
return userData;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,18 +108,14 @@ async function login(ReqData: IReqLogin) {
|
|||||||
ReqData.username,
|
ReqData.username,
|
||||||
);
|
);
|
||||||
if (!dbUser) {
|
if (!dbUser) {
|
||||||
console.log(
|
console.log(`LoginService :> User does not exist (${ReqData.username})`);
|
||||||
`LoginService :> User does not exist (${ReqData.username})`,
|
|
||||||
);
|
|
||||||
return {
|
return {
|
||||||
error: "userNotFound",
|
error: "userNotFound",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (ReqData.password.length < 6) {
|
if (ReqData.password.length < 6) {
|
||||||
console.log("X");
|
console.log("X");
|
||||||
console.log(
|
console.log(`LoginService :> Invalid password (${ReqData.username})`);
|
||||||
`LoginService :> Invalid password (${ReqData.username})`,
|
|
||||||
);
|
|
||||||
return {
|
return {
|
||||||
error: "invalidPassword",
|
error: "invalidPassword",
|
||||||
};
|
};
|
||||||
@ -145,9 +126,7 @@ async function login(ReqData: IReqLogin) {
|
|||||||
);
|
);
|
||||||
if (!isPasswordValid) {
|
if (!isPasswordValid) {
|
||||||
console.log(isPasswordValid);
|
console.log(isPasswordValid);
|
||||||
console.log(
|
console.log(`LoginService :> Invalid password (${ReqData.username})`);
|
||||||
`LoginService :> Invalid password (${ReqData.username})`,
|
|
||||||
);
|
|
||||||
return {
|
return {
|
||||||
error: "invalidPassword",
|
error: "invalidPassword",
|
||||||
};
|
};
|
||||||
@ -213,16 +192,12 @@ async function getAllUsersService() {
|
|||||||
*/
|
*/
|
||||||
async function editUserService(targetId, sanitizedData) {
|
async function editUserService(targetId, sanitizedData) {
|
||||||
if (sanitizedData.password) {
|
if (sanitizedData.password) {
|
||||||
const passwordHash = await getHashFromPassword(
|
const passwordHash = await getHashFromPassword(sanitizedData.password);
|
||||||
sanitizedData.password,
|
|
||||||
);
|
|
||||||
delete sanitizedData.password;
|
delete sanitizedData.password;
|
||||||
logger.info(`Changing password for user "${targetId}"`);
|
logger.info(`Changing password for user "${targetId}"`);
|
||||||
sanitizedData.passwordHash = passwordHash;
|
sanitizedData.passwordHash = passwordHash;
|
||||||
}
|
}
|
||||||
const updatedUserResult = await Db.collection(
|
const updatedUserResult = await Db.collection("users").updateOne(
|
||||||
"users",
|
|
||||||
).updateOne(
|
|
||||||
{
|
{
|
||||||
id: targetId,
|
id: targetId,
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user