Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let Pool = require('pg').Pool;
- let pool = new Pool({
- host: '127.0.0.1',
- user: process.env.DB_USER,
- password: process.env.DB_PWD,
- database: 'savemany'
- });
- let request = require('request');
- let Client = require('node-rest-client').Client;
- let client = new Client();
- module.exports.model = {
- write: (type, description, amount, billname, comment, transfer, tags) => {
- // let transferBill = transfer.split(',')[1];
- // let billId = parseInt(billname.split(',')[0]);
- // let sp = transferBill;
- // let transferType = transfer.split(',')[2];
- let obj = {
- 'type': type,
- 'description': description,
- 'amount': amount,
- 'billname': billname.split(',')[1],
- 'comment': comment,
- 'transfer': transfer.split(',')[1],
- 'tags': tags,
- 'billid': parseInt(billname.split(',')[0])
- };
- writeCommonOperation(obj);
- switch (type) {
- case 'income':
- writeIncome(obj.amount, obj.billname);
- break;
- case 'expenses':
- writeExpenses(obj.amount, obj.billname);
- break;
- case 'transfer':
- writeTransfer(obj.type, obj.transfer, obj.amount, obj.billname);
- break;
- default:
- break;
- }
- amount = parseInt(amount);
- // if (sp)
- // tags = tags + ',' + `${billName} → ${transferBill}`;
- // else
- // tags = tags + ',' + `${billName}`;
- },
- read: (billid) => {
- return new Promise((resolve, reject) => {
- pool.query(`
- SELECT *
- FROM transactions
- WHERE billid=$1`,
- [billid], (err, result) => {
- if (err) reject(err);
- resolve(result.rows);
- });
- })
- },
- delete: (transcastid, cb) => {
- pool.query(`
- DELETE FROM transactions
- WHERE id=$1`,
- [transcastid], (err, result) => {
- if (err) throw err;
- cb();
- });
- },
- all: (username, cb) => {
- pool.query(`
- SELECT *
- FROM bills
- INNER JOIN transactions
- ON transactions.billid = bills.id
- WHERE bills.username = $1
- ORDER BY transactions._date DESC
- `, [username], (err, result) => {
- if (err) throw err;
- cb(result.rows);
- });
- },
- today: (username, cb) => {
- pool.query(`
- SELECT *
- FROM bills
- INNER JOIN transactions
- ON transactions.billid = bills.id
- WHERE bills.username = $1
- AND _date >= now()::date + interval '1h'
- ORDER BY transactions._date DESC`,
- [username], (err, result) => {
- if (err) throw err;
- cb(result.rows);
- });
- },
- reset: (username, cb) => {
- pool.query(`
- DELETE FROM transactions USING bills
- WHERE bills.id = transactions.billid
- AND bills.username=$1`,
- [username], (err, result) => {
- if (err) throw err;
- // cb();
- });
- }
- };
- let getValute = () => {
- let cbrFinance = 'http://www.cbr.ru/scripts/XML_daily.asp';
- return new Promise((resolve, reject) => {
- client.get(cbrFinance, (data, response) => {
- debugger;
- resolve(data);
- });
- });
- };
- let writeIncome = (amount, billName) => {
- pool.query(`
- UPDATE bills
- SET balance=balance+$1
- WHERE name=$2`,
- [amount, billName], (error, result, body) => {
- if (error) throw error;
- }
- );
- };
- let writeExpenses = (amount, billName) => {
- pool.query(`
- UPDATE bills
- SET balance=balance-$1
- WHERE name=$2`,
- [amount, billName], (error, result, body) => {
- if (error) throw error;
- }
- );
- };
- let writeTransfer = (...args) => {
- if (parseInt(args[1]) === 840) {
- getValute().then((valute) => {
- let valtueArr = valute.ValCurs.Valute;
- let EUR;
- let USD = valtueArr.filter((obj) => {
- return obj.NumCode === 840;
- });
- _amount = args[2] / parseInt(USD[0].Value);
- pool.query(`
- UPDATE bills
- SET balance=balance+$1
- WHERE name=$2`,
- [Math.ceil(_amount), transferBill], (error, result, body) => {
- if (error) throw error;
- });
- pool.query(`
- UPDATE bills
- SET balance=balance-$1
- WHERE name=$2`,
- [args[2], args[3]], (error, result, body) => {
- // debugger
- if (error) throw error;
- });
- });
- } else {
- pool.query(`
- UPDATE bills
- SET balance=balance+$1
- WHERE name=$2`,
- [args[2], args[1]], (error, result, body) => {
- // debugger
- if (error) throw error;
- });
- pool.query(`
- UPDATE bills
- SET balance=balance-$1
- WHERE name=$2`,
- [args[2], args[3]], (error, result, body) => {
- if (error) throw error;
- });
- }
- };
- let writeCommonOperation = (obj) => {
- let spTransfer = obj.transfer === undefined ? '' : obj.transfer.split(',')[1];
- if (obj.type === 'transfer')
- tags = obj.tags + ',' + `${obj.billname} → ${obj.transfer}`;
- else
- tags = obj.tags + ',' + `${obj.billname}`;
- pool.query(`
- INSERT INTO transactions
- (description, type, amount, billname, comment, billid, tag, sp, _date)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
- [obj.description, obj.type, obj.amount, obj.billname, obj.comment,
- obj.billid, tags, spTransfer, new Date()],
- (err, result) => {
- if (err) throw new Error(err);
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement