@@ -1945,7 +1945,7 @@ describe('ApprovalService (node era)', () => {
19451945 const req = await svc . openNodeRequest (
19461946 openInput ( [ 'u9' , 'u2' ] , { } , { escalation : { timeoutHours : 1 , action : 'reassign' , escalateTo : 'boss' , notifySubmitter : false } } ) , CTX ,
19471947 ) ;
1948- staffDirectory ( engine , 'u7' ) ;
1948+ staffDirectory ( engine , 'u7' , 'boss' ) ;
19491949 await svc . reassign ( req . id , { actorId : 'u9' , to : 'u7' } , SYS ) ;
19501950 expect ( indexRows ( ) . map ( r => r . approver ) . sort ( ) ) . toEqual ( [ 'u2' , 'u7' ] ) ;
19511951
@@ -2084,6 +2084,7 @@ describe('ApprovalService (node era)', () => {
20842084 } ) ;
20852085
20862086 it ( 'runEscalations: reassign replaces the approver set with escalateTo' , async ( ) => {
2087+ staffDirectory ( engine , 'boss' ) ;
20872088 const req = await svc . openNodeRequest (
20882089 openInput ( [ 'u9' , 'u2' ] , { } , { escalation : { timeoutHours : 1 , action : 'reassign' , escalateTo : 'boss' , notifySubmitter : false } } ) , CTX ,
20892090 ) ;
@@ -2132,6 +2133,137 @@ describe('ApprovalService (node era)', () => {
21322133 expect ( emitted [ 0 ] . audience ) . toEqual ( [ 'u9' , 'u5' ] ) ;
21332134 } ) ;
21342135
2136+ // ── SLA reassign target resolution (#25598) ─────────────────────
2137+ //
2138+ // An escalation `reassign` whose `escalateTo` expands to no position holder
2139+ // falls back to the literal. A literal that resolves to no one used to be
2140+ // written onto the slate, stranding the request the way the reassign door
2141+ // did (#25485). Now the door's predicate judges it: the slate stays, its
2142+ // approvers are told, and the audit row says why.
2143+ describe ( 'SLA reassign target resolution (#25598)' , ( ) => {
2144+ let emitted : any [ ] ;
2145+
2146+ beforeEach ( ( ) => {
2147+ emitted = [ ] ;
2148+ svc . attachMessaging ( { async emit ( input ) { emitted . push ( input ) ; } } ) ;
2149+ } ) ;
2150+
2151+ /** Open an overdue `reassign` escalation to `escalateTo` on `slate`, the opening's notices cleared. */
2152+ async function overdueReassign ( escalateTo : string , recordId = 'opp1' ) : Promise < string > {
2153+ const { id } = ( await svc . openNodeRequest (
2154+ openInput ( [ 'u9' , 'u2' ] , { recordId, record : { id : recordId } } , { escalation : { timeoutHours : 1 , action : 'reassign' , escalateTo, notifySubmitter : true } } ) , CTX ,
2155+ ) ) as { id : string } ;
2156+ expect ( engine . _tables [ 'sys_approval_request' ] . find ( r => r . id === id ) ?. organization_id ) . toBe ( 't1' ) ;
2157+ makeOverdue ( id ) ;
2158+ emitted . splice ( 0 ) ;
2159+ return id ;
2160+ }
2161+
2162+ const requestRow = ( id : string ) => engine . _tables [ 'sys_approval_request' ] . find ( r => r . id === id ) ! ;
2163+ const indexedOf = ( id : string ) => ( engine . _tables [ 'sys_approval_approver' ] ?? [ ] )
2164+ . filter ( r => r . request_id === id ) . map ( r => r . approver ) . sort ( ) ;
2165+ const escalateRow = ( id : string ) => ( engine . _tables [ 'sys_approval_action' ] ?? [ ] )
2166+ . find ( r => r . request_id === id && r . action === 'escalate' ) ;
2167+
2168+ async function expectKeptAndNotified ( id : string , escalateTo : string , why : string ) {
2169+ const before = { ...requestRow ( id ) } ;
2170+ emitted . splice ( 0 ) ;
2171+ expect ( await svc . runEscalations ( ) ) . toMatchObject ( { escalated : 1 } ) ;
2172+ const row = requestRow ( id ) ;
2173+ expect ( row . status ) . toBe ( 'pending' ) ;
2174+ expect ( row . pending_approvers ) . toBe ( 'u9,u2' ) ;
2175+ expect ( row . pending_approver_labels ) . toEqual ( before . pending_approver_labels ) ;
2176+ expect ( indexedOf ( id ) ) . toEqual ( [ 'u2' , 'u9' ] ) ;
2177+ expect ( escalateRow ( id ) ?. comment ) . toBe (
2178+ `reassign → ${ escalateTo } — not applied: ${ why } The pending approvers were kept and notified.` ,
2179+ ) ;
2180+ // The current slate hears the lapse; the target that resolves to no one
2181+ // is not in the audience, and nothing announces a move.
2182+ expect ( emitted . map ( e => e . topic ) ) . toEqual ( [ 'approval.sla_breached' , 'approval.sla_breached' ] ) ;
2183+ expect ( emitted [ 0 ] . audience ) . toEqual ( [ 'u9' , 'u2' ] ) ;
2184+ expect ( emitted . some ( e => e . topic === 'approval.escalated' ) ) . toBe ( false ) ;
2185+ // The submitter is told the action that was TAKEN.
2186+ expect ( emitted [ 1 ] . audience ) . toEqual ( [ 'u1' ] ) ;
2187+ expect ( emitted [ 1 ] . payload . body ) . toContain ( "escalation action 'notify' was taken" ) ;
2188+ // Still single-shot: the escalate row is the marker.
2189+ expect ( await svc . runEscalations ( ) ) . toMatchObject ( { escalated : 0 } ) ;
2190+ }
2191+
2192+ it ( 'keeps the slate when escalateTo names no account (a departed user, a mistyped id)' , async ( ) => {
2193+ const id = await overdueReassign ( 'ghost_user' ) ;
2194+ await expectKeptAndNotified ( id , 'ghost_user' , 'it names no user in this request\'s organization.' ) ;
2195+ } ) ;
2196+
2197+ it ( 'keeps the slate when escalateTo is a position name nobody holds here' , async ( ) => {
2198+ engine . _tables [ 'sys_user_position' ] = [
2199+ { id : 'up1' , user_id : 'u5' , position : 'approvals_supervisor' , organization_id : 't2' } , // other tenant only
2200+ ] ;
2201+ const id = await overdueReassign ( 'approvals_supervisor' ) ;
2202+ await expectKeptAndNotified ( id , 'approvals_supervisor' , 'it names no user in this request\'s organization.' ) ;
2203+ } ) ;
2204+
2205+ it ( 'keeps the slate when escalateTo is a user whose only membership is another organization' , async ( ) => {
2206+ engine . _tables [ 'sys_user' ] = [ { id : 'u_far' } ] ;
2207+ engine . _tables [ 'sys_member' ] = [ { id : 'm1' , user_id : 'u_far' , organization_id : 't2' } ] ;
2208+ const id = await overdueReassign ( 'u_far' ) ;
2209+ await expectKeptAndNotified ( id , 'u_far' , 'it names no user in this request\'s organization.' ) ;
2210+ } ) ;
2211+
2212+ it ( 'keeps the slate when escalateTo is a slot literal no approver acts under, or a position:<p> nobody holds' , async ( ) => {
2213+ const team = await overdueReassign ( 'team:night_shift' ) ;
2214+ await expectKeptAndNotified ( team , 'team:night_shift' , 'it is a slot literal no approver acts under, so no one could ever decide the request.' ) ;
2215+ const ghost = await overdueReassign ( 'position:ghost' , 'opp2' ) ;
2216+ await expectKeptAndNotified ( ghost , 'position:ghost' , 'no one holds position \'ghost\' in this request\'s organization.' ) ;
2217+ } ) ;
2218+
2219+ it ( 'a failed sys_user read moves nothing and records nothing, so the next sweep escalates' , async ( ) => {
2220+ staffDirectory ( engine , 'boss' ) ;
2221+ const id = await overdueReassign ( 'boss' ) ;
2222+ const find = engine . find . bind ( engine ) ;
2223+ ( engine as any ) . find = async ( object : string , q : any ) => {
2224+ if ( object === 'sys_user' ) throw new Error ( 'directory down' ) ;
2225+ return find ( object , q ) ;
2226+ } ;
2227+ expect ( await svc . runEscalations ( ) ) . toMatchObject ( { escalated : 0 } ) ;
2228+ expect ( requestRow ( id ) . pending_approvers ) . toBe ( 'u9,u2' ) ;
2229+ expect ( escalateRow ( id ) ) . toBeUndefined ( ) ;
2230+ expect ( emitted ) . toEqual ( [ ] ) ;
2231+ ( engine as any ) . find = find ;
2232+ expect ( await svc . runEscalations ( ) ) . toMatchObject ( { escalated : 1 } ) ;
2233+ expect ( requestRow ( id ) . pending_approvers ) . toBe ( 'boss' ) ;
2234+ } ) ;
2235+
2236+ it ( 'still reassigns to an account\'s user id, its email, and a position:<p> someone holds here' , async ( ) => {
2237+ engine . _tables [ 'sys_user' ] = [ { id : 'boss' , email : 'boss@example.com' } ] ;
2238+ engine . _tables [ 'sys_member' ] = [ { id : 'm1' , user_id : 'boss' , organization_id : 't1' } ] ;
2239+ engine . _tables [ 'sys_user_position' ] = [
2240+ { id : 'up1' , user_id : 'u5' , position : 'finance' , organization_id : 't1' } ,
2241+ ] ;
2242+ const byId = await overdueReassign ( 'boss' ) ;
2243+ const byEmail = await overdueReassign ( 'boss@example.com' , 'opp2' ) ;
2244+ const byPosition = await overdueReassign ( 'position:finance' , 'opp3' ) ;
2245+ expect ( await svc . runEscalations ( ) ) . toMatchObject ( { escalated : 3 } ) ;
2246+ expect ( requestRow ( byId ) . pending_approvers ) . toBe ( 'boss' ) ;
2247+ expect ( requestRow ( byEmail ) . pending_approvers ) . toBe ( 'boss@example.com' ) ;
2248+ expect ( requestRow ( byPosition ) . pending_approvers ) . toBe ( 'position:finance' ) ;
2249+ expect ( indexedOf ( byPosition ) ) . toEqual ( [ 'position:finance' ] ) ;
2250+ for ( const [ id , to ] of [ [ byId , 'boss' ] , [ byEmail , 'boss@example.com' ] , [ byPosition , 'position:finance' ] ] ) {
2251+ expect ( escalateRow ( id ) ?. comment ) . toBe ( `reassign → ${ to } ` ) ;
2252+ }
2253+ // A `type:value` slot is no messaging address (`notify` drops it), so the
2254+ // position's hand-off announces itself to no one; that is today's notice.
2255+ expect ( emitted . filter ( e => e . topic === 'approval.escalated' ) . map ( e => e . audience ) )
2256+ . toEqual ( [ [ 'boss' ] , [ 'boss@example.com' ] ] ) ;
2257+ const submitterNotices = emitted . filter ( e => e . topic === 'approval.sla_breached' ) ;
2258+ expect ( submitterNotices ) . toHaveLength ( 3 ) ;
2259+ expect ( submitterNotices . every ( e => e . payload . body . includes ( "escalation action 'reassign' was taken" ) ) ) . toBe ( true ) ;
2260+ // …and the holder of the position then decides with the default actor.
2261+ await svc . decideNode ( byPosition , { decision : 'approve' , actorId : 'u5' } ,
2262+ { userId : 'u5' , tenantId : 't1' , positions : [ 'finance' ] , permissions : [ ] } as any ) ;
2263+ expect ( requestRow ( byPosition ) . status ) . toBe ( 'approved' ) ;
2264+ } ) ;
2265+ } ) ;
2266+
21352267 it ( 'runEscalations: skips requests that are not yet due or have no SLA' , async ( ) => {
21362268 await svc . openNodeRequest (
21372269 openInput ( [ 'u9' ] , { } , { escalation : { timeoutHours : 1000 , action : 'auto_approve' } } ) , CTX ,
0 commit comments