Skip to content

Commit f04cf74

Browse files
authored
vending machine route (#6)
* vending machine route * goddamnit tests * vend only in dev environment
1 parent 8d3611a commit f04cf74

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const environmentConfig: EnvironmentConfigType = {
4747
ValidCorsOrigins: [
4848
"http://localhost:3000",
4949
"http://localhost:5173",
50+
"https://merch-pwa.pages.dev",
5051
/^https:\/\/(?:.*\.)?acmuiuc\.pages\.dev$/,
5152
],
5253
AadValidClientId: "39c28870-94e4-47ee-b4fb-affe0bf96c9f",

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import fastifyZodValidationPlugin from "./plugins/validate.js";
1313
import { environmentConfig } from "./config.js";
1414
import organizationsPlugin from "./routes/organizations.js";
1515
import icalPlugin from "./routes/ics.js";
16+
import vendingPlugin from "./routes/vending.js";
1617

1718
const now = () => Date.now();
1819

@@ -68,6 +69,9 @@ async function init() {
6869
api.register(eventsPlugin, { prefix: "/events" });
6970
api.register(organizationsPlugin, { prefix: "/organizations" });
7071
api.register(icalPlugin, { prefix: "/ical" });
72+
if (app.runEnvironment === "dev") {
73+
api.register(vendingPlugin, { prefix: "/vending" });
74+
}
7175
},
7276
{ prefix: "/api/v1" },
7377
);

src/routes/vending.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { FastifyPluginAsync } from "fastify";
2+
import { z } from "zod";
3+
4+
const postSchema = z.object({
5+
name: z.string().min(1),
6+
imageUrl: z.string().url(),
7+
price: z.number().min(0),
8+
});
9+
10+
type VendingItemPostRequest = z.infer<typeof postSchema>;
11+
12+
const vendingPlugin: FastifyPluginAsync = async (fastify, _options) => {
13+
fastify.get("/items", async (request, reply) => {
14+
reply.send({
15+
items: [
16+
{
17+
rowid: 1,
18+
name: "TBD",
19+
image_url:
20+
"https://acm-brand-images.s3.amazonaws.com/square-blue.png",
21+
price: 400,
22+
calories: null,
23+
fat: null,
24+
carbs: null,
25+
fiber: null,
26+
sugar: null,
27+
protein: null,
28+
quantity: 100,
29+
locations: null,
30+
},
31+
],
32+
});
33+
});
34+
fastify.post<{ Body: VendingItemPostRequest }>(
35+
"/items",
36+
{
37+
preValidation: async (request, reply) => {
38+
await fastify.zodValidateBody(request, reply, postSchema);
39+
},
40+
},
41+
async (request, reply) => {
42+
reply.send({ status: "Not implemented." });
43+
},
44+
);
45+
};
46+
47+
export default vendingPlugin;

tests/unit/vending.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect, test } from "vitest";
2+
import init from "../../src/index.js";
3+
4+
const app = await init();
5+
test("Test getting events", async () => {
6+
const response = await app.inject({
7+
method: "GET",
8+
url: "/api/v1/vending/items",
9+
});
10+
expect(response.statusCode).toBe(200);
11+
});

0 commit comments

Comments
 (0)