passing previous name as function parameter to avoid requesting the user multiple times

This commit is contained in:
Miodec 2023-04-21 10:44:40 +02:00
parent 7d4e1ffc03
commit 017ca95236
2 changed files with 7 additions and 7 deletions

View file

@ -212,7 +212,7 @@ export async function updateName(
throw new MonkeyError(409, "You can change your name once every 30 days");
}
await UserDAL.updateName(uid, name);
await UserDAL.updateName(uid, name, user.name);
Logger.logToDb(
"user_name_updated",
`changed name from ${user.name} to ${name}`,

View file

@ -85,7 +85,11 @@ export async function resetUser(uid: string): Promise<void> {
);
}
export async function updateName(uid: string, name: string): Promise<void> {
export async function updateName(
uid: string,
name: string,
previousName: string
): Promise<void> {
if (!isUsernameValid(name)) {
throw new MonkeyError(400, "Invalid username");
}
@ -93,16 +97,12 @@ export async function updateName(uid: string, name: string): Promise<void> {
throw new MonkeyError(409, "Username already taken", name);
}
const user = await getUser(uid, "update name");
const oldName = user.name;
await getUsersCollection().updateOne(
{ uid },
{
$set: { name, lastNameChange: Date.now() },
$unset: { needsToChangeName: "" },
$push: { nameHistory: oldName },
$push: { nameHistory: previousName },
}
);
}