Skip to content

Commit

Permalink
feat: Added WPP.cart.add function
Browse files Browse the repository at this point in the history
  • Loading branch information
icleitoncosta committed Jul 17, 2024
1 parent cf3dca0 commit f2087d7
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 2 deletions.
80 changes: 80 additions & 0 deletions src/cart/functions/add.ts
Original file line number Diff line number Diff line change
@@ -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<CartModel | undefined> {
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);
}
26 changes: 24 additions & 2 deletions src/catalog/functions/getProductById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import { createWid } from '../../util';
import { ProductModel } from '../../whatsapp';
import { queryProduct } from '../../whatsapp/functions';

/**
Expand All @@ -33,7 +32,30 @@ import { queryProduct } from '../../whatsapp/functions';
export async function getProductById(
chatId: string,
productId: number
): Promise<ProductModel> {
): 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;
Expand Down

0 comments on commit f2087d7

Please sign in to comment.