Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(contact): send no last name when the body sent null
Yudhi Armyndharis726756a00858
diff --git a/src/modules/contact/contact.service.spec.ts b/src/modules/contact/contact.service.spec.tsindex 845577fe1..f415adb8e 100644--- a/src/modules/contact/contact.service.spec.ts+++ b/src/modules/contact/contact.service.spec.ts@@ -4,6 +4,8 @@ import { EngineRegistry } from '../../engine/engine-registry.service';import { IWhatsAppEngine } from '../../engine/interfaces/whatsapp-engine.interface';import { EngineNotReadyError } from '../../common/errors/engine-not-ready.error';import { EngineTransportError } from '../../common/errors/engine-transport.error';+import { WwebjsContacts } from '../../engine/adapters/wwebjs-contacts';+import type { WwebjsEngineHost } from '../../engine/adapters/wwebjs-host';describe('ContactService', () => {const makeService = (engine: Partial<IWhatsAppEngine> | undefined) => {@@ -316,6 +318,31 @@ describe('ContactService', () => {expect(upsertContact).toHaveBeenCalledWith('[email protected]', 'Ada', 'Lovelace');});+ // UpsertContactDto lets a JSON null through (@IsOptional skips it). A default parameter ignores null, so+ // the whatsapp-web.js delegate's `lastName = ''` never applied and the page-side save received null.+ it.each([undefined, null])('hands the engine no last name for lastName %p', async lastName => {+ const upsertContact = jest.fn().mockResolvedValue(undefined);+ await makeService({ upsertContact }).upsertContact('s1', '[email protected]', 'Ada', lastName as string | undefined);+ expect(upsertContact).toHaveBeenCalledWith('[email protected]', 'Ada', undefined);+ });++ it('reaches the whatsapp-web.js save with an empty last name when the body sent null', async () => {+ const save = jest.fn().mockResolvedValue(undefined);+ const host = {+ ensureReady: jest.fn(),+ getClient: () => ({ saveOrEditAddressbookContact: save }),+ isPageTransportError: () => false,+ reportIfPageTransportError: jest.fn(),+ logger: { debug: jest.fn() },+ } as unknown as WwebjsEngineHost;+ const contacts = new WwebjsContacts(host);+ const svc = makeService({+ upsertContact: (id: string, first: string, last?: string) => contacts.upsertContact(id, first, last),+ });+ await svc.upsertContact('s1', '[email protected]', 'Ada', null as unknown as string);+ expect(save).toHaveBeenCalledWith('628123', 'Ada', '');+ });+// Same hazard as the lid, different ids: a group/newsletter/broadcast id also carries digits// that would be stored as a phone number for a contact that does not exist.it.each(['[email protected]', '120363000000000000@newsletter', 'status@broadcast'])(diff --git a/src/modules/contact/contact.service.ts b/src/modules/contact/contact.service.tsindex 24ba6fd68..8baa295ef 100644--- a/src/modules/contact/contact.service.ts+++ b/src/modules/contact/contact.service.ts@@ -234,8 +234,10 @@ export class ContactService {upsertContact(sessionId: string, contactId: string, firstName: string, lastName?: string) {this.assertAddressable(contactId);+ // The DTO lets a JSON null through as "no last name". The engines default an omitted one to '' (a default+ // parameter ignores null), so null would otherwise reach the page-side save as a value.return this.getEngine(sessionId)- .upsertContact(this.toAddressableId(contactId), firstName, lastName)+ .upsertContact(this.toAddressableId(contactId), firstName, lastName ?? undefined).finally(() => this.listings.drop(sessionId));}