diff --git a/apps/expo/src/app/home/index.tsx b/apps/expo/src/app/home/index.tsx index 9f6a888f..ddb6730b 100644 --- a/apps/expo/src/app/home/index.tsx +++ b/apps/expo/src/app/home/index.tsx @@ -24,10 +24,12 @@ import { LinearGradient } from "tamagui/linear-gradient"; import type { MenuWithRelations } from "@zotmeal/db"; import type { PeriodName } from "@zotmeal/utils"; +import { restaurantIdEnum } from "@zotmeal/db"; import { getCurrentPeriodName, getRestaurantNameById, PeriodEnum, + restaurantIds, restaurantNames, } from "@zotmeal/utils"; @@ -84,8 +86,6 @@ export function EventToast() { } export function Home() { - // const hello = api.menu.hello.useQuery(); - const { anteateryMenu, brandywineMenu, setAnteateryMenu, setBrandywineMenu } = useMenuStore(); @@ -112,6 +112,8 @@ export function Home() { }), ), ); + console.log(anteateryQuery); + console.log(brandywineQuery); useEffect(() => { if (anteateryQuery?.data) { @@ -121,7 +123,29 @@ export function Home() { if (brandywineQuery?.data) { setBrandywineMenu(brandywineQuery.data); } - }, [anteateryQuery, brandywineQuery, setAnteateryMenu, setBrandywineMenu]); + + if ( + anteateryQuery && + brandywineQuery && + anteateryQuery.isSuccess && + brandywineQuery.isSuccess + ) { + toast.show("There are 5 upcoming events.", { + // message: 'See upcoming events', + duration: 10_000_000, + burntOptions: { + shouldDismissByDrag: true, + from: "bottom", + }, + }); + } + }, [ + anteateryQuery?.data, + brandywineQuery?.data, + setAnteateryMenu, + setBrandywineMenu, + toast, + ]); if (!anteateryQuery || !brandywineQuery) { return Fetching menus; @@ -142,15 +166,6 @@ export function Home() { ); } - toast.show("There are 5 upcoming events.", { - // message: 'See upcoming events', - duration: 10_000_000, - burntOptions: { - shouldDismissByDrag: true, - from: "bottom", - }, - }); - return ( @@ -229,11 +244,13 @@ const PeriodPicker = ({ color, }} selectedValue={periodName} - onValueChange={(itemValue, _) => setPeriodName(itemValue)} + onValueChange={(itemValue, _) => { + setPeriodName(itemValue); + }} > {/* Create a Picker.Item for each period */} {Object.entries(PeriodEnum).map(([period, id]) => ( - + ))} ); diff --git a/apps/expo/src/utils/api.tsx b/apps/expo/src/utils/api.tsx index 2ada14b4..972f7297 100644 --- a/apps/expo/src/utils/api.tsx +++ b/apps/expo/src/utils/api.tsx @@ -1,4 +1,5 @@ import { useState } from "react"; +import { Platform } from "react-native"; import Constants from "expo-constants"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { httpBatchLink, loggerLink } from "@trpc/client"; @@ -19,7 +20,7 @@ export { type RouterInputs, type RouterOutputs } from "@zotmeal/api"; */ const getBaseUrl = () => { /** - * Gets the IP address of your host-machine. If it cannot automatically find i t, + * Gets the IP address of your host-machine. If it cannot automatically find it, * you'll have to manually set it. NOTE: Port 3000 should work for most but confirm * you don't have anything else running on it, or you'd have to change it. * @@ -35,7 +36,12 @@ const getBaseUrl = () => { "Failed to get localhost. Please point to your production server.", ); } - return `http://${localhost}:3000`; + + if (Platform.OS == "android") { + return `http://10.0.2.2:3000`; + } + + return `http://localhost:3000`; }; /** @@ -49,7 +55,7 @@ export function TRPCProvider(props: { children: React.ReactNode }) { links: [ httpBatchLink({ transformer: superjson, - url: `${getBaseUrl()}/api/trpc`, + url: getBaseUrl(), headers() { const headers = new Map(); headers.set("x-trpc-source", "expo-react"); diff --git a/apps/server/package.json b/apps/server/package.json index 2794481a..c0b20c62 100644 --- a/apps/server/package.json +++ b/apps/server/package.json @@ -6,7 +6,7 @@ "scripts": { "clean": "rm -rf .turbo node_modules", "with-env": "dotenv -e ../../.env -- ", - "dev": "pnpm with-env sls offline --noPrependStageInUrl", + "dev": "pnpm with-env sls offline --noPrependStageInUrl --host 127.0.0.1", "deploy": "pnpm with-env AWS_PROFILE=icssc sls deploy --stage production", "test:daily": "pnpm with-env sls invoke local --function updateDaily | pino-pretty", "test:weekly": "pnpm with-env sls invoke local --function getWeekly | pino-pretty" diff --git a/apps/server/src/functions/trpc/handler.ts b/apps/server/src/functions/trpc/handler.ts index 3290ad03..893d7bd1 100644 --- a/apps/server/src/functions/trpc/handler.ts +++ b/apps/server/src/functions/trpc/handler.ts @@ -6,15 +6,14 @@ import { APIGatewayProxyEventV2 } from "aws-lambda"; import { appRouter, createTRPCContext } from "@zotmeal/api"; -const createContext = ({ - event, - context, -}: CreateAWSLambdaContextOptions) => { - const ctx = createTRPCContext({}); - return { ...ctx, event, context }; +const createContext = ( + _opts: CreateAWSLambdaContextOptions, +) => { + const headers = new Headers(); + headers.set("x-trpc-source", "aws-lambda"); + return createTRPCContext({ headers }); }; - -type Context = Awaited>; +// type Context = Awaited>; export const handler = awsLambdaRequestHandler({ router: appRouter, diff --git a/packages/api/src/menus/procedures/getMenu.test.ts b/packages/api/src/menus/procedures/getMenu.test.ts index ceb9e9a2..ef7b7381 100644 --- a/packages/api/src/menus/procedures/getMenu.test.ts +++ b/packages/api/src/menus/procedures/getMenu.test.ts @@ -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"; @@ -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); diff --git a/packages/api/src/menus/router.ts b/packages/api/src/menus/router.ts index 8448f522..b08d2ffa 100644 --- a/packages/api/src/menus/router.ts +++ b/packages/api/src/menus/router.ts @@ -1,14 +1,7 @@ -import axios from "axios"; - import { createTRPCRouter, publicProcedure } from "../trpc"; import { getMenuProcedure } from "./procedures/getMenu"; -const helloProcedure = publicProcedure.query(async (opts) => { - const res = await axios.get("https://jsonplaceholder.typicode.com/todos/1"); - const _ = opts; - console.log(res.data); - return "menu -> hello"; -}); +const helloProcedure = publicProcedure.query(() => "menu -> hello"); export const menuRouter = createTRPCRouter({ get: getMenuProcedure, diff --git a/packages/api/src/menus/services/menu.ts b/packages/api/src/menus/services/menu.ts index 208dc3f7..c0b87b24 100644 --- a/packages/api/src/menus/services/menu.ts +++ b/packages/api/src/menus/services/menu.ts @@ -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), }); @@ -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: { @@ -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; diff --git a/packages/api/src/root.ts b/packages/api/src/root.ts index 5e4b2bb9..12b57834 100644 --- a/packages/api/src/root.ts +++ b/packages/api/src/root.ts @@ -2,13 +2,14 @@ import { eventRouter } from "./events/router"; import { menuRouter } from "./menus"; import { notificationRouter } from "./notifications/router"; import { scheduleRouter } from "./schedules/router"; -import { createTRPCRouter } from "./trpc"; +import { createTRPCRouter, publicProcedure } from "./trpc"; export const appRouter = createTRPCRouter({ menu: menuRouter, event: eventRouter, notification: notificationRouter, schedule: scheduleRouter, + hello: publicProcedure.query(() => "Hello, world!"), }); // export type definition of API diff --git a/packages/api/src/trpc.ts b/packages/api/src/trpc.ts index e1980289..53764b4e 100644 --- a/packages/api/src/trpc.ts +++ b/packages/api/src/trpc.ts @@ -10,7 +10,7 @@ // import type { Session } from "@zotmeal/auth"; // import { auth } from "@zotmeal/auth"; -import { Expo } from "expo-server-sdk"; +// import { Expo } from "expo-server-sdk"; import { initTRPC } from "@trpc/server"; import superjson from "superjson"; import { ZodError } from "zod"; @@ -30,24 +30,18 @@ import { createDrizzle } from "@zotmeal/db"; * @see https://trpc.io/docs/server/context */ -const expo: Expo = new Expo({ - accessToken: process.env.EXPO_ACCESS_TOKEN, -}); +// const expo: Expo = new Expo({ +// accessToken: process.env.EXPO_ACCESS_TOKEN, +// }); const db = createDrizzle({ connectionString: process.env.DATABASE_URL }); -export const createTRPCContext = (opts: { - headers: Headers; - // session: Session | null; -}) => { +export const createTRPCContext = (opts: { headers: Headers }) => { const source = opts.headers.get("x-trpc-source") ?? "unknown"; - console.log(">>> tRPC Request from", source, "by", "someone"); - return { - ...opts, - db, - expo, - }; + console.log(">>> tRPC Request from", source); + + return { db }; }; /**