Skip to content

Commit

Permalink
feat: Added WPP.catalog.setProductVisibility function
Browse files Browse the repository at this point in the history
  • Loading branch information
icleitoncosta committed Aug 13, 2022
1 parent 0b883a3 commit d3a60e3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
39 changes: 39 additions & 0 deletions 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<ProductModel> {
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;
}
1 change: 1 addition & 0 deletions src/assert/index.ts
Expand Up @@ -17,4 +17,5 @@
export * from './assertChat';
export * from './assertColor';
export * from './assertIsBusiness';
export * from './assertProduct';
export * from './assertWid';
40 changes: 40 additions & 0 deletions 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<any> {
await productVisibilitySet([{ isHidden: isHidden, productId: productId }]);
return await assertGetProduct(productId);
}

0 comments on commit d3a60e3

Please sign in to comment.