From 4f0f6d09335459c6a9ea5a0704818a0e5e0e8f6a Mon Sep 17 00:00:00 2001 From: Nur Muhammad Date: Tue, 22 Mar 2022 08:19:17 +0800 Subject: [PATCH] Add send message validation for app-multiple-account.js Just a simple example, for more validations example can be founded in app.js --- app-multiple-account.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/app-multiple-account.js b/app-multiple-account.js index 9feac74..e420fe0 100644 --- a/app-multiple-account.js +++ b/app-multiple-account.js @@ -164,12 +164,36 @@ io.on('connection', function(socket) { }); // Send message -app.post('/send-message', (req, res) => { +app.post('/send-message', async (req, res) => { const sender = req.body.sender; const number = phoneNumberFormatter(req.body.number); const message = req.body.message; - const client = sessions.find(sess => sess.id == sender).client; + const client = sessions.find(sess => sess.id == sender)?.client; + + // Make sure the sender is exists & ready + if (!client) { + return res.status(422).json({ + status: false, + message: `The sender: ${sender} is not found!` + }) + } + + /** + * Check if the number is already registered + * Copied from app.js + * + * Please check app.js for more validations example + * You can add the same here! + */ + const isRegisteredNumber = await client.isRegisteredUser(number); + + if (!isRegisteredNumber) { + return res.status(422).json({ + status: false, + message: 'The number is not registered' + }); + } client.sendMessage(number, message).then(response => { res.status(200).json({