Skip to content

Commit 09f33bc

Browse files
committed
send push notification and update FCM token on zoho
1 parent c70a53d commit 09f33bc

File tree

5 files changed

+1370
-3
lines changed

5 files changed

+1370
-3
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules
2+
logs.json
3+
token.json
4+
.env
5+
key.json
6+
dropbox.json
7+
key2.json

components/student.component.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ const moment = require("moment");
88
const nodemailer = require("nodemailer");
99
const pLimit = require("p-limit");
1010
const limit = pLimit(20);
11+
const admin = require("firebase-admin");
12+
const serviceAccount = require("../key2.json");
13+
14+
admin.initializeApp({
15+
credential: admin.credential.cert(serviceAccount),
16+
});
1117

1218
const getStudentDetails = async (email) => {
1319
try {
@@ -841,6 +847,23 @@ const getTestSeriesDoubtSessions = async (grade) => {
841847
}
842848
};
843849

850+
const sendNotification = async (message, userTokens) => {
851+
try {
852+
const payload = {
853+
notification: {
854+
title: message.title,
855+
body: message.body,
856+
},
857+
tokens: userTokens,
858+
};
859+
860+
const response = await admin.messaging().sendEachForMulticast(payload);
861+
return response;
862+
} catch (error) {
863+
return error;
864+
}
865+
};
866+
844867
const getStoryForUsers = async (grade) => {
845868
try {
846869
return {
@@ -853,6 +876,47 @@ const getStoryForUsers = async (grade) => {
853876
}
854877
};
855878

879+
const updateTokenForUser = async (email, token) => {
880+
try {
881+
const accessToken = await getZohoTokenOptimized();
882+
const zohoConfig = {
883+
headers: {
884+
"Content-Type": "application/json",
885+
"Access-Control-Allow-Origin": "*",
886+
Authorization: `Bearer ${accessToken}`,
887+
},
888+
};
889+
const updateTokenBody = {
890+
data: [
891+
{
892+
Email: email,
893+
FCM_TOKEN: token,
894+
},
895+
],
896+
duplicate_check_fields: ["Email"],
897+
apply_feature_execution: [
898+
{
899+
name: "layout_rules",
900+
},
901+
],
902+
trigger: [],
903+
};
904+
905+
const updateData = await axios.post(
906+
`https://www.zohoapis.com/crm/v6/Contacts/upsert`,
907+
updateTokenBody,
908+
zohoConfig
909+
);
910+
911+
return {
912+
status: 200,
913+
message: "success",
914+
};
915+
} catch (error) {
916+
return error;
917+
}
918+
};
919+
856920
module.exports = {
857921
getStudentDetails,
858922
getStudentOrders,
@@ -864,4 +928,6 @@ module.exports = {
864928
getTestSeriesByGrade,
865929
getTestSeriesDoubtSessions,
866930
getStoryForUsers,
931+
sendNotification,
932+
updateTokenForUser,
867933
};

controllers/student.controller.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const {
1010
getTestSeriesByGrade,
1111
getTestSeriesDoubtSessions,
1212
getStoryForUsers,
13+
sendNotification,
14+
updateTokenForUser,
1315
} = require("../components/student.component");
1416
const {
1517
getZohoTokenOptimized,
@@ -178,6 +180,40 @@ studentRouter.post("/feedback", authMiddleware, async (req, res) => {
178180
}
179181
});
180182

183+
studentRouter.post("/feedback", authMiddleware, async (req, res) => {
184+
try {
185+
const reqData = req.body;
186+
const data = await sendStudentFeedback(reqData);
187+
return res.status(200).send(data);
188+
} catch (error) {
189+
return res.status(error.status || 500).send({
190+
status: "error",
191+
message: error.message,
192+
code: error.status || 500,
193+
});
194+
}
195+
});
196+
197+
studentRouter.post("/send-notification", authMiddleware, async (req, res) => {
198+
try {
199+
const { message, tokens } = req.body;
200+
const data = await sendNotification(message, tokens);
201+
return res.status(200).send(data);
202+
} catch (error) {
203+
return res.status(500).send("Internal Server Error");
204+
}
205+
});
206+
207+
studentRouter.post("/update-fcm-token", authMiddleware, async (req, res) => {
208+
try {
209+
const { email, token } = req.body;
210+
const data = await updateTokenForUser(email, token);
211+
return res.status(200).send(data);
212+
} catch (error) {
213+
return res.status(500).send("Internal Server Error");
214+
}
215+
});
216+
181217
studentRouter.post(
182218
"/update-analysis-sheet",
183219
authMiddleware,

0 commit comments

Comments
 (0)