Fix Admin panel categories modal, add error handling, enforce cursor-pointer globally
This commit is contained in:
1 parent
69502fc380
commit
82ab454647
47 files changed
+2968
-653
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, HttpException } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
@@ -68,35 +68,86 @@ export class AdminService {
|
||||
}
|
||||
|
||||
async getProducts(query: any) {
|
||||
const page = Number(query.page) || 1;
|
||||
const limit = Number(query.limit) || 10;
|
||||
const skip = (page - 1) * limit;
|
||||
try {
|
||||
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 where: any = {};
|
||||
if (query.search) {
|
||||
where.OR = [
|
||||
{ name: { contains: query.search, mode: 'insensitive' } },
|
||||
{ artNo: { contains: query.search } }
|
||||
];
|
||||
}
|
||||
if (query.categoryId) {
|
||||
where.categoryId = query.categoryId;
|
||||
}
|
||||
|
||||
const [data, total] = await Promise.all([
|
||||
this.prisma.product.findMany({
|
||||
where, skip, take: limit, orderBy: { createdAt: 'desc' },
|
||||
include: { category: true }
|
||||
}),
|
||||
this.prisma.product.count({ where })
|
||||
]);
|
||||
|
||||
return { data, meta: { total, page, limit, totalPages: Math.ceil(total / limit) } };
|
||||
} catch (error) {
|
||||
console.error("[AdminService] getProducts error:", error);
|
||||
throw new HttpException(error.message || 'Error fetching products', 500);
|
||||
}
|
||||
}
|
||||
|
||||
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 createProduct(data: any) {
|
||||
return this.prisma.product.create({
|
||||
data: {
|
||||
artNo: data.artNo,
|
||||
name: data.name,
|
||||
scientificTagline: data.scientificTagline,
|
||||
description: data.description,
|
||||
shortDescription: data.shortDescription,
|
||||
categoryId: data.categoryId,
|
||||
categorySlug: data.categorySlug || 'general',
|
||||
priceValue: data.priceValue,
|
||||
priceDisplay: data.priceDisplay,
|
||||
unit: data.unit,
|
||||
packageSize: data.packageSize,
|
||||
dosageLogic: data.dosageLogic,
|
||||
suitableFor: data.suitableFor,
|
||||
imageUrl: data.imageUrl,
|
||||
metaTitle: data.metaTitle,
|
||||
metaDescription: data.metaDescription,
|
||||
keywords: data.keywords,
|
||||
canonicalUrl: data.canonicalUrl,
|
||||
slug: data.slug || data.artNo,
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async updateProduct(id: string, data: any) {
|
||||
return this.prisma.product.update({
|
||||
where: { id },
|
||||
data: {
|
||||
packageSize: data.packageSize,
|
||||
artNo: data.artNo,
|
||||
name: data.name,
|
||||
scientificTagline: data.scientificTagline,
|
||||
description: data.description,
|
||||
shortDescription: data.shortDescription,
|
||||
categoryId: data.categoryId,
|
||||
categorySlug: data.categorySlug,
|
||||
priceValue: data.priceValue,
|
||||
priceDisplay: data.priceDisplay,
|
||||
unit: data.unit,
|
||||
packageSize: data.packageSize,
|
||||
dosageLogic: data.dosageLogic,
|
||||
suitableFor: data.suitableFor,
|
||||
imageUrl: data.imageUrl,
|
||||
metaTitle: data.metaTitle,
|
||||
metaDescription: data.metaDescription,
|
||||
keywords: data.keywords,
|
||||
canonicalUrl: data.canonicalUrl,
|
||||
slug: data.slug || data.artNo,
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -138,20 +189,79 @@ export class AdminService {
|
||||
});
|
||||
}
|
||||
|
||||
// --- Coupons ---
|
||||
async getCoupons() {
|
||||
return this.prisma.coupon.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
include: { _count: { select: { orders: true } } }
|
||||
});
|
||||
// --- Coupons Engine ---
|
||||
async getCoupons(query: any) {
|
||||
const { page = 1, limit = 10, search = '' } = query;
|
||||
const skip = (Number(page) - 1) * Number(limit);
|
||||
|
||||
const where = search ? { code: { contains: search, mode: 'insensitive' as any } } : {};
|
||||
|
||||
const [data, total] = await Promise.all([
|
||||
this.prisma.coupon.findMany({
|
||||
where,
|
||||
skip,
|
||||
take: Number(limit),
|
||||
orderBy: { createdAt: 'desc' },
|
||||
include: { targets: true } // Include polymorphic targets
|
||||
}),
|
||||
this.prisma.coupon.count({ where })
|
||||
]);
|
||||
|
||||
return {
|
||||
data,
|
||||
meta: { total, page: Number(page), limit: Number(limit), totalPages: Math.ceil(total / Number(limit)) }
|
||||
};
|
||||
}
|
||||
|
||||
async createCoupon(data: { code: string; discountValue: number }) {
|
||||
async createCoupon(data: any) {
|
||||
return this.prisma.coupon.create({
|
||||
data: {
|
||||
code: data.code,
|
||||
discountValue: data.discountValue,
|
||||
}
|
||||
type: data.type || 'percent',
|
||||
value: data.value,
|
||||
minCartValue: data.minCartValue || null,
|
||||
maxCartValue: data.maxCartValue || null,
|
||||
maxUses: data.maxUses || null,
|
||||
expiresAt: data.expiresAt ? new Date(data.expiresAt) : null,
|
||||
isActive: data.isActive !== undefined ? data.isActive : true,
|
||||
targets: data.targets && data.targets.length > 0 ? {
|
||||
create: data.targets.map((t: any) => ({
|
||||
targetType: t.targetType,
|
||||
targetId: t.targetId,
|
||||
modifierType: t.modifierType || 'override',
|
||||
modifierValue: t.modifierValue || null
|
||||
}))
|
||||
} : undefined
|
||||
},
|
||||
include: { targets: true }
|
||||
});
|
||||
}
|
||||
|
||||
async updateCoupon(id: string, data: any) {
|
||||
// Delete old targets and recreate them
|
||||
await this.prisma.couponTarget.deleteMany({ where: { couponId: id } });
|
||||
|
||||
return this.prisma.coupon.update({
|
||||
where: { id },
|
||||
data: {
|
||||
code: data.code,
|
||||
type: data.type,
|
||||
value: data.value,
|
||||
minCartValue: data.minCartValue,
|
||||
maxCartValue: data.maxCartValue,
|
||||
maxUses: data.maxUses,
|
||||
expiresAt: data.expiresAt ? new Date(data.expiresAt) : null,
|
||||
isActive: data.isActive,
|
||||
targets: data.targets && data.targets.length > 0 ? {
|
||||
create: data.targets.map((t: any) => ({
|
||||
targetType: t.targetType,
|
||||
targetId: t.targetId,
|
||||
modifierType: t.modifierType || 'override',
|
||||
modifierValue: t.modifierValue || null
|
||||
}))
|
||||
} : undefined
|
||||
},
|
||||
include: { targets: true }
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user