Feature finished

This commit is contained in:
Oscar Martinez 2020-06-11 17:47:59 -05:00
parent 9fe97b99cb
commit 50a77fdeb0
3 changed files with 43 additions and 46 deletions

View File

@ -0,0 +1,3 @@
APIKEY=
API_MODE=
# Available modes: dev, startup, grow

View File

@ -1,4 +1,5 @@
let moment = require('moment'); const moment = require('moment');
const request = require('request-promise');
require('dotenv').config(); require('dotenv').config();
// Source: https://apiv2.bitcoinaverage.com/ // Source: https://apiv2.bitcoinaverage.com/
@ -6,33 +7,27 @@ require('dotenv').config();
const divisaTraceback = async(timestamp, divisa, amount) => { const divisaTraceback = async(timestamp, divisa, amount) => {
let crypto = ['BTC', 'ETH', 'LTC'] let crypto = ['BTC', 'ETH', 'LTC']
let normal = ['USD', 'GBP', 'CNY', 'EUR']; let normal = ['USD', 'GBP', 'CNY', 'EUR'];
try { if(amount > 0) {
if((Date.now() - 7776000000) <= (timestamp * 1000)) { let divisaTraceback = {};
if(crypto.includes(divisa) || normal.includes(divisa)) { let resolution = await getResolution(timestamp);
if(amount >= 0) { if(crypto.includes(divisa)) {
let divisaTraceback = {}; divisaTraceback[crypto.splice(crypto.indexOf(divisa), 1)[0]] = Number(amount);
let resolution = await getResolution(timestamp); Object.assign(divisaTraceback, await requestIteration(normal, divisa, true, timestamp, amount, resolution));
Object.assign(divisaTraceback, await requestIteration(crypto, 'USD', false, timestamp, divisaTraceback['USD'], resolution));
}
else if (normal.includes(divisa)) {
divisaTraceback[normal.splice(normal.indexOf(divisa), 1)[0]] = Number(amount);
Object.assign(divisaTraceback, await requestIteration(crypto, divisa, false, timestamp, amount, resolution));
Object.assign(divisaTraceback, await requestIteration(normal, 'BTC', true, timestamp, divisaTraceback['BTC'], resolution));
} else throw new Error('Currency target invalid');
if(crypto.includes(divisa)) { console.log(` Resolution: ${resolution}`);
divisaTraceback[crypto.splice(crypto.indexOf(divisa), 1)[0]] = Number(amount); return(divisaTraceback);
Object.assign(divisaTraceback, await requestIteration(normal, divisa, true, timestamp, amount, resolution)); } else throw new Error('The amount must be 0 or higher');
Object.assign(divisaTraceback, await requestIteration(crypto, 'USD', false, timestamp, divisaTraceback['USD'], resolution));
}
else {
divisaTraceback[normal.splice(normal.indexOf(divisa), 1)[0]] = Number(amount);
Object.assign(divisaTraceback, await requestIteration(crypto, divisa, false, timestamp, amount, resolution));
Object.assign(divisaTraceback, await requestIteration(normal, 'BTC', true, timestamp, divisaTraceback['BTC'], resolution));
}
return(divisaTraceback);
} else return('The amount must be 1 or higher');
} else return('Currency target invalid');
} else return('Date out of range')
} catch(e) {
throw 'error';
}
}; };
const requestIteration = async(set, divisa, flag, timestamp, amount, resolution) => { const requestIteration = async(set, divisa, flag, timestamp, amount, resolution) => {
timestamp = timestamp.format('X');
let opt = { let opt = {
method: 'GET', method: 'GET',
json: true, json: true,
@ -44,12 +39,14 @@ const requestIteration = async(set, divisa, flag, timestamp, amount, resolution)
for (element in set) { for (element in set) {
if(flag) { if(flag) {
opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${divisa}${set[element]}?at=${timestamp}&resolution=${resolution}`; opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${divisa}${set[element]}?at=${timestamp}&resolution=${resolution}`;
let { average } = await request(opt); let { average, time } = await request(opt);
if(res.time === undefined) res.time = moment.utc(time, 'YYYY-MM-DD HH:mm:ss').format('DD-MM-YYYY HH:mm Z');
res[set[element]] = average * amount; res[set[element]] = average * amount;
} }
else { else {
opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${set[element]}${divisa}?at=${timestamp}&resolution=${resolution}`; opt.uri = `https://apiv2.bitcoinaverage.com/indices/global/history/${set[element]}${divisa}?at=${timestamp}&resolution=${resolution}`;
let { average } = await request(opt); let { average, time } = await request(opt);
if(res.time === undefined) res.time = moment.utc(time, 'YYYY-MM-DD HH:mm:ss').format('DD-MM-YYYY HH:mm Z');
res[set[element]] = amount / average; res[set[element]] = amount / average;
} }
} }
@ -58,17 +55,13 @@ const requestIteration = async(set, divisa, flag, timestamp, amount, resolution)
const getResolution = async(timestamp) => { const getResolution = async(timestamp) => {
let resolution = 'day'; let resolution = 'day';
let momentTime = moment(timestamp); if(process.env.APIMODE == 'dev') {
if(process.env.API_MODE == 'dev') { if(timestamp >= moment.utc().subtract(2, 'hours')) resolution = 'minute';
if(momentTime >= moment().subtract(2, 'hours')) resolution = 'minute'; else if(timestamp >= moment().subtract(10, 'days')) resolution = 'hour';
else if(momentTime >= moment().subtract(10, 'days')) resolution = 'hour'; } else if(process.env.APIMODE == 'startup' || process.env.APIMODE == 'grow') {
if(timestamp >= moment.utc().subtract(24, 'hours')) resolution = 'minute';
} else if(process.env.API_MODE == 'startup' || process.env.API_MODE == 'grow') { else if(timestamp >= moment.utc().subtract(31, 'days')) resolution = 'hour';
if(momentTime >= moment().subtract(24, 'hours')) resolution = 'minute'; } else throw new Error('Bad config at .env file');
else if(momentTime >= moment().subtract(31, 'days')) resolution = 'hour';
} else throw new Error('Bad config');
return resolution; return resolution;
} }

View File

@ -91,7 +91,7 @@ const main = async () => {
} }
while (true) { while (true) {
await init(); await init();
//console.clear(); console.clear();
console.log('Select one to modify the field.\n'); console.log('Select one to modify the field.\n');
let carbonCreditMenu = await generateData(carbonCredit, 0); let carbonCreditMenu = await generateData(carbonCredit, 0);
let { insertMenu } = await inquirer.prompt([{ let { insertMenu } = await inquirer.prompt([{
@ -113,7 +113,7 @@ const main = async () => {
if (await validateInsertion(carbonCredit)) { if (await validateInsertion(carbonCredit)) {
try{ try{
carbonCredit.conversionPrice = await divisaTraceback( carbonCredit.conversionPrice = await divisaTraceback(
moment.utc(carbonCredit.cancelDate + ' +0000', 'DD-MM-YYYY HH:mm Z').unix(), moment.utc(carbonCredit.cancelDate, 'DD-MM-YYYY HH:mm Z'),
carbonCredit.cancelPrice.divisa, carbonCredit.cancelPrice.divisa,
carbonCredit.cancelPrice.qty carbonCredit.cancelPrice.qty
); );
@ -148,10 +148,10 @@ const main = async () => {
} else { } else {
console.clear(); console.clear();
console.log('Insertion aborted'); console.log('Insertion aborted');
break; continue;
} }
} catch(e) { } catch(e) {
console.log(e.message); console.log(`Error at divisaTraceback: ${e.message}`);
} }
} }
else { else {
@ -187,11 +187,12 @@ const main = async () => {
if (input == '') console.log('Input mustn\'t be empty'); if (input == '') console.log('Input mustn\'t be empty');
else if (insertMenu == 'issueDate' || insertMenu == 'cancelDate' || insertMenu == 'ccVintageStart' || insertMenu == 'ccVintageEnd') { else if (insertMenu == 'issueDate' || insertMenu == 'cancelDate' || insertMenu == 'ccVintageStart' || insertMenu == 'ccVintageEnd') {
const date = moment.utc(input + ' +0000', 'DD-MM-YYYY HH:mm'); const date = moment.utc(input + ' +0000', 'DD-MM-YYYY HH:mm');
if(date.isValid() && moment() >= date) { console.log(date);
if(date.isValid() && moment.utc() >= date) {
if(insertMenu != 'cancelDate') carbonCredit[insertMenu] = date.format('DD-MM-YYYY HH:mm Z'); if(insertMenu != 'cancelDate') carbonCredit[insertMenu] = date.format('DD-MM-YYYY HH:mm Z');
else if((date >= moment().subtract(3, 'months') && process.env.API_MODE == 'dev') || else if((date >= moment.utc().subtract(3, 'months') && process.env.APIMODE == 'dev') ||
(date >= moment().subtract(1, 'year') && process.env.API_MODE == 'startup') || (date >= moment.utc().subtract(1, 'year') && process.env.APIMODE == 'startup') ||
(date >= moment().subtract(7, 'year') && process.env.API_MODE == 'grow')) { (date >= moment.utc().subtract(7, 'year') && process.env.APIMODE == 'grow')) {
carbonCredit[insertMenu] = date.format('DD-MM-YYYY HH:mm Z'); carbonCredit[insertMenu] = date.format('DD-MM-YYYY HH:mm Z');
} else console.error(' Date is too old to get pricing data'); } else console.error(' Date is too old to get pricing data');
} else console.error(' Date must follow next syntax: dd-mm-yyyy hh-mm and can\'t be in the future'); } else console.error(' Date must follow next syntax: dd-mm-yyyy hh-mm and can\'t be in the future');
@ -529,7 +530,7 @@ const generateData = async (data, n) => {
menu.push({ name: element, disabled: ' ' }); menu.push({ name: element, disabled: ' ' });
menu = menu.concat(await generateData(data[element], n + 1)); menu = menu.concat(await generateData(data[element], n + 1));
} else { } else {
let space = ' '.repeat(20 - element.length); let space = ' '.repeat(25 - element.length);
space = space.substring(0, space.length - tab.length); space = space.substring(0, space.length - tab.length);
menu.push(`${tab}${element}${space}${data[element]}`); menu.push(`${tab}${element}${space}${data[element]}`);
} }