Skip to content

Commit

Permalink
Update points and coins simultaneously (#202)
Browse files Browse the repository at this point in the history
* Added update profile functions

* removed console

* isNumber check

* type change
  • Loading branch information
AydanPirani authored Feb 18, 2024
1 parent fff19d2 commit 9741015
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 26 deletions.
1 change: 1 addition & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function mockConfig(dbUrl: string) {
...actual.default,
TEST: true,
DB_URL: dbUrl,
REGISTRATION_CLOSE_TIME_MS: 99999999999000,
};

return {
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ const Config = {
DEFAULT_JWT_EXPIRY_TIME: "24h",
QR_EXPIRY_TIME: "20s",
RESUME_URL_EXPIRY_SECONDS: 60,
REGISTRATION_CLOSE_TIME_MS: 1708149975000,

/* Defaults */
DEFAULT_POINT_VALUE: 0,
Expand All @@ -129,7 +130,6 @@ const Config = {
MAX_SHOP_STOCK_PER_ITEM: 128,

RANKING_OFFSET: 1,
REGISTRATION_CLOSE_TIME: 1708149975000,
};

export default Config;
5 changes: 2 additions & 3 deletions src/services/mentor/mentor-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { JwtPayload, Role } from "../auth/auth-models.js";
import { hasElevatedPerms } from "../auth/auth-lib.js";
import { RouterError } from "../../middleware/error-handler.js";
import { NextFunction } from "express-serve-static-core";
import { updatePoints, updateCoins } from "../profile/profile-lib.js";
import { updatePointsAndCoins } from "../profile/profile-lib.js";
import Config from "../../config.js";
import crypto from "crypto";

Expand Down Expand Up @@ -240,8 +240,7 @@ mentorRouter.post("/attendance/", strongJwtVerification, async (req: Request, re
return next(new RouterError(StatusCode.ClientErrorBadRequest, "AlreadyCheckedIn"));
}

await updatePoints(payload.id, pointCoinUpdateValue);
await updateCoins(payload.id, pointCoinUpdateValue);
await updatePointsAndCoins(payload.id, pointCoinUpdateValue);

await Models.MentorOfficeHours.findOneAndUpdate(
{ mentorId: mentorId },
Expand Down
10 changes: 3 additions & 7 deletions src/services/profile/profile-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@ export function isValidLimit(limit: number): boolean {
return limit > 0;
}

/**
* Change a user's points by a specific amount.
* @param userId ID of the user to modify
* @param amount Amount of points to change (note that this can be a negative number too!)
* @returns Promise containing the new user, or the actual attendee profile
*/
export async function updatePoints(userId: string, amount: number): Promise<AttendeeProfile | null> {
export async function updatePointsAndCoins(userId: string, amount: number): Promise<AttendeeProfile | null> {
await updateCoins(userId, amount);

const updateQuery: UpdateQuery<AttendeeProfile> = {
$inc: {
points: amount,
Expand Down
24 changes: 12 additions & 12 deletions src/services/profile/profile-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ import cors from "cors";
import { Request, Router } from "express";
import { NextFunction, Response } from "express-serve-static-core";

import Config from "../../config.js";
import { Avatars } from "../../config.js";
import { isValidLimit, updatePoints, updateCoins } from "./profile-lib.js";
import Config, { Avatars } from "../../config.js";
import { AttendeeProfile } from "../../database/attendee-db.js";
import { RegistrationApplication } from "../../database/registration-db.js";
import { isValidLimit, updatePointsAndCoins } from "./profile-lib.js";

import Models from "../../database/models.js";
import { Query } from "mongoose";
import Models from "../../database/models.js";
import { LeaderboardEntry } from "./profile-models.js";

import { JwtPayload } from "../auth/auth-models.js";
import { strongJwtVerification } from "../../middleware/verify-jwt.js";
// import { ProfileFormat, isValidProfileFormat } from "./profile-formats.js";
import { hasElevatedPerms } from "../auth/auth-lib.js";
import { DeleteResult } from "mongodb";
import { StatusCode } from "status-code-enum";
import { strongJwtVerification } from "../../middleware/verify-jwt.js";
import { hasElevatedPerms } from "../auth/auth-lib.js";
import { JwtPayload } from "../auth/auth-models.js";

import { isNumber } from "../../formatTools.js";
import { RouterError } from "../../middleware/error-handler.js";
import { isValidProfileFormat } from "./profile-formats.js";

Expand Down Expand Up @@ -346,6 +345,10 @@ profileRouter.post("/addpoints", strongJwtVerification, async (req: Request, res

const payload: JwtPayload = res.locals.payload as JwtPayload;

if (!isNumber(points)) {
return next(new RouterError(StatusCode.ClientErrorBadRequest, "NoPoints"));
}

//Sends error if caller doesn't have elevated perms
if (!hasElevatedPerms(payload)) {
return next(new RouterError(StatusCode.ClientErrorForbidden, "Forbidden"));
Expand All @@ -357,10 +360,7 @@ profileRouter.post("/addpoints", strongJwtVerification, async (req: Request, res
return next(new RouterError(StatusCode.ClientErrorBadRequest, "UserNotFound"));
}

await updatePoints(userId, points);
if (points > 0) {
await updateCoins(userId, points);
}
await updatePointsAndCoins(userId, points);

const updatedProfile: AttendeeProfile | null = await Models.AttendeeProfile.findOne({ userId: userId });

Expand Down
2 changes: 1 addition & 1 deletion src/services/registration/registration-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export function getApplication(userId: string): Promise<RegistrationApplication
export function isRegistrationAlive(): boolean {
const currentDateTime = new Date().getTime();

return currentDateTime <= Config.REGISTRATION_CLOSE_TIME;
return currentDateTime <= Config.REGISTRATION_CLOSE_TIME_MS;
}
4 changes: 2 additions & 2 deletions src/services/staff/staff-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Models from "../../database/models.js";
import { StatusCode } from "status-code-enum";
import { checkInResult } from "./staff-formats.js";
import { RouterError } from "../../middleware/error-handler.js";
import { updatePoints } from "../profile/profile-lib.js";
import { updatePointsAndCoins } from "../profile/profile-lib.js";
import { isNumber } from "../../formatTools.js";

export async function performCheckIn(eventId: string, userId: string, points: number = 0): Promise<checkInResult> {
Expand All @@ -26,7 +26,7 @@ export async function performCheckIn(eventId: string, userId: string, points: nu
points = event?.points ?? 0;
}

const newProfile = await updatePoints(userId, points);
const newProfile = await updatePointsAndCoins(userId, points);

if (!newProfile) {
return { success: false, error: new RouterError(StatusCode.ServerErrorInternal, "NoPointsUpdate") };
Expand Down

0 comments on commit 9741015

Please sign in to comment.