This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathrecord-check-deposit.js
147 lines (128 loc) · 5.69 KB
/
record-check-deposit.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
/**
* 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 openwhisk = require('openwhisk');
var Cloudant = require('cloudant');
var request = require('request');
var async = require('async');
/**
* This action is fired in response to newly parsed check data. It then contacts an external payment system and sends a notification.
* This action is idempotent. If it fails, it can be retried.
*
* 1. Fetch the record from the 'parsed' database: to and from account numbers, amount, and confidence.
* 2. Insert it into the 'processed' database which an external system would use, or otherwise invoke that other system.
* 3. Send an email notification to the customer that their check has been processed.
*
* @param params._id The id of the record in the Cloudant 'processed' database
* @param params.CLOUDANT_USERNAME Cloudant username
* @param params.CLOUDANT_PASSWORD Cloudant password
* @param params.CLOUDANT_PROCESSSED_DATABASE Cloudant database to store the processed data to
* @param params.SENDGRID_API_KEY Cloudant password
* @param params.SENDGRID_FROM_ADDRESS Address to set as sender
* @return Standard OpenWhisk success/error response
*/
function main(params) {
var wsk = openwhisk();
// Configure database connection
var cloudant = new Cloudant({
account: params.CLOUDANT_USERNAME,
password: params.CLOUDANT_PASSWORD
});
var processedDb = cloudant.db.use(params.CLOUDANT_PROCESSED_DATABASE);
if (!params.deleted) {
var processed = {};
processed._id = params._id;
processed.toAccount = params.toAccount;
processed.fromAccount = params.fromAccount;
processed.routingNumber = params.routingNumber;
processed.email = params.email;
processed.amount = params.amount;
processed.timestamp = params.timestamp;
return new Promise(function(resolve, reject) {
async.waterfall([
// Insert the check data into the processed database.
function(callback) {
console.log('[record-check-deposit.main] Updating the processed database');
processedDb.insert(processed, function(err, body, head) {
if (err) {
console.log('[record-check-deposit.main] error: processedDb');
console.log(err);
return callback(err);
} else {
console.log('[record-check-deposit.main] success: processedDb');
console.log(body);
return callback(null, processed);
}
});
},
// Send email notification, simulating connectivity to backend system and notifying customer.
function(processed, callback) {
console.log('[record-check-deposit.main] Sending notification email');
subject = 'Check deposit accepted';
content = 'Hello, ';
content += 'your deposit for $' + processed.amount + ' was accepted into your account ' + processed.toAccount + ' on ' + format(processed.timestamp) + '. ';
content += 'For reference, the check number and routing number were: ' + processed.fromAccount + '-' + processed.routingNumber + '. ';
console.log("Mailing: " + '{"personalizations": [{"to": [{"email": "' + processed.email + '"}]}],"from": {"email": "' + params.SENDGRID_FROM_ADDRESS + '"},"subject": "' + subject + '","content": [{"type": "text/plain", "value": "' + content + '"}]}');
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": "' + processed.email + '"}]}],"from": {"email": "' + params.SENDGRID_FROM_ADDRESS + '"},"subject": "' + subject + '","content": [{"type": "text/plain", "value": "' + content + '"}]}'
}, function(err, response, body) {
if (err) {
console.log('[record-check-deposit.main] error: ');
console.log(err);
callback(err);
return;
} else {
console.log('[record-check-deposit.main] success: ');
console.log(body);
callback(null);
return;
}
});
}
],
function(err, result) {
if (err) {
console.log("Error", err);
reject(err);
} else {
resolve({
status: "Success"
});
}
}
);
});
} else {
return Promise.resolve({
status: "Success"
});
}
}
/**
* This function converts from a Unix timestamp into a human readable date
*
* @param timestamp The Unix timestamp
* @return The formatted string
*/
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();
}