Skip to content

Commit

Permalink
Merge pull request #99 from tolulope-od/ft-accounts-transaction-16585…
Browse files Browse the repository at this point in the history
…5312

#165855312 Staff can credit or debit a client's account
  • Loading branch information
tolulope-od committed May 7, 2019
2 parents 7ce9536 + a282174 commit 093d653
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 18 deletions.
2 changes: 1 addition & 1 deletion server/controllers/TransactionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export default class TransactionController {
});
}

if (debitAmount > balance) {
if (parseFloat(debitAmount) > balance) {
return res.status(400).json({
status: 400,
error: `Insufficient funds, your account balance is ${balance}`
Expand Down
34 changes: 26 additions & 8 deletions www/admin/accountinfo.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ <h2 class="label-lg"><strong>ACCOUNTS</strong></h2>
<div class="nav-area"></div>
<div class="shortcut-btns">
<a href="#" onclick="handleStatusChange()" id="accnt-status-btn">Activate Account</a>
<a href="#" onclick="handleAccountDebit()" id="accnt-status-btn">Debit Account</a>
<a href="#" onclick="handleAccountCredit()" id="accnt-status-btn">Credit Account</a>
<a href="#" onclick="showDebitForm()" id="accnt-status-btn">Debit Account</a>
<a href="#" onclick="showCreditForm()" id="accnt-status-btn">Credit Account</a>
<a href="#" onclick="handleAccountDelete()" id="accnt-status-btn" class="accnt-delete-btn"
>Delete Account</a
>
Expand Down Expand Up @@ -109,30 +109,48 @@ <h3 class="accnt-name" id="created-date"></h3>
<div id="myModal" class="modal">
<div class="modal-content" id="account-modal-content">
<span class="close">&times;</span>
<form onsubmit="event.preventDefault()" class="accnt-debit-form" id="accnt-debit-form">
<form
onsubmit="event.preventDefault(); handleAccountDebit();"
class="accnt-debit-form"
id="accnt-debit-form"
>
<h3>Debit Account</h3>
<br />
<label>Amount: </label>
<br />
<input type="number" name="address" placeholder="Enter Amount" required />
<input
type="number"
step="0.01"
name="address"
placeholder="Enter Amount"
id="debit-amount"
required
/>
<br />
<div class="input-field-btn">
<input type="submit" name="confirmDebit" value="Confirm" />
<input type="submit" name="confirmDebit" id="debit-btn" value="Confirm" />
</div>
</form>
<form
onsubmit="event.preventDefault()"
onsubmit="event.preventDefault(); handleAccountCredit();"
class="accnt-credit-form"
id="accnt-credit-form"
>
<h3>Credit Account</h3>
<br />
<label>Amount: </label>
<br />
<input type="number" name="address" placeholder="Enter Amount" required />
<input
type="number"
step="0.01"
name="address"
placeholder="Enter Amount"
id="credit-amount"
required
/>
<br />
<div class="input-field-btn">
<input type="submit" name="confirmCredit" value="Confirm" />
<input type="submit" name="confirmCredit" id="credit-btn" value="Confirm" />
</div>
</form>
<div id="confirm-delete">
Expand Down
16 changes: 11 additions & 5 deletions www/js/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ const modalContent = document.getElementById('account-modal-content');
const para = document.createElement('P');
let text;

const handleError = errMessage => {
para.innerHTML = '';
text = document.createTextNode(errMessage);
para.appendChild(text);
modalContent.appendChild(para);
modal.style.display = 'block';
};

const fetchSingleAccount = event => {
const reference = event.target.getAttribute('data-account-number');
const accountNumber = parseInt(reference, 10);
Expand Down Expand Up @@ -102,11 +110,9 @@ const fetchAccounts = () => {
}
})
.catch(err => {
para.innerHTML = '';
text = document.createTextNode(`${err.message}`);
para.appendChild(text);
modalContent.appendChild(para);
modal.style.display = 'block';
const { log } = console;
log(err.message);
handleError('An error occurred');
});
};

Expand Down
2 changes: 1 addition & 1 deletion www/js/fetchAccounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const fetchAllAccounts = () => {
}
<p class="balance-text">Balance</p>
<h3 class="account-balance" id="accnt-balance">&#x20A6; ${formatNumber(
Math.round(datum.balance * 100) / 10
parseFloat(datum.balance).toFixed(2)
)}</h3>
<p class="balance-text">Account Number</p>
<h3 class="account-balance" id="accnt-number">${datum.accountnumber}</h3>
Expand Down
112 changes: 109 additions & 3 deletions www/js/fetchSingleAccountHistory.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-unused-vars */
/* global window, document, fetch, localStorage, Headers */
/* eslint no-param-reassign: ["error", { "props": true, "ignorePropertyModificationsFor": ["errors"] }] */

Expand All @@ -9,8 +10,19 @@ const para = document.createElement('P');
const transactionsTable = document.getElementById('transactions-table');
const transactionsDisplay = document.getElementById('transactions');
const accountDetails = document.querySelector('.accnt-details');
const debitForm = document.getElementById('accnt-debit-form');
const creditForm = document.getElementById('accnt-credit-form');
const confirmDeleteMsg = document.getElementById('confirm-delete');
let text;

const handleError = errMessage => {
para.innerHTML = '';
text = document.createTextNode(errMessage);
para.appendChild(text);
modalContent.appendChild(para);
modal.style.display = 'block';
};

// Gotten from https://blog.abelotech.com/posts/number-currency-formatting-javascript/
const formatNumber = num => {
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
Expand Down Expand Up @@ -69,10 +81,10 @@ const fetchAccountTransactionHistory = () => {
const transactionDetails = `<tr>
<td>${transaction.createdon.split('T')[0]}</td>
<td>${transaction.type}</td>
<td>&#x20A6; ${formatNumber(Math.round(transaction.amount * 100) / 10)}</td>
<td>&#x20A6; ${formatNumber(transaction.amount)}</td>
<td>&#x20A6; ${formatNumber(Math.round(transaction.oldbalance * 100) / 10)}</td>
<td>&#x20A6; ${formatNumber(Math.round(transaction.newbalance * 100) / 10)}</td>
<td>&#x20A6; ${formatNumber(parseFloat(transaction.oldbalance).toFixed(2))}</td>
<td>&#x20A6; ${formatNumber(parseFloat(transaction.newbalance).toFixed(2))}</td>
<td>${transaction.cashiername}</td>
</tr>`;
Expand Down Expand Up @@ -101,8 +113,102 @@ const fetchAccountTransactionHistory = () => {
});
};

const showDebitForm = () => {
// clear form area before displaying
para.innerHTML = '';
confirmDeleteMsg.style.display = 'none';
creditForm.style.display = 'none';
debitForm.style.display = 'block';
modal.style.display = 'block';
};

const showCreditForm = () => {
// clear form area before displaying
para.innerHTML = '';
confirmDeleteMsg.style.display = 'none';
debitForm.style.display = 'none';
creditForm.style.display = 'block';
modal.style.display = 'block';
};

const handleAccountDebit = () => {
const debitAmount = document.getElementById('debit-amount').value;
const debitBtn = document.getElementById('debit-btn');
const accountNumber = localStorage.getItem('banka-account-number-view');
const token = localStorage.getItem('banka-app-token');
const headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', token);
para.innerHTML = 'Transaction in progress...';
debitBtn.value = 'LOADING..';
debitBtn.disabled = true;
debitBtn.style.backgroundColor = 'grey';
fetch(`${API_PREFIX}/transactions/${accountNumber}/debit`, {
method: 'POST',
headers,
body: JSON.stringify({ debitAmount })
})
.then(res => res.json())
.then(response => {
debitBtn.value = 'Confirm';
debitBtn.disabled = false;
debitBtn.style.backgroundColor = null;
if (response.status === 200) {
para.innerHTML = `${response.message}`;
} else {
para.innerHTML = `${response.error}`;
}
})
.catch(err => {
const { log } = console;
log(err.message);
handleError('An error occurred');
});
};

const handleAccountCredit = () => {
const creditAmount = document.getElementById('credit-amount').value;
const creditBtn = document.getElementById('credit-btn');
const accountNumber = localStorage.getItem('banka-account-number-view');
const token = localStorage.getItem('banka-app-token');
const headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', token);
para.innerHTML = 'Transaction in progress...';
creditBtn.value = 'LOADING..';
creditBtn.disabled = true;
creditBtn.style.backgroundColor = 'grey';
fetch(`${API_PREFIX}/transactions/${accountNumber}/credit`, {
method: 'POST',
headers,
body: JSON.stringify({ creditAmount })
})
.then(res => res.json())
.then(response => {
creditBtn.value = 'Confirm';
creditBtn.disabled = false;
creditBtn.style.backgroundColor = null;
if (response.status === 200) {
para.innerHTML = `${response.message}`;
} else {
para.innerHTML = `${response.error}`;
}
})
.catch(err => {
const { log } = console;
log(err.message);
handleError('An error occurred');
});
};

span.onclick = () => {
modal.style.display = 'none';
};

window.onclick = event => {
if (event.target === modal) {
modal.style.display = 'none';
}
};

window.onload(fetchAccountTransactionHistory());

0 comments on commit 093d653

Please sign in to comment.