Skip to content

Commit

Permalink
Migrate database usage & endpoints (#209)
Browse files Browse the repository at this point in the history
- Migrates database to only be connected when needed
- Migrates /shop/ to /shop/v2/
- Migrates /user/qr/ to /user/v2-qr/
  • Loading branch information
Timothy-Gonzalez committed Feb 24, 2024
1 parent 104dcae commit d78b50a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 29 deletions.
30 changes: 14 additions & 16 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,23 @@ if (!Config.TEST) {
// Automatically convert requests from json
app.use(express.json());

// Initialize database (IMPORTANT: must be before all routers)
app.use(database);

// Add routers for each sub-service
app.use("/admission/", admissionRouter);
app.use("/auth/", authRouter);
app.use("/event/", eventRouter);
app.use("/mail/", mailRouter);
app.use("/mentor/", mentorRouter);
app.use("/newsletter/", newsletterRouter);
app.use("/notification/", notificationRouter);
app.use("/profile/", profileRouter);
app.use("/puzzle/", puzzleRouter);
app.use("/registration/", registrationRouter);
// NOTE: only include database middleware if needed
app.use("/admission/", database, admissionRouter);
app.use("/auth/", database, authRouter);
app.use("/event/", database, eventRouter);
app.use("/mail/", database, mailRouter);
app.use("/mentor/", database, mentorRouter);
app.use("/newsletter/", database, newsletterRouter);
app.use("/notification/", database, notificationRouter);
app.use("/profile/", database, profileRouter);
app.use("/puzzle/", database, puzzleRouter);
app.use("/registration/", database, registrationRouter);
app.use("/s3/", s3Router);
app.use("/shop/", shopRouter);
app.use("/staff/", staffRouter);
app.use("/shop/", database, shopRouter);
app.use("/staff/", database, staffRouter);
app.use("/version/", versionRouter);
app.use("/user/", userRouter);
app.use("/user/", database, userRouter);

// Ensure that API is running
app.get("/", (_: Request, res: Response) => {
Expand Down
6 changes: 3 additions & 3 deletions src/services/shop/shop-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { FilteredShopItemFormat, isValidItemFormat } from "./shop-formats.js";
* @apiGroup Shop
* @apiDescription Get item details for all items in point shop.
*
* TODO: Rename back from /shop/v2/ to /shop/
*
* @apiSuccess (200: Success) {Json} items The items details.
* @apiSuccessExample Example Success Response
* HTTP/1.1 200 OK
Expand All @@ -43,7 +45,7 @@ import { FilteredShopItemFormat, isValidItemFormat } from "./shop-formats.js";
* */

const shopRouter: Router = Router();
shopRouter.get("/", weakJwtVerification, async (_1: Request, res: Response, _2: NextFunction) => {
shopRouter.get("/v2/", weakJwtVerification, async (_1: Request, res: Response, _2: NextFunction) => {
const shopItems: ShopItem[] = await Models.ShopItem.find();

const filteredData: FilteredShopItemFormat[] = shopItems.map((item: ShopItem) => ({
Expand All @@ -58,8 +60,6 @@ shopRouter.get("/", weakJwtVerification, async (_1: Request, res: Response, _2:
return res.status(StatusCode.SuccessOK).send(filteredData);
});

shopRouter.get("/v2/", (_: Request, res: Response) => res.redirect("/shop/"));

/**
* @api {post} /shop/item POST /shop/item
* @apiGroup Shop
Expand Down
5 changes: 3 additions & 2 deletions src/services/user/user-router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ beforeEach(async () => {
await Models.AttendeeProfile.create(TESTER_PROFILE);
});

describe("GET /user/qr/", () => {
// TODO: Revert v2-qr to qr
describe("GET /user/v2-qr/", () => {
it("works for a attendee", async () => {
const mockedGenerateJwtToken = mockGenerateJwtTokenWithWrapper();

const response = await getAsAttendee("/user/qr/").expect(StatusCode.SuccessOK);
const response = await getAsAttendee("/user/v2-qr/").expect(StatusCode.SuccessOK);

const jwtReturned = mockedGenerateJwtToken.mock.results[mockedGenerateJwtToken.mock.results.length - 1]!.value;

Expand Down
10 changes: 2 additions & 8 deletions src/services/user/user-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const userRouter: Router = Router();
* @apiGroup User
* @apiDescription Get a QR code with a pre-defined expiration for the user provided in the JWT token. Since expiry is set to 20 seconds,
* we recommend that the results from this endpoint are not stored, but instead used immediately.
*
* TODO: Rename back from /v2-qr/ to /qr/
*
* @apiSuccess (200: Success) {String} userId User to generate a QR code for
* @apiSuccess (200: Success) {String} qrInfo Stringified QR code for the given user
Expand All @@ -35,14 +37,6 @@ const userRouter: Router = Router();
*
* @apiUse strongVerifyErrors
*/
userRouter.get("/qr/", strongJwtVerification, (_: Request, res: Response) => {
// Return the same payload, but with a shorter expiration time
const payload: JwtPayload = res.locals.payload as JwtPayload;
const token: string = generateJwtToken(payload, false, Config.QR_EXPIRY_TIME);
const uri: string = `hackillinois://user?userToken=${token}`;
return res.status(StatusCode.SuccessOK).send({ userId: payload.id, qrInfo: uri });
});

userRouter.get("/v2-qr/", strongJwtVerification, (_: Request, res: Response) => {
const payload: JwtPayload = res.locals.payload as JwtPayload;
const token: string = generateJwtToken(payload, false, Config.QR_EXPIRY_TIME);
Expand Down

0 comments on commit d78b50a

Please sign in to comment.