This repository was archived by the owner on May 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathalert-customer-event.js
116 lines (107 loc) · 5.42 KB
/
alert-customer-event.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
/**
* Copyright 2016-2017 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var request = require('request-promise');
/**
* 1. Triggered whenever data changes in the database (what about proactive warranty renewal alert?)
* 2. Detect the type of event (order status change ['ordered', 'pending'], warranty renewal)
*
* @param params.id The id of the record in the Cloudant 'service' database, if invoked by a database change
* @param params.appliance The appliance object if invoked by the nightly alarm trigger
* @param params.SENDGRID_API_KEY SendGrid key for sending notifications
* @param params.SENDGRID_FROM_ADDRESS Address to set as sender
* @return Standard OpenWhisk success/error response
*/
function main(params) {
// Inspect the params sent to this action: Either invoked directly from check-warranty-renewal or via change to the 'order' database.
// console.log(params);
if (params.hasOwnProperty('appliance')) {
// Their warranty will expire soon.
// Invoked manually by the check-warranty-renewal action.
console.log('[alert-customer-event.main] invoked by check-warranty-renewal');
var appliance = params.appliance;
email = appliance.owner_email;
subject = 'Warranty expires soon for ' + appliance.serial;
content = 'Hello ' + appliance.owner_name + ', ';
content += 'your appliance with serial number ' + appliance.serial + ' will expire on ' + format(appliance.warranty_expiration) + '. ';
content += 'Contact a representative to renew your warranty.';
return send(email, subject, content, params)
.then(res => ({
success: '[alert-customer-event.main] Success sending notification'
}))
.catch(err => ({
error: '[alert-customer-event.main] Error sending notification: ' + err
}));
} else {
// Triggered by a change event in the order database.
var order = params;
if (order.status == 'ordered') {
// The order was automatically placed for them (in warranty).
console.log('[alert-customer-event.main] triggered by a newly created order under warranty.');
email = order.owner_email;
subject = 'Part automatically ordered for appliance: ' + order.appliance_serial;
content = 'Your appliance told us that one of its parts needed a replacement. Since it is still under warranty until ';
content += format(order.appliance_warranty_expiration) + ', we have automatically ordered a replacement. ';
content += 'It will be delivered soon.';
return send(email, subject, content, params)
.then(res => ({
success: '[alert-customer-event.main] Success sending notification'
}))
.catch(err => ({
error: '[alert-customer-event.main] Error sending notification: ' + err
}));
} else if (order.status == 'pending') {
// The order was entered in pending mode (not in warranty).
console.log('[alert-customer-event.main] triggered by a newly pending order out of warranty.');
email = order.owner_email;
subject = 'Part ready to order for appliance: ' + order.appliance_serial;
content = 'Your appliance told us that one of its parts needed a replacement. Since it is no longer under warranty ';
content += '(it expired on ' + format(order.appliance_warranty_expiration) + '), you will need to approve the pending order. ';
content += 'Complete the form with your payment and the part will be on its way soon.';
return send(email, subject, content, params)
.then(res => ({
success: '[alert-customer-event.main] Success sending notification'
}))
.catch(err => ({
error: '[alert-customer-event.main] Error sending notification: ' + err
}));
} else {
// Some other order status we're not implementing at the moment.
console.log('[alert-customer-event.main] triggered by some other order status.');
return new Promise(function(resolve, reject) {
resolve({
result: 'This workflow is not yet implemented.'
});
});
}
}
}
// Convert from Unix timestamp
function format(timestamp) {
var warranty_expiration_date = new Date(timestamp * 1000);
return (warranty_expiration_date.getMonth() + 1) + '/' + warranty_expiration_date.getDate() + '/' + warranty_expiration_date.getFullYear();
}
// Configure SendGrid (need to use request against the API directly)
function send(email, subject, content, params) {
return request({
url: 'https://api.sendgrid.com/v3/mail/send',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + params.SENDGRID_API_KEY
},
body: '{"personalizations": [{"to": [{"email": "' + email + '"}]}],"from": {"email": "' + params.SENDGRID_FROM_ADDRESS + '"},"subject": "' + subject + '","content": [{"type": "text/plain", "value": "' + content + '"}]}'
});
}