var connectionAsync = require("./connectionasync"); exports.getAll = async function() { const queryResult = await connectionAsync.query('SELECT * FROM Transactions WHERE DeletedDate IS NULL;'); const result = []; if (queryResult && queryResult.rows && queryResult.rows.length > 0) { for(var i = 0 ; i < qqueryResult.rows.length; i++) { const row = queryResult.rows[i]; const trans = {}; trans.id = row.Id; trans.date = row.Date; trans.typeId = row.TypeId; trans.checkNumber = row.CheckNumber; trans.contributorId = row.ContributorId; trans.fundId = row.FundId; trans.description = row.Description; trans.amount = row.Amount; trans.taxYear = row.TaxYear; result.push(trans); } } return result; } exports.getByYear = async function(taxYear) { const queryResult = await connectionAsync.query('SELECT * FROM Transactions WHERE DeletedDate IS NULL AND TaxYear = ? AND GoodsOrServices = 0 ORDER BY Date ASC;', taxYear); const result = []; if (queryResult && queryResult.rows && queryResult.rows.length > 0) { for(var i = 0 ; i < queryResult.rows.length; i++) { const row = queryResult.rows[i]; const trans = {}; trans.id = row.Id; trans.date = row.Date; trans.typeId = row.TypeId; trans.checkNumber = row.CheckNumber; trans.contributorId = row.ContributorId; trans.fundId = row.FundId; trans.description = row.Description; trans.amount = row.Amount; trans.taxYear = row.TaxYear; trans.goodsOrServices = row.GoodsOrServices == 1 ? true : false; result.push(trans); } } return result; } exports.addArray = async function(transactions) { let conn = null; try { conn = await connectionAsync.beginTransaction(); } catch (ex) { throw ex; } const results = []; try { for(let i = 0; i < transactions.length; i++) { const t = transactions[i]; const newTrans = getTransaction(t.date, t.typeId, t.checkNumber, t.contributorId, t.fundId, t.description, t.amount, t.taxYear, t.goodsOrServices); const result = await conn.nonQuery('INSERT INTO Transactions Set ?', newTrans); results.push(result); } await conn.commit(); } catch (ex) { throw ex; } return results; } function getTransaction(date, typeId, checkNumber, contributorId, fundId, description, amount, taxYear, goodsOrServices) { const newTrans = { Date: date, TypeId: typeId, CheckNumber: checkNumber, ContributorId: contributorId, FundId: fundId, Description: description, Amount: amount, TaxYear: taxYear, GoodsOrServices: goodsOrServices }; return newTrans; } exports.add = async function(date, typeId, checkNumber, contributorId, fundId, description, amount, taxYear) { const newTrans = getTransaction(date, typeId, checkNumber, contributorId, fundId, description, amount, taxYear); const newTransResult = await connectionAsync.nonQuery('INSERT INTO Transactions Set ?', newTrans); return newTransResult; }