Download src/utils/tokenSales.js from Mbonea/WifiBiz: direct link, hf CLI and curl.
- Browser
- Download file 1.69 kB
-
https://huggingface.co/spaces/Mbonea/WifiBiz/resolve/main/src/utils/tokenSales.js
- Command line
-
hf download hf://spaces/Mbonea/WifiBiz/src/utils/tokenSales.js
-
curl -L -o tokenSales.js https://huggingface.co/spaces/Mbonea/WifiBiz/resolve/main/src/utils/tokenSales.js
1.69 kB
| const { generateCode } = require('./generateCode'); | |
| async function createAccessTokenForPayment({ | |
| connection, | |
| paymentId, | |
| clientId, | |
| deviceId, | |
| planId, | |
| durationSeconds, | |
| downLimit, | |
| downUnit, | |
| upLimit, | |
| upUnit, | |
| code: preferredCode = null, | |
| omadaVoucherId = null, | |
| omadaVoucherGroupId = null, | |
| }) { | |
| if (!connection) { | |
| throw new Error('Database connection is required'); | |
| } | |
| let lastError = null; | |
| const attempts = preferredCode ? 1 : 5; | |
| for (let attempt = 0; attempt < attempts; attempt += 1) { | |
| const code = String(preferredCode || generateCode()).toUpperCase(); | |
| try { | |
| const [insertResult] = await connection.execute( | |
| `INSERT INTO access_tokens | |
| (client_id, device_id, plan_id, payment_id, code, | |
| omada_voucher_id, omada_voucher_group_id, duration_seconds, | |
| down_limit, down_unit, up_limit, up_unit, status) | |
| VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'unused')`, | |
| [ | |
| clientId, | |
| deviceId, | |
| planId, | |
| paymentId, | |
| code, | |
| omadaVoucherId, | |
| omadaVoucherGroupId, | |
| durationSeconds, | |
| downLimit, | |
| downUnit, | |
| upLimit, | |
| upUnit, | |
| ] | |
| ); | |
| await connection.execute( | |
| `UPDATE payments SET access_token_code = ? WHERE id = ?`, | |
| [code, paymentId] | |
| ); | |
| return { | |
| tokenId: insertResult.insertId, | |
| code, | |
| }; | |
| } catch (err) { | |
| lastError = err; | |
| if (err.code !== 'ER_DUP_ENTRY') throw err; | |
| } | |
| } | |
| throw lastError || new Error('Failed to generate a unique token code'); | |
| } | |
| module.exports = { createAccessTokenForPayment }; | |