feat: add ability to run any method
This commit is contained in:
parent
d609c0ca85
commit
b7d1e21739
6 changed files with 798 additions and 22 deletions
|
|
@ -1855,6 +1855,55 @@ const getContactLidAndPhone = async (req, res) => {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a method on the client associated with the given sessionId.
|
||||
*
|
||||
* @async
|
||||
* @function
|
||||
* @param {Object} req - The HTTP request object containing the chatId and sessionId.
|
||||
* @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<Object>} - 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 runMethod = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Execute a method on the client'
|
||||
#swagger.description = 'Execute a method on the client and return the result'
|
||||
#swagger.requestBody = {
|
||||
required: true,
|
||||
schema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
method: {
|
||||
type: 'string',
|
||||
description: 'The name of the method to execute',
|
||||
example: 'getLabels'
|
||||
},
|
||||
options: {
|
||||
anyOf: [
|
||||
{ type: 'object' },
|
||||
{ type: 'string' }
|
||||
],
|
||||
description: 'The options to pass to the method',
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
*/
|
||||
try {
|
||||
const { method, options } = req.body
|
||||
const client = sessions.get(req.params.sessionId)
|
||||
if (typeof client[method] !== 'function') {
|
||||
throw new Error('Method is not implemented')
|
||||
}
|
||||
const result = options ? await client[method](options) : await client[method]()
|
||||
res.json({ success: true, data: result })
|
||||
} catch (error) {
|
||||
sendErrorResponse(res, 500, error.message)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getClassInfo,
|
||||
acceptInvite,
|
||||
|
|
@ -1904,5 +1953,6 @@ module.exports = {
|
|||
openChatWindowAt,
|
||||
resetState,
|
||||
setBackgroundSync,
|
||||
getContactLidAndPhone
|
||||
getContactLidAndPhone,
|
||||
runMethod
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue