88import { journeys } from "../../emails/flows" ;
99import registry from "../../emails/resources.json" ;
1010import type { Journey } from "../../emails/types" ;
11- import { LoopsApi , LoopsApiError } from "./api" ;
11+ import { LoopsApi } from "./api" ;
1212
1313export const healthUrl = "https://cap.so/api/cron/sync-loops/health" ;
1414
@@ -109,6 +109,8 @@ export async function enforceDeliverySafety(
109109 assert . equal ( identity . teamName , registry . teamName ) ;
110110 const results : { workflow : string ; action : string } [ ] = [ ] ;
111111 for ( const target of options . targets ?? safetyTargets ) {
112+ let sending = false ;
113+ let attemptedUpdate = false ;
112114 try {
113115 const path = `workflows/${ target . workflowId } ` ;
114116 const workflow = await api . request < {
@@ -119,6 +121,7 @@ export async function enforceDeliverySafety(
119121 nodes : Record < string , { nextNodeIds : string [ ] } > ;
120122 } > ( path ) ;
121123 assert . equal ( workflow . name , target . journey . name ) ;
124+ sending = workflow . status === "Sending" ;
122125 assert (
123126 [ "Draft" , "Sending" , "Paused" , "PausedAndQueueing" ] . includes (
124127 workflow . status ,
@@ -171,14 +174,15 @@ export async function enforceDeliverySafety(
171174 guard . appliesDownstream
172175 )
173176 nextFilter = undefined ;
174- if ( nextFilter && workflow . status === "Sending" ) {
177+ if ( nextFilter && sending ) {
175178 results . push ( {
176179 workflow : target . journey . key ,
177180 action : "manual-pause-required" ,
178181 } ) ;
179182 continue ;
180183 }
181184 if ( nextFilter && options . apply ) {
185+ attemptedUpdate = true ;
182186 await api . request ( nodePath , "POST" , {
183187 expectedRevisionId : guard . workflowRevisionId ,
184188 payload : { audienceFilter : nextFilter , appliesDownstream : true } ,
@@ -188,15 +192,17 @@ export async function enforceDeliverySafety(
188192 assert . equal ( confirmed . appliesDownstream , true ) ;
189193 }
190194 results . push ( { workflow : target . journey . key , action } ) ;
191- } catch ( error ) {
192- const sending =
193- error instanceof LoopsApiError &&
194- error . status === 400 &&
195- typeof error . details === "object" &&
196- error . details !== null &&
197- "message" in error . details &&
198- error . details . message ===
199- "This operation is not allowed while the workflow is sending." ;
195+ } catch {
196+ if ( attemptedUpdate && ! sending ) {
197+ try {
198+ const latest = await api . request < { status : string } > (
199+ `workflows/${ target . workflowId } ` ,
200+ ) ;
201+ sending = latest . status === "Sending" ;
202+ } catch {
203+ sending = false ;
204+ }
205+ }
200206 results . push ( {
201207 workflow : target . journey . key ,
202208 action : sending ? "manual-pause-required" : "error" ,
0 commit comments