app/api/workflow/route.ts
import { serve } from "@upstash/workflow/nextjs";
import { checkInventory, brewCoffee, printReceipt } from "@/utils";
export const { POST } = serve(async (ctx) => {
const [coffeeBeansAvailable, cupsAvailable, milkAvailable] =
await Promise.all([
ctx.run("check-coffee-beans", () => checkInventory("coffee-beans")),
ctx.run("check-cups", () => checkInventory("cups")),
ctx.run("check-milk", () => checkInventory("milk")),
]);
// If all ingedients available, brew coffee
if (coffeeBeansAvailable && cupsAvailable && milkAvailable) {
const price = await ctx.run("brew-coffee", async () => {
return await brewCoffee({ style: "cappuccino" });
});
await printReceipt(price);
}
});