Skip to content

Commit

Permalink
vending machine route (#6)
Browse files Browse the repository at this point in the history
* vending machine route

* goddamnit tests

* vend only in dev environment
  • Loading branch information
devksingh4 authored Sep 1, 2024
1 parent 8d3611a commit f04cf74
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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" },
);
Expand Down
47 changes: 47 additions & 0 deletions src/routes/vending.ts
Original file line number Diff line number Diff line change
@@ -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<typeof postSchema>;

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;
11 changes: 11 additions & 0 deletions tests/unit/vending.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit f04cf74

Please sign in to comment.