Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

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

Yudhi Armyndharis7a97228537fb
diff --git a/sdk/javascript/src/types.ts b/sdk/javascript/src/types.ts
index 101d5de26..8a0e8e263 100644
--- a/sdk/javascript/src/types.ts
+++ b/sdk/javascript/src/types.ts
@@ -1292,7 +1292,7 @@ export interface CatalogProduct {
description?: string | null;
/** Absent when the product has no price. */
price?: number;
- currency: string;
+ currency?: string;
/** Absent when `price` is. */
priceFormatted?: string;
imageUrl?: string | null;
diff --git a/src/engine/adapters/baileys-catalog.ts b/src/engine/adapters/baileys-catalog.ts
index 379712d13..7b61aa9c9 100644
--- a/src/engine/adapters/baileys-catalog.ts
+++ b/src/engine/adapters/baileys-catalog.ts
@@ -137,7 +137,8 @@ function mapProduct(p: BaileysProduct): Product {
description: p.description || undefined,
// 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,
+ // Read from the <currency> child the same way, so an item without one has none to report.
+ ...(p.currency && { currency: p.currency }),
imageUrl: Object.values(p.imageUrls ?? {})[0],
url: p.url ?? '',
isAvailable: p.availability === 'in stock',
@@ -145,7 +146,8 @@ function mapProduct(p: BaileysProduct): Product {
};
}
-function formatPrice(price: number, currency: string): string {
+function formatPrice(price: number, currency: string | undefined): string {
+ if (!currency) return new Intl.NumberFormat('en').format(price);
try {
return new Intl.NumberFormat('en', { style: 'currency', currency }).format(price);
} catch {
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index c312f79e4..9f9ed3109 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -6339,6 +6339,26 @@ describe('BaileysAdapter catalog (#905)', () => {
expect(JSON.parse(JSON.stringify(products[0]))).not.toHaveProperty('price');
});
+ // The <currency> child is read the same way, so an item without one arrives with currency undefined.
+ it('getProducts omits currency and formats a bare price for a product without a currency', async () => {
+ const adapter = await ready();
+ fakeSock.getCatalog.mockResolvedValue({
+ products: [
+ baileysProduct({ price: 85000, currency: undefined }),
+ baileysProduct({ id: 'p2', price: NaN, currency: undefined }),
+ ],
+ nextPageCursor: undefined,
+ });
+
+ const { products } = await adapter.getProducts({ page: 1, limit: 10 });
+
+ expect(products[0]).not.toHaveProperty('currency');
+ expect(products[0].price).toBe(85000);
+ expect(products[0].priceFormatted).toBe('85,000');
+ expect(products[1]).not.toHaveProperty('currency');
+ expect(products[1]).not.toHaveProperty('priceFormatted');
+ });
+
it('getProduct returns the product with the matching id', async () => {
const adapter = await ready();
fakeSock.getCatalog.mockResolvedValue({
diff --git a/src/engine/interfaces/whatsapp-engine.interface.ts b/src/engine/interfaces/whatsapp-engine.interface.ts
index a1c0f23ae..7321ba653 100644
--- a/src/engine/interfaces/whatsapp-engine.interface.ts
+++ b/src/engine/interfaces/whatsapp-engine.interface.ts
@@ -509,7 +509,8 @@ export interface Product {
description?: string;
/** Absent when the catalog item carries no price. */
price?: number;
- currency: string;
+ /** Absent when the catalog item carries no currency. */
+ currency?: string;
/** Present only when price is. */
priceFormatted?: string;
imageUrl?: string;
diff --git a/src/modules/catalog/dto/catalog-response.dto.ts b/src/modules/catalog/dto/catalog-response.dto.ts
index b51452b65..d39af8960 100644
--- a/src/modules/catalog/dto/catalog-response.dto.ts
+++ b/src/modules/catalog/dto/catalog-response.dto.ts
@@ -44,13 +44,17 @@ export class ProductDto {
})
price?: number;
- @ApiProperty({ description: 'ISO currency code.', example: 'IDR' })
- currency!: string;
+ @ApiPropertyOptional({
+ description: 'ISO currency code. Absent when the product carries no currency.',
+ example: 'IDR',
+ })
+ currency?: string;
@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. Absent when price is.',
+ 'unrecognised currency code falls back to a plain "CODE amount" pair, and a product with no ' +
+ 'currency shows the bare amount. Present only when price is.',
example: 'IDR 85,000.00',
})
priceFormatted?: string;