-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
164 lines (145 loc) · 5.32 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
164
'use strict';
// include configuration and dependencies
const nodemailer = require('nodemailer');
const schedule = require('node-schedule');
const firebase = require('firebase');
const moment = require('moment');
const config = require('./config.js');
const sender = require('./sender.js');
// setup firebase connection and database refs
firebase.initializeApp(config);
const fbOrders = firebase.database().ref('/orders');
const fbWorkshops = firebase.database().ref('/workshops');
const fbUsers = firebase.database().ref('/users');
const fbMessages = firebase.database().ref('/conversations');
console.log('Wildtree email server running!');
// CHECKS CHANGES ON ORDERS
fbOrders.on('child_changed', (snapshot) => {
let userData = snapshot.val();
let workshopName = '';
const users = {};
fbWorkshops.once('value')
.then((snapshot) => {
return new Promise((resolve, reject) => {
let data = snapshot.child(userData.workshopId).val();
workshopName = data.name;
resolve(data.uid);
});
})
.then((userId) => {
getUsers(userId)
.then((users) => {
let data = {
workshop: workshopName,
newQuantity: userData.quantity,
bundleName: userData.bundleName,
isSpecialOrder: userData.specialOrder
};
let emailObj = {
subject : `The workshop "${data.workshop}" was updated!`,
message: `${users.rep.name} (${users.rep.email}) just updated the workshop "${workshopName}"!\nThe bundle ${data.bundleName} is now at a quantity of ${data.newQuantity}.\n`,
sendTo: users.butcher.email
};
emailObj.message += data.isSpecialOrder ? '\n**It is a special order so some meats have changed!**' : '\nThis is a NORMAL order.';
sendEmail(emailObj);
});
});
});
// CHECK FOR NEW SUBMITTED WORKSHOPS
fbWorkshops.on('child_changed', (snapshot) => {
let workshop = snapshot.val();
if(workshop.isSubmitted && !workshop.isApproved) {
getUsers(workshop.uid)
.then((users) => {
let emailObj = {
subject: `New Workshop "${workshop.name}" submitted for pickup ${moment(workshop.date).format('MM/DD')} @ ${moment(workshop.time).format('hh:mma')}`,
message: `A new workshop was just submitted by ${users.rep.name} (${users.rep.email}) for you to review and approve.\n\nWORKSHOP NAME: ${workshop.name}\nWORKSHOP PICKUP: ${moment(workshop.date).format('MM/DD')} @ ${moment(workshop.time).format('hh:mma')}\nREP NAME: ${users.rep.name}\nREP EMAIL: ${users.rep.email}`,
sendTo: users.butcher.email
}
sendEmail(emailObj);
});
}
if(workshop.isApproved){
getUsers(workshop.uid)
.then((users) => {
let emailObj = {
subject: `Workshop "${workshop.name}" was approved!`,
message: `Your workshop "${workshop.name}" was approved for pickup on ${moment(workshop.date).format('MM/DD')} @ ${moment(workshop.time).format('hh:mma')} by ${users.butcher.name} (${users.butcher.email}).\nYou can continue to add orders until 2 days prior.`,
sendTo: users.rep.email
}
sendEmail(emailObj);
});
}
});
//CHECK FOR UNREAD MESSAGES DAILY
var runJob = schedule.scheduleJob({hour: 20, minute: 5}, function() {
fbMessages.on('child_added', (snapshot) => {
const convo = snapshot.val();
const lastMsg = convo.messages[convo.messages.length-1];
let sender = {};
let receiver = {};
if(!lastMsg.read){
if(lastMsg.authorId === convo.fullUsers[1].userId){
receiver = convo.fullUsers[0];
sender = convo.fullUsers[1];
} else {
receiver = convo.fullUsers[1];
sender = convo.fullUsers[0];
}
const emailObj = {
subject: `You have unread messages`,
message: `You currently have at least one unread message from ${sender.name} (${sender.email}).\n\nMESSAGE: "${lastMsg.text}"\n\n(*Replying to this email will go to the author, but does not post in the app.*)`,
sendTo: receiver.email,
replyTo: sender.email
};
sendEmail(emailObj);
}
});
console.log('**testing scheduled job**');
});
//Get users helper function
let getUsers = (userId) => {
return new Promise((resolve)=> {
let users = fbUsers.once('value')
.then((snapshot) => {
return new Promise((resolve, reject) => {
let data = [];
snapshot.forEach((child) => {
if(child.val().userId === userId) {
data.rep = child.val();
}
if(child.val().isButcher) {
data.butcher = child.val();
}
});
resolve(data);
});
});
resolve(users);
});
};
// Called by watchers to send email
var sendEmail = function(emailObj) {
let smtpTransport = nodemailer.createTransport({
service: 'gmail',
auth: {
user: sender.user,
pass: sender.pass
}
});
let mailOptions = {
from: "WildTreeApp <[email protected]>",
to: emailObj.sendTo,
subject: emailObj.subject,
text: emailObj.message,
replyTo: emailObj.replyTo || "[email protected]"
};
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
} else {
console.log("Message sent: " + response.response);
}
smtpTransport.close();
});
};