-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
121 lines (118 loc) · 3.13 KB
/
server.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
require("dotenv").config();
const express = require("express"),
mongoose = require("mongoose"),
path = require("path"),
PromptController = require("./controllers/thoughtControllers"),
UserController = require("./controllers/UserController"),
db = require("./models"),
sendText = require("./send-sms"),
CronJob = require("cron").CronJob,
PORT = process.env.PORT || 3001,
app = express();
app.use(express.urlencoded({ extended: !0 })),
app.use(express.json()),
app.use(express.static("client/build"));
app.get("/job/:value", (a, b) => {
console.log(a.params.value),
a.params.value
? (meditationJob.start(),
thoughtJob.start(),
upliftingJob.start(),
b.json({ success: !0 }))
: (meditationJob.stop(),
thoughtJob.stop(),
upliftingJob.stop(),
b.json({ success: !1 }));
}),
app.use("/api/user", UserController);
// jobs run automatically. if you run localhost:3001/job/0 they will turn off. Running localhost:3001/job/1 will turn them back on.
app.use("/api/prompts", PromptController),
app.use("/api/user", UserController),
mongoose.connect(
process.env.MONGODB_URI || "mongodb://localhost/positive-thoughts",
{
useNewUrlParser: !0,
useUnifiedTopology: !0,
useCreateIndex: !0,
useFindAndModify: !1,
}
);
const connection = mongoose.connection;
connection.on("connected", () => {
console.log("Mongoose successfully connected.");
}),
connection.on("error", (a) => {
console.log("Mongoose connection error: ", a);
});
var meditationJob = new CronJob(
"* */2 * * *",
function () {
sendMeditation();
},
null,
!0,
"America/New_York"
),
thoughtJob = new CronJob(
"* */3 * * *",
function () {
sendThought();
},
null,
!0,
"America/New_York"
),
upliftingJob = new CronJob(
"* */5 * * *",
function () {
sendUplifting();
},
null,
!0,
"America/New_York"
);
app.get("/api/config", (a, b) => {
b.json({ success: !0 });
});
function sendThought() {
db.Thought.aggregate([{ $sample: { size: 1 } }]).then((a) => {
db.User.find()
.then((b) => {
b.forEach((b) => sendText(a[0].message_text, b.phoneNumber)),
console.log("thoughtSent");
})
.catch(function (a) {
console.log(a);
});
});
}
function sendMeditation() {
db.Meditation.aggregate([{ $sample: { size: 1 } }]).then((a) => {
db.User.find()
.then((b) => {
b.forEach((b) => sendText(a[0].message_text, b.phoneNumber)),
console.log("meditationSent");
})
.catch(function (a) {
console.log(a);
});
});
}
function sendUplifting() {
db.UpliftingQuotes.aggregate([{ $sample: { size: 1 } }]).then((a) => {
db.User.find()
.then((b) => {
b.forEach((b) => sendText(a[0].message_text, b.phoneNumber)),
console.log("UpliftSent");
})
.catch(function (a) {
console.log(a);
});
});
}
app.get("*", (a, b) => {
b.sendFile(path.join(__dirname, "./client/build/index.html"));
}),
app.listen(PORT, () => {
console.log(`App is running on http://localhost:${PORT}`);
});