-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·76 lines (70 loc) · 2.23 KB
/
main.js
File metadata and controls
executable file
·76 lines (70 loc) · 2.23 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
#!/usr/local/bin/node
'use strict';
const getStdin = require('get-stdin');
const R = require('ramda');
const { processPayment } = require('./lib/payment_process');
const { processCardCreation } = require('./lib/credit_card_process');
const { pool } = require('./lib/dal');
const { handleError } = require('./lib/error_handling');
/*
* receive notification via stdin and process in some module
* notification example:
*
* - action process_payment:
* process a new payment on gateway
* {
* action: 'process_payment',
* id: uuid_v4 for payment,
* subscription_id: uuid_v4 for subscription,
* created_at: datetime of payment creation,
* }
*
* - action generate_card:
* process and generate a new card based on valid card_hash
* {
* action: 'generate_card',
* id: uuid_v4 for credit_card
* }
*/
const main = async (notification) => {
console.log('received -> ', notification);
const jsonNotification = JSON.parse(notification),
resource_id = jsonNotification.id,
dbclient = await pool.connect();
switch (jsonNotification.action) {
case 'process_payment':
console.log('processing payment ', resource_id);
const { transaction } = await processPayment(dbclient, resource_id);
console.log('generate transaction with id ', transaction.id);
break;
case 'generate_card':
console.log('processing card ', resource_id);
const card = await processCardCreation(dbclient, resource_id);
console.log('generate card with id ', card.gateway_data.id);
break;
default:
throw new Error('invalid action');
};
dbclient.release();
};
const finishProcessOk = (result) => {
console.log('finished ok ', result);
process.exitCode = 0;
process.exit(0);
};
const finishProcessErr = (result) => {
console.log('finished with error ', result);
handleError(result);
process.exitCode = 1;
process.exit(1);
};
getStdin().then((notification) => {
if(!R.isNil(notification)) {
main(notification)
.then(finishProcessOk)
.catch(finishProcessErr);
} else {
console.log('invalid stdin');
process.exit(1);
}
});