This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
206 lines (172 loc) · 5.65 KB
/
index.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import express from 'express';
import dotenv from 'dotenv';
import fs from 'fs';
import http from 'http';
import moment from 'moment-timezone';
import {parse} from 'csv-parse'
dotenv.config();
const rootPath = process.cwd();
const ratesFilename = `${rootPath}/cnb-rates.txt`;
const ratesURL = 'http://www.cnb.cz/en/financial-markets/foreign-exchange-market/central-bank-exchange-rate-fixing/central-bank-exchange-rate-fixing/daily.txt';
// CNB documentation doesn't provide info on specific timezone, let's assume it's CET
const timezone = 'Europe/Prague'
const tresholdTime = {
hour: 14,
minute: 30,
second: 0,
}
const app = express();
const port = process.env.PORT || 8081;
type CurrencyRecord = {
country: string,
code: string,
rate: number,
};
app.use(function (_, res, next) {
res.setHeader('access-control-allow-origin', '*');
next();
});
app.get('/api/convert', async (req, res) => {
try {
const records = await getRates();
const amountParam = req.query['amount'];
if (amountParam == null || amountParam === '') {
res.status(400).json({error: 'You must provide amount '});
return;
}
const amount = Number(amountParam);
if (Number.isNaN(amount)) {
res.status(400).send({error: 'Provided amount is not a number'});
return;
}
const codeParam = req.query['code'];
if (codeParam == null || codeParam === '') {
res.status(400).send({error: 'You must provide code'});
return;
}
const record = records.find((item) => item.code === codeParam);
if (!record) {
res.status(400).send({error: 'Provided code is not valid'});
return;
}
res.json({
result: amount / record.rate,
});
} catch (e) {
log(e);
res.status(500).json({error: 'Unknown error has occurred'});
}
});
app.get('/api/currencies', async (req, res) => {
try {
const records = await getRates();
res.json(records);
} catch (e) {
log(e)
res.status(500).json({error: 'Unknown error has occurred'});
}
})
app.listen(port, () => {
log(`Server is running at http://localhost:${port}`);
});
const log = (message: any) => {
console.log(`${new Date().toISOString()}: ${message}`)
}
const getRates = async () => {
if (shouldDownloadRatesFile()) {
await downloadRatesFile();
log('Rates file downloaded');
}
return await parseRatesFile();
}
const shouldDownloadRatesFile = () => {
const fileStats = fs.statSync(ratesFilename, {
throwIfNoEntry: false
});
// file does not exist
if (fileStats == null) {
return true;
}
// did the rates changed since the last download?
// note: Following will return true even on weekends. It's unnecessary however it won't hurt anything.
const now = moment.utc().tz(timezone);
const fileModifiedTime = moment.utc(fileStats.mtime).tz(timezone);
const treshold = moment(fileModifiedTime)
.hour(tresholdTime.hour)
.minute(tresholdTime.minute)
.second(tresholdTime.second)
if (fileModifiedTime.isSameOrAfter(treshold)) {
treshold.add(1, 'day');
}
return now.isSameOrAfter(treshold);
}
const deleteRatesFile = () => {
if (!fs.existsSync(ratesFilename)) {
return Promise.resolve();
}
return new Promise<void>((resolve, reject) => {
fs.unlink(ratesFilename, (err) => {
if (err) reject(err);
else resolve();
});
})
}
const downloadRatesFile = () => {
return new Promise<void>((resolve, reject) => {
try {
// first delete the rates file if it exists
deleteRatesFile().catch(reject);
// download the rates files
const file = fs.createWriteStream(ratesFilename);
http.get(ratesURL, (res) => {
res.pipe(file);
file.on('finish', () => {
file.close((err) => {
if (err) reject(err);
else resolve();
});
});
}).on('error', (err) => {
deleteRatesFile().catch(reject);
reject(err);
})
} catch (e) {
deleteRatesFile().catch(reject);
reject(e);
}
});
}
const parseRatesFile = () => {
return new Promise<Array<CurrencyRecord>>((resolve, reject) => {
fs.readFile(ratesFilename, (err, data) => {
if (err) {
return reject(err);
}
// find start of the data; trim first line from the data since it contains metadata
const dataStart = Array.from(data.entries()).findIndex((value) => {
return value[1] == 10; // new line in ASCII table
});
parse(data.subarray(dataStart), {
delimiter: '|',
skip_empty_lines: true,
from: 2,
}, (err, records) => {
if (err) {
return reject(err);
}
const formatted = (records as Array<Array<string>>).reduce((acc, row) => {
return [
...acc,
{
country: row[0],
code: row[3],
rate: Number(row[4]) / Number(row[2]),
}
]
}
, [] as Array<CurrencyRecord>);
resolve(formatted)
})
})
})
}