freshyo/packages/shared/order-glimpse.ts
2026-10-20 19:15:58 +05:30

57 lines
1.6 KiB
TypeScript

/**
* Visibility window for the "Upcoming Order" glimpse.
*
* The glimpse is a short call-to-action on Home/Me, not order history. Show an
* order while its delivery is still upcoming, or for 24 hours after the
* delivery time. Older orders remain available in My Orders.
*/
export const NEXT_ORDER_GLIMPSE_LOOKBACK_HOURS = 24
export const FLASH_DELIVERY_MINUTES = 30
const HOUR_MS = 60 * 60 * 1000
const MINUTE_MS = 60 * 1000
export interface NextOrderGlimpseOrderLike {
isFlashDelivery: boolean
deliveryDate?: string | null
createdAt: string
}
/**
* The moment the order is promised for. Prefers the slot delivery date; a
* flash order without a slot uses the 30-minute promise shown on the card.
*/
function deliveryMoment(order: NextOrderGlimpseOrderLike): Date | null {
if (order.deliveryDate) {
const date = new Date(order.deliveryDate)
if (!Number.isNaN(date.getTime())) return date
}
if (order.isFlashDelivery) {
const created = new Date(order.createdAt)
if (!Number.isNaN(created.getTime())) {
return new Date(created.getTime() + FLASH_DELIVERY_MINUTES * MINUTE_MS)
}
}
return null
}
/**
* True when the order should appear in the next-order glimpse:
* any future delivery, or one that happened within the lookback window.
* Returns false when the delivery moment cannot be determined.
*/
export function isInNextOrderGlimpseWindow(
order: NextOrderGlimpseOrderLike,
now: Date = new Date()
): boolean {
const deliveryAt = deliveryMoment(order)
if (!deliveryAt) return false
return (
deliveryAt.getTime() >=
now.getTime() - NEXT_ORDER_GLIMPSE_LOOKBACK_HOURS * HOUR_MS
)
}