-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom.js
2423 lines (2228 loc) · 98.4 KB
/
custom.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// NPM Modules
var path = require('path'); // used to require a file that is in the same directory (dbjs and customjs)
var dbjsMethods = require(path.resolve(__dirname, "./db.js")); // dbjs methods can be accessed via dbjsMethods var
const jwt = require('jsonwebtoken');
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
var cron = require('node-cron');
const {
google
} = require('googleapis');
const fs = require('fs');
const readline = require('readline');
var moment = require('moment');
const AWS = require('aws-sdk');
const SES_CONFIG = {
accessKeyId: process.env.SES_ACCESS_KEY_ID,
secretAccessKey: process.env.SES_ACCESS_KEY,
region: 'ap-southeast-2',
};
const AWS_SES = new AWS.SES(SES_CONFIG);
// GCAL STUFF
const SCOPES = ['https://www.googleapis.com/auth/calendar'];
const TOKEN_PATH = 'token.json';
function getAccessToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return console.error('Error retrieving access token', err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) return console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}
module.exports = { // this function allows these methods to be accessed anywher ein the app
btnCSS: function () {
var btnCSS = `display: inline-flex; width: fit-content; position: relative; top: -3px; padding: 16px 20px; background: #4950F6; height: 48px; text-align: center; white-space: nowrap; cursor: pointer; justify-content: center; font-size: 14px; letter-spacing: 0px; font-weight: 700; text-transform: uppercase; line-height: 16px; text-decoration: none !important; font-family: sans-serif; color: #fff !important; transition: background .15s ease; border-radius: 10px; height: 1rem; font-size: 12px;font-weight: 700;`;
return (btnCSS);
},
signature: function () {
var sig = `<table><tbody><tr> <td class="avatar" style="width: 75px;" width="75"><img src="https://firebasestorage.googleapis.com/v0/b/createasignature.appspot.com/o/images%2Fcslogotrans.png?alt=media&token=9aa3f8c7-145d-425a-9f5d-5da29c164ba7" style="background-color: #ffffff; display: block; height: 75px; width: 75px; border-radius: 75px; margin-bottom: 10px;" width="75" height="75"></td> </tr></tbody></table>`;
return (sig);
},
nl2br: function (str, is_xhtml) {
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br ' + '/>' : '<br>';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
},
youtube_parser: function (url) {
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
var match = url.match(regExp);
return (match&&match[7].length==11)? match[7] : false;
},
verifyjwtToken: function (res, jwtToken, callback) {
jwt.verify(jwtToken, process.env.JWT_SECRET_KEY, function (err, authData) {
if (err) {
return res.redirect('/login');
} else {
return callback(authData);
}
})
},
verifyjwtTokenTea: function (res, jwtToken, callback) {
jwt.verify(jwtToken, process.env.JWT_SECRET_KEY, function (err, authData) {
if (err) {
return res.redirect('/soho');
} else {
return callback(authData);
}
})
},
refreshSessionVars: function (req, callback) {
var mom = moment().utc();
var lastUpdated = moment(req.session.sessionLastUpdated, 'YYYY-MM-DDTHH:mm:ssZ').utc();
var diff = mom.diff(lastUpdated, 'hours');
if (diff > 1 || diff < 0 || !req.session.sessionLastUpdated) { // if more than 1 hr has passed or updated is in the future or if session is undefined then reload session variables
req.session.sessionLastUpdated = moment().utc().format();
dbjsMethods.getStudentClassForLogin(req.session.csStudentLoggedIn, function (studentClassData) {
if (studentClassData.length > 0) {
dbjsMethods.getLessonForLogin(studentClassData[0].sohoCID, function (lessonData) { // get the number of classes that have happened before NOW + 3 hours - in case ppl log in on the dot of the class and they need sth from onboarding
dbjsMethods.getUnreadMessagesForLogin(req.session.csStudentSlug, function (messagesData) {
dbjsMethods.getRoomForLogin(studentClassData[0].sohoCID, function (roomData) {
dbjsMethods.getUnreadHWCount(req.session.csStudentClassID, function (hwData) {
var today = moment().utc().startOf('day').format(); // date only
dbjsMethods.getTimerForDay(today, studentClassData[0].sohoCID, function (timerData) {
dbjsMethods.getPracticalGroupsJoin(studentClassData[0].sohoCID, function (groupData) {
dbjsMethods.getPMIForClassroom(studentClassData[0].sohoCID, function (teacherPMIData) {
if (timerData.length > 0) {
req.session.sStudyTimer = timerData[0].time; // set study session time to previous value for today
} else {
req.session.sStudyTimer = 0; // no study has been done today
}
// theory meeting url
if (teacherPMIData.length > 0) {
req.session.theoryMeetingURL = teacherPMIData[0].videoConfPMI; // personal meeting room for teacher
}
// practical meeting urls
var practicalMeetingArr = [];
if (groupData.length > 0) {
for (let i = 0; i < groupData.length; i++) {
var options = {
tz: 'UTC'
};
try {
var interval = parser.parseExpression(groupData[i].start, options);
var nextClass = interval.next().toISOString();
} catch (err) {
console.log('Error: ' + err.message);
}
var practicalMeetingObj = {
start: nextClass,
sohoUID: groupData[i].sohoUID,
groupName: groupData[i].groupName,
}
practicalMeetingArr.push(practicalMeetingObj);
}
}
req.session.practicalMeetingURL = practicalMeetingArr;
// set class session vars
if (studentClassData.length == 0) {
req.session.csStudentReadWriteHskLevel = 7;
req.session.csStudentListenSpeakHskLevel = 7;
req.session.csStudentClassLang = 'Mandarin'; // default for browsers
req.session.csStudentClassExist = 0;
req.session.csStudentClassID = 0; // set class id
} else {
if (studentClassData[0].classType == 'standard') {
req.session.csStudentClassType = 'standard';
} else { // free
req.session.csStudentShowOnboarding = 1; // this is a trial student
req.session.csStudentLessonCount = lessonData[0].count;
req.session.csStudentClassType = 'free';
}
if (lessonData[0].count == 0) { // show onboarding stuff if the student has never taken a class in the past
req.session.csStudentShowOnboarding = 1; // this is a trial student
} else {
req.session.csStudentShowOnboarding = 0; // this is not a trial student
}
req.session.csStudentUnreadMessages = messagesData[0].unreadMessagesCount;
req.session.csStudentReadWriteHskLevel = studentClassData[0].readWriteHSKLevel;
req.session.csStudentListenSpeakHskLevel = studentClassData[0].listenSpeakHSKLevel;
req.session.csStudentClassLang = studentClassData[0].lang; // assuming only 1 class for each student
req.session.csStudentClassExist = 1;
req.session.csStudentClassID = studentClassData[0].sohoCID; // set class id
req.session.csStudentCharType = studentClassData[0].charType; // char Type
if (roomData.length > 0) {
req.session.csStudentTeachingRoomID = roomData[0].sohoRID; // teaching room ID
}
}
// hw count
req.session.csStudentHWNotif = hwData[0].count;
return callback();
});
});
});
});
});
});
});
}
});
} else {
return callback();
}
},
emailSohoUserthatForgotPassword: function (email, reqHeadersHost, token) {
console.log('emailSohoUserthatForgotPassword is run');
// Create sendEmail params
var params = {
Destination: { /* required */
ToAddresses: [
email,
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>You are receiving this email because you (or someone else) have requested the reset of the password for your account.<br><br>' +
'Please click on the following link, or paste this into your browser to complete the process: ' +
'http://' + reqHeadersHost + '/sohoreset/' + token + '<br><br>' +
'If you did not request this, please ignore this email and your password will remain unchanged.</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data: 'Culturestride Password Reset'
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
},
emailStudentthatForgotPassword: function (email, reqHeadersHost, token) {
console.log('emailSohoUserthatForgotPassword is run');
// Create sendEmail params
var params = {
Destination: { /* required */
ToAddresses: [
email,
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>You are receiving this email because you (or someone else) have requested the reset of the password for your account.<br><br>' +
'Please click on the following link, or paste this into your browser to complete the process: ' +
'http://' + reqHeadersHost + '/reset/' + token + '<br><br>' +
'If you did not request this, please ignore this email and your password will remain unchanged.</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data: 'Culturestride Password Reset'
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
},
emailSohoStudentforRecordClass: function (email, date, firstName, hoursUsed) {
console.log('emailSohoStudentforRecordClass is run');
var signature = module.exports.signature() // signature
var params = {
Destination: { /* required */
ToAddresses: [
email,
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>Hi ' + firstName + ', <br><br> Your teacher has logged ' + hoursUsed + ' hours of classes with Culturestride on ' + date + '.<br><br>View your class notes and submit homework to your teacher in your <a href="https://culturestride.com/login" target="_blank">Culturestride account</a><br><br>If this is incorrect, please <a href="https://culturestride.com/a/messages" target="_blank">notify your teacher</a>.' + signature + '</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data: 'Class Record - ' + date
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
},
emailSohoStudentforRecordClassTypeB: function (email, date, firstName) {
console.log('emailSohoStudentforRecordClassTypeB is run');
var signature = module.exports.signature() // signature
var params = {
Destination: { /* required */
ToAddresses: [
email,
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>Hi ' + firstName + ', <br><br> Your teacher has logged your attendance to 1 hour of Practical class with Culturestride on ' + date + '.<br><br>View your class notes and submit homework to your teacher in your <a href="https://culturestride.com/login" target="_blank">Culturestride account</a><br><br>If this is incorrect, please <a href="https://culturestride.com/a/messages" target="_blank">notify your teacher</a>.' + signature + '</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data: 'Class Record Practical - ' + date
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
},
emailMichael: function (subject, content) {
console.log('emailMichael is run');
var email = 'michael@culturestride.com';
var params = {
Destination: { /* required */
ToAddresses: [
email,
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>' + content + '</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data: subject
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
},
emailStart: function (stuFirstName, stuLastName,practicalSpeakingLevel, langHome, stuEmail, email, teaName, trialTimeStart, trialTimeEnd, organiseGroupClass) {
console.log('emailStart is run');
var signature = module.exports.signature() // signature
var params = {
Destination: { /* required */
ToAddresses: [
email,
'michael+notifications@culturestride.com',
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>Hi,<br><br>A new student has scheduled in a class:<br><br>Teacher: ' + teaName + '<br>Group Class: ' + organiseGroupClass + '<br>Practical Speaking Level: ' + practicalSpeakingLevel + '<br>Language Spoken at Home (Chinese): ' + langHome + '<br>Start (China time): ' + trialTimeStart + ' UTC<br>End (China time): ' + trialTimeEnd + ' UTC<br><br>Student Email: ' + stuEmail + '<br><br><br>Please confirm you\'re able to take this student otherwise another teacher may be assigned.' + '<br><br><a style="background: #4950F6;color: #fff !important; transition: background .15s ease; border-radius: 10px;font-size: 14px; letter-spacing: 0px; font-weight: 700; text-transform: uppercase; line-height: 16px; text-decoration: none !important;cursor: pointer; justify-content: center; padding: 16px 32px; height: 48px; text-align: center; white-space: nowrap; border: none;" href="https://culturestride.com/sohostudents">Confirm Class</a>' + signature + '</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data: '[ACTION REQUIRED] You have a new student - ' + stuFirstName + ' ' + stuLastName,
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
},
emailStartNoInput: function (email, className,practicalSpeakingLevel, stuEmail, firstName, lastName, langHome, organiseGroupClass) {
console.log('emailStartNoInput is run');
var signature = module.exports.signature() // signature
var params = {
Destination: { /* required */
ToAddresses: [
'michael+notifications@culturestride.com',
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: 'Student details:<br><br>Group Class: ' + organiseGroupClass + '<br>Name: ' + firstName + ' ' + lastName + '<br>Email: ' + stuEmail + '<br>Language Spoken at Home (Chinese): ' + langHome + '<br>Practical Speaking Level: ' + practicalSpeakingLevel + signature + '</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data: className + ' signed up but did not book a Theory lesson',
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
},
emailTeachersEntryForm: function (email, stuFirstName, stuLastName, stuGoal, stuReadingLevel, stuHistory, listeningLevel, writingLevel) {
console.log('emailTeachersEntryForm is run');
var signature = module.exports.signature(); // module.exports is needed to call a local function within module.exports
var btnCSS = module.exports.btnCSS(); // module.exports is needed to call a local function within module.exports
var params = {
Destination: { /* required */
ToAddresses: [
email,
'michael+notifications@culturestride.com',
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>Hi,<br><br>Student Name: ' + stuFirstName + ' ' + stuLastName + '<hr>Learning Context: ' + stuHistory + '<hr>HSK Goal: ' + stuGoal + '<br>Reading Level: HSK ' + stuReadingLevel + '<br>Listening Level: HSK ' + listeningLevel + '<br>Speaking Level: Listen to their speaking level at <a href="https://culturestride.com/sohostudents">https://culturestride.com/sohostudents</a><br>Writing Level: ' + writingLevel + '<br><br><a style="' + btnCSS + '" href="https://culturestride.com/sohostudents">Confirm Class</a><br>' + signature + '</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data: '[STUDENT ENTRY FORM] - ' + stuFirstName + ' ' + stuLastName,
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
},
emailTeacherStuTransferred: function (stuFirstName, stuLastName, level, langHome, stuEmail, email, teaFirstName, teaLastName, confirmed, classType, start, end) {
console.log('emailTeacherStuTransferred is run');
var signature = module.exports.signature() // signature
if (confirmed == 0) { // email teacher to confirm class
var params = {
Destination: { /* required */
ToAddresses: [
email,
'michael+notifications@culturestride.com',
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>Hi,<br><br>A new student has been assigned to you:<br><br>Teacher: ' + teaFirstName + ' ' + teaLastName + '<br>Level: ' + level + '<br>Language Spoken at Home (Chinese): ' + langHome + '<br>Type: ' + classType + '<br>Start (China time): ' + start + ' UTC<br>End (China time): ' + end + ' UTC<br><br>Student Email: ' + stuEmail + '<br><br><br>Please confirm you\'re able to take this student otherwise another teacher may be assigned.' + '<br><br><a style="background: #4950F6;color: #fff !important; transition: background .15s ease; border-radius: 10px;font-size: 14px; letter-spacing: 0px; font-weight: 700; text-transform: uppercase; line-height: 16px; text-decoration: none !important;cursor: pointer; justify-content: center; padding: 16px 32px; height: 48px; text-align: center; white-space: nowrap; border: none;" href="https://culturestride.com/sohostudents">Confirm Class</a>' + signature + '</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data: '[ACTION REQUIRED] You have a new student - ' + stuFirstName + ' ' + stuLastName,
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
} else { // email teacher to message student
var params = {
Destination: { /* required */
ToAddresses: [
email,
'michael+notifications@culturestride.com',
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>Hi,<br><br>A new student has been assigned to you:<br><br>Teacher: ' + teaFirstName + ' ' + teaLastName + '<br>Level: ' + level + '<br>Language Spoken at Home (Chinese): ' + langHome + '<br>Type: ' + classType + '<br>Start (China time): ' + start + ' UTC<br>End (China time): ' + end + ' UTC<br><br>Student Email: ' + stuEmail + '<br><br><br>Please confirm you\'re able to take this student otherwise another teacher may be assigned.' + '<br><br><a style="background: #4950F6;color: #fff !important; transition: background .15s ease; border-radius: 10px;font-size: 14px; letter-spacing: 0px; font-weight: 700; text-transform: uppercase; line-height: 16px; text-decoration: none !important;cursor: pointer; justify-content: center; padding: 16px 32px; height: 48px; text-align: center; white-space: nowrap; border: none;" href="https://culturestride.com/sohomessages">Message Student</a>' + signature + '</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data: '[ACTION REQUIRED] You have a new student - ' + stuFirstName + ' ' + stuLastName,
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
}
},
emailTeacherMessageStuTransferred: function (stuFirstName, stuLastName, level, langHome, stuEmail, email, teaFirstName, teaLastName) {
console.log('emailTeacherStuTransferred is run');
var signature = module.exports.signature() // signature
var params = {
Destination: { /* required */
ToAddresses: [
email,
'michael+notifications@culturestride.com',
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>Hi,<br><br>A new student has been assigned to you:<br><br>Teacher: ' + teaFirstName + ' ' + teaLastName + '<br>Level: ' + level + '<br>Language Spoken at Home (Chinese): ' + langHome + '<br><br>Student Email: ' + stuEmail + '<br><br><br>Please confirm you\'re able to take this student otherwise another teacher may be assigned.' + '<br><br><a style="background: #4950F6;color: #fff !important; transition: background .15s ease; border-radius: 10px;font-size: 14px; letter-spacing: 0px; font-weight: 700; text-transform: uppercase; line-height: 16px; text-decoration: none !important;cursor: pointer; justify-content: center; padding: 16px 32px; height: 48px; text-align: center; white-space: nowrap; border: none;" href="https://culturestride.com/sohomessages">Message Student</a>' + signature + '</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data: '[ACTION REQUIRED] You have a new student - ' + stuFirstName + ' ' + stuLastName,
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
},
uploadUsageDataToStripe: async function (subscriptionItemId, usageQty, type) {
const {
v4: uuid
} = require('uuid');
const subscriptionItemID = subscriptionItemId;
// The usage number you've been keeping track of in your database for the last 24 hours.
const usageQuantityMin = usageQty * 60; // convert it into minutes
// The idempotency key allows you to retry this usage record call if it fails.
const idempotencyKey = uuid();
const timestamp = parseInt(Date.now() / 1000 - 300); // 5 mins before execution
console.log('stripeUpload (' + type + '): ' + subscriptionItemId + ' - ' + usageQuantityMin + ' mins - ' + timestamp);
// upload data to stripe
try {
await stripe.subscriptionItems.createUsageRecord(
subscriptionItemID, {
quantity: usageQuantityMin,
timestamp: timestamp,
action: 'set',
}, {
idempotencyKey,
}
);
} catch (error) {
console.error(`Usage report failed for item ID ${subscriptionItemID} with
idempotency key ${idempotencyKey}: ${error.toString()}`);
}
},
createStripeCustomer: async function (sohoSID, email, firstName, lastName) {
var fullName = firstName + ' ' + lastName;
const customer = await stripe.customers.create({
email: email,
name: fullName,
metadata: {
'sohoSID': sohoSID
}
});
dbjsMethods.updateStripeCustomerId(customer.id, sohoSID);
},
sendMessageNotificationEmail: function (receiverType, firstName, email, senderFirstName, senderLastName, message) {
console.log('sendMessageNotificationEmail is run');
var btnCSS = module.exports.btnCSS(); // module.exports is needed to call a local function within module.exports
var signature = module.exports.signature(); // module.exports is needed to call a local function within module.exports
var messageCSS = 'width: fit-content; background: #f6f9fc; margin: 2px; padding: 7px 15px; border-radius: 20px; max-width: 75%;';
if (receiverType == 'student') {
var url = "https://culturestride.com/a/messages";
} else {
var url = "https://culturestride.com/sohomessages";
}
var params = {
Destination: { /* required */
ToAddresses: [
email
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>Hi ' + firstName + ',<br><br>' + senderFirstName + ' ' + senderLastName + ' sent you messages on Culturestride:<br><p style="' + messageCSS + '">' + message + '</p></html><br><a style="' + btnCSS + '" href="' + url + '" target="_blank" >Reply To Messages</a>' + signature + '</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data: senderFirstName + ' ' + senderLastName + ' sent you new messages',
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
},
emailStudentStartFree: function (email, firstName, trialLessonStart) {
console.log('emailStudentStartFree is run');
var signature = module.exports.signature() // signature
var params = {
Destination: { /* required */
ToAddresses: [
email
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>Hi ' + firstName + ',<br><br>Thanks for joining Culturestride! Your Theory lesson is pending teacher acceptance and details are as follows:<br><br>When: ' + trialLessonStart + '<br>Duration: 45 min<br><br>You should expect an email within 48 hours introducing you to your teacher and asking you to confirm your class attendance, please action on this ASAP.<br><br><b>About Practical class</b><br><br>If you selected a Practical class to join, you should receive a separate confirmation about this class in the next 48 hours.<br><br><b>Before class, you need to</b><ul style="padding-left:10px"><li>Install and sign up for <a href="https://voovmeeting.com/download-center.html?from=1001">Voov</a> (primary meeting software)</li><li>Install and sign up for <a href="https://zoom.us/download">Zoom</a> (backup meeting software)</li></ul><br>' + signature + '</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data: 'Welcome to Culturestride, ' + firstName + '!',
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
},
emailStudentStartFreeNoTheory: function (email, firstName) {
console.log('emailStudentStartFreeNoTheory is run');
var signature = module.exports.signature() // signature
var params = {
Destination: { /* required */
ToAddresses: [
email
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>Hi ' + firstName + ',<br><br>Thanks for joining Culturestride! You have no Theory class bookings at the moment.<br><br><b>About Practical class</b><br><br>If you selected a Practical class to join, you should receive a separate email about this class in the next 48 hours.<br><br><b>Before class, you need to</b><ul style="padding-left:10px"><li>Install and sign up for <a href="https://voovmeeting.com/download-center.html?from=1001">Voov</a> (primary meeting software)</li><li>Install and sign up for <a href="https://zoom.us/download">Zoom</a> (backup meeting software)</li></ul><br>' + signature + '</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data: 'Welcome to Culturestride, ' + firstName + '!',
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
},
emailStudentBookTheoryClass: function (email, firstName, tFirstName, tLastName, start, timezone, joinLink, repeatNo) {
console.log('emailStudentBookTheoryClass is run');
var signature = module.exports.signature() // signature
var params = {
Destination: { /* required */
ToAddresses: [
email
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>Hi ' + firstName + ',<br><br>You booked ' + repeatNo + ' Theory class (' + tFirstName + ' ' + tLastName + ') starting ' + start + ' (' + timezone + ').<br><br>Join link: ' + joinLink + '<br><br><b>Before class, you need to</b><ul style="padding-left:10px"><li>Install and sign up starting <a href="https://voovmeeting.com/download-center.html?from=1001">Voov</a> (primary meeting software)</li><li>Install and sign up for <a href="https://zoom.us/download">Zoom</a> (backup meeting software)</li></ul><br>' + signature + '</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data: repeatNo + ' Theory class booked (' + start + ')',
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
},
emailStudentBookPracticalClass: function (email, firstName, tFirstName, tLastName, start, timezone, joinLink, repeatNo) {
console.log('emailStudentBookPracticalClass is run');
var signature = module.exports.signature() // signature
var params = {
Destination: { /* required */
ToAddresses: [
email
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>Hi ' + firstName + ',<br><br>You booked ' + repeatNo + ' Practical class (' + tFirstName + ' ' + tLastName + ') starting ' + start + ' (' + timezone + ').<br><br>Join link: ' + joinLink + '<br><br>Please note your class will be 30 mins in length if you are the only student in this timeslot, otherwise it will be a 1 hour class.<br><br><b>Before class, you need to</b><ul style="padding-left:10px"><li>Install and sign up for <a href="https://voovmeeting.com/download-center.html?from=1001">Voov</a> (primary meeting software)</li><li>Install and sign up for <a href="https://zoom.us/download">Zoom</a> (backup meeting software)</li></ul><br>' + signature + '</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data: repeatNo + ' Practical class booked (' + start + ')',
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
},
emailStudentConfirmTrialClass: function (email, firstName, lessonStartTime, teacherName, tProfileImage) {
console.log('emailStudentConfirmTrialClass is run');
var signature = module.exports.signature() // signature
var btnCSS = module.exports.btnCSS() // btncss
var imgCSS = `width: 60px;
display: block;
border-radius: 10px;
cursor: pointer;`
var params = {
Destination: { /* required */
ToAddresses: [
email
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>Hi ' + firstName + ',<br><br>Meet your Mandarin teacher who will lead you through your Mandarin learning journey!<br><br><img style="' + imgCSS + '" src="' + tProfileImage + '" ><br>Name: ' + teacherName + '<br>When: ' + lessonStartTime + '<br><br>Please click the confirm attendance button below ASAP, otherwise your class may be cancelled.<br><br><a style="' + btnCSS + '" href="https://culturestride.com/a/onboarding?autoConfirm=1">Confirm Attendance</a><br>' + signature + '</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data:'Confirm your Theory class ASAP',
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
},
emailStudentConfirmPracticalClass: function (firstName, email, time, timezone, link) {
console.log('emailStudentConfirmPracticalClass is run');
var signature = module.exports.signature() // signature
var btnCSS = module.exports.btnCSS() // btncss
var params = {
Destination: { /* required */
ToAddresses: [
email
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>Hi ' + firstName + ',<br><br>Please confirm your attendance to your Culturestride Practical Class.<br><br>When: ' + time + ' (' + timezone + ' time)<br><br>Please click the confirm attendance button below ASAP, otherwise your attendance may be forfeited.<br><br><a style="' + btnCSS + '" href="'+ link +'">Confirm Attendance</a><br>' + signature + '</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data:'Confirm your Practical class ASAP',
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
},
sendEmailToTeacherAboutTrialProcedure: function (stuFirstName, stuLastName, time, firstName, email, level) {
console.log('sendEmailToTeacherAboutTrialProcedure is run');
var signature = module.exports.signature() // signature
var params = {
Destination: { /* required */
ToAddresses: [
email,
'michael+notifications@culturestride.com'
/* more items */
]
},
Message: { /* required */
Body: { /* required */
Html: {
Charset: "UTF-8",
Data: '<html><p>Hi ' + firstName + ',<br><br>You have confirmed your lesson at ' + time + ' with ' + stuFirstName + ' ' + stuLastName + ' (' + level + '). This lesson <b> needs to be confirmed by the student and he/she needs to complete the student entry form </b> to actually happen and will be automatically deleted if he/she doesn\'t do both of these actions. <br><br>To prepare for your lesson:<ol><li><a href="https://culturestride.com/sohomessages" target="_blank">Send a message</a> to your student <b>before</b> the lesson to build your relationship with them</li><li>Prepare material relevant to the student\'s current language level</li></ol>If you have problems or lack materials, please reach out to Masae Zhang (wechat: yumiko830250) or Michael Liang (wechat: michaelliangaus).' + signature + '</p></html>'
}
},
Subject: {
Charset: 'UTF-8',
Data: 'Lesson Pending Student Confirmation - ' + stuFirstName + ' ' + stuLastName,
}
},
Source: 'Culturestride <michael@culturestride.com>', /* required */
};
// Create the promise and SES service object
var sendPromise = new AWS.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states
sendPromise.then(
function(data) {
console.log('Message sent to: ' + email + '(id: ' + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
},
notifyStudentAboutHomeworkAssignment: function (stuFirstName, email, taskLevel, taskQuestion) {
console.log('notifyStudentAboutHomeworkAssignment is run');
// today's date
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
today = dd + '/' + mm + '/' + yyyy;