Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(message): keep a deleted batch row deleted when its run ends
Yudhi Armyndharis2d9b6d809d1e
diff --git a/src/modules/message/bulk-message.service.spec.ts b/src/modules/message/bulk-message.service.spec.tsindex d29c1fba5..93c4155d8 100644--- a/src/modules/message/bulk-message.service.spec.ts+++ b/src/modules/message/bulk-message.service.spec.ts@@ -990,8 +990,13 @@ describe('BulkMessageService.processBatch', () => {// Only the first message (before the guarded cadence write saw the cancel) was sent.expect(engine.sendTextMessage).toHaveBeenCalledTimes(1);// The terminal write persists CANCELLED with reconciled counters — never a PROCESSING rewrite.- const savedBatch = (repo.save.mock.calls as [MessageBatch][]).at(-1)![0];- expect(savedBatch.status).toBe(BatchStatus.CANCELLED);+ // It is an UPDATE, not a save: when the row is gone (its session was deleted, which also reads+ // as a 0-row cadence write), a save would INSERT it again.+ expect(repo.save).not.toHaveBeenCalled();+ expect(repo.update).toHaveBeenLastCalledWith(+ { id: 'b1' },+ expect.objectContaining({ status: BatchStatus.CANCELLED, completedAt: expect.any(Date) as unknown }),+ );});it('sends nothing when the batch row is already CANCELLED at pickup (cancel-before-start)', async () => {@@ -1042,8 +1047,9 @@ describe('BulkMessageService.processBatch', () => {await runProcessBatch();expect(engine.sendTextMessage).toHaveBeenCalledTimes(1); // the remaining two items were not sent- const savedBatch = (repo.save.mock.calls as [MessageBatch][]).at(-1)![0];- expect(savedBatch.status).toBe(BatchStatus.CANCELLED);+ expect(repo.save).not.toHaveBeenCalled();+ const finalPartial = (repo.update.mock.calls as Array<[unknown, { status?: BatchStatus }]>).at(-1)![1];+ expect(finalPartial.status).toBe(BatchStatus.CANCELLED);// No guarded write ever carried a non-cancelled terminal status back to the row.for (const [, partial] of repo.update.mock.calls as Array<[unknown, { status?: BatchStatus }]>) {expect(partial.status).not.toBe(BatchStatus.COMPLETED);diff --git a/src/modules/message/bulk-message.service.ts b/src/modules/message/bulk-message.service.tsindex 09c14dbef..25666d002 100644--- a/src/modules/message/bulk-message.service.ts+++ b/src/modules/message/bulk-message.service.ts@@ -670,22 +670,27 @@ export class BulkMessageService implements OnApplicationBootstrap {// otherwise the message_batches row retains multi-MB media forever. Intermediate (cadence) saves// above keep the payload so a batch interrupted mid-run can still resume from currentIndex.this.stripBatchMediaPayloads(batch.messages);+ const terminal = {+ status: batch.status,+ progress: batch.progress,+ results,+ currentIndex: batch.currentIndex,+ completedAt: batch.completedAt,+ messages: batch.messages,+ } as QueryDeepPartialEntity<MessageBatch>;if (batch.status === BatchStatus.CANCELLED) {- // Persisting CANCELLED can never resurrect a finished batch — save the reconciled counters- // over cancelBatch's own (possibly earlier, staler) write.- await this.batchRepository.save(batch);+ // Persisting CANCELLED can never resurrect a finished batch — write the reconciled counters+ // over cancelBatch's own (possibly earlier, staler) write. An UPDATE, not a save: a row deleted+ // with its session also reads as a cancel, and save() would INSERT it back.+ await this.batchRepository.update({ id: batch.id }, terminal);} else {// A cancel may have committed after the re-read above; the guard IN the UPDATE makes this// terminal write unable to flip a CANCELLED batch back to COMPLETED/FAILED. Zero affected// rows means the cancel won the final race — the batch stays exactly as cancelBatch left it.- const finalized = await this.batchRepository.update({ id: batch.id, status: Not(BatchStatus.CANCELLED) }, {- status: batch.status,- progress: batch.progress,- results,- currentIndex: batch.currentIndex,- completedAt: batch.completedAt,- messages: batch.messages,- } as QueryDeepPartialEntity<MessageBatch>);+ const finalized = await this.batchRepository.update(+ { id: batch.id, status: Not(BatchStatus.CANCELLED) },+ terminal,+ );if (!finalized.affected) {batch.status = BatchStatus.CANCELLED;this.logger.log(`Batch ${batch.batchId} was cancelled just before completion; keeping CANCELLED`);