Skip to content

Commit

Permalink
update schema based on api change (caloriesfromfat was removed)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlustre committed Nov 13, 2024
1 parent 2340e92 commit 87dc1ac
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
"@zotmeal/eslint-config": "workspace:*",
"dotenv-cli": "catalog:"
}
}
}
2 changes: 1 addition & 1 deletion apps/server/src/functions/cron/daily.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const main = async (_event, _context) => {
// log errors if any
results.forEach((result) => {
if (result.status === "rejected")
logger.error("daily() failed:", result.reason);
logger.error(result.reason, "daily() failed:");
});
} catch (error) {
logger.error(error, "Failed to execute daily task");
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
POSTGRES_USER: admin
POSTGRES_PASSWORD: admin
POSTGRES_DB: zotmeal
# TODO: volume is out of date, rn just do docker-compuse up and db:push
# TODO: volume is out of date, rn just do docker-compose up and db:push
# volumes:
# - ./docker/db/zotmeal.sql:/docker-entrypoint-initdb.d/init.sql
# - db_data:/var/lib/postgresql/data
Expand Down
4 changes: 1 addition & 3 deletions packages/api/src/globalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import { logger } from "./logger";

let teardownHappened = false;

/**
* Push schema to test container.
*/
/** Push schema to test container. */
async function pushSchema(connectionString: string) {
logger.info(`Pushing schema to test container (${process.env.TEST_URL})...`);
await promisify(exec)(`pnpm drizzle-kit push --config=../db/test-config.ts`, {
Expand Down
18 changes: 17 additions & 1 deletion packages/api/src/server/daily/daily.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { apiTest } from "@api/apiTest";
import { logger } from "@api/logger";
import { describe } from "vitest";

import { daily } from ".";

describe("daily", () => {
apiTest.todo("should populate db with daily menu");
apiTest(
"should populate db with daily menu",
async ({ expect, db }) => {
await expect(
db.transaction(async (trx) => {
await daily(trx, new Date(), "brandywine");
const menus = await trx.query.menus.findMany();
expect(menus.length).toBeGreaterThan(0);
trx.rollback();
}),
).rejects.toThrowError("Rollback");
},
{ timeout: 60_000 },
);
});
17 changes: 15 additions & 2 deletions packages/api/src/server/daily/parse.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { writeFileSync } from "fs";
import { upsertDish, upsertDishToMenu } from "@api/dishes/services";
import { logger } from "@api/logger";
import { upsertMenu } from "@api/menus/services";
Expand Down Expand Up @@ -28,6 +29,11 @@ export async function getCampusDishMenu(
},
},
);
if (process.env.IS_LOCAL) {
const outPath = `./${restaurantName}-${format(date, "MM-dd-yyyy")}-${periodId}-res.json`;
writeFileSync(outPath, JSON.stringify(res.data), { flag: "w" });
logger.info(`Wrote CampusDish response to ${outPath}.`);
}
return CampusDishMenuSchema.parse(res.data);
}

Expand All @@ -39,9 +45,17 @@ export async function upsertMenusForDate(
): Promise<void> {
const restaurantId = getRestaurantId(restaurantName);

// Get the menu for the given date to first get all the periods and stations
// Get the menu for the given date to first get all the periods and stations.
const menuAtDate = await getCampusDishMenu(date, restaurantName).catch(
(e) => {
logger.error(`❌ Failed to parse CampusDish menu for ${restaurantName}.`);
if (process.env.IS_LOCAL) {
const outPath = `./${restaurantName}-${format(date, "MM-dd-yyyy")}-error.json`;
writeFileSync(outPath, JSON.stringify(e), { flag: "w" });
throw new Error(
`Failed to parse CampusDish menu. Wrote validation error to ${outPath}.`,
);
}
throw e;
},
);
Expand Down Expand Up @@ -133,7 +147,6 @@ export async function upsertMenusForDate(
servingSize: menuProduct.Product.ServingSize,
servingUnit: menuProduct.Product.ServingUnit,
calories: menuProduct.Product.Calories,
caloriesFromFat: menuProduct.Product.CaloriesFromFat,
totalFatG: menuProduct.Product.TotalFat,
transFatG: menuProduct.Product.TransFat,
cholesterolMg: menuProduct.Product.Cholesterol,
Expand Down
1 change: 0 additions & 1 deletion packages/api/src/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ const dish = {
servingSize: "350",
servingUnit: "grams",
calories: "560",
caloriesFromFat: "190",
totalFatG: "21g",
transFatG: "0g",
saturatedFatG: "4g",
Expand Down
1 change: 0 additions & 1 deletion packages/db/src/schema/nutritionInfos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const nutritionInfos = pgTable("nutrition_infos", {
servingSize: text("serving_size"),
servingUnit: text("serving_unit"),
calories: text("calories"),
caloriesFromFat: text("calories_from_fat"),
totalFatG: text("total_fat_g"),
transFatG: text("trans_fat_g"),
saturatedFatG: text("saturated_fat_g"),
Expand Down
1 change: 0 additions & 1 deletion packages/validators/src/campusdish/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const MenuProductSchema = z.object({
ServingSize: z.string().nullable(),
ServingUnit: z.string().nullable(),
Calories: z.string().nullable(),
CaloriesFromFat: z.string().nullable(),
TotalFat: z.string().nullable(),
TransFat: z.string().nullable(),
Cholesterol: z.string().nullable(),
Expand Down

0 comments on commit 87dc1ac

Please sign in to comment.