Skip to content

Commit

Permalink
Fixed getMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
bjsilva1 committed May 17, 2024
1 parent 47051e6 commit dcc6f11
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/api/src/menus/procedures/getMenu.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TRPCError } from "@trpc/server";
import { format } from "date-fns";
import { format, isToday, parseISO } from "date-fns";
import { describe, expect, it } from "vitest";

import { getRestaurantId } from "@zotmeal/utils";
Expand Down Expand Up @@ -54,7 +54,7 @@ describe("menu.get", () => {
restaurant: "brandywine",
});
expect(menu).toBeTruthy();
// expect(isToday(menu.date)).toBeTruthy(); // TODO: re-integrate once getMenu is fixed
expect(isToday(parseISO(menu.date))).toBeTruthy();
expect(menu.restaurantId).toEqual(getRestaurantId("brandywine"));
}, 10_000);

Expand Down
26 changes: 25 additions & 1 deletion packages/api/src/menus/services/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export async function getMenu(
}

const { restaurant } = parsedParams.data;
const requestedDate = parsedParams.data.date;
const requestedPeriod = parsedParams.data.period;

// Attempt to find the restaurant
const fetchedRestaurant = await db.query.RestaurantTable.findFirst({
where: ({ name }, { eq }) => eq(name, restaurant),
});
Expand All @@ -48,7 +51,29 @@ export async function getMenu(
});
}

const requested_restaurant_id = fetchedRestaurant.id

// Attempt to find the menu
const fetchedMenu = await db.query.MenuTable.findFirst({
where: ({ date, period, restaurantId }, { eq, and }) =>
and(eq(date, requestedDate),
eq(period, requestedPeriod),
eq(restaurantId, requested_restaurant_id)),
});

if (!fetchedMenu) {
throw new TRPCError({
message: `menu (${restaurant}, ${requestedPeriod}, ${requestedDate}) not found`,
code: "NOT_FOUND",
});
}

const requestedMenuId = fetchedMenu.id

// Compile stations and dishes for the menu

const rows = await db.query.DishMenuStationJointTable.findMany({
where: ({menuId}, {eq}) => eq(menuId, requestedMenuId),
with: {
dish: {
with: {
Expand All @@ -59,7 +84,6 @@ export async function getMenu(
menu: true,
station: true,
},
limit: 5, // TODO: do findFirst with where clause instead
});

let menuResult: MenuWithRelations | null = null;
Expand Down

0 comments on commit dcc6f11

Please sign in to comment.