Skip to content

Commit

Permalink
smore changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AydanPirani committed Feb 16, 2024
1 parent ebbd8f4 commit 54e573d
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const Config = {
MAX_SHOP_STOCK_PER_ITEM: 128,

RANKING_OFFSET: 1,
REGISTRATION_CLOSE_DATETIME: "2024-02-16T00:06:15-06:00",
};

export default Config;
9 changes: 9 additions & 0 deletions src/services/registration/registration-lib.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import Config from "../../config.js";
import Models from "../../database/models.js";
import { RegistrationApplication } from "../../database/registration-db.js";

export function getApplication(userId: string): Promise<RegistrationApplication | null> {
return Models.RegistrationApplication.findOne({ userId: userId });
}

export function isRegistrationAlive(): boolean {
const targetDateTime = new Date(Config.REGISTRATION_CLOSE_DATETIME);
const currentDate = new Date().toLocaleString("en-US", { timeZone: "America/Chicago" });
const currentDateTime = new Date(currentDate + "-06:00");

return currentDateTime < targetDateTime;
}
10 changes: 10 additions & 0 deletions src/services/registration/registration-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ import { JwtPayload } from "../auth/auth-models.js";

import { sendMail } from "../mail/mail-lib.js";
import { MailInfoFormat } from "../mail/mail-formats.js";
import { isRegistrationAlive } from "./registration-lib.js";

const registrationRouter: Router = Router();

registrationRouter.get("/status/", async (_: Request, res: Response) => {
const isAlive = isRegistrationAlive();
return res.status(StatusCode.SuccessOK).send({ alive: isAlive });
});

/**
* @api {get} /registration/ GET /registration/
* @apiGroup Registration
Expand Down Expand Up @@ -297,6 +303,10 @@ registrationRouter.post("/", strongJwtVerification, async (req: Request, res: Re
* @apiError (500: Internal Server Error) {String} InternalError Server-side error
**/
registrationRouter.post("/submit/", strongJwtVerification, async (_: Request, res: Response, next: NextFunction) => {
if (!isRegistrationAlive()) {
return next(new RouterError(StatusCode.ClientErrorForbidden, "RegistrationClosed"));
}

const payload: JwtPayload = res.locals.payload as JwtPayload;
const userId: string = payload.id;

Expand Down
2 changes: 2 additions & 0 deletions src/services/staff/staff-formats.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RouterError } from "middleware/error-handler.js";
import { StaffShift } from "../../database/staff-db.js";
import { isArrayOfType, isString } from "../../formatTools.js";
import { AttendeeProfile } from "../../database/attendee-db.js";

// Format for default staff attendance input
export interface AttendanceFormat {
Expand Down Expand Up @@ -31,4 +32,5 @@ export interface EventError {
export interface checkInResult {
success: boolean;
error?: RouterError;
profile?: AttendeeProfile;
}
16 changes: 14 additions & 2 deletions src/services/staff/staff-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ 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";

export async function performCheckIn(eventId: string, userId: string): Promise<checkInResult> {
export async function performCheckIn(eventId: string, userId: string, points: number = 0): Promise<checkInResult> {
const eventAttendance = await Models.EventAttendance.findOne({ eventId: eventId });

if (!eventAttendance) {
Expand All @@ -19,5 +20,16 @@ export async function performCheckIn(eventId: string, userId: string): Promise<c
await Models.UserAttendance.findOneAndUpdate({ userId: userId }, { $addToSet: { attendance: eventId } }, { upsert: true });
await Models.EventAttendance.findOneAndUpdate({ eventId: eventId }, { $addToSet: { attendees: userId } }, { upsert: true });

return { success: true };
if (!points) {
const event = await Models.Event.findOne({ eventId: eventId });
points = event?.points ?? 0;
}

const newProfile = await updatePoints(userId, points);

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

return { success: true, profile: newProfile };
}
4 changes: 2 additions & 2 deletions src/services/staff/staff-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ staffRouter.put("/scan-attendee/", strongJwtVerification, async (req: Request, r
const registrationData = await Models.RegistrationApplication.findOne({ userId: userId }).select("dietaryRestrictions");

if (!registrationData) {
return next(new RouterError(StatusCode.ClientErrorFailedDependency, "NoRegistrationData"));
return next(new RouterError(StatusCode.ClientErrorNotFound, "EventNotFound"));
}
const dietaryRestrictions = registrationData["dietaryRestrictions"];

return res.status(StatusCode.SuccessOK).json({ success: true, dietaryRestrictions: dietaryRestrictions });
return res.status(StatusCode.SuccessOK).json({ success: true, ...result.profile, dietaryRestrictions: dietaryRestrictions });
});

/**
Expand Down
16 changes: 8 additions & 8 deletions src/services/user/user-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { AttendeeFollowing } from "database/attendee-db.js";
import { NextFunction } from "express-serve-static-core";
import { RouterError } from "../../middleware/error-handler.js";
import { EventFollowers } from "../../database/event-db.js";

const userRouter: Router = Router();

/**
Expand Down Expand Up @@ -318,19 +319,18 @@ userRouter.put("/scan-event/", strongJwtVerification, async (req: Request, res:
return next(new RouterError(StatusCode.ClientErrorBadRequest, "InvalidParams"));
}

const result = await performCheckIn(eventId, userId);
const eventData = await Models.Event.findOne({ eventId: eventId });
if (!eventData) {
return next(new RouterError(StatusCode.ClientErrorNotFound, "NonexistentEvent"));
}

const result = await performCheckIn(eventId, userId, eventData.points);

if (!result.success) {
return next(result.error);
}

const eventData = await Models.Event.findOne({ eventId: eventId });
if (!eventData) {
return next(new RouterError(StatusCode.ClientErrorFailedDependency, "NonexistantEvent"));
}
const points = eventData["points"];

return res.status(StatusCode.SuccessOK).json({ success: true, points: points });
return res.status(StatusCode.SuccessOK).json({ success: true, ...result.profile });
});

export default userRouter;

0 comments on commit 54e573d

Please sign in to comment.