File size: 2,157 Bytes
2b86c60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Normalized cart model shared by parsers, analysis and the solver. Money is integer paise.

export type Store = 'instamart' | 'blinkit' | 'zepto';
export const STORES: readonly Store[] = ['instamart', 'blinkit', 'zepto'];
export type Unit = 'g' | 'ml' | 'count';

export interface CartLine {
  store: Store;
  name: string;
  brand: string | null;
  sizeValue: number;
  sizeUnit: Unit;
  quantity: number;
  priceMinor: number;
  mrpMinor: number | null;
}

export type FeeKind = 'delivery' | 'handling' | 'small_cart' | 'surge' | 'platform' | 'membership' | 'tip' | 'other';

export interface Fee {
  label: string;
  amountMinor: number;
  kind: FeeKind;
  preselected: boolean;
}

export type ParsedCart =
  | { status: 'ok'; store: Store; lines: CartLine[]; fees: Fee[]; payableMinor: number; capturedAt: string }
  | { status: 'unsupported_layout'; store: Store; reason: string };

export type OkCart = Extract<ParsedCart, { status: 'ok' }>;

/** Parse displayed rupees such as "₹1,234.50" or "Rs. 99" into paise; null when it is not a single amount. */
export function rupeesToMinor(text: string): number | null {
  const match = /^\s*(?:₹|rs\.?|inr)?\s*(\d{1,3}(?:,\d{2,3})*|\d+)(?:\.(\d{1,2}))?\s*$/i.exec(text);
  if (!match) return null;
  const rupees = Number(match[1]!.replace(/,/g, ''));
  const paise = Number((match[2] ?? '').padEnd(2, '0'));
  const value = rupees * 100 + paise;
  return Number.isSafeInteger(value) ? value : null;
}

/** Identity of one product on one store, used to compare the same product across pages. */
export function lineKey(line: Pick<CartLine, 'store' | 'name' | 'sizeValue' | 'sizeUnit'>): string {
  return `${line.store}|${line.name.trim().toLowerCase().replace(/\s+/g, ' ')}|${line.sizeValue}${line.sizeUnit}`;
}

/** A product price the user saw on a store page, kept locally for 24 hours. */
export interface SeenPrice {
  store: Store;
  name: string;
  brand: string | null;
  sizeValue: number;
  sizeUnit: Unit;
  priceMinor: number;
  available: boolean;
  /** SHA-256 of the store's displayed delivery area; the area text itself is never stored. */
  locationKey: string;
  seenAt: string;
}