Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema updates/menu.get #97

Merged
merged 23 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
923d18a
feat: mvp parser
dlustre Feb 2, 2024
6f0f817
refactor: move parse into the menuRouter
dlustre Feb 3, 2024
423192c
refactor: move parse function to its own file
dlustre Feb 3, 2024
a516415
refactor: replace interfaces with zod schemas
dlustre Feb 3, 2024
5e56194
refactor: separate campusdish schemas and zotmeal schemas into their …
dlustre Feb 3, 2024
3a9e36a
refactor: clean up code, imports, and file locations
dlustre Feb 3, 2024
d9c6ca7
creating zod schema for menu.get procedure
anmho Feb 3, 2024
837db9e
creating zod schema for menu.get procedure
anmho Feb 3, 2024
da0f563
Merge 'campusdish-parser'
anmho Feb 3, 2024
006ede4
Update README.md
anmho Feb 3, 2024
e874a1a
update zod schemas to match models from #92
dlustre Feb 3, 2024
d7be912
replace @acme alias with @zotmeal
dlustre Feb 3, 2024
02f64b0
fix: update import for local and switch to Google CampusDish URL
KatyH820 Feb 3, 2024
de01321
chore: update pnpm-lock.yaml
dlustre Feb 3, 2024
7468042
Merge pull request #96 from icssc/campusdish-parser-katy
dlustre Feb 3, 2024
3445550
chore: created utils package and moved parse
anmho Feb 3, 2024
c554e83
Merge pull request #89 from icssc/campusdish-parser
KatyH820 Feb 3, 2024
69fec35
feat: menu.get procedure - get menu from db
anmho Feb 4, 2024
6d31068
moved parse to packages/api
anmho Feb 4, 2024
8e16ca5
merged main
anmho Feb 4, 2024
41665b3
feat: get procedure with error handling and api package refactoring"
anmho Feb 4, 2024
860adfd
tests: added initial tests for menu.parse
anmho Feb 5, 2024
5335282
fix: invalid types after migrating campusdish schemas to match prisma…
anmho Feb 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions apps/server/src/functions/hello/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import {
awsLambdaRequestHandler,
CreateAWSLambdaContextOptions,
} from "@trpc/server/adapters/aws-lambda";
import { appRouter, createTRPCContext } from "@acme/api";
import { APIGatewayProxyEventV2 } from "aws-lambda";

import { appRouter, createTRPCContext } from "@acme/api";

const createContext = ({
event,
context,
Expand All @@ -20,15 +21,4 @@ export const handler = awsLambdaRequestHandler({
createContext,
});

// const handler = async (event) => {
// const res = await axios.get("https://jsonplaceholder.typicode.com/todos/1");
// return {
// statusCode: 200,
// body: JSON.stringify({
// message: `Hello ${"hello"} !`,
// data: res.data,
// }),
// };
// };

export const main = handler;
2 changes: 2 additions & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"@acme/tsconfig": "workspace:^",
"@acme/validators": "workspace:^",
"@trpc/server": "11.0.0-next-beta.236",
"@zotmeal/utils": "workspace:^",
"jest": "^29.7.0",
"superjson": "2.2.1",
"zod": "^3.22.4"
},
Expand Down
5 changes: 5 additions & 0 deletions packages/api/src/router/menu.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { it } from "@jest/globals";

it("fail on invalid menu params", () => {
console.log("testing");
});
77 changes: 73 additions & 4 deletions packages/api/src/router/menu.ts
anmho marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,20 +1,89 @@
import axios from "axios";

import { createTRPCRouter, publicProcedure } from "../trpc";
import { MenuPeriod } from '@acme/db';
import {z} from "zod";

export const menuRouter = createTRPCRouter({
get: publicProcedure.query(({ ctx }) => {
// https://uci.campusdish.com/api/menu/GetMenus?locationId=3314&periodId=49&date=1/19/2024

// const locations = Object.values(LocationNames).map((name) => name.toString());

// enum RestaurantName {
// brandywine,
// anteatery
// }

// const locations = ["a", "b", "c"] as const;


const GetMenuSchema = z.object({
date: z.string().regex(RegExp("/^\d{2}\/\d{2}\/\d{4}$/")),
period: z.nativeEnum(MenuPeriod),
restaurant: z.string()
})


const getMenuProcedure = publicProcedure
.input(GetMenuSchema)
.query(({ ctx, input }) => {
const _ = ctx;
// get a menu

const {date, period, restaurant} = input;

const {db} = ctx;

// date

const restaurantModel = db.restaurant.findFirst({
where: {
name: restaurant
},
include: {
stations: false,
menu: false,
}
});
if (restaurantModel === null) {
return

}



// get the rstaurant with this name



db.menu.findUnique({where: {
date: date,
period: period,
restaurant: location
}})

// get it from prisma
// if not there, call parse procedure
}),
})




import { parse } from "../../../utils/parse";
import { CampusDishResponseSchema, ParsedResponseSchema } from "@acme/validators";

export const menuRouter = createTRPCRouter({
get: ,
hello: publicProcedure.query(async ({ ctx }) => {
const res = await axios.get("https://jsonplaceholder.typicode.com/todos/1");
console.log(res.data);
console.log("hello");
const _ = ctx;
return "hello";
}),
parse: publicProcedure.query(async ({ ctx }) => {
const res = await axios.get("https://uci.campusdish.com/api/menu/GetMenus?locationId=3314&periodId=49&date=1/19/2024");
const validated = CampusDishResponseSchema.parse(res.data);
const parsed = ParsedResponseSchema.parse(parse(validated));
const _ = ctx;
return parsed;
}),
});
26 changes: 13 additions & 13 deletions packages/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,35 @@ model Restaurant {
Menu Menu[]
}


model Station {
id String @id
name String
restaurant Restaurant @relation(fields: [restaurantId], references: [id])
restaurantId String
dishes Dish[]
dishes Dish[]
menu Menu @relation(fields: [menuId], references: [id])
menuId String
}

enum MenuPeriod {
BREAKFAST
BRUNCH
LUNCH
DINNER
LATENIGHT
breakfast
brunch
lunch
dinner
latenight
}

model Menu {
id String @id
period MenuPeriod
id String @id
period MenuPeriod
date DateTime
start DateTime
end DateTime
restaurant Restaurant @relation(fields: [restaurantId], references: [id])
start DateTime
end DateTime
restaurant Restaurant @relation(fields: [restaurantId], references: [id])
restaurantId String
stations Station[]
}

model DietRestriction {
id String @id
containsFish Boolean?
Expand Down Expand Up @@ -97,4 +97,4 @@ model NutritionInfo {
saturatedFat String?
dish Dish @relation(fields: [dishId], references: [id])
dishId String @unique
}
}
1 change: 1 addition & 0 deletions packages/utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# `@zotmeal/utils`
4 changes: 4 additions & 0 deletions packages/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const LocationNames = {
"3314": "Brandywine",
"3056": "Anteatery",
};
1 change: 1 addition & 0 deletions packages/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./constants";
16 changes: 16 additions & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@zotmeal/utils",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"dev": "echo 'Add dev script here'",
"build": "echo 'Add build script here'",
"test": "echo 'Add test script here'",
"lint": "echo 'Add lint script here'"
},
"dependencies": {
"@acme/tsconfig": "workspace:^",
"@acme/validators": "workspace:^"
}
}
53 changes: 53 additions & 0 deletions packages/utils/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { CampusDishResponse } from "@acme/validators";

import { LocationNames } from "./constants";

export function parse(data: CampusDishResponse) {
const uniqueStations = new Set<string>();
data.Menu.MenuStations.forEach((menuStation) => {
uniqueStations.add(
JSON.stringify({
station_id: menuStation.StationId,
restaurant_id: data.LocationId,
name: menuStation.Name,
}),
);
});
const stations = Array.from(uniqueStations).map((station) =>
JSON.parse(station),
);
const parsed = {
restaurant: {
restaurant_id: data.LocationId,
restaurant_name:
LocationNames[data.LocationId as keyof typeof LocationNames],
},
stations,
dishes: data.Menu.MenuProducts.map((menuProduct) => ({
id: menuProduct.Product.ProductId,
station_id: menuProduct.StationId,
name: menuProduct.Product.MarketingName,
description: menuProduct.Product.ShortDescription,
dietary_restriction_info: {
id: menuProduct.Product.ProductId,
contains_eggs: menuProduct.Product.ContainsEggs,
contains_fish: menuProduct.Product.ContainsFish,
contains_milk: menuProduct.Product.ContainsMilk,
contains_peanuts: menuProduct.Product.ContainsPeanuts,
contains_shellfish: menuProduct.Product.ContainsShellfish,
contains_soy: menuProduct.Product.ContainsSoy,
contains_tree_nuts: menuProduct.Product.ContainsTreeNuts,
contains_wheat: menuProduct.Product.ContainsWheat,
contains_sesame: menuProduct.Product.ContainsSesame,
is_gluten_free: menuProduct.Product.IsGlutenFree,
is_halal: menuProduct.Product.IsHalal,
is_kosher: menuProduct.Product.IsKosher,
is_locally_grown: menuProduct.Product.IsLocallyGrown,
is_organic: menuProduct.Product.IsOrganic,
is_vegan: menuProduct.Product.IsVegan,
is_vegetarian: menuProduct.Product.IsVegetarian,
},
})),
};
return parsed;
}
4 changes: 4 additions & 0 deletions packages/utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["@acme/tsconfig/base.json"],
"exclude": ["node_modules"]
}
86 changes: 86 additions & 0 deletions packages/validators/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,89 @@ export const CreatePostSchema = z.object({
content: z.string().min(1),
});

export const GetMenuSchema = z.object({});

const MenuStationSchema = z.object({
StationId: z.string().min(1),
Name: z.string().min(1),
});

const MenuProductSchema = z.object({
StationId: z.string().min(1),
Product: z.object({
ProductId: z.string().min(1),
MarketingName: z.string().min(1),
ShortDescription: z.string(),
ContainsEggs: z.boolean().nullable(),
ContainsFish: z.boolean().nullable(),
ContainsMilk: z.boolean().nullable(),
ContainsPeanuts: z.boolean().nullable(),
ContainsShellfish: z.boolean().nullable(),
ContainsSoy: z.boolean().nullable(),
ContainsTreeNuts: z.boolean().nullable(),
ContainsWheat: z.boolean().nullable(),
ContainsSesame: z.boolean().nullable(),
IsGlutenFree: z.boolean().nullable(),
IsHalal: z.boolean().nullable(),
IsKosher: z.boolean().nullable(),
IsLocallyGrown: z.boolean().nullable(),
IsOrganic: z.boolean().nullable(),
IsVegan: z.boolean().nullable(),
IsVegetarian: z.boolean().nullable(),
}),
});

export const CampusDishResponseSchema = z.object({
LocationId: z.string(),
Menu: z.object({
MenuStations: z.array(MenuStationSchema),
MenuProducts: z.array(MenuProductSchema),
}),
});

export type CampusDishResponse = z.infer<typeof CampusDishResponseSchema>;

export const DietaryRestrictionInfoSchema = z.object({
id: z.string(),
contains_eggs: z.boolean().nullable(),
contains_fish: z.boolean().nullable(),
contains_milk: z.boolean().nullable(),
contains_peanuts: z.boolean().nullable(),
contains_shellfish: z.boolean().nullable(),
contains_soy: z.boolean().nullable(),
contains_tree_nuts: z.boolean().nullable(),
contains_wheat: z.boolean().nullable(),
contains_sesame: z.boolean().nullable(),
is_gluten_free: z.boolean().nullable(),
is_halal: z.boolean().nullable(),
is_kosher: z.boolean().nullable(),
is_locally_grown: z.boolean().nullable(),
is_organic: z.boolean().nullable(),
is_vegan: z.boolean().nullable(),
is_vegetarian: z.boolean().nullable(),
});

export const DishSchema = z.object({
id: z.string(),
station_id: z.string(),
name: z.string(),
description: z.string(),
dietary_restriction_info: DietaryRestrictionInfoSchema,
});

export const RestaurantSchema = z.object({
restaurant_id: z.string(),
restaurant_name: z.string(),
});

export const StationSchema = z.object({
station_id: z.string(),
restaurant_id: z.string(),
name: z.string(),
});

export const ParsedResponseSchema = z.object({
restaurant: RestaurantSchema,
stations: z.array(StationSchema),
dishes: z.array(DishSchema),
});
2 changes: 0 additions & 2 deletions packages/validators/src/menu.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { z } from "zod";

export const GetMenuSchema = z.object({});
Loading
Loading