Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(catalog): omit price for a Baileys product that has none

Yudhi Armyndharisee0aadf8412d
diff --git a/sdk/javascript/src/types.ts b/sdk/javascript/src/types.ts
index 7421b2ea3..101d5de26 100644
--- a/sdk/javascript/src/types.ts
+++ b/sdk/javascript/src/types.ts
@@ -1290,9 +1290,11 @@ export interface CatalogProduct {
id: string;
name: string;
description?: string | null;
- price: number;
+ /** Absent when the product has no price. */
+ price?: number;
currency: string;
- priceFormatted: string;
+ /** Absent when `price` is. */
+ priceFormatted?: string;
imageUrl?: string | null;
url: string;
isAvailable: boolean;
diff --git a/src/engine/adapters/baileys-catalog.ts b/src/engine/adapters/baileys-catalog.ts
index c9ba27c14..379712d13 100644
--- a/src/engine/adapters/baileys-catalog.ts
+++ b/src/engine/adapters/baileys-catalog.ts
@@ -135,9 +135,9 @@ function mapProduct(p: BaileysProduct): Product {
id: p.id,
name: p.name,
description: p.description || undefined,
- price: p.price,
+ // A catalog item without a price parses as NaN, which would reach clients as "price":null.
+ ...(Number.isFinite(p.price) && { price: p.price, priceFormatted: formatPrice(p.price, p.currency) }),
currency: p.currency,
- priceFormatted: formatPrice(p.price, p.currency),
imageUrl: Object.values(p.imageUrls ?? {})[0],
url: p.url ?? '',
isAvailable: p.availability === 'in stock',
diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.ts
index 0b365fff8..3df5b0103 100644
--- a/src/engine/adapters/baileys-messaging.ts
+++ b/src/engine/adapters/baileys-messaging.ts
@@ -318,7 +318,7 @@ export class BaileysMessaging {
title: product.name,
description: product.description,
currencyCode: product.currency,
- priceAmount1000: Math.round(product.price * 1000),
+ priceAmount1000: product.price === undefined ? undefined : Math.round(product.price * 1000),
retailerId: product.retailerId,
url: product.url || undefined,
productImage: { url: product.imageUrl },
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index 2568c3421..580a351b3 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -6316,6 +6316,18 @@ describe('BaileysAdapter catalog (#905)', () => {
});
});
+ // Baileys parses the <price> child with a unary +, so a catalog item without one arrives as NaN.
+ it('getProducts omits price and priceFormatted for a product without a price', async () => {
+ const adapter = await ready();
+ fakeSock.getCatalog.mockResolvedValue({ products: [baileysProduct({ price: NaN })], nextPageCursor: undefined });
+
+ const { products } = await adapter.getProducts({ page: 1, limit: 10 });
+
+ expect(products[0]).not.toHaveProperty('price');
+ expect(products[0]).not.toHaveProperty('priceFormatted');
+ expect(JSON.parse(JSON.stringify(products[0]))).not.toHaveProperty('price');
+ });
+
it('getProduct returns the product with the matching id', async () => {
const adapter = await ready();
fakeSock.getCatalog.mockResolvedValue({
@@ -6360,6 +6372,17 @@ describe('BaileysAdapter catalog (#905)', () => {
expect(res).toEqual({ id: 'M1', timestamp: 1700000005 });
});
+ it('sendProduct sends a product without a price with no priceAmount1000, never NaN', async () => {
+ const adapter = await ready();
+ fakeSock.getCatalog.mockResolvedValue({ products: [baileysProduct({ price: NaN })], nextPageCursor: undefined });
+ fakeSock.sendMessage.mockResolvedValue({ key: { id: 'M1' }, messageTimestamp: 1700000005 });
+
+ await adapter.sendProduct('[email protected]', 'p1');
+
+ const [, content] = fakeSock.sendMessage.mock.calls[0] as [string, { product: { priceAmount1000?: number } }];
+ expect(content.product.priceAmount1000).toBeUndefined();
+ });
+
it('sendProduct rejects NotFound when the product id is unknown', async () => {
const adapter = await ready();
fakeSock.getCatalog.mockResolvedValue({ products: [baileysProduct()], nextPageCursor: undefined });
diff --git a/src/engine/interfaces/whatsapp-engine.interface.ts b/src/engine/interfaces/whatsapp-engine.interface.ts
index 51a6fe8e2..a1c0f23ae 100644
--- a/src/engine/interfaces/whatsapp-engine.interface.ts
+++ b/src/engine/interfaces/whatsapp-engine.interface.ts
@@ -507,9 +507,11 @@ export interface Product {
id: string;
name: string;
description?: string;
- price: number;
+ /** Absent when the catalog item carries no price. */
+ price?: number;
currency: string;
- priceFormatted: string;
+ /** Present only when price is. */
+ priceFormatted?: string;
imageUrl?: string;
url: string;
isAvailable: boolean;
diff --git a/src/modules/catalog/dto/catalog-response.dto.ts b/src/modules/catalog/dto/catalog-response.dto.ts
index e6a2c8d67..b51452b65 100644
--- a/src/modules/catalog/dto/catalog-response.dto.ts
+++ b/src/modules/catalog/dto/catalog-response.dto.ts
@@ -38,19 +38,22 @@ export class ProductDto {
@ApiPropertyOptional({ description: 'Product description, when set.' })
description?: string;
- @ApiProperty({ description: 'Price in the currency’s minor-unit-free numeric form.', example: 85000 })
- price!: number;
+ @ApiPropertyOptional({
+ description: 'Price in the currency’s minor-unit-free numeric form. Absent when the product has no price.',
+ example: 85000,
+ })
+ price?: number;
@ApiProperty({ description: 'ISO currency code.', example: 'IDR' })
currency!: string;
- @ApiProperty({
+ @ApiPropertyOptional({
description:
'Price rendered for display. Synthesized by the gateway from price + currency, so an ' +
- 'unrecognised currency code falls back to a plain "CODE amount" pair.',
+ 'unrecognised currency code falls back to a plain "CODE amount" pair. Absent when price is.',
example: 'IDR 85,000.00',
})
- priceFormatted!: string;
+ priceFormatted?: string;
@ApiPropertyOptional({
description: 'First product image URL. Absent when the product carries no image.',