feat: complete admin panel major functionalities including responsive layout, recharts, custom modals, inline editing, and loading states
This commit is contained in:
1 parent
ee041d4e52
commit
a7ebb8d792
33 files changed
+1311
-173
No files matched your search
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AdminController } from './admin.controller';
|
||||
|
||||
describe('AdminController', () => {
|
||||
let controller: AdminController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [AdminController],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<AdminController>(AdminController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,88 @@
|
||||
import { Controller, Get, UseGuards, Param, Put, Body } from '@nestjs/common';
|
||||
import { AdminService } from './admin.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
|
||||
@Controller('admin')
|
||||
export class AdminController {
|
||||
constructor(private readonly adminService: AdminService) {}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('dashboard/stats')
|
||||
async getDashboardStats() {
|
||||
const stats = await this.adminService.getDashboardStats();
|
||||
return {
|
||||
success: true,
|
||||
data: stats
|
||||
};
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('users')
|
||||
async getUsers() {
|
||||
const users = await this.adminService.getUsers();
|
||||
return {
|
||||
success: true,
|
||||
data: users
|
||||
};
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Put('users/:id/role')
|
||||
async updateUserRole(@Param('id') id: string, @Body('role') role: string) {
|
||||
const user = await this.adminService.updateUserRole(id, role);
|
||||
return {
|
||||
success: true,
|
||||
data: user
|
||||
};
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('products')
|
||||
async getProducts() {
|
||||
const products = await this.adminService.getProducts();
|
||||
return {
|
||||
success: true,
|
||||
data: products
|
||||
};
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Put('products/:id')
|
||||
async updateProduct(@Param('id') id: string, @Body() data: any) {
|
||||
const product = await this.adminService.updateProduct(id, data);
|
||||
return {
|
||||
success: true,
|
||||
data: product
|
||||
};
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('products/:id/delete')
|
||||
async deleteProduct(@Param('id') id: string) {
|
||||
await this.adminService.deleteProduct(id);
|
||||
return {
|
||||
success: true,
|
||||
message: 'Product deleted'
|
||||
};
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('orders')
|
||||
async getOrders() {
|
||||
const orders = await this.adminService.getOrders();
|
||||
return {
|
||||
success: true,
|
||||
data: orders
|
||||
};
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Put('orders/:id/status')
|
||||
async updateOrderStatus(@Param('id') id: string, @Body('status') status: string) {
|
||||
const order = await this.adminService.updateOrderStatus(id, status);
|
||||
return {
|
||||
success: true,
|
||||
data: order
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AdminController } from './admin.controller';
|
||||
import { AdminService } from './admin.service';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [AdminController],
|
||||
providers: [AdminService]
|
||||
})
|
||||
export class AdminModule {}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AdminService } from './admin.service';
|
||||
|
||||
describe('AdminService', () => {
|
||||
let service: AdminService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [AdminService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<AdminService>(AdminService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,84 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
export class AdminService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async getDashboardStats() {
|
||||
// total revenue
|
||||
const orders = await this.prisma.order.findMany({
|
||||
where: { status: { not: 'failed' } },
|
||||
select: { totalAmount: true }
|
||||
});
|
||||
|
||||
const revenue = orders.reduce((sum, o) => sum + Number(o.totalAmount), 0);
|
||||
|
||||
// new orders count (last 24 hours or total depending on logic, let's do total processing)
|
||||
const newOrders = await this.prisma.order.count({
|
||||
where: { status: 'processing' }
|
||||
});
|
||||
|
||||
// active users count
|
||||
const users = await this.prisma.user.count();
|
||||
|
||||
return {
|
||||
revenue,
|
||||
newOrders,
|
||||
users,
|
||||
todayVisits: Math.floor(Math.random() * 500) + 100 // mock visits since we don't track analytics
|
||||
};
|
||||
}
|
||||
|
||||
async getUsers() {
|
||||
return this.prisma.user.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
}
|
||||
|
||||
async updateUserRole(id: string, role: string) {
|
||||
return this.prisma.user.update({
|
||||
where: { id },
|
||||
data: { role }
|
||||
});
|
||||
}
|
||||
|
||||
async getProducts() {
|
||||
return this.prisma.product.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
}
|
||||
|
||||
async updateProduct(id: string, data: any) {
|
||||
return this.prisma.product.update({
|
||||
where: { id },
|
||||
data: {
|
||||
packageSize: data.packageSize,
|
||||
priceValue: data.priceValue,
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async deleteProduct(id: string) {
|
||||
return this.prisma.product.delete({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
|
||||
async getOrders() {
|
||||
return this.prisma.order.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
include: {
|
||||
user: true,
|
||||
orderItems: true
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async updateOrderStatus(id: string, status: string) {
|
||||
return this.prisma.order.update({
|
||||
where: { id },
|
||||
data: { status }
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import { SettingsModule } from './settings/settings.module';
|
||||
import { ThrottlerModule, ThrottlerGuard } from '@nestjs/throttler';
|
||||
import { APP_GUARD } from '@nestjs/core';
|
||||
import { MetricsController } from './common/metrics.controller';
|
||||
import { AdminModule } from './admin/admin.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -25,6 +26,7 @@ import { MetricsController } from './common/metrics.controller';
|
||||
ttl: 60000,
|
||||
limit: 100,
|
||||
}]),
|
||||
AdminModule,
|
||||
],
|
||||
controllers: [MetricsController],
|
||||
providers: [
|
||||
|
||||
@@ -91,4 +91,11 @@ export class AuthController {
|
||||
verifyOtp(@Body() verifyOtpDto: VerifyOtpDto) {
|
||||
return this.authService.verifyOtp(verifyOtpDto);
|
||||
}
|
||||
|
||||
@Post('admin-login')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@ApiOperation({ summary: 'ورود ادمین به پنل مدیریت' })
|
||||
adminLogin(@Body() body: any) {
|
||||
return this.authService.adminLogin(body);
|
||||
}
|
||||
}
|
||||
@@ -63,4 +63,18 @@ export class AuthService {
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async adminLogin(body: any) {
|
||||
if (body.email === 'admin@canina-iran.com' && body.password === 'admin123') {
|
||||
const payload = { sub: 'admin-123', email: body.email, role: 'Admin' };
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
user: { id: 'admin-123', email: body.email, role: 'Admin' },
|
||||
accessToken: this.jwtService.sign(payload),
|
||||
},
|
||||
};
|
||||
}
|
||||
throw new BadRequestException({ message: 'ایمیل یا رمز عبور اشتباه است', error: 'INVALID_CREDENTIALS' });
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,10 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
}
|
||||
|
||||
async validate(payload: any) {
|
||||
if (payload.sub === 'admin-123') {
|
||||
return { id: 'admin-123', email: 'admin@canina-iran.com', role: 'ADMIN' };
|
||||
}
|
||||
|
||||
const user = await this.usersService.findById(payload.sub);
|
||||
if (!user) {
|
||||
throw new UnauthorizedException('کاربر یافت نشد');
|
||||
|
||||
Reference in new issue
Block a user