Skip to content

Commit

Permalink
feat: Added get, create, edit, and delete collections functions (fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
icleitoncosta committed Aug 13, 2022
1 parent 8e8bb22 commit ef6192b
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 2 deletions.
37 changes: 37 additions & 0 deletions src/catalog/functions/createCollection.ts
@@ -0,0 +1,37 @@
/*!
* 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 { ProductCatalogSession } from '../../whatsapp';
import { createCollection as CreateCollection } from '../../whatsapp/functions';

/**
* Create new collection
*
* @example
* ```javascript
* const myCatalog = await WPP.catalog.createCollection('Collection Name', ['565656589898']);
* ```
*
* @return Return collection created
*/
export async function createCollection(
collectionName: string,
productsId: string[]
): Promise<any> {
const { sessionId } = new ProductCatalogSession(true);

return await CreateCollection(collectionName, productsId, `${sessionId}`);
}
34 changes: 34 additions & 0 deletions src/catalog/functions/deleteCollection.ts
@@ -0,0 +1,34 @@
/*!
* 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 { ProductCatalogSession } from '../../whatsapp';
import { deleteCollection as DeleteCollection } from '../../whatsapp/functions';

/**
* Delete a collection
*
* @example
* ```javascript
* const myCatalog = await WPP.catalog.deleteCollection("377095767832354");
* ```
*
* @return Return sucess or error
*/
export async function deleteCollection(collectionId: string): Promise<any> {
const { sessionId } = new ProductCatalogSession(true);
await DeleteCollection(collectionId, `${sessionId}`);
return 'Collection deleted sucessful';
}
49 changes: 49 additions & 0 deletions src/catalog/functions/editCollection.ts
@@ -0,0 +1,49 @@
/*!
* 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 { ProductCatalogSession } from '../../whatsapp';
import { editCollection as EditCollection } from '../../whatsapp/functions';

/**
* Create new collection
*
* @example
* ```javascript
* const myCatalog = await WPP.catalog.EditCollection('565656589898', { collectionName: 'New Name for collection', productsToAdd: ['5656523223'], productsToRemove: ['5656523232']});
* ```
*
* @return Return collection edited
*/
interface paramsEditCollection {
name?: string;
productsToAdd?: string[];
productsToRemove?: string[];
}
export async function editCollection(
collectionId: string,
params: paramsEditCollection
): Promise<any> {
const { sessionId } = new ProductCatalogSession(true);

return await EditCollection(
collectionId,
params.name,
false,
params.productsToAdd || [],
params.productsToRemove || [],
`${sessionId}`
);
}
49 changes: 49 additions & 0 deletions src/catalog/functions/getCollections.ts
@@ -0,0 +1,49 @@
/*!
* 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 { createWid } from '../../util';
import { ProductCollModel } from '../../whatsapp';
import { queryCollectionsIQ } from '../../whatsapp/functions';

/**
* Get collections of catalog
*
* @example
* ```javascript
* // Retrieve 20 collections of chat
* const myCatalog = await WPP.catalog.getCollections('552198554578@c.us', '20');
*
* // Retrieve 20 collections of chat and products arrays limit with 10 products
* const myCatalog = await WPP.catalog.getCollections('552198554578@c.us', '20', '10');
* ```
*
* @return Your collections of products
*/
export async function getCollections(
chatId: string,
qnt?: number,
productsCount?: number
): Promise<ProductCollModel[]> {
const { collections } = await queryCollectionsIQ({
afterCursor: '',
catalogWid: createWid(chatId),
height: 100,
width: 100,
limit: qnt || 10,
productsCount: productsCount || 10,
});
return collections;
}
12 changes: 11 additions & 1 deletion src/whatsapp/collections/CatalogCollection.ts
Expand Up @@ -15,6 +15,7 @@
*/

import { exportModule } from '../exportModule';
import { Wid } from '../misc';
import { CatalogModel } from '../models';
import { Collection } from './Collection';

Expand All @@ -24,15 +25,24 @@ import { Collection } from './Collection';
export declare class CatalogCollection extends Collection<CatalogModel> {
static model: CatalogModel;
static staleCollection?: any;

addMsgAsProduct(e?: any): any;
findProduct(e?: {
catalogWid: any;
productId: any;
productMsgMediaData: any;
productMsgMediaData?: any;
}): any;
findCarouselCatalog(e?: any): any;
findNextProductPage(e?: any): any;
findCollectionMembership(e?: any, t?: any): any;
_queryCatalog(
e?: CatalogModel,
t?: Wid,
n?: any,
r?: any,
a?: any,
o?: any
): any;
}
exportModule(
exports,
Expand Down
65 changes: 65 additions & 0 deletions src/whatsapp/functions/collections.ts
@@ -0,0 +1,65 @@
/*!
* 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 { exportModule } from '../exportModule';
import { Wid } from '../misc';

/** @whatsapp 409465
*/
export declare function createCollection(...args: any[]): Promise<any>;

/** @whatsapp 409465
*/
export declare function deleteCollection(
id: string,
sessionId: string
): Promise<any>;

/** @whatsapp 409465
*/
export declare function editCollection(
collectionId: string,
collectionName: string | undefined,
collectionBoolea: boolean,
productsToAdd: string[],
productsToRemove: string[],
sessionId: string
): Promise<any>;

export interface QueryCollectionsIQtParams {
afterCursor?: string;
catalogWid?: Wid;
directConnectionEncryptedInfo?: any;
height?: 100;
width?: 100;
limit: number;
productsCount?: number;
}

export declare function queryCollectionsIQ(
params: QueryCollectionsIQtParams
): any;

exportModule(
exports,
{
createCollection: 'createCollection',
deleteCollection: 'deleteCollection',
editCollection: 'editCollection',
queryCollectionsIQ: 'queryCollectionsIQ',
},
(m) => m.createCollection
);
2 changes: 2 additions & 0 deletions src/whatsapp/functions/index.ts
Expand Up @@ -16,6 +16,8 @@

export * from './addAndSendMsgToChat';
export * from './blockContact';
export * from './calculateFilehashFromBlob';
export * from './collections';
export * from './createMsgProtobuf';
export * from './createOrUpdateReactions';
export * from './editBusinessProfile';
Expand Down
2 changes: 2 additions & 0 deletions src/whatsapp/models/CatalogModel.ts
Expand Up @@ -53,6 +53,8 @@ export declare class CatalogModel extends Model<CatalogCollection> {
proterties?: ModelPropertiesContructor<CatalogModel>,
options?: ModelOptions
);
productCollection: ProductModel[];
collections: any;
triggerProductUpdate(): any;
triggerMsgUpdate(): any;
markProductCollectionOld(): any;
Expand Down
4 changes: 4 additions & 0 deletions src/whatsapp/models/ProductCollModel.ts
Expand Up @@ -24,10 +24,14 @@ import {

interface Props {
id?: any;
name?: string;
isHidden: boolean;
reviewStatus?: any;
commerceUrl?: any;
rejectReason?: any;
totalItemsCount?: any;
afterCursor?: any;
canAppeal?: any;
}

interface Session {
Expand Down
3 changes: 2 additions & 1 deletion src/whatsapp/models/ProductModel.ts
Expand Up @@ -24,9 +24,10 @@ import {

interface Props {
id?: any;
isHidden: boolean;
isHidden?: boolean;
catalogWid?: any;
url?: any;
name?: string;
description?: any;
availability?: any;
reviewStatus?: any;
Expand Down

0 comments on commit ef6192b

Please sign in to comment.