-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.js
192 lines (172 loc) · 5.24 KB
/
import.js
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
'use strict'
const Promise = require('bluebird')
const csvtojson = require('csvtojson')
const fetch = require('node-fetch')
const moment = require('moment')
const sqlite3 = require('sqlite3').verbose()
const config = require('./config.json')
const columnsMap = {
date: 'Date',
id: 'Ref ID',
type: 'Type',
description: 'Description',
agency: 'Agency',
freelancer: 'Freelancer',
team: 'Team',
account: 'Account Name',
po: 'PO',
amount: 'Amount',
amountLocal: 'Amount in local currency',
currency: 'Currency',
balance: 'Balance'
}
const checkRow = row => {
if (!row.balance && row.balance !== 0) {
console.log('\tRow does not contain balance - pending. Skipping...', row)
return false
}
// if (in_array($type, ['Withdrawal Fee', 'Withdrawal'])) {
// return false
// }
return true
}
const parseCsv = (filename, columnsMap) => {
return new Promise((resolve, reject) => {
const dbFields = Object.keys(columnsMap)
const data = []
csvtojson()
.fromFile(filename)
.on('json', json => {
const row = {}
dbFields.forEach(x => {
let value = json[columnsMap[x]]
switch (x) {
case 'date':
// Jul 19, 2017
value = moment(value, 'MMM DD, YYYY').format('YYYY-MM-DD hh:mm:ss')
break;
case 'id':
value = parseInt(value, 10)
break;
case 'type':
value = value.toLowerCase()
break;
case 'amount':
value = parseFloat(value)
break;
case 'amountLocal':
if (value) value = parseFloat(value)
break;
case 'balance':
if (value) value = parseFloat(value)
break;
default:
}
row[x] = value
})
if (checkRow(row)) data.push(row)
})
.on('done', err => {
console.log('Parsing is done.')
if (err) return reject(err)
return resolve(data)
})
})
}
const insertStatement = row => {
const dbFields = Object.keys(columnsMap)
return new Promise((resolve, reject) => {
db.all("SELECT id FROM statements WHERE id = " + row.id, (err, res) => {
if (err) return reject(err)
return resolve(res)
})
})
.then(rows => {
if (rows.length) {
console.warn(`\tStatement ${row.id} already presents in database. Skipping...`)
return true
}
const sql = "INSERT into statements(" + dbFields.join(',') + ") VALUES (" + dbFields.map(x => "'" + row[x] + "'").join(',') + ")"
// console.log('SQL:', sql)
console.warn(`\tStatement ${row.id} for $${row.amount}\tadded to database.`)
return new Promise((resolve, reject) => {
db.run(sql, err => {
if (err) return reject(err)
return resolve(true)
})
})
})
// .catch(err => console.log(err.message, err))
}
const populateRate = (date, currencyCode) => {
currencyCode = currencyCode || 840
// http://www.nbrb.by/API/ExRates/Rates/840?ParamMode=1&onDate=2016-07-05
const uri = `http://www.nbrb.by/API/ExRates/Rates/${currencyCode}?ParamMode=1&onDate=${date}`
console.log('uri', uri)
return new Promise(resolve => setTimeout(() => resolve(true), 1000))
.then(() => fetch(uri))
.then(function(res) {
if (res.status === 200) return res.json()
throw new Error('Not 200 response from API nbrb: ' + uri)
})
.then(function(json) {
console.log(json)
return json.Cur_OfficialRate
})
.catch(err => console.log(err.message, err))
}
const populateRates = currencyCode => {
// @TODO: ay p2 - move to config
// @TODO: ay p3 - use ISO currency code
currencyCode = currencyCode || 145
return new Promise((resolve, reject) => {
db.get("SELECT GROUP_CONCAT(DISTINCT DATE(date)) as dates FROM statements WHERE rate IS NULL", (err, res) => {
if (err) return reject(err)
return resolve(res.dates ? res.dates.split(',').sort() : [])
})
})
.then(emptyDates => {
if (!emptyDates.length) return true
const startDate = emptyDates[0]
const endDate = emptyDates[emptyDates.length - 1]
const uri = `http://www.nbrb.by/API/ExRates/Rates/Dynamics/${currencyCode}?startDate=${startDate}&endDate=${endDate}`
console.log('\tNBRB API call: ', uri)
return fetch(uri)
.then(res => {
if (res.status === 200) return res.json()
throw new Error('Not 200 response from API nbrb: ' + uri)
})
.then(json => {
const rates = json.reduce((all, rate) => {
// 2017-05-31T00:00:00
const date = moment(rate['Date'], 'YYYY-MM-DDThh:mm:ss').format('YYYY-MM-DD')
all[date] = rate['Cur_OfficialRate']
return all
}, {})
return rates
})
.then(rates => {
return Promise.map(emptyDates, date => {
const stmt = db.prepare("UPDATE statements SET rate = $rate WHERE DATE(date) = $date")
return new Promise((resolve, reject) => {
return db.run('UPDATE statements SET rate = ? WHERE DATE(date) = ?', [rates[date], date], err => {
if (err) return reject(err)
return resolve(true)
})
})
})
})
.catch(err => console.log(err.message, err))
})
.catch(err => console.log(err.message, err))
}
const db = new sqlite3.Database(config.dbFilePath)
const filename = process.argv[2]
parseCsv(filename, columnsMap)
// insert data to database
.then(rows => Promise.all(rows.map(row => insertStatement(row))))
// populate missed currency rates
.then(() => populateRates())
.catch(err => {
console.log(err.message, err)
})