mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-16 12:12:55 -06:00
feat: Add support to get Catalogs and Collections with new routes: '{{baseUrl}}/chat/fetchCatalogs' and '{{baseUrl}}/chat/fetchCollections'
This commit is contained in:
parent
427c994993
commit
e27db0612f
@ -13,6 +13,8 @@ import {
|
|||||||
SendPresenceDto,
|
SendPresenceDto,
|
||||||
UpdateMessageDto,
|
UpdateMessageDto,
|
||||||
WhatsAppNumberDto,
|
WhatsAppNumberDto,
|
||||||
|
getCatalogDto,
|
||||||
|
getCollectionsDto,
|
||||||
} from '@api/dto/chat.dto';
|
} from '@api/dto/chat.dto';
|
||||||
import { InstanceDto } from '@api/dto/instance.dto';
|
import { InstanceDto } from '@api/dto/instance.dto';
|
||||||
import { Query } from '@api/repository/repository.service';
|
import { Query } from '@api/repository/repository.service';
|
||||||
@ -109,4 +111,12 @@ export class ChatController {
|
|||||||
public async blockUser({ instanceName }: InstanceDto, data: BlockUserDto) {
|
public async blockUser({ instanceName }: InstanceDto, data: BlockUserDto) {
|
||||||
return await this.waMonitor.waInstances[instanceName].blockUser(data);
|
return await this.waMonitor.waInstances[instanceName].blockUser(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async fetchCatalog({ instanceName }: InstanceDto, data: getCatalogDto) {
|
||||||
|
return await this.waMonitor.waInstances[instanceName].fetchCatalog(instanceName, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async fetchCatalogCollections({ instanceName }: InstanceDto, data: getCollectionsDto) {
|
||||||
|
return await this.waMonitor.waInstances[instanceName].fetchCatalogCollections(instanceName, data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -126,3 +126,14 @@ export class BlockUserDto {
|
|||||||
number: string;
|
number: string;
|
||||||
status: 'block' | 'unblock';
|
status: 'block' | 'unblock';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class getCatalogDto {
|
||||||
|
number?: string;
|
||||||
|
limit?: number;
|
||||||
|
cursor?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class getCollectionsDto {
|
||||||
|
number?: string;
|
||||||
|
limit?: number;
|
||||||
|
}
|
||||||
|
@ -13,6 +13,8 @@ import {
|
|||||||
SendPresenceDto,
|
SendPresenceDto,
|
||||||
UpdateMessageDto,
|
UpdateMessageDto,
|
||||||
WhatsAppNumberDto,
|
WhatsAppNumberDto,
|
||||||
|
getCatalogDto,
|
||||||
|
getCollectionsDto,
|
||||||
} from '@api/dto/chat.dto';
|
} from '@api/dto/chat.dto';
|
||||||
import {
|
import {
|
||||||
AcceptGroupInvite,
|
AcceptGroupInvite,
|
||||||
@ -121,6 +123,9 @@ import makeWASocket, {
|
|||||||
WAMessageUpdate,
|
WAMessageUpdate,
|
||||||
WAPresence,
|
WAPresence,
|
||||||
WASocket,
|
WASocket,
|
||||||
|
Product,
|
||||||
|
GetCatalogOptions,
|
||||||
|
CatalogCollection,
|
||||||
} from 'baileys';
|
} from 'baileys';
|
||||||
import { Label } from 'baileys/lib/Types/Label';
|
import { Label } from 'baileys/lib/Types/Label';
|
||||||
import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation';
|
import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation';
|
||||||
@ -4534,4 +4539,118 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Catalogs and collections
|
||||||
|
public async fetchCatalog(instanceName: string, data: getCatalogDto) {
|
||||||
|
const jid = data.number ? createJid(data.number) : this.client?.user?.id;
|
||||||
|
const limit = data.limit || 10;
|
||||||
|
const cursor = data.cursor || null;
|
||||||
|
|
||||||
|
const onWhatsapp = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
|
||||||
|
|
||||||
|
if (!onWhatsapp.exists) {
|
||||||
|
throw new BadRequestException(onWhatsapp);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const info = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
|
||||||
|
const business = await this.fetchBusinessProfile(info?.jid);
|
||||||
|
const catalog = await this.getCatalog({ jid: info?.jid, limit, cursor });
|
||||||
|
|
||||||
|
return {
|
||||||
|
wuid: info?.jid || jid,
|
||||||
|
name: info?.name,
|
||||||
|
numberExists: info?.exists,
|
||||||
|
isBusiness: business.isBusiness,
|
||||||
|
catalogLength: catalog?.products.length,
|
||||||
|
catalog: catalog?.products,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
return {
|
||||||
|
wuid: jid,
|
||||||
|
name: null,
|
||||||
|
isBusiness: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getCatalog({
|
||||||
|
jid,
|
||||||
|
limit,
|
||||||
|
cursor,
|
||||||
|
}: GetCatalogOptions): Promise<{ products: Product[]; nextPageCursor: string | undefined }> {
|
||||||
|
try {
|
||||||
|
jid = jid ? createJid(jid) : this.instance.wuid;
|
||||||
|
|
||||||
|
const catalog = await this.client.getCatalog({ jid, limit: limit, cursor: cursor });
|
||||||
|
|
||||||
|
if (!catalog) {
|
||||||
|
return {
|
||||||
|
products: undefined,
|
||||||
|
nextPageCursor: undefined,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return catalog;
|
||||||
|
} catch (error) {
|
||||||
|
throw new InternalServerErrorException('Error getCatalog', error.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async fetchCatalogCollections(instanceName: string, data: getCollectionsDto) {
|
||||||
|
const jid = data.number ? createJid(data.number) : this.client?.user?.id;
|
||||||
|
const limit = data.limit || 10;
|
||||||
|
|
||||||
|
const onWhatsapp = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
|
||||||
|
|
||||||
|
if (!onWhatsapp.exists) {
|
||||||
|
throw new BadRequestException(onWhatsapp);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const info = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
|
||||||
|
const business = await this.fetchBusinessProfile(info?.jid);
|
||||||
|
const catalogCollections = await this.getCollections(info?.jid, limit);
|
||||||
|
|
||||||
|
return {
|
||||||
|
wuid: info?.jid || jid,
|
||||||
|
name: info?.name,
|
||||||
|
numberExists: info?.exists,
|
||||||
|
isBusiness: business.isBusiness,
|
||||||
|
catalogLength: catalogCollections?.length,
|
||||||
|
catalogCollections: catalogCollections,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
return {
|
||||||
|
wuid: jid,
|
||||||
|
name: null,
|
||||||
|
isBusiness: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getCollections(jid?: string | undefined, limit?: number): Promise<CatalogCollection[]> {
|
||||||
|
try {
|
||||||
|
jid = jid ? createJid(jid) : this.instance.wuid;
|
||||||
|
|
||||||
|
const result = await this.client.getCollections(jid, limit);
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
id: undefined,
|
||||||
|
name: undefined,
|
||||||
|
products: [],
|
||||||
|
status: undefined,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.collections;
|
||||||
|
} catch (error) {
|
||||||
|
throw new InternalServerErrorException('Error getCatalog', error.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,8 @@ import {
|
|||||||
readMessageSchema,
|
readMessageSchema,
|
||||||
updateMessageSchema,
|
updateMessageSchema,
|
||||||
whatsappNumberSchema,
|
whatsappNumberSchema,
|
||||||
|
catalogSchema,
|
||||||
|
collectionsSchema,
|
||||||
} from '@validate/validate.schema';
|
} from '@validate/validate.schema';
|
||||||
import { RequestHandler, Router } from 'express';
|
import { RequestHandler, Router } from 'express';
|
||||||
|
|
||||||
@ -267,6 +269,28 @@ export class ChatRouter extends RouterBroker {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return res.status(HttpStatus.CREATED).json(response);
|
return res.status(HttpStatus.CREATED).json(response);
|
||||||
|
})
|
||||||
|
|
||||||
|
.post(this.routerPath('fetchCatalog'), ...guards, async (req, res) => {
|
||||||
|
const response = await this.dataValidate<NumberDto>({
|
||||||
|
request: req,
|
||||||
|
schema: catalogSchema,
|
||||||
|
ClassRef: NumberDto,
|
||||||
|
execute: (instance, data) => chatController.fetchCatalog(instance, data),
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(HttpStatus.OK).json(response);
|
||||||
|
})
|
||||||
|
|
||||||
|
.post(this.routerPath('fetchCollections'), ...guards, async (req, res) => {
|
||||||
|
const response = await this.dataValidate<NumberDto>({
|
||||||
|
request: req,
|
||||||
|
schema: collectionsSchema,
|
||||||
|
ClassRef: NumberDto,
|
||||||
|
execute: (instance, data) => chatController.fetchCatalogCollections(instance, data),
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(HttpStatus.OK).json(response);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -315,3 +315,21 @@ export const profileSchema: JSONSchema7 = {
|
|||||||
isBusiness: { type: 'boolean' },
|
isBusiness: { type: 'boolean' },
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const catalogSchema: JSONSchema7 = {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
number: { type: 'string' },
|
||||||
|
limit: { type: 'number' },
|
||||||
|
cursor: { type: 'string' },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const collectionsSchema: JSONSchema7 = {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
number: { type: 'string' },
|
||||||
|
limit: { type: 'number' },
|
||||||
|
cursor: { type: 'string' },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user