diff --git a/src/Socket/business.ts b/src/Socket/business.ts index 114d4f3..9f94f16 100644 --- a/src/Socket/business.ts +++ b/src/Socket/business.ts @@ -1,6 +1,6 @@ -import { ProductCreate, ProductUpdate, SocketConfig } from '../Types' +import { CatalogCursor, ProductCreate, ProductUpdate, SocketConfig } from '../Types' import { parseCatalogNode, parseCollectionsNode, parseOrderDetailsNode, parseProductNode, toProductNode, uploadingNecessaryImagesOfProduct } from '../Utils/business' -import { jidNormalizedUser, S_WHATSAPP_NET } from '../WABinary' +import { BinaryNode, jidNormalizedUser, S_WHATSAPP_NET } from '../WABinary' import { getBinaryNodeChild } from '../WABinary/generic-utils' import { makeMessagesRecvSocket } from './messages-recv' @@ -12,9 +12,40 @@ export const makeBusinessSocket = (config: SocketConfig) => { waUploadToServer } = sock - const getCatalog = async(jid?: string, limit = 10) => { + const getCatalog = async( + jid?: string, + limit = 10, + cursor?: CatalogCursor + ) => { jid = jid || authState.creds.me?.id jid = jidNormalizedUser(jid!) + + const queryParamNodes: BinaryNode[] = [ + { + tag: 'limit', + attrs: { }, + content: Buffer.from(limit.toString()) + }, + { + tag: 'width', + attrs: { }, + content: Buffer.from('100') + }, + { + tag: 'height', + attrs: { }, + content: Buffer.from('100') + }, + ] + + if(cursor) { + queryParamNodes.push({ + tag: 'after', + attrs: { }, + content: cursor + }) + } + const result = await query({ tag: 'iq', attrs: { @@ -29,23 +60,7 @@ export const makeBusinessSocket = (config: SocketConfig) => { jid, allow_shop_source: 'true' }, - content: [ - { - tag: 'limit', - attrs: { }, - content: Buffer.from(limit.toString()) - }, - { - tag: 'width', - attrs: { }, - content: Buffer.from('100') - }, - { - tag: 'height', - attrs: { }, - content: Buffer.from('100') - } - ] + content: queryParamNodes } ] }) diff --git a/src/Types/Product.ts b/src/Types/Product.ts index ca4d5d7..e227497 100644 --- a/src/Types/Product.ts +++ b/src/Types/Product.ts @@ -70,4 +70,6 @@ export type OrderProduct = { export type OrderDetails = { price: OrderPrice products: OrderProduct[] -} \ No newline at end of file +} + +export type CatalogCursor = string \ No newline at end of file diff --git a/src/Utils/business.ts b/src/Utils/business.ts index f49470e..b2fd3f4 100644 --- a/src/Utils/business.ts +++ b/src/Utils/business.ts @@ -7,7 +7,14 @@ import { getStream, getUrlFromDirectPath, toReadable } from './messages-media' export const parseCatalogNode = (node: BinaryNode) => { const catalogNode = getBinaryNodeChild(node, 'product_catalog') const products = getBinaryNodeChildren(catalogNode, 'product').map(parseProductNode) - return { products } + const paging = getBinaryNodeChild(catalogNode, 'paging') + + return { + products, + nextPageCursor: paging + ? getBinaryNodeChildString(paging, 'after') + : undefined + } } export const parseCollectionsNode = (node: BinaryNode) => {