moved last name change check to the controller

This commit is contained in:
Miodec 2023-04-21 10:43:32 +02:00
parent 08eaef31d2
commit 7d4e1ffc03
2 changed files with 13 additions and 12 deletions

View file

@ -194,17 +194,28 @@ export async function resetUser(
return new MonkeyResponse("User reset");
}
const DAY_IN_SECONDS = 24 * 60 * 60;
const THIRTY_DAYS_IN_SECONDS = DAY_IN_SECONDS * 30;
export async function updateName(
req: MonkeyTypes.Request
): Promise<MonkeyResponse> {
const { uid } = req.ctx.decodedToken;
const { name } = req.body;
const oldUser = await UserDAL.getUser(uid, "update name");
const user = await UserDAL.getUser(uid, "update name");
if (
!user?.needsToChangeName &&
Date.now() - (user.lastNameChange ?? 0) < THIRTY_DAYS_IN_SECONDS
) {
throw new MonkeyError(409, "You can change your name once every 30 days");
}
await UserDAL.updateName(uid, name);
Logger.logToDb(
"user_name_updated",
`changed name from ${oldUser.name} to ${name}`,
`changed name from ${user.name} to ${name}`,
uid
);

View file

@ -85,9 +85,6 @@ export async function resetUser(uid: string): Promise<void> {
);
}
const DAY_IN_SECONDS = 24 * 60 * 60;
const THIRTY_DAYS_IN_SECONDS = DAY_IN_SECONDS * 30;
export async function updateName(uid: string, name: string): Promise<void> {
if (!isUsernameValid(name)) {
throw new MonkeyError(400, "Invalid username");
@ -100,13 +97,6 @@ export async function updateName(uid: string, name: string): Promise<void> {
const oldName = user.name;
if (
!user?.needsToChangeName &&
Date.now() - (user.lastNameChange ?? 0) < THIRTY_DAYS_IN_SECONDS
) {
throw new MonkeyError(409, "You can change your name once every 30 days");
}
await getUsersCollection().updateOne(
{ uid },
{