import { jsonError, jsonOk } from "@/lib/server/api"; import { requireSuperAdminSession } from "@/lib/server/auth/current-user"; import { listPlatformOrganizations, listPlatformUsers } from "@/lib/server/account-store"; import { listBillingLedgerEntries, listBillingPriceRules, listOrganizationWallets } from "@/lib/server/billing-store"; import { ensureDefaultBillingPriceRules } from "@/lib/server/billing-catalog"; import { getBillingAccountConfig } from "@/lib/server/billing-service"; export const runtime = "nodejs"; export const dynamic = "force-dynamic"; export async function GET() { try { await requireSuperAdminSession(); await ensureDefaultBillingPriceRules(); const [organizations, wallets, members, ledger, priceRules] = await Promise.all([ listPlatformOrganizations({ includeDisabled: true }), listOrganizationWallets(), listPlatformUsers({ includeDisabled: true }), listBillingLedgerEntries({ limit: 500 }), listBillingPriceRules({ includeDisabled: true }) ]); const walletByOrganization = new Map(wallets.map((wallet) => [wallet.organizationId, wallet])); return jsonOk({ billingAccount: getBillingAccountConfig(), organizations: organizations.map((organization) => ({ ...organization, wallet: walletByOrganization.get(organization.id) || { organizationId: organization.id, balanceFen: 0, totalRechargedFen: 0, totalChargedFen: 0, updatedAt: organization.updatedAt } })), members: members.map((member) => ({ id: member.id, displayName: member.displayName, phone: member.phone, role: member.role, organizationId: member.organizationId || null, status: member.status })), ledger, priceRules }); } catch (error) { return jsonError(error, 500, { source: "api.admin.billing" }); } }