Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(message): store the label of a clicked button as the body

Yudhi Armyndharis4574cc8b7bdf
diff --git a/src/modules/message/message-send.service.spec.ts b/src/modules/message/message-send.service.spec.ts
index 80a30e13a..11485f051 100644
--- a/src/modules/message/message-send.service.spec.ts
+++ b/src/modules/message/message-send.service.spec.ts
@@ -1357,7 +1357,10 @@ describe('MessageSendService', () => {
},
}),
);
- expect(repository.save).toHaveBeenLastCalledWith(
+ // The only write after the send is this UPDATE; the in-memory entity handed to save() earlier is
+ // mutated afterwards, so asserting on save() would pass without the label ever being written.
+ expect(repository.update).toHaveBeenLastCalledWith(
+ { id: 'msg-uuid-1', sessionId: 'sess-1' },
expect.objectContaining({
body: 'Sim',
status: MessageStatus.SENT,
@@ -1366,7 +1369,40 @@ describe('MessageSendService', () => {
button: { id: 'yes', text: 'Sim' },
},
}),
- { transaction: false },
+ );
+ });
+
+ it('leaves the stored body alone when the engine resolved no label', async () => {
+ mockEngine.clickButton.mockResolvedValueOnce({ ...mockEngineResult });
+
+ await service.clickButton('sess-1', { chatId: '[email protected]', messageId: 'PROMPT-1', buttonId: 'yes', text: 'Yes' });
+
+ const update = repository.update as jest.Mock<unknown, [unknown, Record<string, unknown>]>;
+ const [, patch] = update.mock.calls.at(-1) ?? [];
+ expect(patch).toMatchObject({ status: MessageStatus.SENT });
+ expect(patch).not.toHaveProperty('body');
+ });
+
+ // The echo won the race and merging onto it failed, so this row is the one kept: it still has to carry the label.
+ it('keeps the resolved label on the retained row when merging onto the echo row failed', async () => {
+ (repository.findOne as jest.Mock)
+ .mockResolvedValueOnce(null) // the prompt lookup
+ .mockRejectedValueOnce(new Error('db busy')) // the metadata merge onto the echo row
+ .mockResolvedValueOnce({ id: 'msg-uuid-1' }); // the retained row, read back
+ (repository.update as jest.Mock).mockRejectedValueOnce(
+ Object.assign(new Error('UNIQUE constraint failed'), { code: 'SQLITE_CONSTRAINT_UNIQUE' }),
+ );
+
+ await service.clickButton('sess-1', {
+ chatId: '[email protected]',
+ messageId: 'PROMPT-1',
+ buttonId: 'yes',
+ text: 'Yes',
+ });
+
+ expect(repository.update).toHaveBeenCalledWith(
+ { id: 'msg-uuid-1', status: MessageStatus.PENDING },
+ expect.objectContaining({ status: MessageStatus.SENT, body: 'Sim' }),
);
});
diff --git a/src/modules/message/message-send.service.ts b/src/modules/message/message-send.service.ts
index 60c821b1f..42f4c3073 100644
--- a/src/modules/message/message-send.service.ts
+++ b/src/modules/message/message-send.service.ts
@@ -636,7 +636,7 @@ export class MessageSendService {
button: { id: finalDto.buttonId, text: result.body },
};
}
- return this.persistSentState(message, result);
+ return this.persistSentState(message, result, result.body ? { body: result.body } : undefined);
}
async forward(
@@ -854,7 +854,13 @@ export class MessageSendService {
* DB fault would otherwise mark a delivered message permanently FAILED and (for text) fire
* `message:failed`. Log and return success instead.
*/
- private async persistSentState(message: Message, result: MessageResult): Promise<MessageResponseDto> {
+ private async persistSentState(
+ message: Message,
+ result: MessageResult,
+ // Columns a sender changed in memory after its PENDING insert. The write below is an UPDATE of
+ // fixed columns, so anything not listed here (the resolved button label, for one) is never stored.
+ changed: Pick<QueryDeepPartialEntity<Message>, 'body'> = {},
+ ): Promise<MessageResponseDto> {
// A send whose engine couldn't read the sent message's id back reports an empty id — a forward that
// can't recover the copy, or a WhatsApp Web build that renamed the id field out from under the
// engine. Leave waMessageId unset (NULL) so no ack mis-matches it.
@@ -869,6 +875,7 @@ export class MessageSendService {
const updated = await this.messageRepository.update(
{ id: message.id, sessionId: message.sessionId },
{
+ ...changed,
waMessageId: message.waMessageId,
status: message.status,
timestamp: message.timestamp,
@@ -912,11 +919,12 @@ export class MessageSendService {
}
if (!metadataMerged) {
// Keep the payload-bearing row when the echo merge fails. Mark it SENT without the
- // conflicting engine id so the pending reaper cannot strip successfully sent media.
+ // conflicting engine id so the pending reaper cannot strip successfully sent media. This is the
+ // row that survives, so it carries what the send resolved too (a clicked button's label).
await this.messageRepository
.update(
{ id: message.id, status: MessageStatus.PENDING },
- { status: MessageStatus.SENT, timestamp: result.timestamp },
+ { status: MessageStatus.SENT, timestamp: result.timestamp, ...changed },
)
.catch(err =>
this.logger.warn(`Preserving the sent media row failed (id=${message.id})`, {