Refactor Users DAL (#3223)

* Refactor two calls

* Return void

* Remove update result

* Remove delete result
This commit is contained in:
Bruce Berrios 2022-06-25 10:56:43 -04:00 committed by GitHub
parent 1575595d0d
commit a552c127f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,13 +4,7 @@ import { updateUserEmail } from "../utils/auth";
import { checkAndUpdatePb } from "../utils/pb";
import * as db from "../init/db";
import MonkeyError from "../utils/error";
import {
Collection,
DeleteResult,
ObjectId,
UpdateResult,
WithId,
} from "mongodb";
import { Collection, ObjectId, WithId } from "mongodb";
import Logger from "../utils/logger";
import { flattenObjectDeep } from "../utils/misc";
@ -43,17 +37,14 @@ export async function addUser(
}
}
export async function deleteUser(uid: string): Promise<DeleteResult> {
return await getUsersCollection().deleteOne({ uid });
export async function deleteUser(uid: string): Promise<void> {
await getUsersCollection().deleteOne({ uid });
}
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<UpdateResult> {
export async function updateName(uid: string, name: string): Promise<void> {
if (!isUsernameValid(name)) {
throw new MonkeyError(400, "Invalid username");
}
@ -73,22 +64,17 @@ export async function updateName(
}
await getUsersCollection().updateOne(
{ uid },
{
$push: { nameHistory: oldName },
}
);
return await getUsersCollection().updateOne(
{ uid },
{
$set: { name, lastNameChange: Date.now() },
$unset: { needsToChangeName: "" },
$push: { nameHistory: oldName },
}
);
}
export async function clearPb(uid: string): Promise<UpdateResult> {
return await getUsersCollection().updateOne(
export async function clearPb(uid: string): Promise<void> {
await getUsersCollection().updateOne(
{ uid },
{
$set: {
@ -226,7 +212,7 @@ export async function editTag(
uid: string,
_id: string,
name: string
): Promise<UpdateResult> {
): Promise<void> {
const user = await getUser(uid, "edit tag");
if (
user.tags === undefined ||
@ -234,7 +220,7 @@ export async function editTag(
) {
throw new MonkeyError(404, "Tag not found");
}
return await getUsersCollection().updateOne(
await getUsersCollection().updateOne(
{
uid: uid,
"tags._id": new ObjectId(_id),
@ -243,10 +229,7 @@ export async function editTag(
);
}
export async function removeTag(
uid: string,
_id: string
): Promise<UpdateResult> {
export async function removeTag(uid: string, _id: string): Promise<void> {
const user = await getUser(uid, "remove tag");
if (
user.tags === undefined ||
@ -254,7 +237,7 @@ export async function removeTag(
) {
throw new MonkeyError(404, "Tag not found");
}
return await getUsersCollection().updateOne(
await getUsersCollection().updateOne(
{
uid: uid,
"tags._id": new ObjectId(_id),
@ -263,10 +246,7 @@ export async function removeTag(
);
}
export async function removeTagPb(
uid: string,
_id: string
): Promise<UpdateResult> {
export async function removeTagPb(uid: string, _id: string): Promise<void> {
const user = await getUser(uid, "remove tag pb");
if (
user.tags === undefined ||
@ -274,7 +254,7 @@ export async function removeTagPb(
) {
throw new MonkeyError(404, "Tag not found");
}
return await getUsersCollection().updateOne(
await getUsersCollection().updateOne(
{
uid: uid,
"tags._id": new ObjectId(_id),
@ -289,7 +269,7 @@ export async function updateLbMemory(
mode2: MonkeyTypes.Mode2<MonkeyTypes.Mode>,
language: string,
rank: number
): Promise<UpdateResult> {
): Promise<void> {
const user = await getUser(uid, "update lb memory");
if (user.lbMemory === undefined) user.lbMemory = {};
if (user.lbMemory[mode] === undefined) user.lbMemory[mode] = {};
@ -297,7 +277,7 @@ export async function updateLbMemory(
user.lbMemory[mode][mode2] = {};
}
user.lbMemory[mode][mode2][language] = rank;
return await getUsersCollection().updateOne(
await getUsersCollection().updateOne(
{ uid },
{
$set: { lbMemory: user.lbMemory },
@ -403,9 +383,9 @@ export async function checkIfTagPb(
return ret;
}
export async function resetPb(uid: string): Promise<UpdateResult> {
export async function resetPb(uid: string): Promise<void> {
await getUser(uid, "reset pb");
return await getUsersCollection().updateOne(
await getUsersCollection().updateOne(
{ uid },
{
$set: {
@ -425,8 +405,8 @@ export async function updateTypingStats(
uid: string,
restartCount: number,
timeTyping: number
): Promise<UpdateResult> {
return await getUsersCollection().updateOne(
): Promise<void> {
await getUsersCollection().updateOne(
{ uid },
{
$inc: {
@ -454,19 +434,16 @@ export async function linkDiscord(
}
}
export async function unlinkDiscord(uid: string): Promise<UpdateResult> {
export async function unlinkDiscord(uid: string): Promise<void> {
await getUser(uid, "unlink discord");
return await getUsersCollection().updateOne(
await getUsersCollection().updateOne(
{ uid },
{ $unset: { discordId: "", discordAvatar: "" } }
);
}
export async function incrementBananas(
uid: string,
wpm
): Promise<UpdateResult | null> {
export async function incrementBananas(uid: string, wpm): Promise<void> {
const user = await getUser(uid, "increment bananas");
let best60: number | undefined;
@ -478,13 +455,8 @@ export async function incrementBananas(
if (best60 === undefined || wpm >= best60 - best60 * 0.25) {
//increment when no record found or wpm is within 25% of the record
return await getUsersCollection().updateOne(
{ uid },
{ $inc: { bananas: 1 } }
);
await getUsersCollection().updateOne({ uid }, { $inc: { bananas: 1 } });
}
return null;
}
export function themeDoesNotExist(customThemes, id): boolean {
@ -523,14 +495,14 @@ export async function addTheme(
};
}
export async function removeTheme(uid: string, _id): Promise<UpdateResult> {
export async function removeTheme(uid: string, _id): Promise<void> {
const user = await getUser(uid, "remove theme");
if (themeDoesNotExist(user.customThemes, _id)) {
throw new MonkeyError(404, "Custom theme not found");
}
return await getUsersCollection().updateOne(
await getUsersCollection().updateOne(
{
uid: uid,
"customThemes._id": new ObjectId(_id),
@ -539,18 +511,14 @@ export async function removeTheme(uid: string, _id): Promise<UpdateResult> {
);
}
export async function editTheme(
uid: string,
_id,
theme
): Promise<UpdateResult> {
export async function editTheme(uid: string, _id, theme): Promise<void> {
const user = await getUser(uid, "edit theme");
if (themeDoesNotExist(user.customThemes, _id)) {
throw new MonkeyError(404, "Custom Theme not found");
}
return await getUsersCollection().updateOne(
await getUsersCollection().updateOne(
{
uid: uid,
"customThemes._id": new ObjectId(_id),