This repository was 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 pathfind-new-checks.js
190 lines (173 loc) · 6.04 KB
/
find-new-checks.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
/**
* 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 request = require('request');
var async = require('async');
var fs = require('fs');
/**
* This action is triggered by a new check image added to object storage, or in this case a CouchDB database.
* This action is idempotent. If it fails, it can be retried.
*
* 1. Fetch the record from the 'incoming' object storage container.
* 2. Process the image for account, routing number, and amount move it to another 'processed' database with metadata and a confidence score.
*
* @param params.OBJECT_STORAGE_USER_ID Object storage user id
* @param params.OBJECT_STORAGE_PASSWORD Object storage password
* @param params.OBJECT_STORAGE_PROJECT_ID Object storage project id
* @param params.OBJECT_STORAGE_REGION_NAME Object storage region
* @param params.OBJECT_STORAGE_INCOMING_CONTAINER_NAME Object storage container where the image is
* @return Standard OpenWhisk success/error response
*/
function main(params) {
console.log("Retrieving file list");
var wsk = openwhisk();
// Configure object storage connection
var os = new ObjectStorage(
params.OBJECT_STORAGE_REGION_NAME,
params.OBJECT_STORAGE_PROJECT_ID,
params.OBJECT_STORAGE_USER_ID,
params.OBJECT_STORAGE_PASSWORD
);
return new Promise(function(resolve, reject) {
os.authenticate(function(err, response, body) {
if (err) {
console.log("Authentication failure", err);
whisk.done(null, err);
} else {
os.listFiles(params.OBJECT_STORAGE_INCOMING_CONTAINER_NAME, function(err, response, files) {
console.log(files);
console.log("Found", files.length, "files");
var tasks = files.map(function(file) {
return function(callback) {
asyncCallSaveCheckImagesAction(
"/_/openchecks/save-check-images",
file.name,
file.content_type,
file.last_modified,
callback
);
};
});
async.waterfall(tasks, function(err, result) {
if (err) {
console.log("Error", err);
reject(err);
} else {
resolve({
status: "Success"
});
}
});
});
}
});
});
}
/**
* This function provides a way to invoke other OpenWhisk actions directly and asynchronously
*
* @param actionName The id of the record in the Cloudant 'processed' database
* @param fileName Cloudant username (set once at action update time)
* @param contentType Cloudant password (set once at action update time)
* @param lastModified Cloudant password (set once at action update time)
* @param callback Cloudant password (set once at action update time)
* @return The reference to a configured object storage instance
*/
function asyncCallSaveCheckImagesAction(actionName, fileName, contentType, lastModified, callback) {
console.log("Calling", actionName, "for", fileName);
var wsk = openwhisk();
return new Promise(function(resolve, reject) {
wsk.actions.invoke({
"actionName": actionName,
"params": {
fileName: fileName,
contentType: contentType,
lastModified: lastModified
},
}).then(
function(activation) {
console.log(actionName, "[activation]", activation);
resolve(activation);
}
).catch(
function(error) {
console.log(actionName, "[error]", error);
reject(error);
}
);
});
}
/**
* This is an adapter class for OpenStack OBJECT_STORAGE based object storage.
*
* @param region The id of the record in the Cloudant 'processed' database
* @param projectId Cloudant username (set once at action update time)
* @param userId Cloudant password (set once at action update time)
* @param password Cloudant password (set once at action update time)
* @return The reference to a configured object storage instance
*/
function ObjectStorage(region, projectId, userId, password) {
var self = this;
if (region === "dallas") {
self.baseUrl = "https://dal.objectstorage.open.softlayer.com/v1/AUTH_" + projectId + "/";
} else if (region == "london") {
self.baseUrl = "https://lon.objectstorage.open.softlayer.com/v1/AUTH_" + projectId + "/";
} else {
throw new Error("Invalid Region");
}
self.authenticate = function(callback) {
request({
uri: "https://identity.open.softlayer.com/v3/auth/tokens",
method: 'POST',
json: {
"auth": {
"identity": {
"methods": [
"password"
],
"password": {
"user": {
"id": userId,
"password": password
}
}
},
"scope": {
"project": {
"id": projectId
}
}
}
}
}, function(err, response, body) {
if (!err) {
self.token = response.headers["x-subject-token"];
}
callback(err, response, body);
});
};
self.listFiles = function(container, callback) {
request({
uri: self.baseUrl + container,
method: 'GET',
headers: {
"X-Auth-Token": self.token,
"Accept": "application/json"
},
json: true
}, callback);
};
}