Skip to content

Commit 643518d

Browse files
committed
chore(parser): Numeric is now unconstrained, now truncating numbers after the dot is done in the backend
1 parent 5d1440c commit 643518d

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

parser/main.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ const servicesDir = path.join(__dirname, 'services');
1414
let fiatFetched = false;
1515
let cryptoFetched = false;
1616

17+
function truncate_number(value, decimals) {
18+
const valueStr = value.toString();
19+
const dotIndex = valueStr.indexOf('.');
20+
if (dotIndex === -1) return valueStr;
21+
const desiredLength = dotIndex + decimals + 1;
22+
let truncated = valueStr.slice(0, desiredLength);
23+
24+
if (parseFloat(truncated) === 0 && value > 0) {
25+
return valueStr;
26+
}
27+
return truncated;
28+
}
29+
1730
async function main() {
1831
if (!config['schedule'])
1932
throw new Error('The crontab schedule is not set.');
@@ -76,7 +89,7 @@ async function main() {
7689
[
7790
currency.from_currency,
7891
currency.conv_currency,
79-
currency.rate,
92+
truncate_number(currency.rate, 30),
8093
currency.date,
8194
],
8295
);

parser/models/Currency.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const currencySchema = Joi.object({
1717
'date.base': 'date must be a valid ISO date',
1818
'any.required': 'date is required',
1919
}),
20-
rate: Joi.number().precision(30).positive().required().messages({
20+
rate: Joi.number().positive().required().messages({
2121
'number.base': 'rate must be a number',
2222
'number.positive': 'rate must be a positive number',
2323
'number.precision': 'rate must have no more than 6 decimal places',

schemas/data.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CREATE TABLE IF NOT EXISTS currency(
22
from_currency TEXT NOT NULL,
33
conv_currency TEXT NOT NULL,
4-
rate NUMERIC(30) NOT NULL,
4+
rate NUMERIC NOT NULL,
55
date DATE NOT NULL
66
);
77

0 commit comments

Comments
 (0)