Skip to content

Commit

Permalink
merch support
Browse files Browse the repository at this point in the history
  • Loading branch information
divyack2 committed Sep 16, 2024
1 parent 14f2985 commit fe23265
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 9 deletions.
55 changes: 48 additions & 7 deletions src/services/attendee/attendee-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,37 @@ attendeeRouter.get(
}
);

// Get attendee merch info via email
// Get attendee info via userId
attendeeRouter.get(
"/email",
"/id/:USERID",
RoleChecker([Role.Enum.USER]),
async (req, res, next) => {
try {
const payload = res.locals.payload;
const email = payload.email;
const userId = req.params.USERID;

// Check if the user exists in the database
const user = await Database.ATTENDEE.findOne({ userId });

if (!user) {
return res
.status(StatusCodes.NOT_FOUND)
.json({ error: "UserNotFound" });
}

return res.status(StatusCodes.OK).json(user);
} catch (error) {
next(error);
}
}
);

// Get attendee merch info via email
attendeeRouter.get(
"/email/:EMAIL",
RoleChecker([Role.Enum.STAFF, Role.Enum.ADMIN]),
async (req, res, next) => {
try {
const email = req.params.EMAIL;

Check failure on line 261 in src/services/attendee/attendee-router.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `············`
const user = await Database.ATTENDEE.findOne({ email });

Expand All @@ -246,6 +269,7 @@ attendeeRouter.get(

const merchInfo = {
attendeeName: user.name,
attendeePoints: user.points,
hasButton: user.hasRedeemedMerch!["Button"],
hasCap: user.hasRedeemedMerch!["Cap"],
hasTote: user.hasRedeemedMerch!["Tote"],
Expand All @@ -263,15 +287,32 @@ attendeeRouter.get(

attendeeRouter.post(
"/redeemMerch/:ITEM",
RoleChecker([]),
RoleChecker([Role.Enum.STAFF, Role.Enum.ADMIN]),
async (req, res, next) => {
try {
console.log("STARTING")

Check failure on line 293 in src/services/attendee/attendee-router.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `;`
const payload = res.locals.payload;
const userId = payload.userId;
const merchItem = req.params.ITEM;
console.log("HERE")

Check failure on line 297 in src/services/attendee/attendee-router.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `;`

// Check if the user exists in the database
const user = await Database.ATTENDEE.findOne({ userId });
let user;

// Check if userId or email is provided and query the database accordingly
if (userId) {
user = await Database.ATTENDEE.findOne({ userId });
} else if (req.body.email) {
const email = req.body.email; // TODO: use a validator here?
user = await Database.ATTENDEE.findOne({ email });
}

if (!user) {
return res
.status(StatusCodes.NOT_FOUND)
.json({ error: "UserNotFound" });
}

console.log("FOUND USER")

Check failure on line 315 in src/services/attendee/attendee-router.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `;`

if (!user) {
return res
Expand Down
10 changes: 10 additions & 0 deletions src/services/attendee/attendee-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export const AttendeeSchema = new Schema({
},
{ _id: false }
),
default: {
Button: false,
Tote: false,
Cap: false,
}

Check failure on line 49 in src/services/attendee/attendee-schema.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
},
isEligibleMerch: {
type: new Schema(
Expand All @@ -52,6 +57,11 @@ export const AttendeeSchema = new Schema({
},
{ _id: false }
),
default: {
Button: false,
Tote: false,
Cap: false,
}

Check failure on line 64 in src/services/attendee/attendee-schema.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
},

favorites: [{ type: String }],
Expand Down
24 changes: 23 additions & 1 deletion src/services/checkin/checkin-router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Router } from "express";
import { StatusCodes } from "http-status-codes";
import { ScanValidator } from "./checkin-schema";
import { ScanValidator, MerchScanValidator } from "./checkin-schema";
import RoleChecker from "../../middleware/role-checker";
import { Role } from "../auth/auth-models";
import { validateQrHash } from "./checkin-utils";
Expand Down Expand Up @@ -33,6 +33,28 @@ checkinRouter.post(
}
);

checkinRouter.post(
"/scan/merch",
RoleChecker([Role.Enum.ADMIN, Role.Enum.STAFF]),
async (req, res, next) => {
try {
const { qrCode } = MerchScanValidator.parse(req.body);

const { userId, expTime } = validateQrHash(qrCode);

if (Date.now() / 1000 > expTime) {
return res
.status(StatusCodes.UNAUTHORIZED)
.json({ error: "QR code has expired" });
}

return res.status(StatusCodes.OK).json(userId);
} catch (error) {
next(error);
}
}
);

checkinRouter.post(
"/",
RoleChecker([Role.Enum.ADMIN, Role.Enum.STAFF]),
Expand Down
6 changes: 5 additions & 1 deletion src/services/checkin/checkin-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ const ScanValidator = z.object({
qrCode: z.string(),
});

export { ScanValidator };
const MerchScanValidator = z.object({
qrCode: z.string(),
});

export { ScanValidator, MerchScanValidator };

0 comments on commit fe23265

Please sign in to comment.