From 71c8deeb4a2be93f8311a3ced40f4459d5b5866e Mon Sep 17 00:00:00 2001 From: Anton Voylenko Date: Wed, 1 Jan 2025 16:08:38 +0200 Subject: [PATCH] feat: requestPairingCode method added --- src/controllers/sessionController.js | 49 +++++++++++++ src/routes.js | 1 + swagger.json | 100 +++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) diff --git a/src/controllers/sessionController.js b/src/controllers/sessionController.js index 85276a4..9d82ade 100644 --- a/src/controllers/sessionController.js +++ b/src/controllers/sessionController.js @@ -291,11 +291,60 @@ const terminateAllSessions = async (req, res) => { } } +/** + * Request authentication via pairing code instead of QR code. + * + * @async + * @function + * @param {Object} req - The HTTP request object containing the chatId and sessionId. + * @param {string} req.body.phoneNumber - The phone number in international, symbol-free format (e.g. 12025550108 for US, 551155501234 for Brazil). + * @param {boolean} req.body.showNotification - Show notification to pair on phone number. + * @param {string} req.params.sessionId - The unique identifier of the session associated with the client to use. + * @param {Object} res - The HTTP response object. + * @returns {Promise} - A Promise that resolves with a JSON object containing a success flag and the result of the operation. + * @throws {Error} - If an error occurs during the operation, it is thrown and handled by the catch block. + */ +const requestPairingCode = async (req, res) => { + /* + #swagger.summary = 'Request authentication via pairing code' + #swagger.requestBody = { + required: true, + schema: { + type: 'object', + properties: { + phoneNumber: { + type: 'string', + description: 'Phone number in international, symbol-free format', + example: '12025550108' + }, + showNotification: { + type: 'boolean', + description: 'Show notification to pair on phone number', + example: true + }, + } + }, + } + */ + try { + const { phoneNumber, showNotification = true } = req.body + const client = sessions.get(req.params.sessionId) + if (!client) { + return res.json({ success: false, message: 'session_not_found' }) + } + const result = await client.requestPairingCode(phoneNumber, showNotification) + res.json({ success: true, result }) + } catch (error) { + sendErrorResponse(res, 500, error.message) + } +} + module.exports = { startSession, statusSession, sessionQrCode, sessionQrCodeImage, + requestPairingCode, restartSession, terminateSession, terminateInactiveSessions, diff --git a/src/routes.js b/src/routes.js index f42ece6..93b2658 100644 --- a/src/routes.js +++ b/src/routes.js @@ -40,6 +40,7 @@ sessionRouter.get('/start/:sessionId', middleware.sessionNameValidation, session sessionRouter.get('/status/:sessionId', middleware.sessionNameValidation, sessionController.statusSession) sessionRouter.get('/qr/:sessionId', middleware.sessionNameValidation, sessionController.sessionQrCode) sessionRouter.get('/qr/:sessionId/image', middleware.sessionNameValidation, sessionController.sessionQrCodeImage) +sessionRouter.post('/requestPairingCode/:sessionId', middleware.sessionNameValidation, sessionController.requestPairingCode) sessionRouter.get('/restart/:sessionId', middleware.sessionNameValidation, sessionController.restartSession) sessionRouter.get('/terminate/:sessionId', middleware.sessionNameValidation, sessionController.terminateSession) sessionRouter.get('/terminateInactive', sessionController.terminateInactiveSessions) diff --git a/swagger.json b/swagger.json index 1d277ef..e1fa9e5 100644 --- a/swagger.json +++ b/swagger.json @@ -367,6 +367,106 @@ ] } }, + "/session/requestPairingCode/{sessionId}": { + "post": { + "tags": [ + "Session" + ], + "summary": "Request authentication via pairing code", + "description": "", + "parameters": [ + { + "name": "sessionId", + "in": "path", + "required": true, + "schema": { + "type": "string" + }, + "description": "Unique identifier for the session (alphanumeric and - allowed)", + "example": "f8377d8d-a589-4242-9ba6-9486a04ef80c" + } + ], + "responses": { + "200": { + "description": "OK" + }, + "403": { + "description": "Forbidden.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ForbiddenResponse" + } + } + } + }, + "422": { + "description": "Unprocessable Entity.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorResponse" + } + } + } + }, + "500": { + "description": "Server failure.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorResponse" + } + } + } + } + }, + "security": [ + { + "apiKeyAuth": [] + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "phoneNumber": { + "type": "string", + "description": "Phone number in international, symbol-free format", + "example": "12025550108" + }, + "showNotification": { + "type": "boolean", + "description": "Show notification to pair on phone number", + "example": true + } + } + } + }, + "application/xml": { + "schema": { + "type": "object", + "properties": { + "phoneNumber": { + "type": "string", + "description": "Phone number in international, symbol-free format", + "example": "12025550108" + }, + "showNotification": { + "type": "boolean", + "description": "Show notification to pair on phone number", + "example": true + } + } + } + } + } + } + } + }, "/session/restart/{sessionId}": { "get": { "tags": [