Skip to content

Commit

Permalink
Api added for admin
Browse files Browse the repository at this point in the history
  • Loading branch information
kaveeshadinamidu committed Nov 19, 2022
1 parent 4d19583 commit 62094f8
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/controllers/intrusionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ 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);
res.status(200);
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
Expand Down
12 changes: 12 additions & 0 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions src/repositories/intrusionRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,21 @@ async function getIntrusionImages(intrusionId) {
}
}

async function getTotalIntrusions() {
try {
const response = await prisma.intrusion.count();
return response;
} catch (error) {
return error;
}
}

module.exports = {
addIntrusion,
getIntrusions,
addIntrusionImages,
addIntrusionVideo,
getIntrusionImages,
getLatestIntrusion,
getTotalIntrusions,
};
28 changes: 28 additions & 0 deletions src/repositories/userRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -160,4 +186,6 @@ module.exports = {
deleteToken,
deletePreviousOTP,
getUserById,
getDesktopUserCount,
getMobileUserCount,
};
14 changes: 13 additions & 1 deletion src/services/intrusionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -89,11 +91,21 @@ 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,
addIntrusionImages,
addIntrusionVideo,
getIntrusionImages,
getLatestIntrusion,
getTotalIntrusions,
};
20 changes: 20 additions & 0 deletions src/services/userService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -219,4 +237,6 @@ module.exports = {
getMobileDevice,
getUserSystem,
changePassword,
getTotalUserCount,
getTotalMobileUserCount,
};

0 comments on commit 62094f8

Please sign in to comment.