All files / dal/whatsapp status-rank.ts

100% Statements 3/3
100% Branches 2/2
100% Functions 2/2
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23              14x               12x         5x    
import type { AnyColumn } from "drizzle-orm";
import { type SQL, sql } from "drizzle-orm";
 
/**
 * Meta delivers sent / delivered / read status webhooks with no ordering
 * guarantee. Status may only move forward; `failed` is terminal.
 */
const RANK: Record<string, number> = {
	sent: 1,
	delivered: 2,
	read: 3,
	failed: 4,
};
 
export function waStatusRank(status: string): number {
	return RANK[status] ?? 0;
}
 
/** SQL predicate: the row's current status ranks below `status`. */
export function statusBelow(column: AnyColumn, status: string): SQL {
	return sql`CASE ${column} WHEN 'sent' THEN 1 WHEN 'delivered' THEN 2 WHEN 'read' THEN 3 WHEN 'failed' THEN 4 ELSE 0 END < ${waStatusRank(status)}`;
}