Skip to content

Commit

Permalink
Add Notification and Cron job
Browse files Browse the repository at this point in the history
  • Loading branch information
devamdoshi212 committed Nov 24, 2023
1 parent a2dbce1 commit 1ec2a5a
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 8 deletions.
23 changes: 23 additions & 0 deletions Controller/AthleteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const UsersModel = require("./../Model/UsersModel");
const SportModel = require("./../Model/SportModel");
const ComplaintTypeModel = require("../Model/ComplaintTypeModel");
const ComplaintModel = require("../Model/ComplaintModel");
const Reminder = require("../Model/Reminder");
const { setReminder } = require("../SetReminder");
module.exports.addAthlete = async function (req, res) {
//console.log(req.file);
const BaseUrl = `http://localhost:9999/Athletes/${req.file.originalname}`;
Expand Down Expand Up @@ -411,7 +413,28 @@ module.exports.goalOfAthletes = async function (req, res) {
{ _id: req.query.id },
{ $set: { goals: Athlete.goals } }
);
// let first = new Date(goal.startdate);
// while (first < new Date(goal.targetdate)) {
// first += 86400000;
// setReminder(
// first,
// `This is reminder to complete ${goal.title}`,
// "Goal Reminder",
// [Athlete.userId]
// );
// }
let first = new Date(goal.startdate);

while (first.getTime() < new Date(goal.targetdate).getTime()) {
first.setTime(first.getTime() + 60000);

setReminder(
new Date(first), // Ensure you pass a new Date object to setReminder
`This is a reminder to complete ${goal.title}`,
"Goal Reminder",
[Athlete.userId]
);
}
res.json({
data: Athlete,
msg: "Goals added to Athlete successfully",
Expand Down
14 changes: 14 additions & 0 deletions Model/Reminder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const mongoose = require("mongoose");

const reminderSchema = new mongoose.Schema({
date: Date,
message: String,
title: String,
issent: {
type: Number,
default: 0,
},
userIds: [{ type: mongoose.SchemaTypes.ObjectId, ref: "users" }],
});

module.exports = mongoose.model("reminders", reminderSchema, "reminders");
2 changes: 1 addition & 1 deletion PushNotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports.sendPushNotification = async function (
const fcmEndpoint = "https://fcm.googleapis.com/fcm/send";

const notificationMessage = {
to: userToken,
registration_ids: userToken,
notification: {
title,
body: message,
Expand Down
14 changes: 14 additions & 0 deletions SetReminder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const Reminder = require("./Model/Reminder");

async function setReminder(date, message, title, userIds) {
let reminder = new Reminder({
date,
message,
title,
userIds,
issent: 0,
});
await reminder.save();
}

module.exports = { setReminder };
2 changes: 1 addition & 1 deletion client-app/ipconfig.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default { ip: "192.168.0.103" };
export default { ip: "192.168.137.205" };
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"jsonwebtoken": "^9.0.2",
"mongoose": "^7.6.3",
"multer": "^1.4.5-lts.1",
"node-cron": "^3.0.3",
"nodemailer": "^6.9.7",
"validator": "^13.11.0"
}
Expand Down
37 changes: 31 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require("express");
const cors = require("cors");
const multer = require("multer");
const ejs = require("ejs");
const cron = require("node-cron");

require("./config/dbconfig").getDbConnetion();
const UserRoute = require("./Routes/UserRoutes");
Expand Down Expand Up @@ -39,6 +40,7 @@ const {
getAuthorityComplaint,
} = require("./Controller/AuthorityComplaintController");
const { sendPushNotification } = require("./PushNotification");
const Reminder = require("./Model/Reminder");
const app = express();

//middleware
Expand Down Expand Up @@ -263,8 +265,8 @@ app.get("/atheleteInSportsComplex", AdminController.atheleteInSportsComplex);
app.post("/addSession/:id", SessionController.addSession);
app.get("/getSession", SessionController.getSession);
app.get("/filtersport/:id", filtersportsforcomplex);
app.patch("/updateSportsInSession",SessionController.updateSportsInSession)
app.get("/attendanceSportWise",SessionController.attendanceSportWise)
app.patch("/updateSportsInSession", SessionController.updateSportsInSession);
app.get("/attendanceSportWise", SessionController.attendanceSportWise);

app.get(
"/getInstructorForPayment",
Expand Down Expand Up @@ -298,10 +300,13 @@ app.get(
);
app.get("/agewiseSportCount", UtilizationController.agewiseSportCount);
app.get("/agegrpCount", UtilizationController.agegrpCount);
app.get("/rating",UtilizationController.ratingWiseTop5)
app.get("/sportRatingWiseTop5",UtilizationController.sportRatingWiseTop5)
app.get("/monthWiseEventCount",UtilizationController.monthWiseEventCount)
app.get("/monthWiseComplainCount",UtilizationController.monthWiseComplainCount)
app.get("/rating", UtilizationController.ratingWiseTop5);
app.get("/sportRatingWiseTop5", UtilizationController.sportRatingWiseTop5);
app.get("/monthWiseEventCount", UtilizationController.monthWiseEventCount);
app.get(
"/monthWiseComplainCount",
UtilizationController.monthWiseComplainCount
);

app.post("/remarkRatingByAthlete", async (req, res) => {
let {
Expand Down Expand Up @@ -402,5 +407,25 @@ async function averageRating(userId, sportId) {
});
return total2 / total;
}

cron.schedule("*/1 * * * *", async function () {
console.log("run");
let today = new Date();
let reminder = await Reminder.find({
issent: 0,
date: { $lt: today },
}).populate({ path: "userIds", select: { notificationtoken: 1 } });
for (let index = 0; index < reminder.length; index++) {
const element = reminder[index];
await sendPushNotification(
element.userIds.map((ele) => ele.notificationtoken),
element.title,
element.message
);
element.issent = 1;
element.save();
}
});

app.listen(9999);
console.log("server started at 9999");

0 comments on commit 1ec2a5a

Please sign in to comment.