This repository has been archived by the owner on Jan 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (112 loc) · 3.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import express from 'express';
import _ from 'date-fns';
import mongoose from 'mongoose';
import mongooseDeets from "./config/mongo.json";
import Reminder from './Reminder.schema.js';
import { format } from 'date-fns';
// import newReminder from './newReminder.js';
import AwarenessAgent from './awarenessAgent.js';
import cron from 'node-cron';
var agents = [];
await mongoose.connect(mongooseDeets.url, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
});
function refreshAgents() {
Reminder.find({}, function (err, docs) {
if(!Array.isArray(docs)) return;
docs.forEach(doc => {
var agent = new AwarenessAgent(doc);
agent.reflect();
})
});
}
var myCronJob = cron.schedule('*/5 * * * *', refreshAgents);
var server = express();
import bodyParser from 'body-parser';
server.use( bodyParser.json() ); // to support JSON-encoded bodies
server.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
var port = 3134;
server.listen(port, () => {console.log(`Server started on port ${port}`)});
import { ListAgents, MarkedComplete, AddReminder } from './layouts/useHandlebars.js';
server.use('/public', express.static('static'));
server.post('/sleep', async (req, res, next) => {
if(req.body.id) {
var doc = await Reminder.findOne({_id: req.body.id});
let agent = new AwarenessAgent(doc);
if(agent.asleep===true) {
await agent.reactivate();
} else {
await agent.deactivate();
}
res.redirect(`/edit-reminder?id=${req.body.id}`);
} else {
res.send("No agent found");
}
});
server.post('/clear-reminders', async (req, res, next) => {
if(req.body.action) {
var doc = await Reminder.findOne({_id: req.body.action});
if(doc===null) {
res.send("Reminder not found");
return;
}
doc.remindersSent = [];
await doc.save();
} else {
await Reminder.find({}, (err, docs) => {
docs.forEach(async doc => {
doc.remindersSent = [];
var result = await doc.save();
});
});
}
refreshAgents();
res.send("Okay");
});
refreshAgents();
server.post("/complete", async (req, res, next) => {
var action = req.body.action;
var found = await Reminder.findOne({title:action});
if(!found) {
res.send("No action found");
return;
}
var at = req.body.time ? parseInt(req.body.time) : null;
found = new AwarenessAgent(found);
found.markComplete(at);
res.send(MarkedComplete({title: action}));
});
server.get("/", async (req, res, next) => {
var docs = await Reminder.find({}, () => {
});
var agents = docs.map(agentModel => {
let agent = new AwarenessAgent(agentModel);
var due = agent.isPastDue;
return {
nextReminderInt: agent.nextReminder.getTime(),
title: agent.title,
lastPerformed: format(new Date(agent.lastPerformed), "MMMM do, yyyy, h:mm aaa"),
nextReminder: due ? "" : format(agent.nextReminder, 'MMMM do, yyyy, h:mm aaa'),
timeUntilNextReminder: due ? "" : _.formatDistance(new Date(), agent.nextReminder),
id: agent.id,
status: agent.status,
asleep: agent.asleep || false
}
});
agents.sort((a, b) => {
return a.nextReminderInt > b.nextReminderInt ? 1 : -1;
});
agents.sort((a, b) => {
return Number(a.asleep) - Number(b.asleep);
});
res.send(ListAgents({pageTitle:"List Agents", agents}));
});
import AddReminderRouter from './lib/AddReminder.js';
server.use("/", AddReminderRouter);
import EditReminderRouter from './lib/EditReminder.js';
server.use("/", EditReminderRouter);