-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
110 lines (102 loc) · 3.04 KB
/
index.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
import myCache from './service/cache.js';
import { validateDate, validateToken } from './validation/validateFlags.js';
import { readCsvTransStream } from './portfolio/index.js';
import { portfolioBalanceSummary } from './portfolio/portfolioBalance.js';
import { info } from '../utils/colors.js';
export const checkCmnd = async flags => {
const { date, token } = flags;
const flagKeys = Object.keys(flags);
// check if user didn't add neither the date and token flags
if (!flagKeys.includes('token') && !flagKeys.includes('date')) {
const key = `tokens`;
let cacheExists = myCache.has(key)
? await myCache.get(cacheKey)
: false;
if (cacheExists) {
await portfolioBalanceSummary(cacheExists);
return;
} else {
let tokenAmountObj = await readCsvTransStream(undefined, undefined);
myCache.set(key, tokenAmountObj);
await portfolioBalanceSummary(tokenAmountObj);
return;
}
}
// check if user added the date flag only, but without a value
if (
flagKeys.includes('date') &&
!flagKeys.includes('token') &&
!date.length
)
return console.log(info(`INFO: Please provide a date value`));
else if (!flagKeys.includes('token')) {
const key = `date_${date}`;
if (validateDate(date)) {
let cacheExists = myCache.has(key)
? await myCache.get(cacheKey)
: false;
if (cacheExists) {
await portfolioBalanceSummary(cacheExists);
return;
} else {
let tokenAmountObj = await readCsvTransStream(undefined, date);
myCache.set(key, tokenAmountObj);
await portfolioBalanceSummary(tokenAmountObj);
return;
}
}
return;
}
// check if user added the token flag only, but without a value
if (
flagKeys.includes('token') &&
!flagKeys.includes('date') &&
!token.length
)
return console.log(info('INFO: Please provide a token symbol value'));
else if (!flagKeys.includes('date')) {
const key = `token_${token}`;
if (validateToken(token)) {
let cacheExists = myCache.has(key)
? await myCache.get(cacheKey)
: false;
if (cacheExists) {
await portfolioBalanceSummary(cacheExists);
return;
} else {
let tokenAmountObj = await readCsvTransStream(token, undefined);
myCache.set(key, tokenAmountObj);
await portfolioBalanceSummary(tokenAmountObj);
return;
}
}
return;
}
// check if user added both the date and token flags, but without a value
if (
flagKeys.includes('token') &&
flagKeys.includes('date') &&
(!token.length || !date.length)
)
return console.log(
info('INFO: Please provide a token symbol value and a date value')
);
else if (flagKeys.includes('token') && flagKeys.includes('date')) {
const key = `token_${token}_date_${date}`;
if (validateToken(token) && validateDate(date)) {
let cacheExists = myCache.has(key)
? await myCache.get(cacheKey)
: false;
if (cacheExists) {
await portfolioBalanceSummary(cacheExists);
return;
} else {
let tokenAmountObj = await readCsvTransStream(token, date);
myCache.set(key, tokenAmountObj);
await portfolioBalanceSummary(tokenAmountObj);
return;
}
}
return;
}
};