From 5cf75ebf9e70dfec570e6381ca295bae8f81f738 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Wed, 25 Mar 2020 15:27:38 +0100 Subject: [PATCH] When a transaction can't becreated because the user don't have the permission to take too much money, then we create an invalid transaction --- apps/permission/fixtures/initial.json | 4 +- static/js/consos.js | 24 ++++++++-- static/js/transfer.js | 66 +++++++++++++++++++++------ 3 files changed, 76 insertions(+), 18 deletions(-) diff --git a/apps/permission/fixtures/initial.json b/apps/permission/fixtures/initial.json index 4c7de16d..43d39a36 100644 --- a/apps/permission/fixtures/initial.json +++ b/apps/permission/fixtures/initial.json @@ -327,7 +327,7 @@ "note", "transaction" ], - "query": "[\"AND\", {\"source\": [\"user\", \"note\"]}, {\"amount__lte\": [\"user\", \"note\", \"balance\"]}]", + "query": "[\"AND\", {\"source\": [\"user\", \"note\"]}, [\"OR\", {\"amount__lte\": [\"user\", \"note\", \"balance\"]}, {\"valid\": false}]]", "type": "add", "mask": 1, "field": "", @@ -387,7 +387,7 @@ "note", "recurrenttransaction" ], - "query": "[\"AND\", {\"destination\": [\"club\", \"note\"]}, {\"amount__lte\": {\"F\": [\"ADD\", [\"F\", \"source__balance\"], 5000]}}]", + "query": "[\"AND\", {\"destination\": [\"club\", \"note\"]}, [\"OR\", {\"amount__lte\": {\"F\": [\"ADD\", [\"F\", \"source__balance\"], 5000]}}, {\"valid\": false}]]", "type": "add", "mask": 2, "field": "", diff --git a/static/js/consos.js b/static/js/consos.js index 896f996c..b201e748 100644 --- a/static/js/consos.js +++ b/static/js/consos.js @@ -199,8 +199,26 @@ function consume(source, dest, quantity, amount, reason, type, category, templat "category": category, "template": template }, reset).fail(function (e) { - reset(); - - addMsg("Une erreur est survenue lors de la transaction : " + e.responseText, "danger"); + $.post("/api/note/transaction/transaction/", + { + "csrfmiddlewaretoken": CSRF_TOKEN, + "quantity": quantity, + "amount": amount, + "reason": reason, + "valid": false, + "invalidity_reason": "Solde insuffisant", + "polymorphic_ctype": type, + "resourcetype": "RecurrentTransaction", + "source": source, + "destination": dest, + "category": category, + "template": template + }).done(function() { + reset(); + addMsg("La transaction n'a pas pu être validée pour cause de solde insuffisant.", "danger"); + }).fail(function () { + reset(); + addMsg("Une erreur est survenue lors de la transaction : " + e.responseText, "danger"); + }); }); } diff --git a/static/js/transfer.js b/static/js/transfer.js index c615f932..cfa4f1f9 100644 --- a/static/js/transfer.js +++ b/static/js/transfer.js @@ -73,18 +73,38 @@ $("#transfer").click(function() { "resourcetype": "Transaction", "source": user_id, "destination": dest.id - }, function () { + }).done(function () { addMsg("Le transfert de " + pretty_money(dest.quantity * 100 * $("#amount").val()) + " de votre note " + " vers la note " + dest.name + " a été fait avec succès !", "success"); reset(); - }).fail(function (err) { - addMsg("Le transfert de " - + pretty_money(dest.quantity * 100 * $("#amount").val()) + " de votre note " - + " vers la note " + dest.name + " a échoué : " + err.responseText, "danger"); + }).fail(function () { + $.post("/api/note/transaction/transaction/", + { + "csrfmiddlewaretoken": CSRF_TOKEN, + "quantity": dest.quantity, + "amount": 100 * $("#amount").val(), + "reason": $("#reason").val(), + "valid": false, + "invalidity_reason": "Solde insuffisant", + "polymorphic_ctype": TRANSFER_POLYMORPHIC_CTYPE, + "resourcetype": "Transaction", + "source": user_id, + "destination": dest.id + }).done(function () { + addMsg("Le transfert de " + + pretty_money(dest.quantity * 100 * $("#amount").val()) + " de votre note " + + " vers la note " + dest.name + " a échoué : Solde insuffisant", "danger"); - reset(); + reset(); + }).fail(function (err) { + addMsg("Le transfert de " + + pretty_money(dest.quantity * 100 * $("#amount").val()) + " de votre note " + + " vers la note " + dest.name + " a échoué : " + err.responseText, "danger"); + + reset(); + }); }); }); } @@ -102,18 +122,38 @@ $("#transfer").click(function() { "resourcetype": "Transaction", "source": source.id, "destination": dest.id - }, function () { + }).done(function () { addMsg("Le transfert de " + pretty_money(source.quantity * dest.quantity * 100 * $("#amount").val()) + " de la note " + source.name + " vers la note " + dest.name + " a été fait avec succès !", "success"); reset(); }).fail(function (err) { - addMsg("Le transfert de " - + pretty_money(source.quantity * dest.quantity * 100 * $("#amount").val()) + " de la note " + source.name - + " vers la note " + dest.name + " a échoué : " + err.responseText, "danger"); + $.post("/api/note/transaction/transaction/", + { + "csrfmiddlewaretoken": CSRF_TOKEN, + "quantity": source.quantity * dest.quantity, + "amount": 100 * $("#amount").val(), + "reason": $("#reason").val(), + "valid": false, + "invalidity_reason": "Solde insuffisant", + "polymorphic_ctype": TRANSFER_POLYMORPHIC_CTYPE, + "resourcetype": "Transaction", + "source": source.id, + "destination": dest.id + }).done(function () { + addMsg("Le transfert de " + + pretty_money(source.quantity * dest.quantity * 100 * $("#amount").val()) + " de la note " + source.name + + " vers la note " + dest.name + " a échoué : Solde insuffisant", "danger"); - reset(); + reset(); + }).fail(function (err) { + addMsg("Le transfert de " + + pretty_money(source.quantity * dest.quantity * 100 * $("#amount").val()) + " de la note " + source.name + + " vers la note " + dest.name + " a échoué : " + err.responseText, "danger"); + + reset(); + }); }); }); }); @@ -150,11 +190,11 @@ $("#transfer").click(function() { "last_name": $("#last_name").val(), "first_name": $("#first_name").val(), "bank": $("#bank").val() - }, function () { + }).done(function () { addMsg("Le crédit/retrait a bien été effectué !", "success"); reset(); }).fail(function (err) { - addMsg("Le crédit/transfert a échoué : " + err.responseText, "danger"); + addMsg("Le crédit/retrait a échoué : " + err.responseText, "danger"); reset(); }); }