style: apply standard ESLint & Prettier formatting across backend
Deploy Canina / deploy (push) Successful in 4m58s

This commit is contained in:
parsa aghaei committed 2026-07-29 15:41:53 +03:30
1 parent ae636c8b03
commit 62bd8811fa
66 files changed
+1398 -607

No files matched your search

+121 -77
View File
@@ -13,14 +13,14 @@ export class AdminService {
// total revenue
const orders = await this.prisma.order.findMany({
where: { status: { not: 'failed' } },
select: { totalAmount: true }
select: { totalAmount: true },
});
const revenue = orders.reduce((sum, o) => sum + Number(o.totalAmount), 0);
// new orders count (processing status)
const newOrders = await this.prisma.order.count({
where: { status: 'processing' }
where: { status: 'processing' },
});
// active users count
@@ -40,7 +40,7 @@ export class AdminService {
revenue,
newOrders,
users,
todayVisits
todayVisits,
};
}
@@ -55,7 +55,7 @@ export class AdminService {
{ firstName: { contains: query.search, mode: 'insensitive' } },
{ lastName: { contains: query.search, mode: 'insensitive' } },
{ email: { contains: query.search, mode: 'insensitive' } },
{ mobile: { contains: query.search } }
{ mobile: { contains: query.search } },
];
}
if (query.role) {
@@ -67,17 +67,25 @@ export class AdminService {
}
const [data, total] = await Promise.all([
this.prisma.user.findMany({ where, skip, take: limit, orderBy: { createdAt: 'desc' } }),
this.prisma.user.count({ where })
this.prisma.user.findMany({
where,
skip,
take: limit,
orderBy: { createdAt: 'desc' },
}),
this.prisma.user.count({ where }),
]);
return { data, meta: { total, page, limit, lastPage: Math.ceil(total / limit) } };
return {
data,
meta: { total, page, limit, lastPage: Math.ceil(total / limit) },
};
}
async updateUserRole(id: string, role: string) {
return this.prisma.user.update({
where: { id },
data: { role }
data: { role },
});
}
@@ -91,7 +99,7 @@ export class AdminService {
if (query.search) {
where.OR = [
{ name: { contains: query.search, mode: 'insensitive' } },
{ artNo: { contains: query.search } }
{ artNo: { contains: query.search } },
];
}
if (query.categoryId) {
@@ -99,16 +107,22 @@ export class AdminService {
}
const [data, total] = await Promise.all([
this.prisma.product.findMany({
where, skip, take: limit, orderBy: { createdAt: 'desc' },
include: { category: true, symptoms: true }
this.prisma.product.findMany({
where,
skip,
take: limit,
orderBy: { createdAt: 'desc' },
include: { category: true, symptoms: true },
}),
this.prisma.product.count({ where })
this.prisma.product.count({ where }),
]);
return { data, meta: { total, page, limit, lastPage: Math.ceil(total / limit) } };
return {
data,
meta: { total, page, limit, lastPage: Math.ceil(total / limit) },
};
} catch (error) {
console.error("[AdminService] getProducts error:", error);
console.error('[AdminService] getProducts error:', error);
throw new HttpException(error.message || 'Error fetching products', 500);
}
}
@@ -136,15 +150,17 @@ export class AdminService {
keywords: data.keywords,
canonicalUrl: data.canonicalUrl,
slug: data.slug || data.artNo,
}
},
});
if (data.symptoms && Array.isArray(data.symptoms)) {
await this.prisma.productSymptom.createMany({
data: data.symptoms.map((s: string) => ({
productId: product.id,
symptom: s.trim()
})).filter((s: any) => s.symptom.length > 0)
data: data.symptoms
.map((s: string) => ({
productId: product.id,
symptom: s.trim(),
}))
.filter((s: any) => s.symptom.length > 0),
});
}
@@ -175,17 +191,19 @@ export class AdminService {
keywords: data.keywords,
canonicalUrl: data.canonicalUrl,
slug: data.slug || data.artNo,
}
},
});
if (data.symptoms !== undefined && Array.isArray(data.symptoms)) {
await this.prisma.productSymptom.deleteMany({ where: { productId: id } });
if (data.symptoms.length > 0) {
await this.prisma.productSymptom.createMany({
data: data.symptoms.map((s: string) => ({
productId: id,
symptom: s.trim()
})).filter((s: any) => s.symptom.length > 0)
data: data.symptoms
.map((s: string) => ({
productId: id,
symptom: s.trim(),
}))
.filter((s: any) => s.symptom.length > 0),
});
}
}
@@ -209,9 +227,11 @@ export class AdminService {
where.OR = [
{ id: { contains: query.search } },
{ trackingNumber: { contains: query.search, mode: 'insensitive' } },
{ user: { firstName: { contains: query.search, mode: 'insensitive' } } },
{
user: { firstName: { contains: query.search, mode: 'insensitive' } },
},
{ user: { lastName: { contains: query.search, mode: 'insensitive' } } },
{ user: { phone: { contains: query.search } } }
{ user: { phone: { contains: query.search } } },
];
}
if (query.status) {
@@ -219,25 +239,28 @@ export class AdminService {
}
const [data, total] = await Promise.all([
this.prisma.order.findMany({
where,
skip,
take: limit,
this.prisma.order.findMany({
where,
skip,
take: limit,
orderBy: { createdAt: 'desc' },
include: {
user: true,
include: {
user: true,
orderItems: {
include: {
product: true
}
product: true,
},
},
coupon: true
}
coupon: true,
},
}),
this.prisma.order.count({ where })
this.prisma.order.count({ where }),
]);
return { data, meta: { total, page, limit, lastPage: Math.ceil(total / limit) } };
return {
data,
meta: { total, page, limit, lastPage: Math.ceil(total / limit) },
};
}
async updateOrderStatus(id: string, status: string, trackingNumber?: string) {
@@ -252,10 +275,10 @@ export class AdminService {
user: true,
orderItems: {
include: {
product: true
}
}
}
product: true,
},
},
},
});
}
@@ -263,23 +286,30 @@ export class AdminService {
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 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
include: { targets: true }, // Include polymorphic targets
}),
this.prisma.coupon.count({ where })
this.prisma.coupon.count({ where }),
]);
return {
data,
meta: { total, page: Number(page), limit: Number(limit), lastPage: Math.ceil(total / Number(limit)) }
meta: {
total,
page: Number(page),
limit: Number(limit),
lastPage: Math.ceil(total / Number(limit)),
},
};
}
@@ -294,16 +324,19 @@ export class AdminService {
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
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 }
include: { targets: true },
});
}
@@ -322,41 +355,52 @@ export class AdminService {
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
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 }
include: { targets: true },
});
}
async toggleCoupon(id: string, isActive: boolean) {
return this.prisma.coupon.update({
where: { id },
data: { isActive }
data: { isActive },
});
}
async deleteCoupon(id: string) {
return this.prisma.coupon.delete({
where: { id }
where: { id },
});
}
// --- Settings ---
async getSettings() {
const keys = ['SHIPPING_FEE', 'MIN_ORDER_AMOUNT', 'B2B_DISCOUNT_PERCENT', 'MAINTENANCE_MODE'];
const keys = [
'SHIPPING_FEE',
'MIN_ORDER_AMOUNT',
'B2B_DISCOUNT_PERCENT',
'MAINTENANCE_MODE',
];
const settings = await this.prisma.uiText.findMany({
where: { key: { in: keys } }
where: { key: { in: keys } },
});
// Transform to an object { SHIPPING_FEE: '50000', ... }
return settings.reduce((acc, curr) => ({ ...acc, [curr.key]: curr.value }), {});
return settings.reduce(
(acc, curr) => ({ ...acc, [curr.key]: curr.value }),
{},
);
}
async updateSettings(data: Record<string, string>) {
@@ -365,10 +409,10 @@ export class AdminService {
return this.prisma.uiText.upsert({
where: { key },
update: { value: String(value) },
create: { key, value: String(value) }
create: { key, value: String(value) },
});
});
await this.prisma.$transaction(operations);
return this.getSettings();
}