Skip to content
This repository has been archived by the owner on Apr 1, 2022. It is now read-only.

Add ability to cancel transaction #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions bob2k14/soda_server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 :<br/><br/>" + 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)
{
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 18 additions & 2 deletions bob2k14/soda_server/ui_src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('<tr><td>Id</td><td>' + xact.id + '<td></tr>');
$("#transactiondetails-table tbody").append('<tr><td>Time</td><td>' + xact.xacttime + '<td></tr>');
Expand All @@ -305,8 +306,9 @@ export class Client
$("#transactiondetails-table tbody").append('<tr><td>Source</td><td>' + xact.source + '<td></tr>');
$("#transactiondetails-table tbody").append('<tr><td>Barcode</td><td>' + xact.barcode + '<td></tr>');
$("#transactiondetails-table tbody").append('<tr><td>User ID</td><td>' + xact.userid + '<td></tr>');
$("#transactioncancelui").data('id', arrIndex);
client.setUIscreen(client, 'transactions-detail');
});
});

}
);
Expand Down Expand Up @@ -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);
});
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div id="transactions-cancel" class="ui-screen">
<div class="row">
<div class="col-xs-5">
<div class="list-group">
@@include('snippets/tsidebutton.html', {
"id" : "transactions-exitbtn",
"target" : "transactions",
"fa" : "fa-sign-out",
"text" : "Back"
})
</div>
</div>
<div class="col-xs-7">
<p class="lead">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?</p>
<p class="lead">Transaction Id: </p>
<p class="lead" id="cancel-trans-id">0</p>
<p class="lead">Transaction Amount: </p>
<p class="lead" id="cancel-trans-amount">0.00</p>
<a href='#' class='btn btn-info' id='transactioncancel'>Cancel Transaction</a>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<tbody>
</tbody>
</table>
<a href='#' class='btn btn-info' id='transactioncancelui'>Cancel Transaction</a>
</div>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions bob2k14/soda_server/ui_src/kiosk.html
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down