@davoodmehraban89 / VELYQOR / commits / 6507bfc
Fork 0

Intelligence, Advanced Charting & Trading Platform

feat(risk): pure bounded-retry delivery outbox states and explicit UI failure messages

davoodmehraban89 committed Oct 21, 2026 at 08:53 UTC 6507bfc349affec42f1950a027b6564e1ca49cc9
1 file changed +60
packages/market-domain/src/notification-outbox.mjs new
+60
@@ -0,0 +1,60 @@
1 +/**
2 + * Notification outbox state transitions; transport injected, never imported.
3 + * Caller MUST persist via an atomic compare-and-swap store for cross-worker
4 + * idempotency. Pure logic cannot promise exactly-once network delivery.
5 + */
6 +const allowed=new Set(['PENDING','LEASED','SENT','FAILED','ACKNOWLEDGED']);
7 +export function notificationRecord({id,recipientChannel,eventId,createdAt,maxAttempts=3}){
8 + if(typeof id!=='string'||!id||typeof eventId!=='string'||!eventId||
9 + !['BALE','PWA','EITA'].includes(recipientChannel)||!Number.isInteger(maxAttempts)||
10 + maxAttempts<1||maxAttempts>10||!Number.isFinite(Date.parse(createdAt)))
11 + throw new RangeError('invalid message metadata');
12 + return {id,eventId,recipientChannel,status:'PENDING',createdAt,
13 + attempts:0,maxAttempts,leaseUntil:null,providerMessageId:null,deliveredAt:null,
14 + failedAt:null,nextRetryAt:null,reason:null};
15 +}
16 +export function claimDelivery(record,{now,leaseMs=30000}){
17 + if(!allowed.has(record?.status)||!Number.isFinite(Date.parse(now))||
18 + !Number.isSafeInteger(leaseMs)||leaseMs<=0)throw new RangeError('invalid claim');
19 + if(record.status==='SENT'||record.status==='ACKNOWLEDGED'||record.attempts>=record.maxAttempts)
20 + return {record,claimed:false};
21 + const at=Date.parse(now);
22 + if(record.status==='LEASED'&&Date.parse(record.leaseUntil)>at)
23 + return {record,claimed:false};
24 + if(record.nextRetryAt&&Date.parse(record.nextRetryAt)>at)
25 + return {record,claimed:false};
26 + const updated={...record,status:'LEASED',attempts:record.attempts+1,
27 + leaseUntil:new Date(at+leaseMs).toISOString(),nextRetryAt:null};
28 + return {record:updated,claimed:true};
29 +}
30 +export function recordDeliveryOutcome(record,{at,success,providerMessageId=null,retryDelayMs=60000}){
31 + if(record?.status!=='LEASED'||!Number.isFinite(Date.parse(at))||
32 + typeof success!=='boolean'||!Number.isSafeInteger(retryDelayMs)||retryDelayMs<1000)
33 + throw new RangeError('invalid completion');
34 + if(success){
35 + if(typeof providerMessageId!=='string'||!providerMessageId)
36 + throw new RangeError('verified provider message ID required');
37 + return {...record,status:'SENT',leaseUntil:null,providerMessageId,
38 + deliveredAt:at,reason:null};
39 + }
40 + const exhausted=record.attempts>=record.maxAttempts;
41 + return {...record,status:'FAILED',leaseUntil:null,failedAt:at,
42 + nextRetryAt:exhausted?null:new Date(Date.parse(at)+retryDelayMs).toISOString(),
43 + reason:exhausted?'ATTEMPTS_EXHAUSTED':'RETRY_PENDING'};
44 +}
45 +export function acknowledgeDelivery(record,{at}){
46 + if(record?.status!=='SENT'||!Number.isFinite(Date.parse(at))||
47 + Date.parse(at)<Date.parse(record.deliveredAt))
48 + throw new RangeError('only sent messages may be acknowledged');
49 + return {...record,status:'ACKNOWLEDGED',acknowledgedAt:at};
50 +}
51 +export function notificationUiStatus(record){
52 + switch(record?.status){
53 + case 'PENDING':return 'در صف ارسال';
54 + case 'LEASED':return 'در حال تلاش برای ارسال';
55 + case 'SENT':return 'ارسال توسط ارائه‌دهنده پذیرفته شد؛ مشاهده کاربر تأییدنشده';
56 + case 'ACKNOWLEDGED':return 'دریافت توسط کاربر تأیید شد';
57 + case 'FAILED':return record.reason==='ATTEMPTS_EXHAUSTED'?'ارسال ناموفق؛ تلاش مجدد متوقف شد':'ارسال ناموفق؛ تلاش مجدد زمان‌بندی شده';
58 + default:return 'وضعیت اعلان تأییدنشده';
59 + }
60 +}