From 62094f8e3228f7c5eeb730bc8aa0c40dd9215a11 Mon Sep 17 00:00:00 2001 From: kaveeshadinamidu Date: Sat, 19 Nov 2022 22:50:54 +0530 Subject: [PATCH] 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, };