Skip to content

Commit

Permalink
Merge pull request #47 from Ninety-Camera/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
kaveeshadinamidu authored Nov 19, 2022
2 parents e25a3e7 + 62094f8 commit 7e1ce36
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 31 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,
};
60 changes: 31 additions & 29 deletions src/services/notificationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
module.exports = { sendNotifications };
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 7e1ce36

Please sign in to comment.