-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
163 lines (136 loc) · 4.83 KB
/
config.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
const Conf = require("conf");
const config = require("dotenv").config;
config();
const INTEREST_RATE = process.env.INTEREST_RATE || "";
const MORTGAGE_PAYEE_ID = process.env.MORTGAGE_PAYEE_ID || "";
const MORTGAGE_PAYEE_NAME = process.env.MORTGAGE_PAYEE_NAME || "";
const MORTGAGE_ACCOUNT_ID = process.env.MORTGAGE_ACCOUNT_ID || "";
const MAIN_ACCOUNT_ID = process.env.MAIN_ACCOUNT_ID || "";
const GHOSTFOLIO_SERVER_URL = process.env.GHOSTFOLIO_SERVER_URL || "";
const GHOSTFOLIO_TOKEN = process.env.GHOSTFOLIO_TOKEN || "";
const PAYEE_REGEX_MATCH = process.env.PAYEE_REGEX_MATCH || "";
const ACTUAL_SERVER_URL = process.env.ACTUAL_SERVER_URL || "";
const ACTUAL_SERVER_PASSWORD = process.env.ACTUAL_SERVER_PASSWORD || "";
const CRON_EXPRESSION = process.env.CRON_EXPRESSION || "0 */4 * * *";
const ACTUAL_SYNC_ID = process.env.ACTUAL_SYNC_ID || "";
const ENABLE_HOLD_INCOME_FOR_NEXT_MONTH = stringToBoolean(process.env.ENABLE_HOLD_INCOME_FOR_NEXT_MONTH);
const ENABLE_INTEREST_CALCULATION= stringToBoolean(process.env.ENABLE_INTEREST_CALCULATION);
const ENABLE_PAYEE_RENAME = stringToBoolean(process.env.ENABLE_PAYEE_RENAME);
const ENABLE_BANK_SYNC = stringToBoolean(process.env.ENABLE_BANK_SYNC);
const ENABLE_GHOSTFOLIO_SYNC = stringToBoolean(process.env.ENABLE_GHOSTFOLIO_SYNC);
function stringToBoolean(stringValue){
switch(stringValue?.toLowerCase()?.trim()){
case "true":
case "yes":
case "1":
return true;
case "false":
case "no":
case "0":
case null:
case undefined:
return false;
default:
return JSON.parse(stringValue);
}
}
function validateEnv(variables){
// Assert that all required environment variables are set
Object.entries(variables).forEach(([key, value]) => {
if (!value) {
throw new Error(`Missing environment variable: ${key}`);
}
})
}
function getAppConfigFromEnv() {
validateEnv({
ACTUAL_SERVER_URL,
ACTUAL_SERVER_PASSWORD,
ACTUAL_SYNC_ID,
CRON_EXPRESSION,
})
if (ENABLE_INTEREST_CALCULATION) {
validateEnv({
MORTGAGE_PAYEE_ID,
MORTGAGE_PAYEE_NAME,
MORTGAGE_ACCOUNT_ID,
MAIN_ACCOUNT_ID,
INTEREST_RATE,
})
}
if (ENABLE_PAYEE_RENAME) {
validateEnv({
PAYEE_REGEX_MATCH,
})
}
GHOSTFOLIO_ACCOUNT_MAPPING = {}
GHOSTFOLIO_ACTUAL_PAYEE_NAME_MAPPING = {}
if (ENABLE_GHOSTFOLIO_SYNC) {
ghostfolioAccount = process.env.GHOSTFOLIO_ACCOUNT
actualAccount = process.env.GHOSTFOLIO_ACTUAL_ACCOUNT
ghotfolioPayeeName = process.env.GHOSTFOLIO_ACTUAL_PAYEE_NAME
if (!ghostfolioAccount){
throw new Error(`Missing environment variable: GHOSTFOLIO_ACCOUNT`);
}
if (!actualAccount){
throw new Error(`Missing environment variable: GHOSTFOLIO_ACTUAL_ACCOUNT`);
}
if (!ghotfolioPayeeName){
throw new Error(`Missing environment variable: GHOSTFOLIO_ACTUAL_PAYEE_NAME`);
}
GHOSTFOLIO_ACCOUNT_MAPPING[ghostfolioAccount] = actualAccount;
GHOSTFOLIO_ACTUAL_PAYEE_NAME_MAPPING[ghostfolioAccount] = ghotfolioPayeeName;
var i = 1;
while(true){
ghostfolioAccount = process.env[`GHOSTFOLIO_ACCOUNT_${i}`] || ""
actualAccount = process.env[`GHOSTFOLIO_ACTUAL_ACCOUNT_${i}`] || ""
ghotfolioPayeeName = process.env[`GHOSTFOLIO_ACTUAL_PAYEE_NAME_${i}`] || ""
if (!ghostfolioAccount || !actualAccount || !ghotfolioPayeeName) {
break;
}
i++;
GHOSTFOLIO_ACCOUNT_MAPPING[ghostfolioAccount] = actualAccount;
GHOSTFOLIO_ACTUAL_PAYEE_NAME_MAPPING[ghostfolioAccount] = ghotfolioPayeeName;
}
validateEnv({
GHOSTFOLIO_SERVER_URL,
GHOSTFOLIO_TOKEN,
})
}
const appConfig = {
ACTUAL_SERVER_URL,
ACTUAL_SERVER_PASSWORD,
ACTUAL_SYNC_ID,
CRON_EXPRESSION,
PAYEE_REGEX_MATCH,
MORTGAGE_PAYEE_ID,
MORTGAGE_PAYEE_NAME,
MORTGAGE_ACCOUNT_ID,
MAIN_ACCOUNT_ID,
INTEREST_RATE,
ENABLE_INTEREST_CALCULATION,
ENABLE_PAYEE_RENAME,
ENABLE_GHOSTFOLIO_SYNC,
GHOSTFOLIO_ACCOUNT_MAPPING,
GHOSTFOLIO_ACTUAL_PAYEE_NAME_MAPPING,
GHOSTFOLIO_SERVER_URL,
GHOSTFOLIO_TOKEN,
ENABLE_HOLD_INCOME_FOR_NEXT_MONTH,
ENABLE_BANK_SYNC
}
return appConfig
}
function getConf(username) {
const appConfig = getAppConfigFromEnv();
const key = `${username}`;
const tmp = new Conf({
configName: key
});
tmp.set("user", key);
tmp.set("budget_id", appConfig.ACTUAL_SYNC_ID)
return tmp;
}
module.exports = {
getAppConfigFromEnv,
getConf
}