218 lines
7.8 KiB
JavaScript
218 lines
7.8 KiB
JavaScript
const { sessions } = require('../sessions')
|
|
const { sendErrorResponse } = require('../utils')
|
|
|
|
/**
|
|
* Retrieves information about a WhatsApp contact by ID.
|
|
*
|
|
* @async
|
|
* @function
|
|
* @param {Object} req - The request object.
|
|
* @param {Object} res - The response object.
|
|
* @param {string} req.params.sessionId - The ID of the current session.
|
|
* @param {string} req.body.contactId - The ID of the contact to retrieve information for.
|
|
* @throws {Error} If there is an error retrieving the contact information.
|
|
* @returns {Object} The contact information object.
|
|
*/
|
|
const getClassInfo = async (req, res) => {
|
|
try {
|
|
const { contactId } = req.body
|
|
const client = sessions.get(req.params.sessionId)
|
|
const contact = await client.getContactById(contactId)
|
|
if (!contact) {
|
|
sendErrorResponse(res, 404, 'Contact not Found')
|
|
}
|
|
res.json({ success: true, result: contact })
|
|
} catch (error) {
|
|
sendErrorResponse(res, 500, error.message)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Blocks a WhatsApp contact by ID.
|
|
*
|
|
* @async
|
|
* @function
|
|
* @param {Object} req - The request object.
|
|
* @param {Object} res - The response object.
|
|
* @param {string} req.params.sessionId - The ID of the current session.
|
|
* @param {string} req.body.contactId - The ID of the contact to block.
|
|
* @throws {Error} If there is an error blocking the contact.
|
|
* @returns {Object} The result of the blocking operation.
|
|
*/
|
|
const block = async (req, res) => {
|
|
try {
|
|
const { contactId } = req.body
|
|
const client = sessions.get(req.params.sessionId)
|
|
const contact = await client.getContactById(contactId)
|
|
if (!contact) {
|
|
sendErrorResponse(res, 404, 'Contact not Found')
|
|
}
|
|
const result = await contact.block()
|
|
res.json({ success: true, result })
|
|
} catch (error) {
|
|
sendErrorResponse(res, 500, error.message)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Retrieves the 'About' information of a WhatsApp contact by ID.
|
|
*
|
|
* @async
|
|
* @function
|
|
* @param {Object} req - The request object.
|
|
* @param {Object} res - The response object.
|
|
* @param {string} req.params.sessionId - The ID of the current session.
|
|
* @param {string} req.body.contactId - The ID of the contact to retrieve 'About' information for.
|
|
* @throws {Error} If there is an error retrieving the contact information.
|
|
* @returns {Object} The 'About' information of the contact.
|
|
*/
|
|
const getAbout = async (req, res) => {
|
|
try {
|
|
const { contactId } = req.body
|
|
const client = sessions.get(req.params.sessionId)
|
|
const contact = await client.getContactById(contactId)
|
|
if (!contact) {
|
|
sendErrorResponse(res, 404, 'Contact not Found')
|
|
}
|
|
const result = await contact.getAbout()
|
|
res.json({ success: true, result })
|
|
} catch (error) {
|
|
sendErrorResponse(res, 500, error.message)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Retrieves the chat information of a contact with a given contactId.
|
|
*
|
|
* @async
|
|
* @function getChat
|
|
* @param {Object} req - The request object.
|
|
* @param {Object} res - The response object.
|
|
* @param {string} req.params.sessionId - The session ID.
|
|
* @param {string} req.body.contactId - The ID of the client whose chat information is being retrieved.
|
|
* @throws {Error} If the contact with the given contactId is not found or if there is an error retrieving the chat information.
|
|
* @returns {Promise<void>} A promise that resolves with the chat information of the contact.
|
|
*/
|
|
const getChat = async (req, res) => {
|
|
try {
|
|
const { contactId } = req.body
|
|
const client = sessions.get(req.params.sessionId)
|
|
const contact = await client.getContactById(contactId)
|
|
if (!contact) { sendErrorResponse(res, 404, 'Contact not Found') }
|
|
const result = await contact.getChat()
|
|
res.json({ success: true, result })
|
|
} catch (error) {
|
|
sendErrorResponse(res, 500, error.message)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Retrieves the formatted number of a contact with a given contactId.
|
|
*
|
|
* @async
|
|
* @function getFormattedNumber
|
|
* @param {Object} req - The request object.
|
|
* @param {Object} res - The response object.
|
|
* @param {string} req.params.sessionId - The session ID.
|
|
* @param {string} req.body.contactId - The ID of the client whose chat information is being retrieved.
|
|
* @throws {Error} If the contact with the given contactId is not found or if there is an error retrieving the chat information.
|
|
* @returns {Promise<void>} A promise that resolves with the formatted number of the contact.
|
|
*/
|
|
const getFormattedNumber = async (req, res) => {
|
|
try {
|
|
const { contactId } = req.body
|
|
const client = sessions.get(req.params.sessionId)
|
|
const contact = await client.getContactById(contactId)
|
|
if (!contact) { sendErrorResponse(res, 404, 'Contact not Found') }
|
|
const result = await contact.getFormattedNumber()
|
|
res.json({ success: true, result })
|
|
} catch (error) {
|
|
sendErrorResponse(res, 500, error.message)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Retrieves the country code of a contact with a given contactId.
|
|
*
|
|
* @async
|
|
* @function getCountryCode
|
|
* @param {Object} req - The request object.
|
|
* @param {Object} res - The response object.
|
|
* @param {string} req.params.sessionId - The session ID.
|
|
* @param {string} req.body.contactId - The ID of the client whose chat information is being retrieved.
|
|
* @throws {Error} If the contact with the given contactId is not found or if there is an error retrieving the chat information.
|
|
* @returns {Promise<void>} A promise that resolves with the country code of the contact.
|
|
*/
|
|
const getCountryCode = async (req, res) => {
|
|
try {
|
|
const { contactId } = req.body
|
|
const client = sessions.get(req.params.sessionId)
|
|
const contact = await client.getContactById(contactId)
|
|
if (!contact) { sendErrorResponse(res, 404, 'Contact not Found') }
|
|
const result = await contact.getCountryCode()
|
|
res.json({ success: true, result })
|
|
} catch (error) {
|
|
sendErrorResponse(res, 500, error.message)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Retrieves the profile picture url of a contact with a given contactId.
|
|
*
|
|
* @async
|
|
* @function getProfilePicUrl
|
|
* @param {Object} req - The request object.
|
|
* @param {Object} res - The response object.
|
|
* @param {string} req.params.sessionId - The session ID.
|
|
* @param {string} req.body.contactId - The ID of the client whose chat information is being retrieved.
|
|
* @throws {Error} If the contact with the given contactId is not found or if there is an error retrieving the chat information.
|
|
* @returns {Promise<void>} A promise that resolves with the profile picture url of the contact.
|
|
*/
|
|
const getProfilePicUrl = async (req, res) => {
|
|
try {
|
|
const { contactId } = req.body
|
|
const client = sessions.get(req.params.sessionId)
|
|
const contact = await client.getContactById(contactId)
|
|
if (!contact) { sendErrorResponse(res, 404, 'Contact not Found') }
|
|
const result = await contact.getProfilePicUrl() || null
|
|
res.json({ success: true, result })
|
|
} catch (error) {
|
|
sendErrorResponse(res, 500, error.message)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Unblocks the contact with a given contactId.
|
|
*
|
|
* @async
|
|
* @function unblock
|
|
* @param {Object} req - The request object.
|
|
* @param {Object} res - The response object.
|
|
* @param {string} req.params.sessionId - The session ID.
|
|
* @param {string} req.body.contactId - The ID of the client whose contact is being unblocked.
|
|
* @throws {Error} If the contact with the given contactId is not found or if there is an error unblocking the contact.
|
|
* @returns {Promise<void>} A promise that resolves with the result of unblocking the contact.
|
|
*/
|
|
const unblock = async (req, res) => {
|
|
try {
|
|
const { contactId } = req.body
|
|
const client = sessions.get(req.params.sessionId)
|
|
const contact = await client.getContactById(contactId)
|
|
if (!contact) { sendErrorResponse(res, 404, 'Contact not Found') }
|
|
const result = await contact.unblock()
|
|
res.json({ success: true, result })
|
|
} catch (error) {
|
|
sendErrorResponse(res, 500, error.message)
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
getClassInfo,
|
|
block,
|
|
getAbout,
|
|
getChat,
|
|
unblock,
|
|
getFormattedNumber,
|
|
getCountryCode,
|
|
getProfilePicUrl
|
|
}
|