Skip to content

Commit

Permalink
added route: /chat/deleteChat/{instanceName}
Browse files Browse the repository at this point in the history
  • Loading branch information
jrCleber committed Sep 2, 2024
1 parent ea56cbd commit a64817f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
30 changes: 28 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,6 @@ paths:
summary: Fetch a Single Instance
description: |
This endpoint is used to fetch details of a specific instance identified by the `{instance}` path parameter.
security:
- apikeyAuth: []
parameters:
- name: instance
in: path
Expand Down Expand Up @@ -1461,6 +1459,34 @@ paths:
description: Successful response
content:
application/json: {}

/chat/deleteChat/{instanceName}:
delete:
tags:
- Chat Controller
summary: Delete Message
description: |
This endpoint is used to delete a chat in the WhatsApp API. Deleting a chat removes all messages and chat history associated with it.
It requires the `instanceName` parameter in the path and the `chatId` parameter in the query.
parameters:
- name: instanceName
in: path
schema:
type: string
required: true
description: '- required'
example: 'codechat_v1'
- name: chatId
in: query
schema:
type: string
required: true
description: ID of the chat to be deleted
responses:
'200':
description: Successful response
content:
application/json: {}

/chat/fetchProfilePictureUrl/{instanceName}:
post:
Expand Down
4 changes: 4 additions & 0 deletions src/whatsapp/controllers/chat.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export class ChatController {
return await this.waMonitor.waInstances.get(instanceName).archiveChat(data);
}

public async deleteChat({ instanceName }: InstanceDto, data: string) {
return await this.waMonitor.waInstances.get(instanceName).deleteChat(data);
}

public async deleteMessage({ instanceName }: InstanceDto, data: DeleteMessage) {
return await this.waMonitor.waInstances.get(instanceName).deleteMessage(data);
}
Expand Down
12 changes: 11 additions & 1 deletion src/whatsapp/routers/chat.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,17 @@ export function ChatRouter(chatController: ChatController, ...guards: RequestHan
execute: (instance, data) => chatController.deleteMessage(instance, data),
});

return res.status(HttpStatus.CREATED).json(response);
return res.status(HttpStatus.OK).json(response);
})
.delete(routerPath('deleteChat'), ...guards, async (req, res) => {
const instance = req.params as unknown as InstanceDto;
const query = req.query as Record<string, string>;
if (!query?.chatId) {
return res.status(HttpStatus.BAD_REQUEST).json({ message: 'chatId is required' });
}
const response = await chatController.deleteChat(instance, query?.chatId);

return res.status(HttpStatus.OK).json(response);
})
.post(routerPath('fetchProfilePictureUrl'), ...guards, async (req, res) => {
const response = await dataValidate<NumberDto>({
Expand Down
40 changes: 37 additions & 3 deletions src/whatsapp/services/whatsapp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import makeWASocket, {
delay,
DisconnectReason,
downloadMediaMessage,
fetchLatestBaileysVersion,
generateWAMessageFromContent,
getContentType,
getDevice,
Expand Down Expand Up @@ -79,7 +78,7 @@ import {
} from '../../config/env.config';
import { Logger } from '../../config/logger.config';
import { INSTANCE_DIR, ROOT_DIR } from '../../config/path.config';
import { readFileSync } from 'fs';
import { lstat, readFileSync } from 'fs';
import { join } from 'path';
import axios, { AxiosError } from 'axios';
import qrcode, { QRCodeToDataURLOptions } from 'qrcode';
Expand Down Expand Up @@ -1778,6 +1777,39 @@ export class WAStartupService {
}
}

public async deleteChat(chatId: string) {
try {
const lastMessage = await this.repository.message.findFirst({
where: { keyRemoteJid: this.createJid(chatId) },
orderBy: { messageTimestamp: 'desc' },
});
if (!lastMessage) {
throw new Error('Chat not found');
}

await this.client.chatModify(
{
delete: true,
lastMessages: [
{
key: {
id: lastMessage.keyId,
fromMe: lastMessage.keyFromMe,
remoteJid: lastMessage.keyRemoteJid,
},
messageTimestamp: lastMessage.messageTimestamp,
},
],
},
lastMessage.keyRemoteJid,
);

return { deletedAt: new Date(), chatId: lastMessage.keyRemoteJid };
} catch (error) {
throw new BadRequestException('Error while deleting chat', error?.message);
}
}

public async readMessages(data: ReadMessageIdDto) {
const keys: proto.IMessageKey[] = [];
try {
Expand Down Expand Up @@ -1844,7 +1876,7 @@ export class WAStartupService {
if (!everyOne) {
await this.client.chatModify(
{
delete: true,
delete: false as any,
lastMessages: [
{
key: {
Expand All @@ -1869,6 +1901,8 @@ export class WAStartupService {
remoteJid: message.keyRemoteJid,
},
});

return { deletedAt: new Date(), message };
} catch (error) {
throw new InternalServerErrorException(
'Error while deleting message for everyone',
Expand Down

0 comments on commit a64817f

Please sign in to comment.