Initial SMM panel implementation
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
const { getDb } = require('../config/db');
|
||||
const { formatMoney, parseMoney } = require('../utils/money');
|
||||
|
||||
function calculateProviderCost(providerPrice, quantity) {
|
||||
return (parseMoney(String(providerPrice)) * BigInt(quantity) + 500n) / 1000n;
|
||||
}
|
||||
|
||||
async function listAdminUsers() {
|
||||
return getDb()('users')
|
||||
.select('id', 'username', 'email', 'role', 'status', 'balance', 'created_at', 'updated_at')
|
||||
.orderBy('created_at', 'desc')
|
||||
.orderBy('id', 'desc');
|
||||
}
|
||||
|
||||
async function getAdminStats() {
|
||||
const db = getDb();
|
||||
const [[usersTotal], [activeUsers], [pendingDeposits], [pendingOrders], orders] = await Promise.all([
|
||||
db('users').count({ count: '*' }),
|
||||
db('users').where({ status: 'active' }).count({ count: '*' }),
|
||||
db('deposits').where({ status: 'pending' }).count({ count: '*' }),
|
||||
db('orders').whereIn('status', ['pending', 'processing', 'in_progress']).count({ count: '*' }),
|
||||
db('orders')
|
||||
.join('services', 'services.id', 'orders.service_id')
|
||||
.select('orders.charge', 'orders.quantity', 'services.provider_price', 'orders.status')
|
||||
]);
|
||||
let grossSales = 0n;
|
||||
let providerCost = 0n;
|
||||
|
||||
for (const order of orders) {
|
||||
if (!['processing', 'in_progress', 'completed', 'partial'].includes(order.status)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
grossSales += parseMoney(order.charge);
|
||||
providerCost += calculateProviderCost(order.provider_price, order.quantity);
|
||||
}
|
||||
|
||||
return {
|
||||
users_total: Number(usersTotal.count),
|
||||
users_active: Number(activeUsers.count),
|
||||
deposits_pending: Number(pendingDeposits.count),
|
||||
orders_pending: Number(pendingOrders.count),
|
||||
gross_sales: formatMoney(grossSales),
|
||||
provider_cost: formatMoney(providerCost),
|
||||
profit: formatMoney(grossSales - providerCost)
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getAdminStats,
|
||||
listAdminUsers
|
||||
};
|
||||
Reference in New Issue
Block a user