// Shared loading for the app proxy pages. Public: never depends on a logged-in customer (brief ยง8). import db from "../db.server"; import { offerInclude } from "./offers.server"; import { compile } from "./compile"; import { planOf } from "./plans"; export const PROXY_CACHE = "public, max-age=60"; export async function runningOffersForShop(domain: string, country: string | null) { const shop = await db.shop.findUnique({ where: { domain } }); if (!shop || shop.uninstalledAt) return null; const offers = await db.offer.findMany({ where: { shopId: shop.id, active: true, published: true }, include: offerInclude }); // Same selection as the discount function: running + allowed by the plan. const result = compile( { planCode: shop.planCode, currency: shop.currency, timezone: shop.timezone, settings: shop.settings, monthOrders: 0 }, offers, ); const included = new Set(result.included); const running = offers .filter((o) => included.has(o.id)) .filter((o) => !o.countries.length || (country && o.countries.includes(country))) .sort((a, b) => result.included.indexOf(a.id) - result.included.indexOf(b.id)); return { shop, running, ctx: { currency: shop.currency, timezone: shop.timezone, branding: planOf(shop.planCode).branding }, }; }