Skip to content
Snippets Groups Projects
Commit 9a4a7e4c authored by Dominik Hebeler's avatar Dominik Hebeler
Browse files

fixed response code for too many charges

parent 8865ca7a
Branches
No related tags found
No related merge requests found
Pipeline #7269 passed
...@@ -108,7 +108,7 @@ Can take either amount or price. If both supplied only amount is taken into acco ...@@ -108,7 +108,7 @@ Can take either amount or price. If both supplied only amount is taken into acco
### Example Response ### Example Response
Successfull discharge will have a response code of `201` Successfull discharge will have a response code of `201`
If the key cannot be charged because it is already charged with too many Orders response code will be `403` If the key cannot be charged because it is already charged with too many Orders response code will be `423`
```json ```json
{ {
......
...@@ -26,7 +26,7 @@ router.use((req, res, next) => { ...@@ -26,7 +26,7 @@ router.use((req, res, next) => {
} }
}); });
router.post("/key/create", (req, res) => { router.post("/key/create", async (req, res) => {
let amount = req.body.amount; let amount = req.body.amount;
if (!amount) { if (!amount) {
// If amount is not given but price is // If amount is not given but price is
...@@ -46,11 +46,9 @@ router.post("/key/create", (req, res) => { ...@@ -46,11 +46,9 @@ router.post("/key/create", (req, res) => {
/** /**
* @type {Order} * @type {Order}
*/ */
let key = await Key.GET_NEW_KEY();
let new_order; let new_order;
Key.GET_NEW_KEY() Order.CREATE_NEW_ORDER(amount, key.get_key(), new Manual(note))
.then((key) =>
Order.CREATE_NEW_ORDER(amount, key.get_key(), new Manual(note))
)
.then((order) => { .then((order) => {
new_order = order; new_order = order;
return new_order.captureOrder(); return new_order.captureOrder();
...@@ -64,9 +62,11 @@ router.post("/key/create", (req, res) => { ...@@ -64,9 +62,11 @@ router.post("/key/create", (req, res) => {
}); });
}) })
.catch((reason) => { .catch((reason) => {
res.status(403).json({ res.status(423).json({
code: 403, code: 423,
error: "Cannot create Order for given Key", error: reason,
key: key.get_key(),
charged: 0
}); });
}); });
}); });
...@@ -115,7 +115,7 @@ router.post("/key/:key/discharge", (req, res) => { ...@@ -115,7 +115,7 @@ router.post("/key/:key/discharge", (req, res) => {
}); });
}); });
router.post("/key/:key/charge", (req, res) => { router.post("/key/:key/charge", async (req, res) => {
let amount = req.body.amount; let amount = req.body.amount;
if (!amount) { if (!amount) {
// If amount is not given but price is // If amount is not given but price is
...@@ -137,24 +137,27 @@ router.post("/key/:key/charge", (req, res) => { ...@@ -137,24 +137,27 @@ router.post("/key/:key/charge", (req, res) => {
/** /**
* @type {Order} * @type {Order}
*/ */
let key = await Key.GET_KEY(req.params.key);
let new_order = null; let new_order = null;
Order.CREATE_NEW_ORDER(amount, req.params.key, new Manual(note)) Order.CREATE_NEW_ORDER(amount, key.get_key(), new Manual(note))
.then((order) => { .then((order) => {
new_order = order; new_order = order;
return new_order.captureOrder(); return new_order.captureOrder();
}) })
.then(() => new_order.chargeKey()) .then(() => new_order.chargeKey())
.then(async () => { .then(() => {
res.status(201).json({ res.status(201).json({
key: await new_order.getKeyFromOrderLink(), key: key.get_key(),
order_id: new_order.getOrderID(), order_id: new_order.getOrderID(),
charged: new_order.getAmount(), charged: new_order.getAmount(),
}); });
}) })
.catch((reason) => { .catch((reason) => {
res.status(403).json({ res.status(423).json({
code: 403, code: 423,
error: "Cannot create Order for given Key", error: reason,
key: key.get_key(),
charged: 0
}); });
}); });
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment