chore(workspace): eradicate all tsc errors and eslint warnings across frontend, admin, and backend
Deploy Canina / deploy (push) Failing after 29s
Deploy Canina / deploy (push) Failing after 29s
This commit is contained in:
1 parent
4659a7b1de
commit
9f11d8fb2d
98 files changed
+1272
-1009
No files matched your search
+161
-105
@@ -1,7 +1,64 @@
|
||||
import { Injectable, HttpException, NotFoundException } from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { RedisService } from '../redis/redis.service';
|
||||
|
||||
export class PaginationQuery {
|
||||
page?: number | string;
|
||||
limit?: number | string;
|
||||
search?: string;
|
||||
role?: string;
|
||||
categoryId?: string;
|
||||
status?: string;
|
||||
}
|
||||
|
||||
export class ProductInput {
|
||||
artNo?: string;
|
||||
nameFa?: string;
|
||||
nameEn?: string;
|
||||
scientificTagline?: string;
|
||||
description?: string;
|
||||
shortDescription?: string;
|
||||
categoryId?: string;
|
||||
categorySlug?: string;
|
||||
priceValue?: number;
|
||||
priceDisplay?: string;
|
||||
unit?: string;
|
||||
packageSize?: number;
|
||||
dosageLogic?: string;
|
||||
suitableFor?: string;
|
||||
imageUrl?: string;
|
||||
images?: string | string[];
|
||||
podcastUrl?: string | null;
|
||||
videoUrl?: string | null;
|
||||
pdfUrl?: string | null;
|
||||
metaTitle?: string;
|
||||
metaDescription?: string;
|
||||
keywords?: string;
|
||||
canonicalUrl?: string;
|
||||
slug?: string;
|
||||
symptoms?: string[];
|
||||
}
|
||||
|
||||
export class CouponTargetInput {
|
||||
targetType!: string;
|
||||
targetId!: string;
|
||||
modifierType?: string;
|
||||
modifierValue?: number;
|
||||
}
|
||||
|
||||
export class CouponInput {
|
||||
code!: string;
|
||||
type?: string;
|
||||
value!: number;
|
||||
minCartValue?: number;
|
||||
maxCartValue?: number;
|
||||
maxUses?: number;
|
||||
expiresAt?: string | Date;
|
||||
isActive?: boolean;
|
||||
targets?: CouponTargetInput[];
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class AdminService {
|
||||
constructor(
|
||||
@@ -10,7 +67,6 @@ export class AdminService {
|
||||
) {}
|
||||
|
||||
async getDashboardStats() {
|
||||
// total revenue
|
||||
const orders = await this.prisma.order.findMany({
|
||||
where: { status: { not: 'failed' } },
|
||||
select: { totalAmount: true },
|
||||
@@ -18,15 +74,12 @@ export class AdminService {
|
||||
|
||||
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' },
|
||||
});
|
||||
|
||||
// active users count
|
||||
const users = await this.prisma.user.count();
|
||||
|
||||
// Today's request count from Redis counter (set by MetricsController)
|
||||
const todayKey = `visits:${new Date().toISOString().split('T')[0]}`;
|
||||
let todayVisits = 0;
|
||||
try {
|
||||
@@ -57,12 +110,12 @@ export class AdminService {
|
||||
};
|
||||
}
|
||||
|
||||
async getUsers(query: any) {
|
||||
async getUsers(query: PaginationQuery = {}) {
|
||||
const page = Number(query.page) || 1;
|
||||
const limit = Number(query.limit) || 10;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const where: any = {};
|
||||
const where: Prisma.UserWhereInput = {};
|
||||
if (query.search) {
|
||||
where.OR = [
|
||||
{ firstName: { contains: query.search, mode: 'insensitive' } },
|
||||
@@ -102,51 +155,18 @@ export class AdminService {
|
||||
};
|
||||
}
|
||||
|
||||
async adjustUserWallet(
|
||||
userId: string,
|
||||
amount: number,
|
||||
type: 'deposit' | 'withdrawal' | 'refund',
|
||||
description?: string,
|
||||
) {
|
||||
const user = await this.prisma.user.findUnique({ where: { id: userId } });
|
||||
if (!user) {
|
||||
throw new NotFoundException('کاربر مورد نظر یافت نشد');
|
||||
}
|
||||
|
||||
const adjustAmount =
|
||||
type === 'withdrawal' ? -Math.abs(amount) : Math.abs(amount);
|
||||
const currentBalance = Number(user.walletBalance || 0);
|
||||
|
||||
if (adjustAmount < 0 && currentBalance + adjustAmount < 0) {
|
||||
throw new HttpException(
|
||||
'موجودی کیف پول کاربر برای کسر این مبلغ کافی نیست',
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
||||
return this.prisma.$transaction([
|
||||
this.prisma.walletTransaction.create({
|
||||
data: {
|
||||
userId,
|
||||
amount: Math.abs(amount),
|
||||
type: type === 'withdrawal' ? 'withdrawal' : 'deposit',
|
||||
status: 'completed',
|
||||
description:
|
||||
description ||
|
||||
(type === 'refund'
|
||||
? 'بازگشت وجه توسط ادمین'
|
||||
: type === 'deposit'
|
||||
? 'شارژ توسط ادمین'
|
||||
: 'کسر توسط ادمین'),
|
||||
},
|
||||
}),
|
||||
this.prisma.user.update({
|
||||
where: { id: userId },
|
||||
data: {
|
||||
walletBalance: { increment: adjustAmount },
|
||||
},
|
||||
}),
|
||||
]);
|
||||
async getUserDetails(id: string) {
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
addresses: true,
|
||||
pets: true,
|
||||
orders: { orderBy: { createdAt: 'desc' }, take: 10 },
|
||||
walletTransactions: { orderBy: { createdAt: 'desc' }, take: 20 },
|
||||
},
|
||||
});
|
||||
if (!user) throw new NotFoundException(`کاربری با شناسه ${id} یافت نشد.`);
|
||||
return user;
|
||||
}
|
||||
|
||||
async updateUserRole(id: string, role: string) {
|
||||
@@ -156,16 +176,16 @@ export class AdminService {
|
||||
});
|
||||
}
|
||||
|
||||
async getProducts(query: any) {
|
||||
async getProducts(query: PaginationQuery = {}) {
|
||||
try {
|
||||
const page = Number(query.page) || 1;
|
||||
const limit = Number(query.limit) || 10;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const where: any = {};
|
||||
const where: Prisma.ProductWhereInput = {};
|
||||
if (query.search) {
|
||||
where.OR = [
|
||||
{ name: { contains: query.search, mode: 'insensitive' } },
|
||||
{ nameFa: { contains: query.search, mode: 'insensitive' } },
|
||||
{ artNo: { contains: query.search } },
|
||||
];
|
||||
}
|
||||
@@ -189,29 +209,30 @@ export class AdminService {
|
||||
meta: { total, page, limit, lastPage: Math.ceil(total / limit) },
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('[AdminService] getProducts error:', error);
|
||||
throw new HttpException(error.message || 'Error fetching products', 500);
|
||||
const err = error as { message?: string };
|
||||
console.error('[AdminService] getProducts error:', err);
|
||||
throw new HttpException(err.message || 'Error fetching products', 500);
|
||||
}
|
||||
}
|
||||
|
||||
async createProduct(data: any) {
|
||||
async createProduct(data: ProductInput) {
|
||||
const product = await this.prisma.product.create({
|
||||
data: {
|
||||
artNo: data.artNo,
|
||||
nameFa: data.nameFa,
|
||||
nameEn: data.nameEn,
|
||||
scientificTagline: data.scientificTagline,
|
||||
description: data.description,
|
||||
shortDescription: data.shortDescription,
|
||||
categoryId: data.categoryId,
|
||||
artNo: data.artNo || `ART-${Date.now()}`,
|
||||
nameFa: data.nameFa || '',
|
||||
nameEn: data.nameEn || '',
|
||||
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,
|
||||
priceValue: data.priceValue || 0,
|
||||
priceDisplay: data.priceDisplay || '',
|
||||
unit: data.unit || 'عدد',
|
||||
packageSize: data.packageSize || 100,
|
||||
dosageLogic: data.dosageLogic || '',
|
||||
suitableFor: data.suitableFor || 'سگ',
|
||||
imageUrl: data.imageUrl || '',
|
||||
images: Array.isArray(data.images)
|
||||
? data.images
|
||||
: data.images
|
||||
@@ -220,11 +241,11 @@ export class AdminService {
|
||||
podcastUrl: data.podcastUrl || null,
|
||||
videoUrl: data.videoUrl || null,
|
||||
pdfUrl: data.pdfUrl || null,
|
||||
metaTitle: data.metaTitle,
|
||||
metaDescription: data.metaDescription,
|
||||
keywords: data.keywords,
|
||||
canonicalUrl: data.canonicalUrl,
|
||||
slug: data.slug || data.artNo,
|
||||
metaTitle: data.metaTitle || '',
|
||||
metaDescription: data.metaDescription || '',
|
||||
keywords: data.keywords || '',
|
||||
canonicalUrl: data.canonicalUrl || '',
|
||||
slug: data.slug || data.artNo || `slug-${Date.now()}`,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -235,14 +256,14 @@ export class AdminService {
|
||||
productId: product.id,
|
||||
symptom: s.trim(),
|
||||
}))
|
||||
.filter((s: any) => s.symptom.length > 0),
|
||||
.filter((s: { symptom: string }) => s.symptom.length > 0),
|
||||
});
|
||||
}
|
||||
|
||||
return product;
|
||||
}
|
||||
|
||||
async updateProduct(id: string, data: any) {
|
||||
async updateProduct(id: string, data: ProductInput) {
|
||||
const existing = await this.prisma.product.findUnique({ where: { id } });
|
||||
if (!existing) {
|
||||
throw new NotFoundException(`محصولی با شناسه ${id} یافت نشد.`);
|
||||
@@ -266,7 +287,11 @@ export class AdminService {
|
||||
dosageLogic: data.dosageLogic,
|
||||
suitableFor: data.suitableFor,
|
||||
imageUrl: data.imageUrl,
|
||||
images: Array.isArray(data.images) ? data.images : undefined,
|
||||
images: Array.isArray(data.images)
|
||||
? data.images
|
||||
: data.images
|
||||
? [data.images]
|
||||
: undefined,
|
||||
podcastUrl: data.podcastUrl !== undefined ? data.podcastUrl : undefined,
|
||||
videoUrl: data.videoUrl !== undefined ? data.videoUrl : undefined,
|
||||
pdfUrl: data.pdfUrl !== undefined ? data.pdfUrl : undefined,
|
||||
@@ -287,7 +312,7 @@ export class AdminService {
|
||||
productId: id,
|
||||
symptom: s.trim(),
|
||||
}))
|
||||
.filter((s: any) => s.symptom.length > 0),
|
||||
.filter((s: { symptom: string }) => s.symptom.length > 0),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -301,21 +326,20 @@ export class AdminService {
|
||||
});
|
||||
}
|
||||
|
||||
async getOrders(query: any) {
|
||||
async getOrders(query: PaginationQuery = {}) {
|
||||
const page = Number(query.page) || 1;
|
||||
const limit = Number(query.limit) || 10;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const where: any = {};
|
||||
const where: Prisma.OrderWhereInput = {};
|
||||
if (query.search) {
|
||||
where.OR = [
|
||||
{ id: { contains: query.search } },
|
||||
{ trackingNumber: { 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: { mobile: { contains: query.search } } },
|
||||
];
|
||||
}
|
||||
if (query.status) {
|
||||
@@ -348,7 +372,7 @@ export class AdminService {
|
||||
}
|
||||
|
||||
async updateOrderStatus(id: string, status: string, trackingNumber?: string) {
|
||||
const dataToUpdate: any = { status };
|
||||
const dataToUpdate: Prisma.OrderUpdateInput = { status };
|
||||
if (trackingNumber !== undefined) {
|
||||
dataToUpdate.trackingNumber = trackingNumber;
|
||||
}
|
||||
@@ -366,22 +390,22 @@ export class AdminService {
|
||||
});
|
||||
}
|
||||
|
||||
// --- Coupons Engine ---
|
||||
async getCoupons(query: any) {
|
||||
const { page = 1, limit = 10, search = '' } = query;
|
||||
const skip = (Number(page) - 1) * Number(limit);
|
||||
async getCoupons(query: PaginationQuery = {}) {
|
||||
const page = Number(query.page) || 1;
|
||||
const limit = Number(query.limit) || 10;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const where = search
|
||||
? { code: { contains: search, mode: 'insensitive' as any } }
|
||||
const where: Prisma.CouponWhereInput = query.search
|
||||
? { code: { contains: query.search, mode: 'insensitive' } }
|
||||
: {};
|
||||
|
||||
const [data, total] = await Promise.all([
|
||||
this.prisma.coupon.findMany({
|
||||
where,
|
||||
skip,
|
||||
take: Number(limit),
|
||||
take: limit,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
include: { targets: true }, // Include polymorphic targets
|
||||
include: { targets: true },
|
||||
}),
|
||||
this.prisma.coupon.count({ where }),
|
||||
]);
|
||||
@@ -390,14 +414,14 @@ export class AdminService {
|
||||
data,
|
||||
meta: {
|
||||
total,
|
||||
page: Number(page),
|
||||
limit: Number(limit),
|
||||
lastPage: Math.ceil(total / Number(limit)),
|
||||
page,
|
||||
limit,
|
||||
lastPage: Math.ceil(total / limit),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async createCoupon(data: any) {
|
||||
async createCoupon(data: CouponInput) {
|
||||
return this.prisma.coupon.create({
|
||||
data: {
|
||||
code: data.code,
|
||||
@@ -411,7 +435,7 @@ export class AdminService {
|
||||
targets:
|
||||
data.targets && data.targets.length > 0
|
||||
? {
|
||||
create: data.targets.map((t: any) => ({
|
||||
create: data.targets.map((t) => ({
|
||||
targetType: t.targetType,
|
||||
targetId: t.targetId,
|
||||
modifierType: t.modifierType || 'override',
|
||||
@@ -424,8 +448,7 @@ export class AdminService {
|
||||
});
|
||||
}
|
||||
|
||||
async updateCoupon(id: string, data: any) {
|
||||
// Delete old targets and recreate them
|
||||
async updateCoupon(id: string, data: CouponInput) {
|
||||
await this.prisma.couponTarget.deleteMany({ where: { couponId: id } });
|
||||
|
||||
return this.prisma.coupon.update({
|
||||
@@ -442,7 +465,7 @@ export class AdminService {
|
||||
targets:
|
||||
data.targets && data.targets.length > 0
|
||||
? {
|
||||
create: data.targets.map((t: any) => ({
|
||||
create: data.targets.map((t) => ({
|
||||
targetType: t.targetType,
|
||||
targetId: t.targetId,
|
||||
modifierType: t.modifierType || 'override',
|
||||
@@ -472,12 +495,11 @@ export class AdminService {
|
||||
const settings = await this.prisma.uiText.findMany();
|
||||
return settings.reduce(
|
||||
(acc, curr) => ({ ...acc, [curr.key]: curr.value }),
|
||||
{},
|
||||
{} as Record<string, string>,
|
||||
);
|
||||
}
|
||||
|
||||
async updateSettings(data: Record<string, string>) {
|
||||
// Upsert all keys
|
||||
const operations = Object.entries(data).map(([key, value]) => {
|
||||
return this.prisma.uiText.upsert({
|
||||
where: { key },
|
||||
@@ -490,12 +512,12 @@ export class AdminService {
|
||||
return this.getSettings();
|
||||
}
|
||||
|
||||
async getPets(query: any) {
|
||||
async getPets(query: PaginationQuery = {}) {
|
||||
const page = Number(query.page) || 1;
|
||||
const limit = Number(query.limit) || 10;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const where: any = {};
|
||||
const where: Prisma.PetWhereInput = {};
|
||||
if (query.search) {
|
||||
where.OR = [
|
||||
{ name: { contains: query.search, mode: 'insensitive' } },
|
||||
@@ -572,4 +594,38 @@ export class AdminService {
|
||||
async deleteDoctor(id: string) {
|
||||
return this.prisma.doctor.delete({ where: { id } });
|
||||
}
|
||||
|
||||
async adjustUserWallet(
|
||||
userId: string,
|
||||
amount: number,
|
||||
type: 'deposit' | 'withdrawal' | 'refund',
|
||||
description?: string,
|
||||
) {
|
||||
const user = await this.prisma.user.findUnique({ where: { id: userId } });
|
||||
if (!user) {
|
||||
throw new NotFoundException(`کاربری با شناسه ${userId} یافت نشد.`);
|
||||
}
|
||||
|
||||
const transaction = await this.prisma.walletTransaction.create({
|
||||
data: {
|
||||
userId,
|
||||
amount,
|
||||
type,
|
||||
status: 'completed',
|
||||
description: description || 'تغییر دستی توسط مدیر سیستم',
|
||||
},
|
||||
});
|
||||
|
||||
const isIncrement = type === 'deposit' || type === 'refund';
|
||||
await this.prisma.user.update({
|
||||
where: { id: userId },
|
||||
data: {
|
||||
walletBalance: isIncrement
|
||||
? { increment: amount }
|
||||
: { decrement: amount },
|
||||
},
|
||||
});
|
||||
|
||||
return transaction;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user