From 4d195834e4770848236f50d9f9b0b3009b95ef21 Mon Sep 17 00:00:00 2001 From: kaveeshadinamidu Date: Sat, 19 Nov 2022 17:40:07 +0530 Subject: [PATCH] Code refactored --- src/services/notificationService.js | 60 +++++++++++++++-------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/src/services/notificationService.js b/src/services/notificationService.js index 64406e7..3e6e49e 100644 --- a/src/services/notificationService.js +++ b/src/services/notificationService.js @@ -4,39 +4,41 @@ const createOutput = require("../helpers/createOutput"); var serviceAccount = require("./firebase-conf.json"); admin.initializeApp({ - credential: admin.credential.cert(serviceAccount) + credential: admin.credential.cert(serviceAccount), }); - -function sendNotifications(tokens){ +function sendNotifications(tokens) { const message = { - notification :{ - title:"Intrusion Detected", - body:"An intrusion detected in your cctv system", - + notification: { + title: "Intrusion Detected", + body: "An intrusion detected in your cctv system", }, - tokens:tokens, - } - return new Promise((resolve,reject)=>{ - admin.messaging().sendMulticast(message).then((response)=>{ - console.log(response); - if (response.failureCount > 0) { - const failedTokens = []; - response.responses.forEach((resp, idx) => { - if (!resp.success) { - failedTokens.push(tokens[idx]); - } - }); - resolve(createOutput(400,"Error occured while sending to some devices")); - - }else{ - resolve(createOutput(200,"Notifications sended succesfully!")); - } - }).catch((e) =>{ - reject(e); - }); + tokens: tokens, + }; + return new Promise((resolve, reject) => { + admin + .messaging() + .sendMulticast(message) + .then((response) => { + console.log(response); + if (response.failureCount > 0) { + const failedTokens = []; + response.responses.forEach((resp, idx) => { + if (!resp.success) { + failedTokens.push(tokens[idx]); + } + }); + resolve( + createOutput(400, "Error occured while sending to some devices") + ); + } else { + resolve(createOutput(200, "Notifications sended succesfully!")); + } + }) + .catch((e) => { + reject(e); + }); }); - } -module.exports = {sendNotifications} \ No newline at end of file +module.exports = { sendNotifications };