feat: add nextPageCursor

This commit is contained in:
Adhiraj Singh
2022-12-08 13:30:31 +05:30
parent 30e2cb5c4c
commit c96a7652d9
3 changed files with 46 additions and 22 deletions

View File

@@ -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
}
]
})

View File

@@ -70,4 +70,6 @@ export type OrderProduct = {
export type OrderDetails = {
price: OrderPrice
products: OrderProduct[]
}
}
export type CatalogCursor = string

View File

@@ -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) => {