Skip to content

Commit

Permalink
feat: Added WPP.cart.get function
Browse files Browse the repository at this point in the history
  • Loading branch information
icleitoncosta committed Jul 17, 2024
1 parent 670474d commit 5f205d0
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/cart/functions/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*!
* 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.
*/

/**
* Get products in cart chat
*
* @example
* ```javascript
* const cart = WPP.cart.get('[number]@c.us');
* ```
*
* @category Cart
*/

import { CartModel, CartStore } from '../../whatsapp';

export function get(wid: string): CartModel | undefined {
return CartStore.get(wid);
}
55 changes: 55 additions & 0 deletions src/cart/functions/getThumbFromCart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*!
* 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.
*/

/**
* Get thumb of a cart
*
* @example
* ```javascript
* const cart = WPP.cart.getThumbFromCart('[number]@c.us');
* ```
*
* @category Cart
*/

import { createWid } from '../../util';
import { CartStore, CatalogStore } from '../../whatsapp';

export async function getThumbFromCart(wid: string): Promise<string> {
const cart = await CartStore.findCart(wid);

const item = cart?.cartItemCollection?.at(0);
if (!item || !cart) return '';

const catalog = CatalogStore.get(createWid(wid) as any);
if (!catalog) return '';

const product = (catalog.productCollection as any).get(item.id);
if (!product) return '';

const productImage = await product?.getProductImageCollectionHead();
if (!productImage) return '';

const mediaData = productImage?.mediaData;
if (mediaData == null) return '';

if (mediaData.preview) {
const base64 = await mediaData?.preview?.getBase64();
if (base64 != null) return base64;
}

return '';
}
23 changes: 23 additions & 0 deletions src/cart/functions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*!
* 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.
*/

export { add } from './add';
export { clear } from './clear';
export { get } from './get';
export { getThumbFromCart } from './getThumbFromCart';
export { remove } from './remove';
export { submit } from './submit';
export { update } from './update';

0 comments on commit 5f205d0

Please sign in to comment.