feat: update group controller

This commit is contained in:
Anton Voylenko 2025-06-07 23:18:24 +03:00
commit 5628991556
3 changed files with 103 additions and 75 deletions

View file

@ -250,7 +250,7 @@ const createGroup = async (req, res) => {
example: [] example: []
}, },
options: { options: {
type: 'array', type: 'object',
description: 'An object that handles options for group creation', description: 'An object that handles options for group creation',
example: {} example: {}
}, },

View file

@ -33,17 +33,17 @@ const getClassInfo = async (req, res) => {
* Adds participants to a group chat. * Adds participants to a group chat.
* @async * @async
* @function * @function
* @param {Object} req - The request object containing the chatId and contactIds in the body. * @param {Object} req - The request object containing the chatId and participantIds in the body.
* @param {string} req.body.chatId - The ID of the group chat. * @param {string} req.body.chatId - The ID of the group chat.
* @param {Array<string>} req.body.contactIds - An array of contact IDs to be added to the group. * @param {Array<string>} req.body.participantIds - An array of participant IDs to be added to the group.
* @param {Object} res - The response object. * @param {Object} res - The response object.
* @returns {Object} Returns a JSON object containing a success flag and the updated participants list. * @returns {Object} Returns a JSON object containing a success flag and the updated participants list.
* @throws {Error} Throws an error if the chat is not a group chat. * @throws {Error} Throws an error if the chat is not a group chat.
*/ */
const addParticipants = async (req, res) => { const addParticipants = async (req, res) => {
/* /*
#swagger.summary = 'Add the participant(s)' #swagger.summary = 'Add the participants'
#swagger.description = 'Add a list of participants by id to the group' #swagger.description = 'Add a list of participants by ID to the group'
#swagger.requestBody = { #swagger.requestBody = {
required: true, required: true,
schema: { schema: {
@ -54,10 +54,10 @@ const addParticipants = async (req, res) => {
description: 'Unique WhatsApp id for the given chat group', description: 'Unique WhatsApp id for the given chat group',
example: 'XXXXXXXXXX@g.us' example: 'XXXXXXXXXX@g.us'
}, },
contactIds: { participantIds: {
type: 'string', type: 'array',
description: 'Unique WhatsApp identifier for the contact', description: 'Unique WhatsApp identifiers for the participants',
example: '6281288888887@c.us' example: ['6281288888887@c.us']
}, },
options: { options: {
type: 'object', type: 'object',
@ -69,11 +69,14 @@ const addParticipants = async (req, res) => {
} }
*/ */
try { try {
const { chatId, contactIds, options = {} } = req.body const { chatId, contactIds, participantIds, options = {} } = req.body
const client = sessions.get(req.params.sessionId) const client = sessions.get(req.params.sessionId)
const chat = await client.getChatById(chatId) const chat = await client.getChatById(chatId)
if (!chat.isGroup) { throw new Error('The chat is not a group') } if (!chat.isGroup) { throw new Error('The chat is not a group') }
const result = Object.keys(options).length ? await chat.addParticipants(contactIds, options) : await chat.addParticipants(contactIds) // support old property name
const addParticipants = participantIds || contactIds
const participantIdsArray = Array.isArray(addParticipants) ? addParticipants : [addParticipants]
const result = Object.keys(options).length ? await chat.addParticipants(participantIdsArray, options) : await chat.addParticipants(participantIdsArray)
res.json({ success: true, result }) res.json({ success: true, result })
} catch (error) { } catch (error) {
sendErrorResponse(res, 500, error.message) sendErrorResponse(res, 500, error.message)
@ -92,7 +95,7 @@ const addParticipants = async (req, res) => {
*/ */
const removeParticipants = async (req, res) => { const removeParticipants = async (req, res) => {
/* /*
#swagger.summary = 'Remove the participant(s)' #swagger.summary = 'Remove the participants'
#swagger.description = 'Remove a list of participants by ID to the group' #swagger.description = 'Remove a list of participants by ID to the group'
#swagger.requestBody = { #swagger.requestBody = {
required: true, required: true,
@ -104,21 +107,24 @@ const removeParticipants = async (req, res) => {
description: 'Unique WhatsApp id for the given chat group', description: 'Unique WhatsApp id for the given chat group',
example: 'XXXXXXXXXX@g.us' example: 'XXXXXXXXXX@g.us'
}, },
contactIds: { participantIds: {
type: 'string', type: 'array',
description: 'Unique WhatsApp identifier for the contact', description: 'Unique WhatsApp identifiers for the participants',
example: '6281288888887@c.us' example: ['6281288888887@c.us']
} }
} }
} }
} }
*/ */
try { try {
const { chatId, contactIds } = req.body const { chatId, contactIds, participantIds } = req.body
const client = sessions.get(req.params.sessionId) const client = sessions.get(req.params.sessionId)
const chat = await client.getChatById(chatId) const chat = await client.getChatById(chatId)
if (!chat.isGroup) { throw new Error('The chat is not a group') } if (!chat.isGroup) { throw new Error('The chat is not a group') }
const result = await chat.removeParticipants(contactIds) // support old property name
const addParticipants = participantIds || contactIds
const participantIdsArray = Array.isArray(addParticipants) ? addParticipants : [addParticipants]
const result = await chat.removeParticipants(participantIdsArray)
res.json({ success: true, result }) res.json({ success: true, result })
} catch (error) { } catch (error) {
sendErrorResponse(res, 500, error.message) sendErrorResponse(res, 500, error.message)
@ -137,8 +143,8 @@ const removeParticipants = async (req, res) => {
*/ */
const promoteParticipants = async (req, res) => { const promoteParticipants = async (req, res) => {
/* /*
#swagger.summary = 'Promote the participant(s)' #swagger.summary = 'Promote the participants'
#swagger.description = 'Promote participants by IDs to admins' #swagger.description = 'Promote participants by ID to admins'
#swagger.requestBody = { #swagger.requestBody = {
required: true, required: true,
schema: { schema: {
@ -149,21 +155,24 @@ const promoteParticipants = async (req, res) => {
description: 'Unique WhatsApp id for the given chat group', description: 'Unique WhatsApp id for the given chat group',
example: 'XXXXXXXXXX@g.us' example: 'XXXXXXXXXX@g.us'
}, },
contactIds: { participantIds: {
type: 'string', type: 'array',
description: 'Unique WhatsApp identifier for the contact', description: 'Unique WhatsApp identifiers for the participants',
example: '6281288888887@c.us' example: ['6281288888887@c.us']
} }
} }
} }
} }
*/ */
try { try {
const { chatId, contactIds } = req.body const { chatId, contactIds, participantIds } = req.body
const client = sessions.get(req.params.sessionId) const client = sessions.get(req.params.sessionId)
const chat = await client.getChatById(chatId) const chat = await client.getChatById(chatId)
if (!chat.isGroup) { throw new Error('The chat is not a group') } if (!chat.isGroup) { throw new Error('The chat is not a group') }
const result = await chat.promoteParticipants(contactIds) // support old property name
const addParticipants = participantIds || contactIds
const participantIdsArray = Array.isArray(addParticipants) ? addParticipants : [addParticipants]
const result = await chat.promoteParticipants(participantIdsArray)
res.json({ success: true, result }) res.json({ success: true, result })
} catch (error) { } catch (error) {
sendErrorResponse(res, 500, error.message) sendErrorResponse(res, 500, error.message)
@ -182,8 +191,8 @@ const promoteParticipants = async (req, res) => {
*/ */
const demoteParticipants = async (req, res) => { const demoteParticipants = async (req, res) => {
/* /*
#swagger.summary = 'Demote the participant(s)' #swagger.summary = 'Demote the participants'
#swagger.description = 'Demote participants by ids to regular users' #swagger.description = 'Demote participants by ID to regular users'
#swagger.requestBody = { #swagger.requestBody = {
required: true, required: true,
schema: { schema: {
@ -194,21 +203,24 @@ const demoteParticipants = async (req, res) => {
description: 'Unique WhatsApp id for the given chat group', description: 'Unique WhatsApp id for the given chat group',
example: 'XXXXXXXXXX@g.us' example: 'XXXXXXXXXX@g.us'
}, },
contactIds: { participantIds: {
type: 'string', type: 'array',
description: 'Unique WhatsApp identifier for the contact', description: 'Unique WhatsApp identifiers for the participants',
example: '6281288888887@c.us' example: ['6281288888887@c.us']
} }
} }
} }
} }
*/ */
try { try {
const { chatId, contactIds } = req.body const { chatId, contactIds, participantIds } = req.body
const client = sessions.get(req.params.sessionId) const client = sessions.get(req.params.sessionId)
const chat = await client.getChatById(chatId) const chat = await client.getChatById(chatId)
if (!chat.isGroup) { throw new Error('The chat is not a group') } if (!chat.isGroup) { throw new Error('The chat is not a group') }
const result = await chat.demoteParticipants(contactIds) // support old property name
const addParticipants = participantIds || contactIds
const participantIdsArray = Array.isArray(addParticipants) ? addParticipants : [addParticipants]
const result = await chat.demoteParticipants(participantIdsArray)
res.json({ success: true, result }) res.json({ success: true, result })
} catch (error) { } catch (error) {
sendErrorResponse(res, 500, error.message) sendErrorResponse(res, 500, error.message)

View file

@ -1163,7 +1163,7 @@
"example": [] "example": []
}, },
"options": { "options": {
"type": "array", "type": "object",
"description": "An object that handles options for group creation", "description": "An object that handles options for group creation",
"example": {} "example": {}
} }
@ -1185,7 +1185,7 @@
"example": [] "example": []
}, },
"options": { "options": {
"type": "array", "type": "object",
"description": "An object that handles options for group creation", "description": "An object that handles options for group creation",
"example": {} "example": {}
} }
@ -6838,8 +6838,8 @@
"tags": [ "tags": [
"Group Chat" "Group Chat"
], ],
"summary": "Add the participant(s)", "summary": "Add the participants",
"description": "Add a list of participants by id to the group", "description": "Add a list of participants by ID to the group",
"parameters": [ "parameters": [
{ {
"name": "sessionId", "name": "sessionId",
@ -6914,10 +6914,12 @@
"description": "Unique WhatsApp id for the given chat group", "description": "Unique WhatsApp id for the given chat group",
"example": "XXXXXXXXXX@g.us" "example": "XXXXXXXXXX@g.us"
}, },
"contactIds": { "participantIds": {
"type": "string", "type": "array",
"description": "Unique WhatsApp identifier for the contact", "description": "Unique WhatsApp identifiers for the participants",
"example": "6281288888887@c.us" "example": [
"6281288888887@c.us"
]
}, },
"options": { "options": {
"type": "object", "type": "object",
@ -6942,10 +6944,12 @@
"description": "Unique WhatsApp id for the given chat group", "description": "Unique WhatsApp id for the given chat group",
"example": "XXXXXXXXXX@g.us" "example": "XXXXXXXXXX@g.us"
}, },
"contactIds": { "participantIds": {
"type": "string", "type": "array",
"description": "Unique WhatsApp identifier for the contact", "description": "Unique WhatsApp identifiers for the participants",
"example": "6281288888887@c.us" "example": [
"6281288888887@c.us"
]
}, },
"options": { "options": {
"type": "object", "type": "object",
@ -6970,8 +6974,8 @@
"tags": [ "tags": [
"Group Chat" "Group Chat"
], ],
"summary": "Demote the participant(s)", "summary": "Demote the participants",
"description": "Demote participants by ids to regular users", "description": "Demote participants by ID to regular users",
"parameters": [ "parameters": [
{ {
"name": "sessionId", "name": "sessionId",
@ -7046,10 +7050,12 @@
"description": "Unique WhatsApp id for the given chat group", "description": "Unique WhatsApp id for the given chat group",
"example": "XXXXXXXXXX@g.us" "example": "XXXXXXXXXX@g.us"
}, },
"contactIds": { "participantIds": {
"type": "string", "type": "array",
"description": "Unique WhatsApp identifier for the contact", "description": "Unique WhatsApp identifiers for the participants",
"example": "6281288888887@c.us" "example": [
"6281288888887@c.us"
]
} }
} }
} }
@ -7063,10 +7069,12 @@
"description": "Unique WhatsApp id for the given chat group", "description": "Unique WhatsApp id for the given chat group",
"example": "XXXXXXXXXX@g.us" "example": "XXXXXXXXXX@g.us"
}, },
"contactIds": { "participantIds": {
"type": "string", "type": "array",
"description": "Unique WhatsApp identifier for the contact", "description": "Unique WhatsApp identifiers for the participants",
"example": "6281288888887@c.us" "example": [
"6281288888887@c.us"
]
} }
} }
} }
@ -7280,8 +7288,8 @@
"tags": [ "tags": [
"Group Chat" "Group Chat"
], ],
"summary": "Promote the participant(s)", "summary": "Promote the participants",
"description": "Promote participants by IDs to admins", "description": "Promote participants by ID to admins",
"parameters": [ "parameters": [
{ {
"name": "sessionId", "name": "sessionId",
@ -7356,10 +7364,12 @@
"description": "Unique WhatsApp id for the given chat group", "description": "Unique WhatsApp id for the given chat group",
"example": "XXXXXXXXXX@g.us" "example": "XXXXXXXXXX@g.us"
}, },
"contactIds": { "participantIds": {
"type": "string", "type": "array",
"description": "Unique WhatsApp identifier for the contact", "description": "Unique WhatsApp identifiers for the participants",
"example": "6281288888887@c.us" "example": [
"6281288888887@c.us"
]
} }
} }
} }
@ -7373,10 +7383,12 @@
"description": "Unique WhatsApp id for the given chat group", "description": "Unique WhatsApp id for the given chat group",
"example": "XXXXXXXXXX@g.us" "example": "XXXXXXXXXX@g.us"
}, },
"contactIds": { "participantIds": {
"type": "string", "type": "array",
"description": "Unique WhatsApp identifier for the contact", "description": "Unique WhatsApp identifiers for the participants",
"example": "6281288888887@c.us" "example": [
"6281288888887@c.us"
]
} }
} }
} }
@ -7390,7 +7402,7 @@
"tags": [ "tags": [
"Group Chat" "Group Chat"
], ],
"summary": "Remove the participant(s)", "summary": "Remove the participants",
"description": "Remove a list of participants by ID to the group", "description": "Remove a list of participants by ID to the group",
"parameters": [ "parameters": [
{ {
@ -7466,10 +7478,12 @@
"description": "Unique WhatsApp id for the given chat group", "description": "Unique WhatsApp id for the given chat group",
"example": "XXXXXXXXXX@g.us" "example": "XXXXXXXXXX@g.us"
}, },
"contactIds": { "participantIds": {
"type": "string", "type": "array",
"description": "Unique WhatsApp identifier for the contact", "description": "Unique WhatsApp identifiers for the participants",
"example": "6281288888887@c.us" "example": [
"6281288888887@c.us"
]
} }
} }
} }
@ -7483,10 +7497,12 @@
"description": "Unique WhatsApp id for the given chat group", "description": "Unique WhatsApp id for the given chat group",
"example": "XXXXXXXXXX@g.us" "example": "XXXXXXXXXX@g.us"
}, },
"contactIds": { "participantIds": {
"type": "string", "type": "array",
"description": "Unique WhatsApp identifier for the contact", "description": "Unique WhatsApp identifiers for the participants",
"example": "6281288888887@c.us" "example": [
"6281288888887@c.us"
]
} }
} }
} }