-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigPlugin.js
More file actions
112 lines (90 loc) · 3.74 KB
/
configPlugin.js
File metadata and controls
112 lines (90 loc) · 3.74 KB
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
var merchantLib = require('./lib/merchantLib');
var dbClient = require('./lib/dbClient');
var redisClient = require('./lib/redisClient');
var redisMerchantKey = 'config_merchant_';
var configLog;
var ConfigPlugin = function (db_ENV, redis_ENV, logPlugin) {
// Setup configLog and database/redis clients
setLogger(logPlugin); // <-- Must run BEFORE database/redis clients to set configLog
merchantLib.loggerSetup(configLog);
dbClient.loggerSetup(configLog);
dbClient.dbConnect(db_ENV);
redisClient.loggerSetup(configLog);
redisClient.redisConnect(redis_ENV);
this.merchantLookup = function (internalID, external_CB) {
configLog.debug('Merchant lookup started');
if (redis_ENV) { // <-- Allows plugin to run without Redis
redisClient.redisLookup(redisMerchantKey + internalID, function(err, res){
if (err) {
// Cannot connect to Redis, search database and update cache
configLog.error(err);
findMerchant_updateRedis(internalID, external_CB);
} else if (res === null) {
// No record found in Redis, search database and update cache
findMerchant_updateRedis(internalID, external_CB);
} else {
// Redis record found and returned
return external_CB(null, res);
}
});
} else {
// No redis client, search database without updating cache
merchantLib.merchantFind(internalID, external_CB);
}
};
this.merchantInvoiceConfig = function (internalID, external_CB) {
this.merchantLookup(internalID, function(err, merchant){
if (err) {
configLog.error(err);
return external_CB(err, null);
} else {
// Merchant found, extracting/returning invoiceConfig for merchant
var merchantInvoice = merchant.invoiceConfig;
merchantInvoice.internalID = internalID;
return external_CB(null, merchantInvoice);
}
});
};
this.merchantSave = function (newMerchantObj, external_CB) {
merchantLib.merchantCreate(newMerchantObj, external_CB);
};
this.merchantUpdate = function (origMerchant, updateObj, external_CB) {
merchantLib.merchantModify(origMerchant, updateObj, external_CB);
};
this.merchantDelete = function (merchant, external_CB) {
merchantLib.merchantRemove(merchant, external_CB);
};
this.getToken = function (newMerchantObj, external_CB) {
merchantLib.merchantFindOrCreate(newMerchantObj, external_CB);
}
};
function findMerchant_updateRedis (internalID, external_CB) {
merchantLib.merchantFind(internalID, function (err, merchant) {
if (err) {
// Error connecting to database, exit with error
configLog.error(err);
return external_CB(err, null);
} else {
// Updates cache but does NOT wait and does NOT handle success/failure
redisClient.redisUpdate(redisMerchantKey + internalID, merchant);
// Merchant found in database and returned
return external_CB(null, merchant);
}
});
}
// Setup functions
function setLogger (logPlugin) {
var defaultLog = {
info: function(msg) { console.log(msg); },
debug: function(msg) { console.log(msg); },
error: function(msg) { console.log(msg); },
fatal: function(msg) { console.log(msg); } };
if (logPlugin) { configLog = logPlugin; }
else { configLog = defaultLog; }
}
function testingStub (testFunctions){
}
module.exports = {
ConfigPlugin: ConfigPlugin,
testingStub: testingStub
};