From f2087d773c836a731d8a013c510796f1958ce471 Mon Sep 17 00:00:00 2001 From: icleitoncosta Date: Wed, 17 Jul 2024 19:53:55 -0300 Subject: [PATCH] feat: Added WPP.cart.add function --- src/cart/functions/add.ts | 80 +++++++++++++++++++++++++ src/catalog/functions/getProductById.ts | 26 +++++++- 2 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 src/cart/functions/add.ts diff --git a/src/cart/functions/add.ts b/src/cart/functions/add.ts new file mode 100644 index 0000000000..12130d08fe --- /dev/null +++ b/src/cart/functions/add.ts @@ -0,0 +1,80 @@ +/*! + * Copyright 2024 WPPConnect Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Add product in cart + * + * @example + * ```javascript + * const cart = WPP.cart.add('[number]@c.us', [ + * { id: 'productId', qnt: 2 }, + * ]); + * ``` + * + * @category Cart + */ + +import { getProductById } from '../../catalog'; +import { WPPError } from '../../util'; +import { CartModel, CartStore, ProductModel } from '../../whatsapp'; +import { + addProductToCart, + updateProductQuantityCart, +} from '../../whatsapp/functions'; + +export async function add( + chatId: string, + products: { + id: string; + qnt: number; + }[] +): Promise { + if (!chatId || !products) { + throw new WPPError( + 'send_required_params', + `For add item in cart send chatId and products array` + ); + } + for (const product of products) { + const get = await getProductById(chatId, product.id as any); + if (get) { + const prod = new ProductModel({ + id: get.id, + isHidden: get.is_hidden || false, + catalogWid: chatId, + url: get.url, + name: get.name, + description: get.description, + availability: get.availability, + maxAvailable: (get as any)?.maxAvailable, + reviewStatus: get?.capability_to_review_status[0].value, + currency: get.currency, + priceAmount1000: get.price, + salePriceAmount1000: null, + retailerId: get.retailer_id, + imageCount: + get.image_cdn_urls.length + get.additional_image_cdn_urls.length, + additionalImageCdnUrl: get.additional_image_cdn_urls, + additionalImageHashes: get.image_hashes_for_whatsapp, + imageCdnUrl: get.image_cdn_urls[0].value, + imageHash: get.image_hashes_for_whatsapp[0], + }); + await addProductToCart(prod); + await updateProductQuantityCart(prod, product.qnt); + } + } + return CartStore.findCart(chatId); +} diff --git a/src/catalog/functions/getProductById.ts b/src/catalog/functions/getProductById.ts index 7c3d63acc1..dcdc7c922c 100644 --- a/src/catalog/functions/getProductById.ts +++ b/src/catalog/functions/getProductById.ts @@ -15,7 +15,6 @@ */ import { createWid } from '../../util'; -import { ProductModel } from '../../whatsapp'; import { queryProduct } from '../../whatsapp/functions'; /** @@ -33,7 +32,30 @@ import { queryProduct } from '../../whatsapp/functions'; export async function getProductById( chatId: string, productId: number -): Promise { +): Promise<{ + id: string; + retailer_id: string; + name: string; + description: string; + url: string; + currency: string; + price: string; + is_hidden: boolean; + max_available: number; + availability: string; + checkmark: boolean; + image_hashes_for_whatsapp: string[]; + image_cdn_urls: { + key: 'requested' | 'full'; + value: string; + }[]; + additional_image_cdn_urls: any[]; + whatsapp_product_can_appeal: boolean; + capability_to_review_status: { + key: 'WHATSAPP'; + value: 'APPROVED'; + }[]; +}> { const wid = createWid(chatId); const { data } = await queryProduct(wid, productId); return data;