-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: invalid types after migrating campusdish schemas to match prisma…
… schema
- Loading branch information
Showing
20 changed files
with
10,409 additions
and
5,057 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,23 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import data from "./campus_dish_schema.json"; | ||
import { CampusDishResponseSchema } from "@zotmeal/validators"; | ||
|
||
import campus_dish_response from "./campus_dish_response.json"; | ||
import { parseCampusDish } from "./parse"; | ||
|
||
describe("parse campus dish", () => { | ||
it("parses campus dish response", () => { | ||
expect(parseCampusDish(data)).not.toBeNull(); | ||
expect(() => { | ||
const validated = CampusDishResponseSchema.parse(campus_dish_response); | ||
// open a db instance | ||
const menu = parseCampusDish(validated); | ||
console.log(menu); | ||
|
||
expect(menu).not.toBeNull(); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it("", () => { | ||
// add an integration test, ideally using testcontainers | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,108 @@ | ||
import axios from "axios"; | ||
import { z } from "zod"; | ||
|
||
import type { Menu } from "@zotmeal/db"; | ||
import type { | ||
// CampusDishMenuStation, | ||
CampusDishResponse, | ||
// ParsedResponse, | ||
DietRestrictionSchema, | ||
DishSchema, | ||
} from "@zotmeal/validators"; | ||
import { getPeriodId, getRestaurantId } from "@zotmeal/utils"; | ||
import { | ||
getPeriodId, | ||
getRestaurantById, | ||
getRestaurantId, | ||
} from "@zotmeal/utils"; | ||
import { CampusDishResponseSchema } from "@zotmeal/validators"; | ||
|
||
import { publicProcedure } from "../../trpc"; | ||
import { GetMenuSchema } from "./get"; | ||
|
||
// We will examine the response and update our database state | ||
// Add new dishes | ||
// Add new menu | ||
// Add new hours | ||
export function parseCampusDish(data: Record<string, unknown>): Menu | null { | ||
const response = CampusDishResponseSchema.parse(data); | ||
if (response === null) { | ||
return null; | ||
} | ||
console.log(response); | ||
// we should get a menu object with a full nested hierarchy | ||
// we want to return a menu tree | ||
// validate schema with zod | ||
// fail if it does not match | ||
export const parseMenuProcedure = publicProcedure | ||
// .input(GetMenuSchema) | ||
.query(async ({ ctx, input }) => { | ||
const { db } = ctx; | ||
const _ = db; | ||
// const { date, restaurant, period } = input; | ||
|
||
// const periodId = getPeriodId(period); | ||
// const restaurantId = getRestaurantId(restaurant); | ||
|
||
const res = await axios.get( | ||
"https://uci-campusdish-com.translate.goog/api/menu/GetMenus?locationId=3314&periodId=49&date=1/19/2024", | ||
); | ||
// const res = await axios.get( | ||
// `https://uci-campusdish-com.translate.goog/api/menu/GetMenus?locationId=${restaurantId}&periodId=${periodId}&date=${date}`, | ||
// ); | ||
try { | ||
const validated = CampusDishResponseSchema.parse(res); | ||
} catch (e) { | ||
console.log(e); | ||
throw e; | ||
} | ||
|
||
const menu = parseCampusDish(validated); | ||
|
||
return menu; | ||
}); | ||
|
||
export function parseCampusDish(response: CampusDishResponse) { | ||
const uniqueStations = new Set<string>(); | ||
response.Menu.MenuStations.forEach((menuStation) => { | ||
uniqueStations.add( | ||
JSON.stringify({ | ||
station_id: menuStation.StationId, | ||
restaurant_id: data.LocationId, | ||
restaurant_id: response.LocationId, | ||
name: menuStation.Name, | ||
}), | ||
); | ||
}); | ||
|
||
// const stations = Array.from(uniqueStations).map( | ||
// (station) => JSON.parse(station) as CampusDishMenuStation, | ||
// ); | ||
|
||
// // prefetch the periods | ||
|
||
// 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 null; | ||
} | ||
|
||
export const parseMenuProcedure = publicProcedure | ||
.input(GetMenuSchema) | ||
.query(async ({ input }) => { | ||
const { date, restaurant, period } = input; | ||
|
||
// get the period by name from the db | ||
|
||
// use the db as the source of truth | ||
|
||
const periodId = getPeriodId(period); | ||
const restaurantId = getRestaurantId(restaurant); | ||
const stations = Array.from(uniqueStations).map( | ||
(station) => | ||
JSON.parse(station) as { | ||
station_id: string; | ||
restaurant_id: string; | ||
name: string; | ||
}, | ||
); | ||
|
||
// const res = await axios.get( | ||
// "https://uci-campusdish-com.translate.goog/api/menu/GetMenus?locationId=3314&periodId=49&date=1/19/2024", | ||
// ); | ||
const res = await axios.get( | ||
`https://uci-campusdish-com.translate.goog/api/menu/GetMenus?locationId=${restaurantId}&periodId=${periodId}&date=${date}`, | ||
); | ||
|
||
const data = res.data as CampusDishResponse; | ||
const dishes = response.Menu.MenuProducts.map((menuProduct) => { | ||
type DietRestriction = z.infer<typeof DietRestrictionSchema>; | ||
const dietRestriction = { | ||
containsEggs: menuProduct.Product.ContainsEggs, | ||
containsFish: menuProduct.Product.ContainsFish, | ||
containsMilk: menuProduct.Product.ContainsMilk, | ||
containsPeanuts: menuProduct.Product.ContainsPeanuts, | ||
containsShellfish: menuProduct.Product.ContainsShellfish, | ||
containsSoy: menuProduct.Product.ContainsSoy, | ||
containsTreeNuts: menuProduct.Product.ContainsTreeNuts, | ||
containsWheat: menuProduct.Product.ContainsWheat, | ||
containsSesame: menuProduct.Product.ContainsSesame, | ||
isGlutenFree: menuProduct.Product.IsGlutenFree, | ||
isHalal: menuProduct.Product.IsHalal, | ||
isKosher: menuProduct.Product.IsKosher, | ||
isLocallyGrown: menuProduct.Product.IsLocallyGrown, | ||
isOrganic: menuProduct.Product.IsOrganic, | ||
isVegan: menuProduct.Product.IsVegan, | ||
isVegetarian: menuProduct.Product.IsVegetarian, | ||
} as DietRestriction; | ||
|
||
// parseCampusDish(data); | ||
type Dish = z.infer<typeof DishSchema>; | ||
|
||
return data; | ||
return { | ||
id: menuProduct.MenuProductId, | ||
stationId: menuProduct.StationId, | ||
name: menuProduct.Product.MarketingName, | ||
description: menuProduct.Product.ShortDescription, | ||
dietRestriction: dietRestriction, | ||
} as Dish; | ||
}); | ||
const parsed = { | ||
restaurant: { | ||
restaurant_id: response.LocationId, | ||
restaurant_name: getRestaurantById(response.LocationId), | ||
}, | ||
stations, | ||
dishes: dishes, | ||
}; | ||
return parsed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.