mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-02-27 08:13:28 +08:00
passing previous name as function parameter to avoid requesting the user multiple times
This commit is contained in:
parent
7d4e1ffc03
commit
017ca95236
2 changed files with 7 additions and 7 deletions
|
@ -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}`,
|
||||
|
|
|
@ -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 },
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue