Last active
May 6, 2018 15:31
-
-
Save mrts/86d3329ba2463966f853802e58036fa8 to your computer and use it in GitHub Desktop.
Merit API getpurchorders request example with saving to CSV
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
// Merit API spec: | |
// https://www.merit.ee/juhend/muud/Merit_Aktiva_API_specification.pdf | |
// Install required packages: | |
// npm install crypto-js moment request request-debug | |
const CryptoJS = require('crypto-js'); | |
const moment = require('moment'); | |
const axios = require('axios') | |
const json2csv = require('json2csv').parse; | |
const API_KEY = '...'; | |
const API_ID = '...'; | |
const URL = 'https://aktiva.merit.ee/api/v1/getpurchorders' | |
const VENDOR_REGEXES = [ | |
/aktsiaselts Tartu Veevärk/, | |
/Eesti Energia Aktsiaselts|Elektrum Eesti OÜ/, | |
/AS Tartu Keskkatlamaja/, | |
/Osaühing EKOVIR/, | |
/SECURITAS EESTI AKTSIASELTS/ | |
] | |
function calculateSignature(timestamp) { | |
const dataString = API_ID + timestamp | |
const hash = CryptoJS.HmacSHA256(dataString, API_KEY) | |
const signature = CryptoJS.enc.Base64.stringify(hash) | |
return signature | |
} | |
async function makeApiRequest(queryParams, requestBody) { | |
const options = { | |
url: URL, | |
params: queryParams, | |
headers: { 'content-type': 'application/json' }, | |
data: requestBody | |
} | |
const response = await axios(options) | |
return JSON.parse(response.data) | |
} | |
function makeRequestBody(month) { | |
const end = month < 11 ? { y: 2017, M: month + 1, d: 1} : { y: 2017, M: 11, d: 31} | |
return { | |
periodStart: moment({ y: 2017, M: month, d: 1}).format('YYYYMMDD'), | |
periodEnd: moment(end).format('YYYYMMDD') | |
} | |
} | |
function filterAndCleanupPurchases(purchases, vendorNameRegex) { | |
const electricityPurchases = purchases.filter(p => p.VendorName.match(vendorNameRegex)) | |
return electricityPurchases.map(p => { | |
const date = p.TransactionDate.replace(/\/Date\((\d+)\)\//, '$1') | |
return { | |
vendor: p.VendorName, | |
billNumber: p.BillNo, | |
date: moment(Number(date)).format('YYYY-MM-DD'), | |
amount: p.TotalAmount, | |
tax: p.TaxAmount | |
} | |
}) | |
} | |
async function processPurchases() { | |
const timestamp = moment().format('YYYYMMDDHHmmss') | |
const signature = calculateSignature(timestamp) | |
const queryParams = { | |
ApiId: API_ID, | |
timestamp: timestamp, | |
signature: signature | |
} | |
result = {} | |
VENDOR_REGEXES.forEach(vendorRegex => result[String(vendorRegex)] = []) | |
for (let month of Array(12).keys()) { | |
const requestBody = makeRequestBody(month) | |
try { | |
const purchases = await makeApiRequest(queryParams, requestBody) | |
for (let vendorRegex of VENDOR_REGEXES) { | |
const filteredPurchases = filterAndCleanupPurchases(purchases, vendorRegex) | |
result[String(vendorRegex)].push(...filteredPurchases) | |
} | |
console.log('Month ' + (month + 1) + ' done') | |
} catch (e) { | |
console.log('Request error: ', e) | |
} | |
} | |
const csvFile = fs.openSync('purchases.csv', 'w') | |
Object.entries(result).forEach((entry) => { // entry = [key, value] | |
const csv = json2csv(entry[1]) | |
fs.writeSync(csvFile, csv) | |
fs.writeSync(csvFile, "\r\n\r\n") | |
}) | |
} | |
processPurchases() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment