From 4d195834e4770848236f50d9f9b0b3009b95ef21 Mon Sep 17 00:00:00 2001 From: kaveeshadinamidu Date: Sat, 19 Nov 2022 17:40:07 +0530 Subject: [PATCH 1/2] 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 }; From 62094f8e3228f7c5eeb730bc8aa0c40dd9215a11 Mon Sep 17 00:00:00 2001 From: kaveeshadinamidu Date: Sat, 19 Nov 2022 22:50:54 +0530 Subject: [PATCH 2/2] Api added for admin --- src/controllers/intrusionController.js | 7 ++++++- src/controllers/userController.js | 12 +++++++++++ src/repositories/intrusionRepository.js | 10 +++++++++ src/repositories/userRepository.js | 28 +++++++++++++++++++++++++ src/services/intrusionService.js | 14 ++++++++++++- src/services/userService.js | 20 ++++++++++++++++++ 6 files changed, 89 insertions(+), 2 deletions(-) diff --git a/src/controllers/intrusionController.js b/src/controllers/intrusionController.js index ac8b298..907b0f6 100644 --- a/src/controllers/intrusionController.js +++ b/src/controllers/intrusionController.js @@ -4,7 +4,6 @@ const { authenticateToken } = require("../helpers/accessToken"); const router = express.Router(); const intrusionService = require("../services/intrusionService"); -const notificationService = require("../services/notificationService"); router.get("/get/:systemId", authenticateToken, async (req, res) => { const response = await intrusionService.getIntrusions(req.params.systemId); @@ -12,6 +11,12 @@ router.get("/get/:systemId", authenticateToken, async (req, res) => { res.send(response); }); +router.get("/all/count", authenticateToken, async (req, res) => { + const response = await intrusionService.getTotalIntrusions(); + res.status(200); + res.send(response); +}); + router.get("/latest/:systemId", authenticateToken, async (req, res) => { const response = await intrusionService.getLatestIntrusion( req.params.systemId diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 1ad993c..b3d2087 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -11,6 +11,18 @@ router.get("/", authenticateToken, (req, res) => { res.send("Check"); }); +router.get("/alluser/desktop", authenticateToken, async (req, res) => { + const response = await userService.getTotalUserCount(); + res.status(200); + res.send(response); +}); + +router.get("/alluser/mobile", authenticateToken, async (req, res) => { + const response = await userService.getTotalMobileUserCount(); + res.status(200); + res.send(response); +}); + router.get("/mobile/check/:userId", authenticateToken, async (req, res) => { const result = await userService.getMobileDevice(req.params.userId); res.status(200); diff --git a/src/repositories/intrusionRepository.js b/src/repositories/intrusionRepository.js index d67c0bc..92aab9e 100644 --- a/src/repositories/intrusionRepository.js +++ b/src/repositories/intrusionRepository.js @@ -73,6 +73,15 @@ async function getIntrusionImages(intrusionId) { } } +async function getTotalIntrusions() { + try { + const response = await prisma.intrusion.count(); + return response; + } catch (error) { + return error; + } +} + module.exports = { addIntrusion, getIntrusions, @@ -80,4 +89,5 @@ module.exports = { addIntrusionVideo, getIntrusionImages, getLatestIntrusion, + getTotalIntrusions, }; diff --git a/src/repositories/userRepository.js b/src/repositories/userRepository.js index 70dccca..e847ccc 100644 --- a/src/repositories/userRepository.js +++ b/src/repositories/userRepository.js @@ -148,6 +148,32 @@ async function deletePreviousOTP(userId) { } } +async function getDesktopUserCount() { + try { + const response = await prisma.user.count({ + where: { + role: "ADMIN", + }, + }); + return response; + } catch (error) { + throw error; + } +} + +async function getMobileUserCount() { + try { + const response = await prisma.user.count({ + where: { + role: "USER", + }, + }); + return response; + } catch (error) { + throw error; + } +} + module.exports = { registerUser, getUser, @@ -160,4 +186,6 @@ module.exports = { deleteToken, deletePreviousOTP, getUserById, + getDesktopUserCount, + getMobileUserCount, }; diff --git a/src/services/intrusionService.js b/src/services/intrusionService.js index 8f9c954..4d8c614 100644 --- a/src/services/intrusionService.js +++ b/src/services/intrusionService.js @@ -18,7 +18,9 @@ async function addIntrusion(data) { const response = await intrusionRepository.addIntrusion(data); const deviceResult = await deviceRepository.getMobileDevices(data.systemId); const devices = deviceResult.map((item) => item.id); - const notificationResult = await notificationService.sendNotifications(devices); + const notificationResult = await notificationService.sendNotifications( + devices + ); return createOutput(201, { intrusion: response, @@ -89,6 +91,15 @@ async function getLatestIntrusion(systemId) { } } +async function getTotalIntrusions() { + try { + const response = await intrusionRepository.getTotalIntrusions(); + return createOutput(200, response); + } catch (error) { + return createOutput(500, "Error occured in getting the intrusions"); + } +} + module.exports = { addIntrusion, getIntrusions, @@ -96,4 +107,5 @@ module.exports = { addIntrusionVideo, getIntrusionImages, getLatestIntrusion, + getTotalIntrusions, }; diff --git a/src/services/userService.js b/src/services/userService.js index a51af5e..b0f7178 100644 --- a/src/services/userService.js +++ b/src/services/userService.js @@ -209,6 +209,24 @@ async function getUserSystem(userId) { } } +async function getTotalUserCount() { + try { + const response = await userRepository.getDesktopUserCount(); + return createOutput(200, response); + } catch (error) { + return createOutput(500, "Error in getting the user count"); + } +} + +async function getTotalMobileUserCount() { + try { + const response = await userRepository.getMobileUserCount(); + return createOutput(200, response); + } catch (error) { + return createOutput(500, "Error in getting the user count"); + } +} + module.exports = { registerUser, logInUser, @@ -219,4 +237,6 @@ module.exports = { getMobileDevice, getUserSystem, changePassword, + getTotalUserCount, + getTotalMobileUserCount, };