Files
tabungin/apps/api/dist/admin/admin-users.service.js
dwindown 12850ab12d feat: complete admin backend controllers and services
- AdminPlansController & Service (CRUD, reorder)
- AdminPaymentMethodsController & Service (CRUD, reorder)
- AdminPaymentsController & Service (verify, reject, pending count)
- AdminUsersController & Service (search, suspend, grant pro access, stats)
- AdminConfigController & Service (dynamic config management)
- Wire all controllers into AdminModule
- Import AdminModule in AppModule

Admin API Routes:
- GET/POST/PUT/DELETE /admin/plans
- GET/POST/PUT/DELETE /admin/payment-methods
- GET /admin/payments (with status filter)
- POST /admin/payments/:id/verify
- POST /admin/payments/:id/reject
- GET /admin/users (with search)
- POST /admin/users/:id/grant-pro
- GET/POST/DELETE /admin/config

All routes protected by AuthGuard + AdminGuard
2025-10-11 14:32:45 +07:00

148 lines
4.8 KiB
JavaScript

"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdminUsersService = void 0;
const common_1 = require("@nestjs/common");
const prisma_service_1 = require("../prisma/prisma.service");
let AdminUsersService = class AdminUsersService {
prisma;
constructor(prisma) {
this.prisma = prisma;
}
async findAll(search) {
return this.prisma.user.findMany({
where: search
? {
OR: [
{ email: { contains: search, mode: 'insensitive' } },
{ name: { contains: search, mode: 'insensitive' } },
],
}
: undefined,
select: {
id: true,
email: true,
name: true,
role: true,
emailVerified: true,
createdAt: true,
lastLoginAt: true,
suspendedAt: true,
_count: {
select: {
wallets: true,
transactions: true,
},
},
},
orderBy: { createdAt: 'desc' },
});
}
async findOne(id) {
return this.prisma.user.findUnique({
where: { id },
include: {
subscriptions: {
include: {
plan: true,
},
},
_count: {
select: {
wallets: true,
transactions: true,
payments: true,
},
},
},
});
}
async updateRole(id, role) {
return this.prisma.user.update({
where: { id },
data: { role },
});
}
async suspend(id, reason) {
return this.prisma.user.update({
where: { id },
data: {
suspendedAt: new Date(),
suspendedReason: reason,
},
});
}
async unsuspend(id) {
return this.prisma.user.update({
where: { id },
data: {
suspendedAt: null,
suspendedReason: null,
},
});
}
async grantProAccess(userId, planSlug, durationDays) {
const plan = await this.prisma.plan.findUnique({
where: { slug: planSlug },
});
if (!plan) {
throw new Error('Plan not found');
}
const now = new Date();
const endDate = new Date(now);
endDate.setDate(endDate.getDate() + durationDays);
const existing = await this.prisma.subscription.findUnique({
where: { userId },
});
if (existing) {
return this.prisma.subscription.update({
where: { userId },
data: {
planId: plan.id,
status: 'active',
startDate: now,
endDate,
},
});
}
else {
return this.prisma.subscription.create({
data: {
userId,
planId: plan.id,
status: 'active',
startDate: now,
endDate,
},
});
}
}
async getStats() {
const totalUsers = await this.prisma.user.count();
const activeSubscriptions = await this.prisma.subscription.count({
where: { status: 'active' },
});
const suspendedUsers = await this.prisma.user.count({
where: { suspendedAt: { not: null } },
});
return {
totalUsers,
activeSubscriptions,
suspendedUsers,
};
}
};
exports.AdminUsersService = AdminUsersService;
exports.AdminUsersService = AdminUsersService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [prisma_service_1.PrismaService])
], AdminUsersService);
//# sourceMappingURL=admin-users.service.js.map