diff options
| author | Adam Soltys <[email protected]> | 2021-01-15 15:31:46 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2021-01-15 15:31:46 -0800 |
| commit | d01159ce9eee956dca32fd69525c0edd5aae9477 (patch) | |
| tree | e6a4d4a928daffb5bcedbc3411a0dfded6ff422f /routes/users.js | |
| parent | d03dad4d85ccd107068719c4b68cbd6a255864f6 (diff) | |
| download | coinos-server-d01159ce9eee956dca32fd69525c0edd5aae9477.tar.gz coinos-server-d01159ce9eee956dca32fd69525c0edd5aae9477.zip | |
add more transactions and locks
Diffstat (limited to 'routes/users.js')
| -rw-r--r-- | routes/users.js | 73 |
1 files changed, 54 insertions, 19 deletions
diff --git a/routes/users.js b/routes/users.js index c9d60585..41fdb154 100644 --- a/routes/users.js +++ b/routes/users.js @@ -508,14 +508,14 @@ app.post( } = req.body; try { - let account = await db.Account.findOne({ - where: { id, user_id: user.id } - }); - - if (!account || (account.pubkey && !pubkey)) throw new Error("Problem updating account"); + await db.transaction(async transaction => { + let account = await db.Account.findOne({ + where: { id, user_id: user.id }, + lock: transaction.LOCK.UPDATE, + transaction + }); - await db.Account.update( - { + let params = { name, ticker, precision, @@ -526,18 +526,19 @@ app.post( hide, index, privkey - }, - { - where: { id, user_id: user.id } - } - ); + }; - account = await db.Account.findOne({ - where: { id } - }); + if (!account || (account.pubkey && !pubkey)) delete params.pubkey; - emit(user.username, "account", account); - res.end(); + await db.Account.update(params, { + where: { id, user_id: user.id }, + transaction + }); + + await account.reload({ transaction }); + emit(user.username, "account", account); + res.end(); + }); } catch (e) { l.error(e.message); return res.status(500).send("There was a problem updating the account"); @@ -676,8 +677,8 @@ app.post( { transaction } ); - account.balance -= source.amount; - await account.save(); + await account.decrement({ balance: source.amount }, { transaction }); + await account.reload({ transaction }); payment = payment.get({ plain: true }); payment.account = account.get({ plain: true }); @@ -711,3 +712,37 @@ app.post( res.send(await bcrypt.compare(password, user.password)); }) ); + +app.get( + "/isInternal", + auth, + ah(async function(req, res) { + let { user } = req; + let { address } = req.query; + if (!address) throw new Error("Address not provided"); + + let invoice = await db.Invoice.findOne({ + where: { address }, + include: { + attributes: ["username"], + model: db.User, + as: "user" + } + }); + + if (invoice) { + let info; + try { + info = await bc.getAddressInfo(address); + } catch (e) { + info = await lq.getAddressInfo(address); + } + if (info.ismine) { + emit(user.username, "to", invoice.user); + return res.send(true); + } + } + + res.send(false); + }) +); |
