diff options
| author | Adam Soltys <[email protected]> | 2025-07-25 19:35:08 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2025-07-25 19:35:08 -0700 |
| commit | 6b21771f95885a73f317381a33a3dbac8b6144f9 (patch) | |
| tree | 9005e9b3ac6e99829c882d530e28636b5db13ab1 | |
| parent | 59fe8d2e24085239b15ca282c182d5c7cd1ce4de (diff) | |
| download | coinos-server-6b21771f95885a73f317381a33a3dbac8b6144f9.tar.gz coinos-server-6b21771f95885a73f317381a33a3dbac8b6144f9.zip | |
authorizations
| -rw-r--r-- | routes/payments.ts | 81 |
1 files changed, 45 insertions, 36 deletions
diff --git a/routes/payments.ts b/routes/payments.ts index 0fcd64cd..c2921fcb 100644 --- a/routes/payments.ts +++ b/routes/payments.ts @@ -271,49 +271,58 @@ export default { amount = parseInt(amount); if (amount < 0) fail("Invalid amount"); - const managers = await db.sMembers(`fund:${id}:managers`); - if (managers.length && !managers.includes(user.id)) fail("Unauthorized"); + const rates = await g("rates"); - const result: any = await db.debit( - `fund:${id}`, - "", - "Insufficient funds", - amount, - 0, - 0, - 0, - 0, - ); - if (result.err) fail(result.err); + const authorization = await g(`authorization:${id}`); + if (authorization) { + const { currency, fiat } = authorization; + amount = sats(fiat / rates[currency]); - const rates = await g("rates"); - const { currency } = user; - const rate = rates[currency]; + const sender = await getUser(authorization.uid); + await db.del(`authorization:${id}`); + res.send(await sendInternal({ amount, sender, recipient: user })); + } else { + const managers = await db.sMembers(`fund:${id}:managers`); + if (managers.length && !managers.includes(user.id)) + fail("Unauthorized"); - if (!iid) { - iid = v4(); - await s(`invoice:${iid}`, { - currency, - id: iid, + const result: any = await db.debit( + `fund:${id}`, + "", + "Insufficient funds", + amount, + 0, + 0, + 0, + 0, + ); + if (result.err) fail(result.err); + + if (!iid) { + iid = v4(); + await s(`invoice:${iid}`, { + currency: user.currency, + id: iid, + hash: iid, + rate: rates[user.currency], + uid, + received: 0, + }); + } + + const payment = await credit({ + aid: user.id, hash: iid, - rate, - uid, - received: 0, + amount, + memo: id, + ref: id, + type: PaymentType.fund, }); - } - const payment = await credit({ - aid: user.id, - hash: iid, - amount, - memo: id, - ref: id, - type: PaymentType.fund, - }); + await db.lPush(`fund:${id}:payments`, payment.id); - await db.lPush(`fund:${id}:payments`, payment.id); - - res.send({ payment }); + res.send({ payment }); + } } catch (e) { warn("problem withdrawing from fund", user.username, e.message); bail(res, e.message); |
