Created
October 17, 2018 00:31
-
-
Save jcuffe/5c4f9a22f0cd6b6d90d637288c22b558 to your computer and use it in GitHub Desktop.
ESI intermediate server to transform data into usable format for the client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const axios = require('axios'); | |
const endpoints = require('./endpoints'); | |
const knex = require('knex')({ | |
client: 'sqlite3', | |
connection: { | |
filename: "./sde.sqlite" | |
} | |
}); | |
const app = express(); | |
const asyncMiddleware = (fn) => | |
(req, res, next) => | |
Promise | |
.resolve(fn(req, res, next)) | |
.catch(next); | |
app.get('/transactions/:corpId', asyncMiddleware(async (req, res) => { | |
const { authorization } = req.headers; | |
const { corpId } = req.params; | |
const options = { headers: { authorization } }; | |
const corpTransactions = (await Promise.all( | |
(await axios.get(endpoints.corpTransactions(corpId), options)) | |
.data | |
.filter(tx => tx.is_buy) | |
.map(async tx => { | |
const { date, quantity, type_id: typeId, unit_price: unitPrice } = tx; | |
const { typeName } = await knex.table("invTypes").where({ typeID: typeId }).first("typeName"); | |
return { typeName, quantity, unitPrice, date, typeId }; | |
}))) | |
.reduce((dict, tx) => ({ ...dict, [tx.typeId]: dict[tx.typeId] ? [...dict[tx.typeId], tx ] : [tx] }), {}); | |
res.json({ corpAssets, corpTransactions }); | |
})); | |
app.listen(5000, () => console.log("listening...")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment