import { and, eq, gt, inArray, isNull, sql } from 'drizzle-orm' import { db } from '../db/db_index' import { orderItems, orders, productSkus, type OrderItemSnapshot } from '../db/schema' import { nullableDate } from './date' const IST_OFFSET_MS = 5.5 * 60 * 60 * 1000 const DAY_MS = 24 * 60 * 60 * 1000 /** * Start (UTC) of the IST calendar day two days before `now`. Orders created * before this instant are archived: today, yesterday and the day before stay. */ export function istCalendarCutoffUtc(now: Date = new Date()): Date { const ist = new Date(now.getTime() + IST_OFFSET_MS) const startOfIstDayUtc = Date.UTC(ist.getUTCFullYear(), ist.getUTCMonth(), ist.getUTCDate()) - IST_OFFSET_MS return new Date(startOfIstDayUtc - 2 * DAY_MS) } export interface ArchiveSkip { orderId: number reason: string } export interface ArchiveSummary { scanned: number archived: number skipped: ArchiveSkip[] } export interface ArchiveStore { loadSource(orderIds: number[]): Promise<{ rows: ArchiveSourceRow[]; skuById: Map }> applyArchive(orderId: number, snapshot: OrderItemSnapshot[], verifiedIds: number[]): Promise listEligibleOrderIds(cutoff: Date, cursor: number, limit: number): Promise } export type ArchiveSourceRow = { id: number orderId: number skuId: number quantity: string price: string discountedPrice: string | null is_packaged: boolean is_package_verified: boolean packagedAt: Date | null } export type ArchiveSkuRow = { id: number name: string | null images: string[] | null isDeleted: boolean product: { name: string } | null features: { featureName: string | null; featureValue: string }[] } function isFiniteNumberString(value: string, options: { min: number }): boolean { if (value.trim() !== value || value === '') return false const parsed = Number(value) return Number.isFinite(parsed) && parsed >= options.min } export function validateSnapshot( source: ArchiveSourceRow[], snapshot: OrderItemSnapshot[] ): string | null { if (source.length === 0) return 'order has no items' if (!Array.isArray(snapshot) || snapshot.length === 0) return 'snapshot is empty' if (snapshot.length !== source.length) { return `snapshot length ${snapshot.length} != source ${source.length}` } const sourceIds = new Set(source.map((row) => row.id)) const seen = new Set() for (const line of snapshot) { if (!Number.isInteger(line.id) || line.id <= 0) return 'invalid item id' if (seen.has(line.id)) return `duplicate item id ${line.id}` seen.add(line.id) if (!sourceIds.has(line.id)) return `unknown item id ${line.id}` if (!Number.isInteger(line.skuId) || line.skuId <= 0) return `invalid skuId for item ${line.id}` if (typeof line.productName !== 'string' || line.productName.trim() === '') { return `missing productName for item ${line.id}` } if (typeof line.quantity !== 'string' || !isFiniteNumberString(line.quantity, { min: Number.MIN_VALUE })) { return `invalid quantity for item ${line.id}` } if (typeof line.price !== 'string' || !isFiniteNumberString(line.price, { min: 0 })) { return `invalid price for item ${line.id}` } if ( line.discountedPrice !== null && (typeof line.discountedPrice !== 'string' || !isFiniteNumberString(line.discountedPrice, { min: 0 })) ) { return `invalid discountedPrice for item ${line.id}` } if (typeof line.isPackaged !== 'boolean') return `invalid isPackaged for item ${line.id}` if (typeof line.isPackageVerified !== 'boolean') return `invalid isPackageVerified for item ${line.id}` if (line.packagedAt !== null) { if (typeof line.packagedAt !== 'string' || Number.isNaN(new Date(line.packagedAt).getTime())) { return `invalid packagedAt for item ${line.id}` } } if (line.image !== null && typeof line.image !== 'string') return `invalid image for item ${line.id}` if (!Array.isArray(line.features)) return `invalid features for item ${line.id}` for (const feature of line.features) { if (!feature || typeof feature.featureValue !== 'string') { return `invalid feature for item ${line.id}` } if (feature.featureName !== null && typeof feature.featureName !== 'string') { return `invalid feature name for item ${line.id}` } } } if (seen.size !== sourceIds.size) return 'snapshot is missing source ids' return null } export function buildSnapshot( rows: ArchiveSourceRow[], skuById: Map ): { snapshot?: OrderItemSnapshot[]; reason?: string } { const snapshot: OrderItemSnapshot[] = [] for (const row of rows) { const sku = skuById.get(row.skuId) if (!sku || !sku.product) return { reason: `missing catalogue data for item ${row.id}` } snapshot.push({ id: row.id, skuId: row.skuId, quantity: row.quantity, price: row.price, discountedPrice: row.discountedPrice, isPackaged: row.is_packaged, isPackageVerified: row.is_package_verified, packagedAt: nullableDate(row.packagedAt)?.toISOString() ?? null, productName: sku.product.name, skuName: sku.name, image: Array.isArray(sku.images) && sku.images.length > 0 ? sku.images[0] : null, features: (sku.features ?? []).map((feature) => ({ featureName: feature.featureName, featureValue: feature.featureValue, })), }) } return { snapshot } } function chunks(values: T[], size: number): T[][] { const out: T[][] = [] for (let index = 0; index < values.length; index += size) { out.push(values.slice(index, index + size)) } return out } async function loadSource(orderIds: number[]): Promise<{ rows: ArchiveSourceRow[]; skuById: Map }> { const rows: ArchiveSourceRow[] = [] for (const chunk of chunks(orderIds, 50)) { const chunkRows = (await db .select() .from(orderItems) .where(inArray(orderItems.orderId, chunk))) as unknown as ArchiveSourceRow[] rows.push(...chunkRows) } const skuIds = [...new Set(rows.map((row) => row.skuId))] const skuById = new Map() const skus: ArchiveSkuRow[] = [] for (const chunk of chunks(skuIds, 50)) { const chunkSkus = (await db.query.productSkus.findMany({ where: inArray(productSkus.id, chunk), with: { product: { columns: { name: true } }, features: true, }, })) as unknown as ArchiveSkuRow[] skus.push(...chunkSkus) } for (const sku of skus) skuById.set(sku.id, sku) return { rows, skuById } } async function applyArchiveToDb( orderId: number, snapshot: OrderItemSnapshot[], verifiedIds: number[] ): Promise { const update = db .update(orders) .set({ itemsJson: snapshot, itemsArchivedAt: new Date() }) .where( and( eq(orders.id, orderId), isNull(orders.itemsJson), sql`(SELECT COUNT(*) FROM order_items WHERE order_id = ${orderId}) = ${verifiedIds.length}`, sql`NOT EXISTS (SELECT 1 FROM order_items WHERE order_id = ${orderId} AND id NOT IN (${sql.join( verifiedIds.map((id) => sql`${id}`), sql`, ` )}))` ) ) const remove = db .delete(orderItems) .where( and( eq(orderItems.orderId, orderId), sql`EXISTS (SELECT 1 FROM orders WHERE id = ${orderId} AND items_json IS NOT NULL)` ) ) await db.batch([update, remove]) const [archivedOrder] = await db .select({ itemsJson: orders.itemsJson }) .from(orders) .where(eq(orders.id, orderId)) .limit(1) return archivedOrder?.itemsJson != null } async function listEligibleOrderIds(cutoff: Date, cursor: number, limit: number): Promise { const rows = await db .select({ id: orders.id }) .from(orders) .where( and( isNull(orders.itemsJson), gt(orders.id, cursor), sql`datetime(${orders.createdAt}) < datetime(${cutoff.toISOString()})` ) ) .orderBy(orders.id) .limit(limit) return rows.map((row) => row.id) } const defaultArchiveStore: ArchiveStore = { loadSource, applyArchive: applyArchiveToDb, listEligibleOrderIds, } /** * Archives the given orders. Each order is validated first; only valid orders * are written (guarded `db.batch(update + delete)`, atomic on D1). Invalid orders * keep their `order_items` rows and are reported in `skipped`. */ export async function archiveOrderItems( orderIds: number[], store: ArchiveStore = defaultArchiveStore ): Promise { const ids = [...new Set(orderIds.filter((id) => Number.isFinite(id)))] const summary: ArchiveSummary = { scanned: ids.length, archived: 0, skipped: [] } if (ids.length === 0) return summary const { rows, skuById } = await store.loadSource(ids) const rowsByOrder = new Map() for (const row of rows) { const list = rowsByOrder.get(row.orderId) ?? [] list.push(row) rowsByOrder.set(row.orderId, list) } for (const orderId of ids) { const orderRows = rowsByOrder.get(orderId) ?? [] const built = buildSnapshot(orderRows, skuById) if (!built.snapshot) { summary.skipped.push({ orderId, reason: built.reason ?? 'invalid snapshot' }) continue } const validationError = validateSnapshot(orderRows, built.snapshot) if (validationError) { summary.skipped.push({ orderId, reason: validationError }) continue } const verifiedIds = orderRows.map((row) => row.id) const applied = await store.applyArchive(orderId, built.snapshot, verifiedIds) if (applied) { summary.archived += 1 } else { summary.skipped.push({ orderId, reason: 'source changed during archive' }) } } return summary } export interface ArchiveRunOptions { now?: Date batchSize?: number maxBatches?: number } /** Runs the archive over every unarchived order older than the IST cutoff. */ export async function runOrderItemsArchive( options: ArchiveRunOptions = {}, store: ArchiveStore = defaultArchiveStore ): Promise { const now = options.now ?? new Date() const batchSize = options.batchSize ?? 100 const maxBatches = options.maxBatches ?? 20 const cutoff = istCalendarCutoffUtc(now) const summary: ArchiveSummary = { scanned: 0, archived: 0, skipped: [] } let cursor = 0 for (let batch = 0; batch < maxBatches; batch += 1) { const ids = await store.listEligibleOrderIds(cutoff, cursor, batchSize) if (ids.length === 0) break cursor = ids[ids.length - 1] const result = await archiveOrderItems(ids, store) summary.scanned += result.scanned summary.archived += result.archived summary.skipped.push(...result.skipped) if (ids.length < batchSize) break } console.log( `[order-items-archive] scanned=${summary.scanned} archived=${summary.archived} skipped=${summary.skipped.length}` ) for (const skip of summary.skipped) { console.log(`[order-items-archive] skipped order ${skip.orderId}: ${skip.reason}`) } return summary }