diff options
| author | Adam Soltys <[email protected]> | 2022-09-02 14:47:08 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2022-09-02 14:47:14 -0700 |
| commit | 3161dd7ee95b6c7d70f8341298dd4fab84cda8b0 (patch) | |
| tree | 16cd6c55d5dc481331b1e8b640d179f193e7ea13 /routes/invoices.js | |
| parent | 7a8d64833de8873afb92982b3f5bfff59b30c542 (diff) | |
| download | coinos-server-3161dd7ee95b6c7d70f8341298dd4fab84cda8b0.tar.gz coinos-server-3161dd7ee95b6c7d70f8341298dd4fab84cda8b0.zip | |
es6 migration
Diffstat (limited to 'routes/invoices.js')
| -rw-r--r-- | routes/invoices.js | 184 |
1 files changed, 89 insertions, 95 deletions
diff --git a/routes/invoices.js b/routes/invoices.js index 9e3fedb7..a65ff7ba 100644 --- a/routes/invoices.js +++ b/routes/invoices.js @@ -1,113 +1,107 @@ -import { Op } from 'sequelize'; -import axios from 'axios'; +import app from "../app.js"; +import { optionalAuth } from "../lib/passport.js"; +import { Op } from "@sequelize/core"; +import axios from "axios"; -app.get( - "/invoice", - ah(async (req, res, next) => { - try { - const invoice = await db.Invoice.findOne({ - where: { - uuid: req.query.uuid - }, - include: { - model: db.User, - as: "user", - attributes: ["username", "currency"] - } - }); +app.get("/invoice", async (req, res, next) => { + try { + const invoice = await db.Invoice.findOne({ + where: { + uuid: req.query.uuid + }, + include: { + model: db.User, + as: "user", + attributes: ["username", "currency"] + } + }); - res.send(invoice); - } catch (e) { - l.error("couldn't find invoice", e); - } - }) -); + res.send(invoice); + } catch (e) { + l.error("couldn't find invoice", e); + } +}); -app.post( - "/invoice", - optionalAuth, - ah(async (req, res, next) => { - try { - let { liquidAddress, id, invoice, user, tx } = req.body; - let { blindkey } = invoice; +app.post("/invoice", optionalAuth, async (req, res, next) => { + try { + let { liquidAddress, id, invoice, user, tx } = req.body; + let { blindkey } = invoice; - if (invoice.amount < 0) throw new Error("amount out of range"); - if ( - invoice.tip > invoice.amount || - invoice.tip > 1000000 || - invoice.tip < 0 - ) - throw new Error("tip amount out of range"); + if (invoice.amount < 0) throw new Error("amount out of range"); + if ( + invoice.tip > invoice.amount || + invoice.tip > 1000000 || + invoice.tip < 0 + ) + throw new Error("tip amount out of range"); - if (liquidAddress) { - l.info("conversion request for", liquidAddress, invoice.text); - convert[invoice.text] = { address: liquidAddress, tx }; - } + if (liquidAddress) { + l.info("conversion request for", liquidAddress, invoice.text); + convert[invoice.text] = { address: liquidAddress, tx }; + } - if (!user) ({ user } = req); - else { - user = await db.User.findOne({ - where: { - username: user.username - } - }); - } - if (!user) throw new Error("user not provided"); - if (!invoice.currency) invoice.currency = user.currency; - if (!invoice.rate) invoice.rate = app.get("rates")[invoice.currency]; - if (invoice.tip > invoice.amount || invoice.tip > 1000000) - throw new Error("tip is too large"); - if (invoice.tip < 0 || invoice.amount < 0) - throw new Error("invalid amount"); - invoice.user_id = user.id; - invoice.account_id = user.account_id; + if (!user) ({ user } = req); + else { + user = await db.User.findOne({ + where: { + username: user.username + } + }); + } + if (!user) throw new Error("user not provided"); + if (!invoice.currency) invoice.currency = user.currency; + if (!invoice.rate) invoice.rate = app.get("rates")[invoice.currency]; + if (invoice.tip > invoice.amount || invoice.tip > 1000000) + throw new Error("tip is too large"); + if (invoice.tip < 0 || invoice.amount < 0) + throw new Error("invalid amount"); + invoice.user_id = user.id; + invoice.account_id = user.account_id; - l.info( - "creating invoice", - user.username, - invoice.network, - invoice.amount, - invoice.tip, - invoice.currency, - invoice.text && - `${invoice.text.substr(0, 8)}..${invoice.text.substr(-6)}` - ); + l.info( + "creating invoice", + user.username, + invoice.network, + invoice.amount, + invoice.tip, + invoice.currency, + invoice.text && `${invoice.text.substr(0, 8)}..${invoice.text.substr(-6)}` + ); - if (!invoice.tip) invoice.tip = 0; + if (!invoice.tip) invoice.tip = 0; - const exists = - invoice.text && - (await db.Invoice.findOne({ - where: { - [Op.or]: { - address: invoice.address || "", - unconfidential: invoice.unconfidential || "", - text: invoice.text - } + const exists = + invoice.text && + (await db.Invoice.findOne({ + where: { + [Op.or]: { + address: invoice.address || "", + unconfidential: invoice.unconfidential || "", + text: invoice.text } - })); - - invoice = exists - ? await exists.update(invoice) - : await db.Invoice.create(invoice); - addresses[invoice.address] = user.username; - if (invoice.unconfidential) { - addresses[invoice.unconfidential] = user.username; - if (blindkey) await lq.importBlindingKey(invoice.address, blindkey); - } + } + })); - res.send(invoice); - } catch (e) { - l.error(e.message, e.stack); - res.status(500).send(`Problem during invoice creation: ${e.message}`); + invoice = exists + ? await exists.update(invoice) + : await db.Invoice.create(invoice); + addresses[invoice.address] = user.username; + if (invoice.unconfidential) { + addresses[invoice.unconfidential] = user.username; + if (blindkey) await lq.importBlindingKey(invoice.address, blindkey); } - }) -); + + res.send(invoice); + } catch (e) { + l.error(e.message, e.stack); + res.status(500).send(`Problem during invoice creation: ${e.message}`); + } +}); app.post( "/:username/:network/invoice", optionalAuth, - ah(async (req, res, next) => { + async (req, res, next) => { let { network, username } = req.params; - }) + } ); |
