MicProtocol / apps /api /dist /admin /admin-billing.service.js
Benaah's picture
Upload folder using huggingface_hub
303c85f verified
Raw History Blame Contribute Delete
7.14 kB
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var AdminBillingService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdminBillingService = void 0;
const common_1 = require("@nestjs/common");
const prisma_service_1 = require("../prisma/prisma.service");
const audit_service_1 = require("../audit/audit.service");
const client_1 = require("@prisma/client");
const shared_types_1 = require("@micprotocol/shared-types");
const admin_permission_decorator_1 = require("../common/decorators/admin-permission.decorator");
let AdminBillingService = AdminBillingService_1 = class AdminBillingService {
prisma;
audit;
logger = new common_1.Logger(AdminBillingService_1.name);
constructor(prisma, audit) {
this.prisma = prisma;
this.audit = audit;
}
async listSubscriptions() {
const workspaces = await this.prisma.workspace.findMany({
orderBy: { name: "asc" },
select: {
id: true,
name: true,
slug: true,
subscriptionPlanId: true,
subscriptionStatus: true,
suspendedAt: true,
},
});
return workspaces.map((w) => ({
workspaceId: w.id,
workspaceName: w.name,
slug: w.slug,
plan: w.subscriptionPlanId,
status: w.subscriptionStatus,
suspended: !!w.suspendedAt,
}));
}
async getWallet(workspaceId) {
const workspace = await this.prisma.workspace.findUnique({
where: { id: workspaceId },
select: { id: true, name: true },
});
if (!workspace)
throw new common_1.NotFoundException(`Workspace ${workspaceId} not found`);
const wallet = await this.prisma.wallet.findUnique({ where: { workspaceId } });
if (!wallet)
return null;
return {
workspaceId: wallet.workspaceId,
workspaceName: workspace.name,
balance: wallet.balance.toNumber(),
currency: wallet.currency,
updatedAt: wallet.updatedAt,
};
}
async listWallets() {
const wallets = await this.prisma.wallet.findMany({
orderBy: { updatedAt: "desc" },
include: { workspace: { select: { name: true } } },
});
return wallets.map((w) => ({
workspaceId: w.workspaceId,
workspaceName: w.workspace.name,
balance: w.balance.toNumber(),
currency: w.currency,
updatedAt: w.updatedAt,
}));
}
async listTransactions(walletId, limit = 50) {
return this.prisma.walletTransaction.findMany({
where: { walletId },
orderBy: { createdAt: "desc" },
take: Math.min(limit, 200),
});
}
async refund(workspaceId, amount, reference, description, actorUserId) {
if (amount <= 0)
throw new common_1.BadRequestException("Refund amount must be positive");
const workspace = await this.prisma.workspace.findUnique({
where: { id: workspaceId },
select: { id: true, name: true },
});
if (!workspace)
throw new common_1.NotFoundException(`Workspace ${workspaceId} not found`);
let wallet = await this.prisma.wallet.findUnique({ where: { workspaceId } });
if (!wallet) {
wallet = await this.prisma.wallet.create({
data: { workspaceId, balance: new client_1.Prisma.Decimal(0), currency: "USD" },
});
}
const transaction = await this.prisma.walletTransaction.create({
data: {
walletId: wallet.id,
type: "refund",
status: "completed",
amount: new client_1.Prisma.Decimal(amount),
currency: wallet.currency,
reference,
description,
},
});
await this.audit.log({
userId: actorUserId,
action: "admin.billing.refund",
entityType: "WalletTransaction",
entityId: transaction.id,
metadata: {
workspaceId,
workspaceName: workspace.name,
amount,
currency: wallet.currency,
reference,
description,
},
});
return transaction;
}
async planOverride(workspaceId, planId, actorUserId) {
const workspace = await this.prisma.workspace.findUnique({
where: { id: workspaceId },
select: { id: true, name: true, subscriptionPlanId: true },
});
if (!workspace)
throw new common_1.NotFoundException(`Workspace ${workspaceId} not found`);
const updated = await this.prisma.workspace.update({
where: { id: workspaceId },
data: { subscriptionPlanId: planId },
});
await this.audit.log({
userId: actorUserId,
action: "admin.billing.plan_override",
entityType: "Workspace",
entityId: workspaceId,
metadata: {
before: { subscriptionPlanId: workspace.subscriptionPlanId },
after: { subscriptionPlanId: planId },
},
});
return updated;
}
};
exports.AdminBillingService = AdminBillingService;
__decorate([
(0, admin_permission_decorator_1.requireAdminPermission)(shared_types_1.AdminPermission.BILLING_REFUND),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String, Number, String, Object, String]),
__metadata("design:returntype", Promise)
], AdminBillingService.prototype, "refund", null);
__decorate([
(0, admin_permission_decorator_1.requireAdminPermission)(shared_types_1.AdminPermission.BILLING_PLAN_OVERRIDE),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String, String, String]),
__metadata("design:returntype", Promise)
], AdminBillingService.prototype, "planOverride", null);
exports.AdminBillingService = AdminBillingService = AdminBillingService_1 = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [prisma_service_1.PrismaService,
audit_service_1.AuditService])
], AdminBillingService);
//# sourceMappingURL=admin-billing.service.js.map