( tableId: string, scopeFilter?: Filter, spareExcludedRowIds?: string[] )
| 754 | * (select-all-minus-deselections Stop) to spare row-scoped dispatches whose |
| 755 | * rows are ALL deselected — that work wasn't in the stopped selection. */ |
| 756 | export async function markActiveDispatchesCancelled( |
| 757 | tableId: string, |
| 758 | scopeFilter?: Filter, |
| 759 | spareExcludedRowIds?: string[] |
| 760 | ): Promise<DispatchRow[]> { |
| 761 | const cancelled = await db |
| 762 | .update(tableRunDispatches) |
| 763 | .set({ status: 'cancelled', cancelledAt: new Date() }) |
| 764 | .where( |
| 765 | and( |
| 766 | eq(tableRunDispatches.tableId, tableId), |
| 767 | inArray(tableRunDispatches.status, [...ACTIVE_DISPATCH_STATUSES]), |
| 768 | scopeFilter |
| 769 | ? sql`${tableRunDispatches.scope}->'filter' = ${JSON.stringify(scopeFilter)}::jsonb` |
| 770 | : undefined, |
| 771 | // coalesce(false): table-wide dispatches have no scope.rowIds (NULL <@ x |
| 772 | // is NULL) and must still cancel. |
| 773 | spareExcludedRowIds && spareExcludedRowIds.length > 0 |
| 774 | ? sql`NOT coalesce( |
| 775 | ${tableRunDispatches.scope}->'rowIds' <@ ${JSON.stringify(spareExcludedRowIds)}::jsonb |
| 776 | AND jsonb_array_length(${tableRunDispatches.scope}->'rowIds') > 0, |
| 777 | false |
| 778 | )` |
| 779 | : undefined |
| 780 | ) |
| 781 | ) |
| 782 | .returning() |
| 783 | const dispatches = cancelled.map((row) => ({ |
| 784 | id: row.id, |
| 785 | tableId: row.tableId, |
| 786 | workspaceId: row.workspaceId, |
| 787 | requestId: row.requestId, |
| 788 | mode: row.mode as DispatchMode, |
| 789 | scope: row.scope as DispatchScope, |
| 790 | status: 'cancelled' as DispatchStatus, |
| 791 | cursor: row.cursor, |
| 792 | limit: (row.limit as DispatchLimit | null) ?? null, |
| 793 | processedCount: row.processedCount, |
| 794 | isManualRun: row.isManualRun, |
| 795 | triggeredByUserId: row.triggeredByUserId, |
| 796 | requestedAt: row.requestedAt, |
| 797 | })) |
| 798 | await Promise.all( |
| 799 | dispatches.map((d) => |
| 800 | appendTableEvent({ |
| 801 | kind: 'dispatch', |
| 802 | tableId: d.tableId, |
| 803 | dispatchId: d.id, |
| 804 | status: 'cancelled', |
| 805 | scope: d.scope, |
| 806 | cursor: d.cursor, |
| 807 | mode: d.mode, |
| 808 | isManualRun: d.isManualRun, |
| 809 | }) |
| 810 | ) |
| 811 | ) |
| 812 | return dispatches |
| 813 | } |
no test coverage detected