252 lines
7.0 KiB
JavaScript
252 lines
7.0 KiB
JavaScript
'use strict';
|
|
|
|
const _ = require('lodash');
|
|
const router = new (require('express').Router)();
|
|
const Busboy = require('busboy');
|
|
const split = require('split');
|
|
|
|
const db = require('../db');
|
|
|
|
const parse = require('../api/parse');
|
|
|
|
const {
|
|
parseAccount,
|
|
parseAccountType,
|
|
parseCompanyName,
|
|
parseSRU,
|
|
parseTransaction,
|
|
parseTicket,
|
|
parseYearDates,
|
|
parseOrganizationNumber,
|
|
} = require('../api/parseLine');
|
|
|
|
const queries = {
|
|
saveAccount(account) {
|
|
db('accounts').insert(h)
|
|
}
|
|
}
|
|
|
|
const handlers = require('../handlers');
|
|
|
|
router.get('/parse', (req, res) => res.send('<h1>Parse</h1>'));
|
|
router.post('/sie', (req, res) => {
|
|
const busboy = new Busboy({ headers: req.headers });
|
|
|
|
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
|
|
let accounts = [];
|
|
let ticketId = null;
|
|
let companyDone = false;
|
|
let yearDone = false;
|
|
let accountsDone = false;
|
|
let accountIds = [];
|
|
let details = {};
|
|
const transactions = [];
|
|
let currentYearDates = [];
|
|
let currentYear;
|
|
let previousYearDates = [];
|
|
let company;
|
|
|
|
// let saving = false;
|
|
const splitStream = file.pipe(split())
|
|
.on('data', async (line) => {
|
|
line = line.trim();
|
|
|
|
if (line[0] !== '#') return;
|
|
|
|
const lineType = line.slice(1, line.indexOf(' '));
|
|
console.log(lineType);
|
|
|
|
if (!companyDone) {
|
|
switch (lineType) {
|
|
case 'ORGNR': {
|
|
const organizationNumber = parseOrganizationNumber(line);
|
|
|
|
splitStream.pause();
|
|
|
|
company = await handlers.getCompanyByOrganizationNumber(organizationNumber) || { organization_number: organizationNumber };
|
|
|
|
splitStream.resume();
|
|
|
|
return;
|
|
}
|
|
case 'FNAMN': {
|
|
const companyName = parseCompanyName(line);
|
|
|
|
if (!company.name || company.name !== companyName) {
|
|
company.name = companyName;
|
|
}
|
|
|
|
return;
|
|
}
|
|
case 'RAR': {
|
|
if (!company.id) {
|
|
splitStream.pause();
|
|
|
|
company = await handlers.createCompany(company);
|
|
|
|
companyDone = true;
|
|
|
|
splitStream.resume();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (companyDone && !yearDone) {
|
|
switch (lineType) {
|
|
case 'RAR': {
|
|
const dates = parseYearDates(line);
|
|
|
|
if (dates.current) {
|
|
currentYearDates = _.omit(dates, 'current');
|
|
} else {
|
|
previousYearDates = _.omit(dates, 'current');
|
|
}
|
|
|
|
return;
|
|
}
|
|
case 'KONTO': {
|
|
splitStream.pause();
|
|
|
|
const previousYear = await handlers.getYearByCompanyIdAndDates(company.id, previousYearDates);
|
|
|
|
// currentYear = await handlers.getYearByCompanyIdAndDates(company.id, currentYearDates) || Object.assign({ company_id: company.id }, _.mapKeys(currentYearDates, (value, key) => `${key}_date`);
|
|
currentYear = Object.assign({ company_id: company.id }, _.mapKeys(currentYearDates, (value, key) => `${key}_date`));
|
|
|
|
if (previousYear) currentYear.previous_year_id = previousYear.id;
|
|
|
|
// TODO remove all entries for year
|
|
currentYear = await handlers.createYear(currentYear);
|
|
|
|
yearDone = true;
|
|
|
|
splitStream.resume();
|
|
}
|
|
}
|
|
}
|
|
|
|
if (companyDone && yearDone && !accountsDone) {
|
|
console.log(lineType);
|
|
console.log(line);
|
|
switch (lineType) {
|
|
case 'KONTO':
|
|
const account = parseAccount(line);
|
|
if (!account || !account.number) console.log(account);
|
|
accounts.push(account);
|
|
|
|
return;
|
|
case 'KTYP': {
|
|
const { number, type } = parseAccountType(line);
|
|
|
|
const account = accounts.find((account) => account.number === number) || { number };
|
|
|
|
if (account) account.account_type_id = type;
|
|
|
|
return;
|
|
}
|
|
case 'SRU': {
|
|
const { number, sru } = parseSRU(line);
|
|
|
|
const account = accounts.find((account) => account.number === number) || { number };
|
|
|
|
// if (account)
|
|
account.sru = sru;
|
|
|
|
return;
|
|
}
|
|
case 'VER':
|
|
console.log('shhould be saviung accounts');
|
|
splitStream.pause();
|
|
// console.log(accounts);
|
|
accounts.forEach((account) => {
|
|
if (!account.account_type_id) console.log(account);
|
|
});
|
|
|
|
await db('accounts').returning(['id', 'number']).insert(accounts).then((res) => {
|
|
accounts = res;
|
|
}).catch((err) => {
|
|
console.log(err);
|
|
});
|
|
|
|
accountsDone = true;
|
|
|
|
splitStream.resume();
|
|
}
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
if (companyDone && yearDone && accountsDone) {
|
|
switch (lineType) {
|
|
case 'VER': {
|
|
splitStream.pause();
|
|
|
|
const ticket = parseTicket(line);
|
|
|
|
await db('tickets').returning('id').insert(_.omit(ticket, 'transactions')).then((res) => {
|
|
ticketId = res[0];
|
|
}).catch((err) => console.log(err));
|
|
|
|
splitStream.resume();
|
|
|
|
return;
|
|
}
|
|
case 'TRANS': {
|
|
const transaction = parseTransaction(line);
|
|
|
|
transaction.ticket_id = ticketId;
|
|
|
|
const account = accounts.find((account) => account.number === transaction.accountNumber);
|
|
|
|
delete transaction.accountNumber;
|
|
|
|
transaction.account_id = account.id;
|
|
|
|
transactions.push(transaction);
|
|
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
})
|
|
.on('end', () => {
|
|
db('transactions').insert(transactions)
|
|
.catch((err) => console.log(err));
|
|
});
|
|
});
|
|
|
|
busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
|
|
console.log('Field [' + fieldname + ']: value: ' + inspect(val));
|
|
});
|
|
|
|
busboy.on('finish', function() {
|
|
console.log('Done parsing form!');
|
|
// res.writeHead(303, { Connection: 'close', Location: '/' });
|
|
// res.end();
|
|
|
|
res.sendStatus(204);
|
|
});
|
|
|
|
req.pipe(busboy);
|
|
|
|
// tickets.forEach(({ type, number, date, dateCreated, title, transactions }) => {
|
|
// db.query(`INSERT INTO tickets (number, type, title, date, date_created) VALUES ('${number}', '${type}', '${title}', '2016-11-01', '2016-11-01') RETURNING id`, (err, result) => {
|
|
// if (err) throw err;
|
|
|
|
// console.log(result.rows[0]);
|
|
// const ticketId = result.rows[0].id;
|
|
|
|
// transactions.forEach(({ account, amount }) => {
|
|
// db.query(`INSERT INTO transactions (account, amount, ticket) VALUES ('${account}', '${amount}', '${ticketId}')`, (err, result) => {
|
|
// console.log(result);
|
|
// });
|
|
// });
|
|
// });
|
|
// })
|
|
|
|
});
|
|
|
|
module.exports = router;
|
|
|