This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
forked from ferjul17/monpetitplacement_sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
call-all-apis.ts
178 lines (151 loc) · 4.53 KB
/
call-all-apis.ts
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
/* eslint-disable import/no-extraneous-dependencies */
import prompt from 'prompt';
import 'dotenv/config';
import { logger } from './logger';
import { Api } from './src';
import { isExternalError, isGatewayError } from './src/errors';
async function retrieveCreds() {
let { USERNAME, PASSWORD } = process.env;
if (USERNAME === undefined) {
const response = await prompt.get(['username']);
USERNAME = response.username as string;
if (USERNAME) {
throw new Error(`Missing env var "username".`);
}
}
if (PASSWORD === undefined) {
const response = await prompt.get(['password']);
PASSWORD = response.password as string;
if (!PASSWORD) {
throw new Error(`Missing env var "password".`);
}
}
return {
USERNAME,
PASSWORD,
};
}
(async () => {
const { USERNAME, PASSWORD } = await retrieveCreds();
const api = new Api();
const res = await api.login({ username: USERNAME, password: PASSWORD });
if (isExternalError(res)) {
logger.fatal('login error', res);
return;
}
if (isGatewayError(res)) {
logger.fatal('gateway error', res);
return;
}
const token = res.access_token;
const user = await api.getMe({ token });
if (isExternalError(user)) {
logger.fatal('getMe error', res);
return;
}
const profileNames = ['volontaire', 'energique', 'ambitieux', 'intrepide'];
const publicInvestorProfiles = profileNames.map((profile) => {
return api.getInvestProfileHistory({
profile,
});
});
const profiles = await Promise.all(publicInvestorProfiles);
logger.warn({
action: 'login',
username: USERNAME,
});
if (isGatewayError(user)) {
logger.fatal('getUser error', user);
return;
}
const userId = user.id;
const investProfileCategories = await api.getInvestProfileCategories({ token });
const investProfiles = await api.getInvestProfiles({ token });
const userCoupons = await api.getUserCoupons({ token, userId });
const coupons = await api.getCoupons({ token, userId });
const userKycs = await api.getUserKycs({ token, userId });
if (isExternalError(userKycs)) {
logger.fatal('getUserKycs error', userKycs);
return;
}
if (isGatewayError(userKycs)) {
logger.fatal('gateway error', res);
return;
}
const members = userKycs['hydra:member'];
if (!user.investmentAccounts) {
logger.error({
message: 'no investmentAccounts found',
user,
});
return;
}
const advicesDTO = await Promise.all(
user.investmentAccounts?.map((investmentAccount) => {
return api.getAdviceDTO({
token,
userInvestmentAccountId: parseInt(investmentAccount.id, 10),
});
})
);
// Below doesn't work if no strategy in account
const activeInvestmentAccounts = user.investmentAccounts.filter(
(investmentAccount) => investmentAccount.status !== 'pending'
);
if (!activeInvestmentAccounts.length) {
logger.error({
message: 'No active investment accounts found',
accounts: user.investmentAccounts,
});
return;
}
logger.warn('Are you still waiting for a strategy ?');
prompt.start();
const response = await prompt.get(['strategyComplete']);
if (response.strategyComplete !== 'y') {
return;
}
const advices = await Promise.all(
members.map((adviceId) => api.getAdvice({ token, adviceId: parseInt(adviceId.id, 10) }))
);
if (advices && !advices.length) {
logger.warn('No advices found');
}
// below is denied for inactive investment accounts
const availableProducts = await Promise.all(
members.map(({ id }) => api.getAvailableProducts({ token, userKycsId: parseInt(id, 10) }))
);
const userFinancialCapitals = await Promise.all(
activeInvestmentAccounts.map(({ id }) =>
api.getUserFinancialCapital({ token, userInvestmentAccountId: parseInt(id, 10) })
)
);
const userInvestmentValuesInput = await Promise.all(
activeInvestmentAccounts.map(({ id }) =>
api.getUserInvestmentValuesInput({ token, userInvestmentAccountId: parseInt(id, 10) })
)
);
const userInvestmentAccountProducts = await Promise.all(
activeInvestmentAccounts.map(({ id }) =>
api.getUserInvestmentAccountProducts({ token, userInvestmentAccountId: parseInt(id, 10) })
)
);
logger.info({
profiles,
investProfileCategories,
investProfiles,
user,
userCoupons,
coupons,
userKycs,
advicesDTO,
advices,
availableProducts,
userFinancialCapitals,
userInvestmentValuesInput,
userInvestmentAccountProducts,
});
})().catch((e) => {
logger.fatal(e);
process.exit(-1);
});