feat: implement advanced reports, pagination for all tables, global settings, and discounts modules

This commit is contained in:
parsa committed 2026-06-12 16:28:06 +03:30
1 parent a7ebb8d792
commit 69502fc380
14 files changed
+1042 -71

No files matched your search

+127 -16
View File
@@ -30,10 +30,34 @@ export class AdminService {
};
}
async getUsers() {
return this.prisma.user.findMany({
orderBy: { createdAt: 'desc' },
});
async getUsers(query: any) {
const page = Number(query.page) || 1;
const limit = Number(query.limit) || 10;
const skip = (page - 1) * limit;
const where: any = {};
if (query.search) {
where.OR = [
{ firstName: { contains: query.search, mode: 'insensitive' } },
{ lastName: { contains: query.search, mode: 'insensitive' } },
{ email: { contains: query.search, mode: 'insensitive' } },
{ mobile: { contains: query.search } }
];
}
if (query.role) {
if (query.role === 'B2B') {
where.role = { contains: 'B2B' };
} else {
where.role = query.role;
}
}
const [data, total] = await Promise.all([
this.prisma.user.findMany({ where, skip, take: limit, orderBy: { createdAt: 'desc' } }),
this.prisma.user.count({ where })
]);
return { data, meta: { total, page, limit, totalPages: Math.ceil(total / limit) } };
}
async updateUserRole(id: string, role: string) {
@@ -43,10 +67,28 @@ export class AdminService {
});
}
async getProducts() {
return this.prisma.product.findMany({
orderBy: { createdAt: 'desc' },
});
async getProducts(query: any) {
const page = Number(query.page) || 1;
const limit = Number(query.limit) || 10;
const skip = (page - 1) * limit;
const where: any = {};
if (query.search) {
where.OR = [
{ name: { contains: query.search, mode: 'insensitive' } },
{ artNo: { contains: query.search } }
];
}
if (query.category) {
where.category = query.category;
}
const [data, total] = await Promise.all([
this.prisma.product.findMany({ where, skip, take: limit, orderBy: { createdAt: 'desc' } }),
this.prisma.product.count({ where })
]);
return { data, meta: { total, page, limit, totalPages: Math.ceil(total / limit) } };
}
async updateProduct(id: string, data: any) {
@@ -65,14 +107,28 @@ export class AdminService {
});
}
async getOrders() {
return this.prisma.order.findMany({
orderBy: { createdAt: 'desc' },
include: {
user: true,
orderItems: true
}
});
async getOrders(query: any) {
const page = Number(query.page) || 1;
const limit = Number(query.limit) || 10;
const skip = (page - 1) * limit;
const where: any = {};
if (query.search) {
where.id = { contains: query.search };
}
if (query.status) {
where.status = query.status;
}
const [data, total] = await Promise.all([
this.prisma.order.findMany({
where, skip, take: limit, orderBy: { createdAt: 'desc' },
include: { user: true, orderItems: true }
}),
this.prisma.order.count({ where })
]);
return { data, meta: { total, page, limit, totalPages: Math.ceil(total / limit) } };
}
async updateOrderStatus(id: string, status: string) {
@@ -81,4 +137,59 @@ export class AdminService {
data: { status }
});
}
// --- Coupons ---
async getCoupons() {
return this.prisma.coupon.findMany({
orderBy: { createdAt: 'desc' },
include: { _count: { select: { orders: true } } }
});
}
async createCoupon(data: { code: string; discountValue: number }) {
return this.prisma.coupon.create({
data: {
code: data.code,
discountValue: data.discountValue,
}
});
}
async toggleCoupon(id: string, isActive: boolean) {
return this.prisma.coupon.update({
where: { id },
data: { isActive }
});
}
async deleteCoupon(id: string) {
return this.prisma.coupon.delete({
where: { id }
});
}
// --- Settings ---
async getSettings() {
const keys = ['SHIPPING_FEE', 'MIN_ORDER_AMOUNT', 'B2B_DISCOUNT_PERCENT', 'MAINTENANCE_MODE'];
const settings = await this.prisma.uiText.findMany({
where: { key: { in: keys } }
});
// Transform to an object { SHIPPING_FEE: '50000', ... }
return settings.reduce((acc, curr) => ({ ...acc, [curr.key]: curr.value }), {});
}
async updateSettings(data: Record<string, string>) {
// Upsert all keys
const operations = Object.entries(data).map(([key, value]) => {
return this.prisma.uiText.upsert({
where: { key },
update: { value: String(value) },
create: { key, value: String(value) }
});
});
await this.prisma.$transaction(operations);
return this.getSettings();
}
}