Intelligence, Advanced Charting & Trading Platform
feat(notification): consume atomic permit and revalidate consent before a single Bale send
davoodmehraban89 committed
Oct 23, 2026 at 02:38 UTC
ca5b00d1f907a439cd90379d3d46bbdfb1888e00
1 file changed
+65
packages/market-domain/src/leased-bale-dispatch.mjs
new
+65
@@ -0,0 +1,65 @@
1
+/**
2
+ * Server-only notification egress coordinator.
3
+ * Consumes a DB-backed ONE-SEND permit, then rechecks consent and finally
4
+ * invokes the PR #33 Bale adapter through a trusted injected callback.
5
+ * No secrets persisted or logged; this module never self-schedules/retries.
6
+ *
7
+ * Depends on PR #34 outbox + migration 0006 begin_egress/egress_allowed.
8
+ * NOT PRODUCTION-AUTHORIZED: sender role and recipient ownership unverified.
9
+ */
10
+const uuid=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
11
+const beginSql='SELECT public.alert_notification_begin_egress($1::uuid,$2::uuid,$3::timestamptz) AS permitted';
12
+const egressSql='SELECT public.alert_notification_egress_allowed($1::uuid,$2::uuid,$3::timestamptz) AS permitted';
13
+const finishSql='SELECT public.notification_outbox_finish($1::uuid,$2::uuid,$3::text,$4::text,$5::timestamptz,60) AS outcome';
14
+async function permitted(db,sql,params){
15
+ const result=await db.query(sql,params);
16
+ if(!Array.isArray(result?.rows)||result.rows.length!==1||
17
+ typeof result.rows[0].permitted!=='boolean')
18
+ throw new Error('UNTRUSTED_DB_AUTHORIZATION_RESPONSE');
19
+ return result.rows[0].permitted;
20
+}
21
+async function finish(db,{outboxId,claimToken,decision,messageId,at}){
22
+ const r=await db.query(finishSql,[outboxId,claimToken,decision,messageId,at]);
23
+ if(!Array.isArray(r?.rows)||r.rows.length!==1||
24
+ typeof r.rows[0].outcome!=='string')
25
+ throw new Error('UNTRUSTED_DB_COMPLETION_RESPONSE');
26
+ return r.rows[0].outcome;
27
+}
28
+export async function dispatchLeasedBale({
29
+ db,baleOneMessage,outboxId,claimToken,token,chatId,text,at
30
+}={}){
31
+ if(typeof db?.query!=='function'||typeof baleOneMessage!=='function'||
32
+ !uuid.test(outboxId)||!uuid.test(claimToken)||
33
+ typeof at!=='string'||!Number.isFinite(Date.parse(at)))
34
+ throw new TypeError('invalid server dispatch prerequisites');
35
+ if(typeof token!=='string'||token.length<12||
36
+ typeof chatId!=='string'||!/^[1-9][0-9]{0,15}$/.test(chatId)||
37
+ typeof text!=='string'||text.length<1||text.length>1000)
38
+ throw new TypeError('invalid private dispatch data');
39
+ const params=[outboxId,claimToken,at];
40
+ // Linearization point: this SQL row UPDATE makes a second caller unable
41
+ // to obtain another send permit. No network call occurs before success.
42
+ if(!await permitted(db,beginSql,params))
43
+ return {status:'BLOCKED',reason:'SEND_PERMIT_NOT_AVAILABLE',sent:false};
44
+ // Check opt-in immediately before network. A revoked recipient is blocked.
45
+ if(!await permitted(db,egressSql,params)){
46
+ const state=await finish(db,{outboxId,claimToken,decision:'UNKNOWN',
47
+ messageId:null,at});
48
+ return {status:'BLOCKED',reason:'RECIPIENT_NOT_CURRENTLY_AUTHORIZED',
49
+ persistedOutcome:state,sent:false};
50
+ }
51
+ let outcome;
52
+ try{outcome=await baleOneMessage({token,chatId,text})}
53
+ catch{outcome={status:'UNKNOWN'}}
54
+ // Any ambiguous transport error is terminal UNKNOWN, not an auto retry.
55
+ const accepted=outcome?.status==='SENT'&&
56
+ typeof outcome.messageId==='string'&&outcome.messageId.length>0;
57
+ const persistedOutcome=await finish(db,{outboxId,claimToken,
58
+ decision:accepted?'SENT':'UNKNOWN',
59
+ messageId:accepted?outcome.messageId:null,at});
60
+ if(accepted&&persistedOutcome==='SENT')
61
+ return {status:'PROVIDER_ACCEPTED',messageId:outcome.messageId,
62
+ persistedOutcome,sent:true,readConfirmed:false};
63
+ return {status:'UNKNOWN',reason:'OUTBOUND_RESULT_UNVERIFIED_OR_NOT_PERSISTED',
64
+ persistedOutcome,sent:accepted,readConfirmed:false};
65
+}