Files
photoreport/packages/contracts/src/bundles.ts
spark 69df3a1fd6
ci / rust (push) Successful in 7m1s
ci / typescript (push) Failing after 9m18s
desktop-release / build (deb,appimage, linux-x86_64, ubuntu-latest) (push) Failing after 28m39s
Photo packages: N photos or the whole gallery for one price, applied in the cart by itself
Vendas → Produtos → Pacotes; contracts applyBundles prices the cart on the site and the order in the API the same way.

Claude-Session: https://claude.ai/code/session_01HT6iXLYUUp9pJz5Kza9xCW
2026-10-13 05:32:12 +01:00

105 lines
4.1 KiB
TypeScript

import { z } from "zod";
/**
* Photo packages (the old storefront's "pacotes"): N photos, or the whole gallery, as one product for one price.
* The buyer just fills the cart; the cheapest mix of single photos and packages is worked out here, by the site
* (to show the price) and by the API (to charge it), so both always agree.
*/
export const BundleFields = z.object({
name: z.string().trim().min(1).max(80),
/** Null: offered in every gallery of the organization. */
galleryId: z.uuid().nullable().default(null),
/** The digital product (size) the photos come as. */
productId: z.uuid(),
/** Photos in the package; 0: the whole gallery. */
photoCount: z.number().int().min(0).max(1000),
priceCents: z.number().int().min(0).max(10_000_000),
active: z.boolean().default(true),
position: z.number().int().min(0).default(0),
});
/** A package has 2 or more photos, or 0 (the whole gallery). */
export const validBundleCount = (n: number) => n === 0 || n >= 2;
export const BundleInput = BundleFields.refine((b) => validBundleCount(b.photoCount), { message: "a package has 2 or more photos (0: the whole gallery)", path: ["photoCount"] });
export type BundleInput = z.infer<typeof BundleInput>;
export interface BundleSummary {
id: string;
name: string;
galleryId: string | null;
galleryTitle: string | null;
productId: string;
productName: string;
photoCount: number;
priceCents: number;
active: boolean;
position: number;
}
/** What a gallery page offers (active packages of active digital products). */
export interface BundleOffer {
id: string;
name: string;
productId: string;
photoCount: number;
priceCents: number;
}
export interface BundledLine {
unitCents: number;
bundle: BundleOffer | null;
}
/**
* Prices the lines with the packages: for each product, the digital lines (one photo each) are split into
* singles and packages at the lowest total; the whole-gallery package applies when every photo of the gallery
* (`galleryPhotos` of them) is in the cart. A package's price is spread over its photos (the cents left over go
* to the first ones), so each line still carries its own price. Lines keep their order.
*/
export function applyBundles<L extends { productId: string; quantity: number; unitCents: number }>(lines: L[], bundles: BundleOffer[], galleryPhotos: number): BundledLine[] {
const out: BundledLine[] = lines.map((l) => ({ unitCents: l.unitCents, bundle: null }));
const byProduct = new Map<string, number[]>();
lines.forEach((l, i) => {
if (l.quantity !== 1 || !bundles.some((b) => b.productId === l.productId)) return;
byProduct.set(l.productId, [...(byProduct.get(l.productId) ?? []), i]);
});
for (const [productId, idx] of byProduct) {
const k = idx.length;
const offers = bundles.filter((b) => b.productId === productId);
const counted = offers.filter((b) => b.photoCount >= 2 && b.photoCount <= k);
// cost[i]: cheapest price for the first i lines; pick[i]: the package that ends there (null: a single photo).
const cost: number[] = [0];
const pick: (BundleOffer | null)[] = [null];
for (let i = 1; i <= k; i++) {
cost[i] = cost[i - 1]! + lines[idx[i - 1]!]!.unitCents;
pick[i] = null;
for (const b of counted) {
if (b.photoCount <= i && cost[i - b.photoCount]! + b.priceCents < cost[i]!) {
cost[i] = cost[i - b.photoCount]! + b.priceCents;
pick[i] = b;
}
}
}
const whole = galleryPhotos > 0 && k >= galleryPhotos ? offers.filter((b) => b.photoCount === 0).sort((a, b) => a.priceCents - b.priceCents)[0] : undefined;
if (whole && whole.priceCents < cost[k]!) {
spread(out, idx, whole);
continue;
}
for (let i = k; i > 0; ) {
const b = pick[i];
if (!b) {
i -= 1;
continue;
}
spread(out, idx.slice(i - b.photoCount, i), b);
i -= b.photoCount;
}
}
return out;
}
function spread(out: BundledLine[], idx: number[], bundle: BundleOffer) {
const base = Math.floor(bundle.priceCents / idx.length);
const extra = bundle.priceCents - base * idx.length;
idx.forEach((i, n) => (out[i] = { unitCents: base + (n < extra ? 1 : 0), bundle }));
}