feat: implement product CRUD on MD

This commit is contained in:
Adhiraj Singh
2022-03-07 09:09:08 +05:30
parent d6065015e6
commit 6967e53164
2 changed files with 201 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
import { CatalogCollection, CatalogStatus, OrderDetails, OrderProduct, Product } from '../Types'
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildBuffer, getBinaryNodeChildren, getBinaryNodeChildString } from '../WABinary'
import { CatalogCollection, CatalogStatus, OrderDetails, OrderProduct, Product, ProductCreate, ProductUpdate } from '../Types'
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString } from '../WABinary'
export const parseCatalogNode = (node: BinaryNode) => {
const catalogNode = getBinaryNodeChild(node, 'product_catalog')
@@ -11,7 +11,7 @@ export const parseCollectionsNode = (node: BinaryNode) => {
const collectionsNode = getBinaryNodeChild(node, 'collections')
const collections = getBinaryNodeChildren(collectionsNode, 'collection').map<CatalogCollection>(
collectionNode => {
const id = parseCatalogId(collectionNode)
const id = getBinaryNodeChildString(collectionNode, 'id')
const name = getBinaryNodeChildString(collectionNode, 'name')
const products = getBinaryNodeChildren(collectionNode, 'product').map(parseProductNode)
@@ -35,7 +35,7 @@ export const parseOrderDetailsNode = (node: BinaryNode) => {
productNode => {
const imageNode = getBinaryNodeChild(productNode, 'image')
return {
id: parseCatalogId(productNode),
id: getBinaryNodeChildString(productNode, 'id'),
name: getBinaryNodeChildString(productNode, 'name'),
imageUrl: getBinaryNodeChildString(imageNode, 'url'),
price: +getBinaryNodeChildString(productNode, 'price'),
@@ -58,9 +58,112 @@ export const parseOrderDetailsNode = (node: BinaryNode) => {
return orderDetails
}
const parseProductNode = (productNode: BinaryNode) => {
export const toProductNode = (productId: string | undefined, product: ProductCreate | ProductUpdate) => {
const attrs: BinaryNode['attrs'] = { }
const content: BinaryNode[] = [ ]
if(typeof productId !== 'undefined') {
content.push({
tag: 'id',
attrs: { },
content: Buffer.from(productId)
})
}
if(typeof product.name !== 'undefined') {
content.push({
tag: 'name',
attrs: { },
content: Buffer.from(product.name)
})
}
if(typeof product.description !== 'undefined') {
content.push({
tag: 'description',
attrs: { },
content: Buffer.from(product.description)
})
}
if(typeof product.retailerId !== 'undefined') {
content.push({
tag: 'retailer_id',
attrs: { },
content: Buffer.from(product.retailerId)
})
}
if(product.imageUrls.length) {
content.push({
tag: 'media',
attrs: { },
content: product.imageUrls.map(
url => ({
tag: 'image',
attrs: { },
content: [
{
tag: 'url',
attrs: { },
content: Buffer.from(url)
}
]
})
)
})
}
if(typeof product.price !== 'undefined') {
content.push({
tag: 'price',
attrs: { },
content: Buffer.from(product.price.toString())
})
}
if(typeof product.currency !== 'undefined') {
content.push({
tag: 'currency',
attrs: { },
content: Buffer.from(product.currency)
})
}
if('originCountryCode' in product) {
if(typeof product.originCountryCode === 'undefined') {
attrs.compliance_category = 'COUNTRY_ORIGIN_EXEMPT'
} else {
content.push({
tag: 'compliance_info',
attrs: { },
content: [
{
tag: 'country_code_origin',
attrs: { },
content: Buffer.from(product.originCountryCode)
}
]
})
}
}
if(typeof product.isHidden !== 'undefined') {
attrs.is_hidden = product.isHidden.toString()
}
const node: BinaryNode = {
tag: 'product',
attrs,
content
}
return node
}
export const parseProductNode = (productNode: BinaryNode) => {
const isHidden = productNode.attrs.is_hidden === 'true'
const id = parseCatalogId(productNode)
const id = getBinaryNodeChildString(productNode, 'id')
const mediaNode = getBinaryNodeChild(productNode, 'media')
const statusInfoNode = getBinaryNodeChild(productNode, 'status_info')
@@ -98,10 +201,4 @@ const parseStatusInfo = (mediaNode: BinaryNode): CatalogStatus => {
status: getBinaryNodeChildString(node, 'status'),
canAppeal: getBinaryNodeChildString(node, 'can_appeal') === 'true',
}
}
const parseCatalogId = (node: BinaryNode) => {
const idNode = getBinaryNodeChildBuffer(node, 'id')
const id = Buffer.from(idNode).readBigUInt64LE().toString()
return id
}