From 1a7dcafbdf4b0c7122e6dab65e8cd0d765e61d68 Mon Sep 17 00:00:00 2001 From: "Alexeev A.A" Date: Thu, 21 Nov 2024 10:30:59 +0000 Subject: [PATCH] Added support group membership request --- src/controller/groupController.ts | 176 ++++++++++++++++++++++++++++++ src/routes/index.ts | 18 +++ 2 files changed, 194 insertions(+) diff --git a/src/controller/groupController.ts b/src/controller/groupController.ts index f2a5e0bd43..5f723a7288 100644 --- a/src/controller/groupController.ts +++ b/src/controller/groupController.ts @@ -506,6 +506,182 @@ export async function demoteParticipant(req: Request, res: Response) { } } +export async function approveGroupMembershipRequest(req: Request, res: Response) { + /** + #swagger.tags = ["Group"] + #swagger.autoBody=false + #swagger.security = [{ + "bearerAuth": [] + }] + #swagger.parameters["session"] = { + schema: 'NERDWHATS_AMERICA' + } + #swagger.requestBody = { + required: true, + "@content": { + "application/json": { + schema: { + type: "object", + properties: { + "groupId": { type: "string" }, + "phone": { type: "string" } + }, + required: ["groupId", "phone"] + }, + examples: { + "Default": { + value: { + "groupId": "", + "phone": "5521999999999" + } + } + } + } + } + } + */ + const { groupId, phone } = req.body; + + try { + const arrayGroups: any = []; + for (const group of groupToArray(groupId)) { + await req.client.approveGroupMembershipRequest(group, contactToArray(phone)); + arrayGroups.push(group); + } + + return res.status(201).json({ + status: 'success', + response: { + message: 'Group membership Request(s) approve successfully', + participants: phone, + groups: arrayGroups, + }, + }); + } catch (e) { + req.logger.error(e); + return res.status(500).json({ + status: 'error', + message: "Error approve membership request's", + error: e, + }); + } +} + +export async function rejectGroupMembershipRequest(req: Request, res: Response) { + /** + #swagger.tags = ["Group"] + #swagger.autoBody=false + #swagger.security = [{ + "bearerAuth": [] + }] + #swagger.parameters["session"] = { + schema: 'NERDWHATS_AMERICA' + } + #swagger.requestBody = { + required: true, + "@content": { + "application/json": { + schema: { + type: "object", + properties: { + "groupId": { type: "string" }, + "phone": { type: "string" } + }, + required: ["groupId", "phone"] + }, + examples: { + "Default": { + value: { + "groupId": "", + "phone": "5521999999999" + } + } + } + } + } + } + */ + const { groupId, phone } = req.body; + + try { + const arrayGroups: any = []; + for (const group of groupToArray(groupId)) { + await req.client.rejectGroupMembershipRequest(group, contactToArray(phone)); + arrayGroups.push(group); + } + + return res.status(201).json({ + status: 'success', + response: { + message: 'Group membership Request(s) reject successfully', + participants: phone, + groups: arrayGroups, + }, + }); + } catch (e) { + req.logger.error(e); + return res.status(500).json({ + status: 'error', + message: "Error reject membership request's", + error: e, + }); + } +} + +export async function getGroupMembershipRequests(req: Request, res: Response) { + /** + #swagger.tags = ["Group"] + #swagger.autoBody=false + #swagger.security = [{ + "bearerAuth": [] + }] + #swagger.parameters["session"] = { + schema: 'NERDWHATS_AMERICA' + } + #swagger.requestBody = { + required: true, + "@content": { + "application/json": { + schema: { + type: "object", + properties: { + "groupId": { type: "string" } + }, + required: ["groupId"] + }, + examples: { + "Default": { + value: { + "groupId": "" + } + } + } + } + } + } + */ + const { groupId } = req.params; + + try { + let response = {}; + const arrayGroups: any = []; + + for (const group of groupToArray(groupId)) { + response = await req.client.getGroupMembershipRequests(group); + arrayGroups.push(response); + } + + return res.status(200).json({ status: 'success', response: arrayGroups }); + } catch (e) { + req.logger.error(e); + return res.status(500).json({ + status: 'error', + message: 'Error retrieving group membership requests(s)', + error: e, + }); + } +} + export async function getGroupAdmins(req: Request, res: Response) { /** #swagger.tags = ["Group"] diff --git a/src/routes/index.ts b/src/routes/index.ts index 32e307eb17..7abe25db5c 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -312,6 +312,24 @@ routes.post( statusConnection, GroupController.demoteParticipant ); +routes.get( + '/api/:session/group-membership-request/:groupId', + verifyToken, + statusConnection, + GroupController.getGroupMembershipRequests +); +routes.post( + '/api/:session/approve-group-membership-request', + verifyToken, + statusConnection, + GroupController.approveGroupMembershipRequest +); +routes.post( + '/api/:session/reject-group-membership-request', + verifyToken, + statusConnection, + GroupController.rejectGroupMembershipRequest +); routes.post( '/api/:session/group-info-from-invite-link', verifyToken,