Working Transactions
parent
a81aa76bf0
commit
dae8ca0616
|
|
@ -5,6 +5,7 @@ import { Contribution } from './contribution';
|
||||||
import { FormBuilder, FormGroup, Validators, FormArray } from '@angular/forms';
|
import { FormBuilder, FormGroup, Validators, FormArray } from '@angular/forms';
|
||||||
import { checkNumberValidator } from './check-number-validator';
|
import { checkNumberValidator } from './check-number-validator';
|
||||||
import { contributorValidator } from './contributor-validator';
|
import { contributorValidator } from './contributor-validator';
|
||||||
|
import { TransactionService } from 'src/app/services/transaction.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-add-transaction-page',
|
selector: 'app-add-transaction-page',
|
||||||
|
|
@ -26,7 +27,7 @@ export class AddTransactionPageComponent implements OnInit {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
constructor(private userService: UserService, private formBuilder: FormBuilder) {
|
constructor(private userService: UserService, private formBuilder: FormBuilder, private transactionService: TransactionService) {
|
||||||
this.form = this.formBuilder.group({
|
this.form = this.formBuilder.group({
|
||||||
contributions: this.formBuilder.array([this.getContribution()])
|
contributions: this.formBuilder.array([this.getContribution()])
|
||||||
});
|
});
|
||||||
|
|
@ -148,7 +149,10 @@ export class AddTransactionPageComponent implements OnInit {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(transactions);
|
console.log(transactions);
|
||||||
|
this.transactionService.createFromArray(transactions).subscribe(x =>{
|
||||||
|
this.submitButtonText = 'Submit';
|
||||||
|
this.submitButtonDisabled = false;
|
||||||
|
}, e => console.log(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import { Observable, Subject, throwError, of } from 'rxjs';
|
||||||
import { catchError, map, tap, first } from 'rxjs/operators';
|
import { catchError, map, tap, first } from 'rxjs/operators';
|
||||||
|
|
||||||
import { TRANSACTION_CREATE_URL } from '../constants/urls';
|
import { TRANSACTION_CREATE_URL } from '../constants/urls';
|
||||||
|
import { Transaction } from '../components/add-transaction-page/transaction';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TransactionService {
|
export class TransactionService {
|
||||||
|
|
@ -17,6 +18,14 @@ export class TransactionService {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createFromArray(transactions: Transaction[]) {
|
||||||
|
const body = {
|
||||||
|
transactions: transactions
|
||||||
|
};
|
||||||
|
return this.httpClient.post(TRANSACTION_CREATE_URL, body, this.options)
|
||||||
|
.pipe(tap(res => console.log(res)), catchError(this.handleError));
|
||||||
|
}
|
||||||
|
|
||||||
create(date: Date, typeId: number, fundId: number, contributorId: number, description: string, amount: number, taxYear: number) {
|
create(date: Date, typeId: number, fundId: number, contributorId: number, description: string, amount: number, taxYear: number) {
|
||||||
const body = {
|
const body = {
|
||||||
date: date,
|
date: date,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
var connection = require('./connection');
|
var connection = require('./connection');
|
||||||
|
|
||||||
|
let inTransaction = false;
|
||||||
|
|
||||||
exports.query = function(sql, args) {
|
exports.query = function(sql, args) {
|
||||||
return new Promise(function(resolve, reject){
|
return new Promise(function(resolve, reject){
|
||||||
connection.query(sql,args,function(error,rows,fields){
|
connection.query(sql,args,function(error,rows,fields){
|
||||||
|
|
@ -23,3 +25,71 @@ exports.nonQuery = function(sql, args) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports.beginTransaction = function() {
|
||||||
|
const self = this;
|
||||||
|
if (self.inTransaction === true) {
|
||||||
|
throw 'Already in a transaction';
|
||||||
|
}
|
||||||
|
self.inTransaction = true;
|
||||||
|
return new Promise(function(resolve, reject){
|
||||||
|
connection.beginTransaction(function(err){
|
||||||
|
if (err) {
|
||||||
|
self.inTransaction = false;
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
|
resolve(self);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.rollback = function() {
|
||||||
|
const self = this;
|
||||||
|
return new Promise(function(resolve, reject){
|
||||||
|
if (self.inTransaction === true) {
|
||||||
|
resolve();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
connection.rollback(function(err){
|
||||||
|
self.inTransaction = false;
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.commit = function() {
|
||||||
|
const self = this;
|
||||||
|
return new Promise(function(resolve, reject){
|
||||||
|
if (self.inTransaction === false) {
|
||||||
|
resolve();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
connection.commit(function(err){
|
||||||
|
self.inTransaction = false;
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.end = function(){
|
||||||
|
return new Promise(function(resolve, reject){
|
||||||
|
connection.end(function(err){
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,29 @@ exports.getAll = async function() {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.add = async function(date, typeId, check, contributorId, fundId, description, amount, taxYear) {
|
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.check, t.contributorId, t.fundId, t.description, t.amount, t.taxYear);
|
||||||
|
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, check, contributorId, fundId, description, amount, taxYear) {
|
||||||
const newTrans = {
|
const newTrans = {
|
||||||
Date: date,
|
Date: date,
|
||||||
TypeId: typeId,
|
TypeId: typeId,
|
||||||
|
|
@ -33,6 +55,11 @@ exports.add = async function(date, typeId, check, contributorId, fundId, descrip
|
||||||
Amount: amount,
|
Amount: amount,
|
||||||
TaxYear: taxYear
|
TaxYear: taxYear
|
||||||
};
|
};
|
||||||
|
return newTrans;
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.add = async function(date, typeId, check, contributorId, fundId, description, amount, taxYear) {
|
||||||
|
const newTrans = getTransaction(date, typeId, check, contributorId, fundId, description, amount, taxYear);
|
||||||
const newTransResult = await connectionAsync.nonQuery('INSERT INTO Transactions Set ?', newTrans);
|
const newTransResult = await connectionAsync.nonQuery('INSERT INTO Transactions Set ?', newTrans);
|
||||||
return newTransResult;
|
return newTransResult;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ router.use(upload.single('file'),function(req,res,next){
|
||||||
token.id = +req.signedCookies.tokenId;
|
token.id = +req.signedCookies.tokenId;
|
||||||
token.value = req.signedCookies.tokenValue;
|
token.value = req.signedCookies.tokenValue;
|
||||||
|
|
||||||
auth.verifyToken(token,function(error,isValid){
|
auth.verifyToken(token,function(error,isValid,user){
|
||||||
if (error){
|
if (error){
|
||||||
removeFile(filename);
|
removeFile(filename);
|
||||||
res.status(400).json({"status":400,"message":"error validating token"});
|
res.status(400).json({"status":400,"message":"error validating token"});
|
||||||
|
|
|
||||||
|
|
@ -3,28 +3,15 @@ var router = express.Router();
|
||||||
var dbTransactions = require("../../database/transactions");
|
var dbTransactions = require("../../database/transactions");
|
||||||
|
|
||||||
router.post("/a/",async function(req,res) {
|
router.post("/a/",async function(req,res) {
|
||||||
if (!res.locals.user || !res.locals.user.canDo || !res.locals.user.canDo('transactions_add')) {
|
if (!res.locals.user || !res.locals.user.canDo || !res.locals.user.canDo('user_add')) {
|
||||||
res.status(401).json({"status":401,"message":"you are not authorized to add a transaction"});
|
res.status(401).json({"status":401,"message":"you are not authorized to add a transaction"});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const result = await dbTransactions.add(req.body.date, req.body.typeId, req.body.check, req.body.contributorId, req.body.fundId, req.body.descriptions, req.body.amount, req.body.taxYear);
|
if (!req.body.transactions || !req.body.transactions.length || req.body.transactions.length === 0) {
|
||||||
console.log(result);
|
res.status(400).json({"status":400,"message":"must pass an array of transactions in the body"});
|
||||||
console.log("new user");
|
|
||||||
console.log(req.body);
|
|
||||||
console.log(res.locals.user);
|
|
||||||
console.log(res.locals.user.canDo('sermons_add'));
|
|
||||||
console.log(res.locals.user.canDo('sermons_addd'));
|
|
||||||
return;
|
|
||||||
if (!req.body.lastName){
|
|
||||||
res.status(400).json({"status":400,"message":"last name is required fields in the body"});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
var newUser = await dbUsers.createUser(req.body.firstName, req.body.lastName, req.body.street, req.body.city, req.body.state, req.body.zip, req.body.country);
|
|
||||||
res.status(201).json({"status":201,"message":"user created","user":newUser});
|
|
||||||
} catch (ex) {
|
|
||||||
res.status(500).json({"status":500,"message":ex});
|
|
||||||
}
|
}
|
||||||
|
const result = await dbTransactions.addArray(req.body.transactions);
|
||||||
|
res.status(200).json({"status":200,"message":"transactions added","result":result});
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
Loading…
Reference in New Issue