1
+ // Load the AWS SDK for Node.js
2
+ var AWS = require ( 'aws-sdk' ) ;
3
+ var DynamoDB = require ( '../database/DynamoDB' ) ;
4
+ // Set the region
5
+ AWS . config . update ( { region : 'us-east-1' } ) ;
6
+ var baseURL = 'https://master.d34e7o1nxh5a7i.amplifyapp.com/?query=' ;
7
+
8
+ // Create sendEmail params
9
+ var params = {
10
+ Destination : { /* required */
11
+ ToAddresses : [ ]
12
+ } ,
13
+ Message : { /* required */
14
+ Body : { /* required */
15
+ Html : {
16
+ Charset : "UTF-8" ,
17
+ Data : ""
18
+ } ,
19
+ Text : {
20
+ Charset : "UTF-8" ,
21
+ Data : "TEXT_FORMAT_BODY"
22
+ }
23
+ } ,
24
+ Subject : {
25
+ Charset : 'UTF-8' ,
26
+ Data : 'SafeHopper ALERT!'
27
+ }
28
+ } ,
29
+ Source :
'[email protected] ' , /* required */
30
+ ReplyToAddresses : [
31
+
32
+ /* more items */
33
+ ] ,
34
+ } ;
35
+
36
+ exports . sendEmail = function ( req ) {
37
+ getContacts ( req ) ;
38
+ }
39
+
40
+ function sendMessage ( contacts , alertId , name ) {
41
+ if ( contacts != null ) {
42
+ var text = "SafeHopper Alert!\n" + name +
43
+ " is requesting your help.\n\n Follow this link to see their current loction:\n" +
44
+ baseURL + alertId ;
45
+ params . Message . Body . Html . Data = text ;
46
+ for ( var i = 0 ; i < contacts . length ; i ++ ) {
47
+ params . Destination . ToAddresses [ i ] = contacts [ i ] . email ;
48
+ }
49
+
50
+ // Create the promise and SES service object
51
+ var sendPromise = new AWS . SES ( { apiVersion : '2010-12-01' } ) . sendEmail ( params ) . promise ( ) ;
52
+
53
+ // Handle promise's fulfilled/rejected states
54
+ sendPromise . then (
55
+ function ( data ) {
56
+ console . log ( data . MessageId ) ;
57
+ } ) . catch (
58
+ function ( err ) {
59
+ console . error ( err , err . stack ) ;
60
+ } ) ;
61
+ }
62
+ }
63
+
64
+ function getContacts ( req ) {
65
+ var body = {
66
+ userEmail : req . email ,
67
+ }
68
+ DynamoDB . getContacts ( body ) . then ( result => {
69
+ sendMessage ( result , req . alertId , req . name ) ;
70
+ } ) . catch ( err => {
71
+ // Error was generated
72
+ console . log ( "ERROR: can't get contacts" ) ;
73
+ } )
74
+ }
0 commit comments