From 63c2f24c2a9d892e97d73323b3a20daf327508ed Mon Sep 17 00:00:00 2001 From: Bing Wen Tan Date: Sun, 22 Oct 2023 15:41:20 +0800 Subject: [PATCH] shortened if checks --- .../src/definitions/webhookHandlers/whatsapp.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/functions/src/definitions/webhookHandlers/whatsapp.ts b/functions/src/definitions/webhookHandlers/whatsapp.ts index dbf58060..d88fc2fa 100644 --- a/functions/src/definitions/webhookHandlers/whatsapp.ts +++ b/functions/src/definitions/webhookHandlers/whatsapp.ts @@ -17,13 +17,7 @@ const app = express() // Accepts POST requests at /webhook endpoint app.post("/whatsapp", async (req, res) => { if (req.body.object) { - if ( - req.body.entry && - req.body.entry[0].id && - req.body.entry[0].changes && - req.body.entry[0].changes[0] && - req.body.entry[0].changes[0].value - ) { + if (req?.body?.entry?.[0]?.changes?.[0]?.value) { let value = req.body.entry[0].changes[0].value let phoneNumberId = value.metadata.phone_number_id let wabaID = req.body.entry[0].id @@ -41,7 +35,7 @@ app.post("/whatsapp", async (req, res) => { (phoneNumberId === checkerPhoneNumberId && wabaID === checkerWabaId) || (phoneNumberId === userPhoneNumberId && wabaID === userWabaId) ) { - if (value.messages && value.messages[0]) { + if (value?.messages?.[0]) { let message = value.messages[0] let type = message.type if ( @@ -77,7 +71,7 @@ app.post("/whatsapp", async (req, res) => { } } res.sendStatus(200) - } else if (value.statuses && value.statuses[0]) { + } else if (value?.statuses?.[0]) { let status = value.statuses[0] let bot = phoneNumberId === checkerPhoneNumberId ? "checker" : "user" if (status.status === "failed") {