HyperFlow / extension /src /analyze.ts
Gaurav711's picture
Deploy 891f179
2b86c60 verified
Raw History Blame Contribute Delete
3.08 kB
// Turns what the user has seen into a report: fee findings for the open cart and the cheapest split.
import type { Highs } from 'highs';
import { findMatches, type Match } from './match';
import { lineKey, type OkCart, type SeenPrice, type Store } from './model';
import { solveSplit, type SplitItem, type SplitResult, type StoreFees } from './split';
import { xray, type Finding } from './xray';
export interface Report {
store: Store;
findings: Finding[];
matches: { line: string; matches: Match[] }[];
split: SplitResult | null;
/** Assumptions the numbers depend on, shown with the result. */
assumptions: string[];
}
export const ASSUMPTIONS = [
'Other stores are compared only for products you viewed there in the last 24 hours, at your current delivery location.',
"Each store's fees are the ones its own cart page last showed, treated as fixed per order.",
'Purchase limits on other stores are not visible, so the quantity in this cart is assumed available.',
];
/** Conservative fee model from a cart page: observed fees apply to every order, no waivers assumed. */
export function storeFeesFrom(cart: OkCart): StoreFees {
const sum = (kinds: string[]) => cart.fees.filter((f) => kinds.includes(f.kind)).reduce((s, f) => s + f.amountMinor, 0);
return { deliveryFeeMinor: sum(['delivery', 'surge']), freeDeliveryThresholdMinor: null,
handlingFeeMinor: sum(['handling', 'platform', 'small_cart']), smallCartFeeMinor: 0, smallCartThresholdMinor: null };
}
export function buildSplitInput(cart: OkCart, carts: Partial<Record<Store, OkCart>>, matchesByLine: Match[][]) {
const stores: Record<string, StoreFees> = { [cart.store]: storeFeesFrom(cart) };
const items: SplitItem[] = cart.lines.map((line, i) => {
const offers: SplitItem['offers'] = { [cart.store]: { priceMinor: line.priceMinor, available: true, purchaseLimit: line.quantity } };
for (const m of matchesByLine[i] ?? []) {
const other = carts[m.seen.store];
if (!m.confirmed || !other) continue; // unknown fees or an unconfirmed match never enter the split
stores[m.seen.store] = storeFeesFrom(other);
offers[m.seen.store] = { priceMinor: m.seen.priceMinor, available: m.seen.available, purchaseLimit: line.quantity };
}
return { id: `line-${i}`, quantity: line.quantity, offers };
});
return { items, stores };
}
export function analyze(highs: Highs, cart: OkCart, carts: Partial<Record<Store, OkCart>>, seen: SeenPrice[],
confirmations: ReadonlySet<string> = new Set()): Report {
const ownPrices = new Map(seen.filter((p) => p.store === cart.store).map((p) => [lineKey(p), p.priceMinor]));
const matchesByLine = cart.lines.map((line) => findMatches(line, seen, confirmations));
const { items, stores } = buildSplitInput(cart, carts, matchesByLine);
return {
store: cart.store,
findings: xray(cart, ownPrices),
matches: cart.lines.map((line, i) => ({ line: line.name, matches: matchesByLine[i]! })),
split: cart.lines.length ? solveSplit(highs, items, stores) : null,
assumptions: ASSUMPTIONS,
};
}