Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(message): stamp completedAt when reaping an orphaned batch

Yudhi Armyndharis8fbe8a11b83e
diff --git a/src/modules/message/bulk-message.service.spec.ts b/src/modules/message/bulk-message.service.spec.ts
index 837564ce7..f6ae7f95f 100644
--- a/src/modules/message/bulk-message.service.spec.ts
+++ b/src/modules/message/bulk-message.service.spec.ts
@@ -113,6 +113,16 @@ describe('BulkMessageService.onApplicationBootstrap', () => {
expect(repo.save).not.toHaveBeenCalled();
});
+ // FAILED is terminal, and the status route documents completedAt as null only while a batch runs.
+ it('stamps completedAt on a batch it fails, like every other terminal write', async () => {
+ repo.find.mockResolvedValue([{ id: 'b1', status: BatchStatus.PROCESSING, messages: [] }]);
+
+ await service.onApplicationBootstrap();
+
+ const written = (repo.update.mock.calls[0] as [unknown, Partial<MessageBatch>])[1];
+ expect(written.completedAt).toBeInstanceOf(Date);
+ });
+
it('does nothing when there are no orphaned batches', async () => {
repo.find.mockResolvedValue([]);
await service.onApplicationBootstrap();
diff --git a/src/modules/message/bulk-message.service.ts b/src/modules/message/bulk-message.service.ts
index bbec8f49c..b7d452ea4 100644
--- a/src/modules/message/bulk-message.service.ts
+++ b/src/modules/message/bulk-message.service.ts
@@ -157,6 +157,7 @@ export class BulkMessageService implements OnApplicationBootstrap {
this.stripBatchMediaPayloads(batch.messages);
const failed = await this.batchRepository.update({ id: batch.id, status: BatchStatus.PROCESSING }, {
status: BatchStatus.FAILED,
+ completedAt: new Date(),
messages: batch.messages,
} as QueryDeepPartialEntity<MessageBatch>);
return Boolean(failed.affected);