From d3a60e337d897e700dee6540a548d020320a5efa Mon Sep 17 00:00:00 2001 From: icleitoncosta Date: Fri, 12 Aug 2022 22:29:03 -0300 Subject: [PATCH] feat: Added WPP.catalog.setProductVisibility function --- src/assert/assertProduct.ts | 39 ++++++++++++++++++ src/assert/index.ts | 1 + src/catalog/functions/setProductVisibility.ts | 40 +++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 src/assert/assertProduct.ts create mode 100644 src/catalog/functions/setProductVisibility.ts diff --git a/src/assert/assertProduct.ts b/src/assert/assertProduct.ts new file mode 100644 index 0000000000..3381f74866 --- /dev/null +++ b/src/assert/assertProduct.ts @@ -0,0 +1,39 @@ +/*! + * Copyright 2021 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. + */ + +import { WPPError } from '../util'; +import { CatalogStore, ProductModel, UserPrefs } from '../whatsapp'; + +export class InvalidProduct extends WPPError { + constructor(readonly id: string) { + super('product_not_found', `Product not found for ${id}`); + } +} +export async function assertGetProduct( + productId: string +): Promise { + const product = await CatalogStore.findProduct({ + catalogWid: UserPrefs.getMaybeMeUser(), + productId: productId, + }); + const Product = product[0].msgProductCollection._index[productId]; + + if (!Product) { + throw new InvalidProduct(productId); + } + + return Product; +} diff --git a/src/assert/index.ts b/src/assert/index.ts index b8c06fadbe..6e9e14d7b1 100644 --- a/src/assert/index.ts +++ b/src/assert/index.ts @@ -17,4 +17,5 @@ export * from './assertChat'; export * from './assertColor'; export * from './assertIsBusiness'; +export * from './assertProduct'; export * from './assertWid'; diff --git a/src/catalog/functions/setProductVisibility.ts b/src/catalog/functions/setProductVisibility.ts new file mode 100644 index 0000000000..45922d306a --- /dev/null +++ b/src/catalog/functions/setProductVisibility.ts @@ -0,0 +1,40 @@ +/*! + * Copyright 2022 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. + */ + +import { productVisibilitySet } from '../../whatsapp/functions'; +import { assertGetProduct } from '../../assert'; + +/** + * Get your current catalog + * + * @example + * ```javascript + * // Set product visibility hidden + * const myCatalog = await WPP.catalog.setProductVisibility(54985569989897, true); + * ``` + * // Set product visible + * const myCatalog = await WPP.catalog.setProductVisibility(54985569989897, false); + * ``` + * + * @return Return sucess of product visibility set + */ +export async function setProductVisibility( + productId: any, + isHidden: boolean +): Promise { + await productVisibilitySet([{ isHidden: isHidden, productId: productId }]); + return await assertGetProduct(productId); +}