Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(message): stop a send merge undoing a delivered ack

Yudhi Armyndharis50e5d6149c63
diff --git a/src/modules/message/message-send.service.spec.ts b/src/modules/message/message-send.service.spec.ts
index 436465781..8ed6bbf7a 100644
--- a/src/modules/message/message-send.service.spec.ts
+++ b/src/modules/message/message-send.service.spec.ts
@@ -314,11 +314,10 @@ describe('MessageSendService', () => {
await service.sendText('sess-1', { chatId: '[email protected]', text: 'hi' });
- // SENT state merged onto the echo row; the redundant PENDING row dropped.
- expect(repository.update).toHaveBeenCalledWith(
- { sessionId: 'sess-1', waMessageId: 'wa-msg-1' },
- expect.objectContaining({ status: MessageStatus.SENT }),
- );
+ // Timestamp merged onto the echo row; the redundant PENDING row dropped. `status` is NOT
+ // written: the echo row is already SENT and an ack may have advanced it further.
+ const [, mergePatch] = (repository.update as jest.Mock).mock.calls[0] as [unknown, object];
+ expect(mergePatch).not.toHaveProperty('status');
expect(repository.delete).toHaveBeenCalledWith({ id: 'msg-uuid-1' });
const persisted = (hookManager.execute as jest.Mock).mock.calls.filter(
([ev]: unknown[]) => ev === 'message:persisted',
@@ -1153,11 +1152,12 @@ describe('MessageSendService', () => {
expect(repository.update).toHaveBeenCalledWith(
{ sessionId: 'sess-1', waMessageId: 'wa-bulk-1' },
expect.objectContaining({
- status: MessageStatus.SENT,
timestamp: 1706868000,
metadata: bulkRow.metadata,
}),
);
+ const [, bulkPatch] = (repository.update as jest.Mock).mock.calls[0] as [unknown, object];
+ expect(bulkPatch).not.toHaveProperty('status');
expect(saved).toEqual(expect.objectContaining({ id: 'echo-row' }));
});
@@ -1221,8 +1221,18 @@ describe('MessageSendService', () => {
expect(result.messageId).toBe('wa-msg-1'); // send reported success
expect(repository.update).toHaveBeenCalledWith(
{ sessionId: 'sess-1', waMessageId: 'wa-msg-1' },
- expect.objectContaining({ status: MessageStatus.SENT, timestamp: 1706868000 }),
+ expect.objectContaining({ timestamp: 1706868000 }),
);
+ /**
+ * The delivery state is not in that patch, and that is the point.
+ *
+ * The row it merges onto is the own-send echo's, inserted SENT, and the ack path advances it
+ * forward-only. This merge could only ever write PENDING or SENT, so it was never an upgrade:
+ * when a `delivered` ack won the race, writing SENT here dragged the row back and the
+ * dashboard showed one tick for a message the recipient already had.
+ */
+ const [, sentPatch] = (repository.update as jest.Mock).mock.calls[0] as [unknown, object];
+ expect(sentPatch).not.toHaveProperty('status');
expect(repository.delete).toHaveBeenCalledWith({ id: 'msg-uuid-1' });
});
diff --git a/src/modules/message/message-send.service.ts b/src/modules/message/message-send.service.ts
index 2afadef19..b0878e781 100644
--- a/src/modules/message/message-send.service.ts
+++ b/src/modules/message/message-send.service.ts
@@ -577,8 +577,12 @@ export class MessageSendService {
const saved = await this.messageRepository.save(message).catch(async (err: unknown) => {
const waMessageId = message.waMessageId;
if (!waMessageId || !isUniqueViolation(err)) throw err;
+ // No `status` here on purpose. The row this collides with is the own-send echo's, inserted
+ // SENT, and the ack path advances it forward-only (ackStatusTransitionFrom). This write only
+ // ever carries PENDING or SENT, so it can never be an upgrade: it is a no-op, or it drags a
+ // DELIVERED/READ row back to SENT when an ack won the race. Leave the delivery state to the
+ // one writer that owns it.
const patch: QueryDeepPartialEntity<Message> = {
- status: message.status,
timestamp: message.timestamp,
};
// Only when this write actually carries metadata worth merging: a text item must not blank
@@ -673,7 +677,10 @@ export class MessageSendService {
messageId: message.id,
},
);
- const patch: QueryDeepPartialEntity<Message> = { status: MessageStatus.SENT, timestamp: result.timestamp };
+ // `status` is deliberately absent: the echo row is already SENT, and an ack that landed
+ // first has advanced it further. Writing SENT here would undo that. See the sibling merge
+ // in saveOutgoingMessage.
+ const patch: QueryDeepPartialEntity<Message> = { timestamp: result.timestamp };
if (message.metadata && !isUrlPointerMetadata(message.metadata)) {
patch.metadata = message.metadata as QueryDeepPartialEntity<Record<string, unknown>>;
}