From 1a3e6f0ea2ffb06368e3c78b16c4616cba3765f3 Mon Sep 17 00:00:00 2001 From: "shravanrn@gmail.com" Date: Sun, 18 Nov 2018 11:00:50 +0100 Subject: [PATCH] Add ability to cancel transaction. Cancelled transaction shows up as a deposit with the transaction id. An email is sent to the chez bob email about the cancelled transaction. Note that a particular transaction could be cancelled multiple times as this is not tracked. --- bob2k14/soda_server/src/server.ts | 59 +++++++++++++++++++ bob2k14/soda_server/ui_src/client.ts | 20 ++++++- .../screens/transactions_cancel.html | 22 +++++++ .../screens/transactions_detail.html | 1 + bob2k14/soda_server/ui_src/kiosk.html | 1 + 5 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 bob2k14/soda_server/ui_src/htinclude/screens/transactions_cancel.html diff --git a/bob2k14/soda_server/src/server.ts b/bob2k14/soda_server/src/server.ts index 4211c7d..3c5048d 100644 --- a/bob2k14/soda_server/src/server.ts +++ b/bob2k14/soda_server/src/server.ts @@ -605,6 +605,59 @@ class sodad_server { }) } + cancel_transaction (server: sodad_server, client: string, trans_id : number) + { + return redisclient.hgetAsync("sodads:" + client, "uid") + .then(function (uid){ + if (uid == null) + { throw "User could not be found from session."; } + return models.Transactions.find({ + where: { userid : uid, id : trans_id } + }); + }) + .then(function (xact) { + + var transType = "BUY"; + if(xact.xacttype.substring(0, transType.length) !== transType){ + server.clientchannels[client].displayerror("fa-warning", "Failed to Cancel Transaction", "Can only cancel transactions where you bought something"); + return; + } + + var amt = xact.xactvalue; + if(parseFloat(amt) >= 0 || amt[0] != '-') { + throw "Unexpected transaction amount"; + return; + } + + //Convert the value of the expenditure to a positive amount + var restoreAmount = amt.slice(1); + return redisclient.hgetallAsync("sodads:" + client) + .then(function(udata) { + log.info("Cancel transaction from " + udata.username + " id: " + trans_id + " amt: " + restoreAmount); + var mailOpts = { + from: server.initdata.cbemail, + to: server.initdata.cbemail, + cc: udata.email, + subject: '[cb_canceltrans] Transaction ' + trans_id + ' cancelled, user: ' + udata.username + ' amt: ' + restoreAmount, + html: 'User ' + udata.username + " cancelled transaction " + trans_id + " for amount :

" + restoreAmount, + text: 'User ' + udata.username + " cancelled transaction " + trans_id + " for amount :\n\n" + restoreAmount, + }; + return mailtransport.sendMailAsync(mailOpts); + }) + .then(function (response) { + log.info("Cancelling transaction"); + server.balance_transaction(server, client, "ADD " + restoreAmount + " (cancelled transaction " + trans_id + ")", + "Deposit " + restoreAmount + " (cancelled transaction " + trans_id + ")", null, restoreAmount); + server.clientchannels[client].displayerror("fa-check", "Canceled Transaction", "Transaction " + trans_id + " has been canceled. Account has been credited with $" + restoreAmount); + server.updateuser(server, client); + }); + }) + .catch(function (e) { + log.error("Failed to Cancel Transaction " + e); + server.clientchannels[client].displayerror("fa-warning", "Failed to Cancel Transaction", "Failed to Cancel Transaction"); + }); + } + //maybe make this a library function debunyanize (log: string[], callback: (Object, string) => void) { @@ -1576,6 +1629,12 @@ class sodad_server { log.info("Setting password (enabled=" + enable + ") for client " + client); return server.changepassword(server, client, enable, newpassword, oldpassword); }, + cancel_transaction:function(trans_id) + { + var client = this.id; + log.info("Cancellng transaction for client " + client); + return server.cancel_transaction(server, client, trans_id); + }, vendstock: function() { var client = this.id; diff --git a/bob2k14/soda_server/ui_src/client.ts b/bob2k14/soda_server/ui_src/client.ts index 11d30ad..237ef61 100644 --- a/bob2k14/soda_server/ui_src/client.ts +++ b/bob2k14/soda_server/ui_src/client.ts @@ -296,7 +296,8 @@ export class Client }); $(".transactiondetail").on('click', function(e) { - var xact = client.currenttransactions[$(this).data('id')]; + var arrIndex = $(this).data('id'); + var xact = client.currenttransactions[arrIndex]; $("#transactiondetails-table tbody").empty(); $("#transactiondetails-table tbody").append('Id' + xact.id + ''); $("#transactiondetails-table tbody").append('Time' + xact.xacttime + ''); @@ -305,8 +306,9 @@ export class Client $("#transactiondetails-table tbody").append('Source' + xact.source + ''); $("#transactiondetails-table tbody").append('Barcode' + xact.barcode + ''); $("#transactiondetails-table tbody").append('User ID' + xact.userid + ''); + $("#transactioncancelui").data('id', arrIndex); client.setUIscreen(client, 'transactions-detail'); - }); + }); } ); @@ -959,6 +961,20 @@ export class Client }); } }); + + $("#transactioncancelui").on('click', function() { + var arrIndex = $(this).data('id'); + var xact = client.currenttransactions[arrIndex]; + $("#cancel-trans-id").text(xact.id); + $("#cancel-trans-amount").text(xact.xactvalue); + $("#transactioncancel").data('id', arrIndex); + client.setUIscreen(client, 'transactions-cancel'); + }); + + $("#transactioncancel").on('click', function(e) { + var xact = client.currenttransactions[$(this).data('id')]; + client.server_channel.cancel_transaction(xact.id); + }); } diff --git a/bob2k14/soda_server/ui_src/htinclude/screens/transactions_cancel.html b/bob2k14/soda_server/ui_src/htinclude/screens/transactions_cancel.html new file mode 100644 index 0000000..c06a210 --- /dev/null +++ b/bob2k14/soda_server/ui_src/htinclude/screens/transactions_cancel.html @@ -0,0 +1,22 @@ +
+
+
+
+ @@include('snippets/tsidebutton.html', { + "id" : "transactions-exitbtn", + "target" : "transactions", + "fa" : "fa-sign-out", + "text" : "Back" + }) +
+
+
+

An email will be sent to the Chez Bob admins about this canceled transaction to check for abuse. Are you sure you want to proceed?

+

Transaction Id:

+

0

+

Transaction Amount:

+

0.00

+ Cancel Transaction +
+
+
\ No newline at end of file diff --git a/bob2k14/soda_server/ui_src/htinclude/screens/transactions_detail.html b/bob2k14/soda_server/ui_src/htinclude/screens/transactions_detail.html index e392be5..233c867 100644 --- a/bob2k14/soda_server/ui_src/htinclude/screens/transactions_detail.html +++ b/bob2k14/soda_server/ui_src/htinclude/screens/transactions_detail.html @@ -20,6 +20,7 @@ + Cancel Transaction diff --git a/bob2k14/soda_server/ui_src/kiosk.html b/bob2k14/soda_server/ui_src/kiosk.html index 271e370..8299bd1 100644 --- a/bob2k14/soda_server/ui_src/kiosk.html +++ b/bob2k14/soda_server/ui_src/kiosk.html @@ -71,6 +71,7 @@ @@include('screens/mainpurchase.html') @@include('screens/options.html') @@include('screens/transactions.html') + @@include('screens/transactions_cancel.html') @@include('screens/transactions_detail.html') @@include('screens/manualpurchase.html') @@include('screens/manualdeposit.html')