This repository was archived by the owner on Jun 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.js
More file actions
331 lines (295 loc) · 10.3 KB
/
Copy pathMessage.js
File metadata and controls
331 lines (295 loc) · 10.3 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
'use strict';
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./tnAchieves.sqlite');
//my fake objects, to be replaced by a ton of csv parsed ones
let csvEmailObjs = [
{
clicked: "N",
contact: "Ame Plows",
contactId: "0036100000uHOWD",
contactRecordType: "student",
dateSent: "9/4/2017 1:04:00 PM",
deleted: "N",
email: "myaddress60979@email.net",
emailName: "Class of 2017 TM3 - One Week Student",
fromName: "Record Owner",
hardBounce: "N",
numberOfTotalClicks: 4,
numberOfUniqueClicks: 1,
opened: "Y",
softBounce: "N",
subjectLine: "Deadline Reminder"
},
{
clicked: "N",
contact: "Ame Plows",
contactId: "0036100000uHOWD",
contactRecordType: "student",
dateSent: "9/4/2017 1:04:00 PM",
deleted: "N",
email: "myaddress60979@email.net",
emailName: "Class of 2017 TM3 - One Week Student",
fromName: "Record Owner",
hardBounce: "N",
numberOfTotalClicks: 1,
numberOfUniqueClicks: 1,
opened: "Y",
softBounce: "N",
subjectLine: "You are a college student!"
},
]
let csvMessageObjs = [
{
clicked: "N",
contact: "Ame Plows",
contactId: "0036100000uHOWD",
contactRecordType: "student",
createdDate: "10-03-2016",
dateBounced: "12-4-2016",
dateOpened: "10-14-2016",
dateUnsubscribed: "10-14-2016",
inboundNumber: "1 888 555 5556",
dateSent: "9/4/2017 1:04:00 PM",
deleted: "N",
inboundNumber: "615 555 5599",
mobileNumber: "615 555 2234",
email: "myaddress60979@email.net",
emailName: "Class of 2017 TM3 - One Week Student",
fromName: "Record Owner",
hardBounce: "N",
numberOfTotalClicks: 4,
numberOfUniqueClicks: 1,
opened: "Y",
softBounce: "N",
subjectLine: "Deadline Reminder",
SMS: "SMS TEXT EXAMPLE",
campaign: "campaign",
deliveryStatus: "Delivered",
disableSMSOnTrigger: "N",
sentStatus: "Y"
},
{
clicked: "N",
contact: "Ame Plows",
contactId: "0036100000uHOWD",
contactRecordType: "student",
dateSent: "9/4/2017 1:04:00 PM",
createdDate: "08-03-2016",
dateBounced: "05-4-2016",
dateOpened: "12-14-2016",
dateUnsubscribed: "10-30-2016",
inboundNumber: "1 888 555 5796",
deleted: "N",
inboundNumber: "615 555 5599",
mobileNumber: "615 555 2234",
email: "myaddress60979@email.net",
emailName: "Class of 2017 TM3 - One Week Student",
fromName: "Record Owner",
hardBounce: "N",
numberOfTotalClicks: 1,
numberOfUniqueClicks: 1,
opened: "Y",
softBounce: "N",
subjectLine: "You are a college student!",
SMS: "SMS TEXT EXAMPLE",
campaign: "campaign",
deliveryStatus: "Delivered",
disableSMSOnTrigger: "N",
sentStatus: "Y"
},
]
//Methods for the EMAIL data
//this will not need to be in the production version as the DB will already exist! Just for my build purposes -EL
// db.serialize( () => {
// // // keeping these commented out pieces for generating the table for order reference -- in the INPUT stage, the order matters a lot
// db.run(`DROP TABLE IF EXISTS EmailFiles`);
// db.run(`CREATE TABLE IF NOT EXISTS EmailFiles(
// email_id INTEGER PRIMARY KEY NOT NULL,
// clicked TEXT NOT NULL,
// contact TEXT NOT NULL,
// contactId TEXT NOT NULL,
// contactRecordType TEXT NOT NULL,
// dateBounced DATE,
// dateOpened DATE,
// dateSent DATE NOT NULL,
// dateUnsubscribed DATE,
// deleted TEXT NOT NULL,
// email TEXT NOT NULL,
// emailName TEXT NOT NULL,
// fromAddress TEXT,
// fromName TEXT NOT NULL,
// hardBounce TEXT NOT NULL,
// numberOfTotalClicks INTEGER NOT NULL,
// numberOfUniqueClicks INTEGER NOT NULL,
// opened TEXT NOT NULL,
// relatedStudentContactId TEXT,
// softBounce TEXT NOT NULL,
// subjectLine TEXT
// )`);
// module.exports.prepEmailData = (data) => {
// console.log('printData firing', data["Contact Record Type"]);
// data.forEach(d => {
// let emailObj= {};
// emailObj.clicked = d.Clicked;
// emailObj.contact = d.Contact;
// emailObj.contactId = d["Contact ID"];
// emailObj.contactRecordType = d["Contact Record Type"];
// emailObj.dateBounced = d["Date Bounced"];
// emailObj.dateOpened = d["Date Opened"];
// emailObj.dateSent = d["Date Sent"];
// emailObj.dateUnsubscribed = ["Date Unsubscribed"];
// emailObj.deleted = d.Deleted;
// emailObj.email = d.Email;
// emailObj.emailName = d["Email Name"];
// emailObj.fromAddress = d["From Address"];
// emailObj.fromName = d["From Name"];
// emailObj.hardBounce = d["Hard Bounce"];
// emailObj.numberOfTotalClicks = d["Number Of Total Clicks"];
// emailObj.numberOfUniqueClicks = d["Number Of Unique Clicks"];
// emailObj.opened = d.Opened;
// emailObj.relatedStudentContactId = d["Related Student Contact ID"];
// emailObj.softBounce = d["Soft Bounce"];
// emailObj.subjectLine = d["Subject Line"];
// runInsertToDB(emailObj);
// });
// };
// //POST from upload, iterate over each of the items in the parsed array of objects:
// module.exports.insertEmailsIntoDB = (csvEmailObjs) => {
// csvEmailObjs.forEach( (emailObj) => {
// db.run(`INSERT INTO EmailFiles VALUES (null,
// '${emailObj.clicked}',
// '${emailObj.contact}',
// '${emailObj.contactId}',
// '${emailObj.contactRecordType}',
// '${emailObj.dateBounced}',
// '${emailObj.dateOpened}',
// '${emailObj.dateSent}',
// '${emailObj.dateUnsubscribed}',
// '${emailObj.deleted}',
// '${emailObj.email}',
// '${emailObj.emailName}',
// '${emailObj.fromAddress}',
// '${emailObj.fromName}',
// '${emailObj.hardBounce}',
// ${emailObj.numberOfTotalClicks},
// ${emailObj.numberOfUniqueClicks},
// '${emailObj.opened}',
// '${emailObj.relatedStudentContactId}',
// '${emailObj.softBounce}',
// '${emailObj.subjectLine}')`);
// });
// }
// //start LS make new table
// db.run(`DROP TABLE IF EXISTS Messages`);
// db.run(`CREATE TABLE IF NOT EXISTS Messages(
// message_id INTEGER PRIMARY KEY NOT NULL,
// messageType TEXT NOT NULL,
// contactId TEXT NOT NULL,
// clicked TEXT NOT NULL,
// contact TEXT NOT NULL,
// contactRecordType TEXT NOT NULL,
// createdDate TEXT,
// dateBounced DATE,
// dateOpened DATE,
// dateSent DATE NOT NULL,
// dateUnsubscribed DATE,
// deleted TEXT NOT NULL,
// inboundNumber TEXT,
// mobileNumber TEXT,
// email TEXT NOT NULL,
// emailName TEXT NOT NULL,
// fromAddress TEXT,
// fromName TEXT NOT NULL,
// hardBounce TEXT NOT NULL,
// numberOfTotalClicks INTEGER NOT NULL,
// numberOfUniqueClicks INTEGER NOT NULL,
// opened TEXT NOT NULL,
// relatedStudentContactId TEXT,
// softBounce TEXT NOT NULL,
// subjectLine TEXT,
// SMS TEXT,
// campaign TEXT,
// deliveryStatus TEXT,
// disableSMSOnTrigger TEXT,
// sentStatus TEXT
// )`);
// //POST from upload, iterate over each of the items in the parsed array of objects:
module.exports.insertMessagesIntoDB = (csvMessageObjs) => {
csvEmailObjs.forEach( (messageObj) => {
db.run(`INSERT INTO messages VALUES (null,
'${messageObj.messageType}',
'${messageObj.contactId}',
'${messageObj.clicked}',
'${messageObj.contact}',
'${messageObj.contactRecordType}',
'${messageObj.createdDate}',
'${messageObj.dateBounced}',
'${messageObj.dateOpened}',
'${messageObj.dateSent}',
'${messageObj.dateUnsubscribed}',
'${messageObj.deleted}',
'${messageObj.inboundNumber}',
'${messageObj.mobileNumber}',
'${messageObj.email}',
'${messageObj.emailName}',
'${messageObj.fromAddress}',
'${messageObj.fromName}',
'${messageObj.hardBounce}',
${messageObj.numberOfTotalClicks},
${messageObj.numberOfUniqueClicks},
'${messageObj.opened}',
'${messageObj.relatedStudentContactId}',
'${messageObj.softBounce}',
'${messageObj.subjectLine}',
'${messageObj.SMS}',
'${messageObj.campaign}',
'${messageObj.deliveryStatus}',
'${messageObj.disableSMSOnTrigger}',
'${messageObj.sentStatus}'
)`);
});
}
//uncommenting this will call this function and input those objects above
// module.exports.insertEmailsIntoDB(csvEmailObjs);
// module.exports.insertMessagesIntoDB(csvMessageObjs);
// });
module.exports.searchByContactId = (contactId) => {
return new Promise( (resolve, reject) => {
db.all(`SELECT * FROM Messages WHERE contactId = "${contactId}"`, (err, messagesData) => {
if (err) return reject(err);//if error, pass on to error handler
resolve(messagesData);
});
});
};
module.exports.uploadFileToDB = (file) => {
console.log("file going in", file);
return new Promise( (resolve, reject) => {
db.all(`INSERT INTO EmailFiles VALUES (null,
'${file.contactId}',
'${file.clicked}',
'${file.contact}',
'${file.contactRecordType}',
'${file.dateBounced}',
'${file.dateOpened}',
'${file.dateSent}',
'${file.dateUnsubscribed}',
'${file.deleted}',
'${file.email}',
'${file.emailName}',
'${file.fromAddress}',
'${file.fromName}',
'${file.hardBounce}',
${file.numberOfTotalClicks},
${file.numberOfUniqueClicks},
'${file.opened}',
'${file.relatedStudentContactId}',
'${file.softBounce}',
'${file.subjectLine}')`,
(err, successMessage) => {
if (err) return reject(err);//if error, pass on to error handler
resolve(successMessage);
});
});
};
//Needed methods: GET by contactId and by contact, also by relatedStudentContactId at the same time as the contactId search
//DELETE - when the record is 3 years old- GET all things that are three years old, then iterate over their IDs and delete them?