From f04cf74513dcbe7cec4b3e36e596254a0a9c3893 Mon Sep 17 00:00:00 2001 From: Dev Singh Date: Sat, 31 Aug 2024 19:14:37 -0500 Subject: [PATCH] vending machine route (#6) * vending machine route * goddamnit tests * vend only in dev environment --- src/config.ts | 1 + src/index.ts | 4 ++++ src/routes/vending.ts | 47 ++++++++++++++++++++++++++++++++++++++ tests/unit/vending.test.ts | 11 +++++++++ 4 files changed, 63 insertions(+) create mode 100644 src/routes/vending.ts create mode 100644 tests/unit/vending.test.ts diff --git a/src/config.ts b/src/config.ts index 6658742..3db89d9 100644 --- a/src/config.ts +++ b/src/config.ts @@ -47,6 +47,7 @@ const environmentConfig: EnvironmentConfigType = { ValidCorsOrigins: [ "http://localhost:3000", "http://localhost:5173", + "https://merch-pwa.pages.dev", /^https:\/\/(?:.*\.)?acmuiuc\.pages\.dev$/, ], AadValidClientId: "39c28870-94e4-47ee-b4fb-affe0bf96c9f", diff --git a/src/index.ts b/src/index.ts index 2891797..491257a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,7 @@ import fastifyZodValidationPlugin from "./plugins/validate.js"; import { environmentConfig } from "./config.js"; import organizationsPlugin from "./routes/organizations.js"; import icalPlugin from "./routes/ics.js"; +import vendingPlugin from "./routes/vending.js"; const now = () => Date.now(); @@ -68,6 +69,9 @@ async function init() { api.register(eventsPlugin, { prefix: "/events" }); api.register(organizationsPlugin, { prefix: "/organizations" }); api.register(icalPlugin, { prefix: "/ical" }); + if (app.runEnvironment === "dev") { + api.register(vendingPlugin, { prefix: "/vending" }); + } }, { prefix: "/api/v1" }, ); diff --git a/src/routes/vending.ts b/src/routes/vending.ts new file mode 100644 index 0000000..0f467f0 --- /dev/null +++ b/src/routes/vending.ts @@ -0,0 +1,47 @@ +import { FastifyPluginAsync } from "fastify"; +import { z } from "zod"; + +const postSchema = z.object({ + name: z.string().min(1), + imageUrl: z.string().url(), + price: z.number().min(0), +}); + +type VendingItemPostRequest = z.infer; + +const vendingPlugin: FastifyPluginAsync = async (fastify, _options) => { + fastify.get("/items", async (request, reply) => { + reply.send({ + items: [ + { + rowid: 1, + name: "TBD", + image_url: + "https://acm-brand-images.s3.amazonaws.com/square-blue.png", + price: 400, + calories: null, + fat: null, + carbs: null, + fiber: null, + sugar: null, + protein: null, + quantity: 100, + locations: null, + }, + ], + }); + }); + fastify.post<{ Body: VendingItemPostRequest }>( + "/items", + { + preValidation: async (request, reply) => { + await fastify.zodValidateBody(request, reply, postSchema); + }, + }, + async (request, reply) => { + reply.send({ status: "Not implemented." }); + }, + ); +}; + +export default vendingPlugin; diff --git a/tests/unit/vending.test.ts b/tests/unit/vending.test.ts new file mode 100644 index 0000000..e03979c --- /dev/null +++ b/tests/unit/vending.test.ts @@ -0,0 +1,11 @@ +import { expect, test } from "vitest"; +import init from "../../src/index.js"; + +const app = await init(); +test("Test getting events", async () => { + const response = await app.inject({ + method: "GET", + url: "/api/v1/vending/items", + }); + expect(response.statusCode).toBe(200); +});