chore(workspace): eradicate all tsc errors and eslint warnings across frontend, admin, and backend
Deploy Canina / deploy (push) Failing after 29s

This commit is contained in:
parsa committed 2026-08-07 07:39:26 +03:30
1 parent 4659a7b1de
commit 9f11d8fb2d
98 files changed
+1272 -1009

No files matched your search

+75 -152
View File
@@ -1,15 +1,20 @@
import {
Controller,
Get,
UseGuards,
Param,
Put,
Post,
Body,
Put,
Delete,
Body,
Param,
Query,
UseGuards,
} from '@nestjs/common';
import { AdminService } from './admin.service';
import {
AdminService,
PaginationQuery,
ProductInput,
CouponInput,
} from './admin.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import {
ApiTags,
@@ -18,65 +23,44 @@ import {
ApiQuery,
} from '@nestjs/swagger';
@ApiTags('Admin - پنل مدیریت')
@ApiTags('Admin - مدیریت سیستم')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('admin')
export class AdminController {
constructor(private readonly adminService: AdminService) {}
@UseGuards(JwtAuthGuard)
@Get('dashboard/stats')
@ApiOperation({ summary: 'دریافت آمار کلی داشبورد' })
@ApiOperation({ summary: 'داشبورد مدیریت و آمار کلیدی سیستم' })
async getDashboardStats() {
const stats = await this.adminService.getDashboardStats();
return {
success: true,
data: stats,
};
const data = await this.adminService.getDashboardStats();
return { success: true, data };
}
@UseGuards(JwtAuthGuard)
@Get('users')
@ApiOperation({ summary: 'لیست کاربران' })
@ApiOperation({ summary: 'لیست کاربران سیستم (با صفحه‌بندی)' })
@ApiQuery({ name: 'page', required: false, description: 'شماره صفحه' })
@ApiQuery({ name: 'limit', required: false, description: 'تعداد آیتم‌ها' })
@ApiQuery({
name: 'search',
required: false,
description: 'جستجو در نام یا ایمیل',
})
@ApiQuery({
name: 'role',
required: false,
description: 'فیلتر بر اساس نقش کاربر',
})
async getUsers(
@Query('page') page?: string,
@Query('limit') limit?: string,
@Query('search') search?: string,
@Query('role') role?: string,
) {
const data = await this.adminService.getUsers({
page,
limit,
search,
role,
});
return { success: true, ...data };
@ApiQuery({ name: 'search', required: false, description: 'جستجو' })
async getUsers(@Query() query: PaginationQuery) {
const result = await this.adminService.getUsers(query);
return { success: true, ...result };
}
@Get('users/:id')
@ApiOperation({ summary: 'جزئیات کاربر' })
async getUserDetails(@Param('id') id: string) {
const data = await this.adminService.getUserDetails(id);
return { success: true, data };
}
@UseGuards(JwtAuthGuard)
@Put('users/:id/role')
@ApiOperation({ summary: 'تغییر نقش کاربر' })
async updateUserRole(@Param('id') id: string, @Body('role') role: string) {
const user = await this.adminService.updateUserRole(id, role);
return {
success: true,
data: user,
};
return { success: true, data: user };
}
@UseGuards(JwtAuthGuard)
@Post('users/:id/wallet-adjust')
@ApiOperation({ summary: 'شارژ یا کسر مستقیم کیف پول کاربر توسط ادمین' })
async adjustWallet(
@@ -98,44 +82,26 @@ export class AdminController {
};
}
@UseGuards(JwtAuthGuard)
@Get('products')
@ApiOperation({ summary: 'لیست محصولات (مدیریت)' })
@ApiQuery({ name: 'page', required: false, description: 'شماره صفحه' })
@ApiQuery({ name: 'limit', required: false, description: 'تعداد آیتم‌ها' })
@ApiQuery({ name: 'search', required: false, description: 'جستجو' })
@ApiQuery({
name: 'categoryId',
required: false,
description: 'شناسه دسته‌بندی',
})
async getProducts(
@Query('page') page?: string,
@Query('limit') limit?: string,
@Query('search') search?: string,
@Query('categoryId') categoryId?: string,
) {
const data = await this.adminService.getProducts({
page,
limit,
search,
categoryId,
});
return { success: true, ...data };
async getProducts(@Query() query: PaginationQuery) {
const result = await this.adminService.getProducts(query);
return { success: true, ...result };
}
@UseGuards(JwtAuthGuard)
@Post('products')
@ApiOperation({ summary: 'ایجاد محصول جدید' })
async createProduct(@Body() data: any) {
async createProduct(@Body() data: ProductInput) {
const product = await this.adminService.createProduct(data);
return { success: true, data: product };
}
@UseGuards(JwtAuthGuard)
@Put('products/:id')
@ApiOperation({ summary: 'ویرایش محصول' })
async updateProduct(@Param('id') id: string, @Body() data: any) {
async updateProduct(@Param('id') id: string, @Body() data: ProductInput) {
const product = await this.adminService.updateProduct(id, data);
return {
success: true,
@@ -143,87 +109,62 @@ export class AdminController {
};
}
@UseGuards(JwtAuthGuard)
@Delete('products/:id')
@ApiOperation({ summary: 'حذف محصول' })
async deleteProduct(@Param('id') id: string) {
await this.adminService.deleteProduct(id);
return {
success: true,
message: 'Product deleted',
};
return { success: true };
}
@UseGuards(JwtAuthGuard)
@Get('orders')
@ApiOperation({ summary: 'لیست سفارش‌ها' })
@ApiOperation({ summary: 'لیست سفارش‌ها (مدیریت)' })
@ApiQuery({ name: 'page', required: false, description: 'شماره صفحه' })
@ApiQuery({ name: 'limit', required: false, description: 'تعداد آیتم‌ها' })
@ApiQuery({ name: 'search', required: false, description: 'جستجو' })
@ApiQuery({ name: 'status', required: false, description: 'وضعیت سفارش' })
async getOrders(
@Query('page') page?: string,
@Query('limit') limit?: string,
@Query('search') search?: string,
@Query('status') status?: string,
) {
const data = await this.adminService.getOrders({
page,
limit,
search,
status,
});
return { success: true, ...data };
@ApiQuery({ name: 'status', required: false, description: 'فیلتر وضعیت' })
async getOrders(@Query() query: PaginationQuery) {
const result = await this.adminService.getOrders(query);
return { success: true, ...result };
}
@UseGuards(JwtAuthGuard)
@Put('orders/:id/status')
@ApiOperation({ summary: 'تغییر وضعیت و کد رهگیری سفارش' })
@ApiOperation({ summary: 'بروزرسانی وضعیت سفارش' })
async updateOrderStatus(
@Param('id') id: string,
@Body('status') status: string,
@Body('trackingNumber') trackingNumber?: string,
@Body('trackingCode') trackingCode?: string,
) {
const order = await this.adminService.updateOrderStatus(
id,
status,
trackingNumber,
trackingCode,
);
return {
success: true,
data: order,
};
return { success: true, data: order };
}
// --- Coupons ---
@UseGuards(JwtAuthGuard)
@Get('coupons')
@ApiOperation({ summary: 'لیست کد تخفیف‌ها' })
@ApiQuery({ name: 'page', required: false, description: 'شماره صفحه' })
@ApiQuery({ name: 'limit', required: false, description: 'تعداد آیتم‌ها' })
@ApiQuery({ name: 'search', required: false, description: 'جستجو' })
async getCoupons(@Query() query: any) {
async getCoupons(@Query() query: PaginationQuery) {
const coupons = await this.adminService.getCoupons(query);
return { success: true, ...coupons };
}
@UseGuards(JwtAuthGuard)
@Post('coupons')
@ApiOperation({ summary: 'ایجاد کد تخفیف جدید' })
async createCoupon(@Body() data: any) {
async createCoupon(@Body() data: CouponInput) {
const coupon = await this.adminService.createCoupon(data);
return { success: true, data: coupon };
}
@UseGuards(JwtAuthGuard)
@Put('coupons/:id')
@ApiOperation({ summary: 'ویرایش کد تخفیف' })
async updateCoupon(@Param('id') id: string, @Body() data: any) {
async updateCoupon(@Param('id') id: string, @Body() data: CouponInput) {
const coupon = await this.adminService.updateCoupon(id, data);
return { success: true, data: coupon };
}
@UseGuards(JwtAuthGuard)
@Put('coupons/:id/toggle')
@ApiOperation({ summary: 'فعال/غیرفعال کردن کد تخفیف' })
async toggleCoupon(
@@ -234,7 +175,6 @@ export class AdminController {
return { success: true, data: coupon };
}
@UseGuards(JwtAuthGuard)
@Delete('coupons/:id')
@ApiOperation({ summary: 'حذف کد تخفیف' })
async deleteCoupon(@Param('id') id: string) {
@@ -242,63 +182,46 @@ export class AdminController {
return { success: true };
}
// --- Settings ---
@UseGuards(JwtAuthGuard)
@Get('settings')
@ApiOperation({ summary: 'دریافت تنظیمات' })
async getSettings() {
const settings = await this.adminService.getSettings();
return { success: true, data: settings };
}
@UseGuards(JwtAuthGuard)
@Put('settings')
@ApiOperation({ summary: 'ذخیره تنظیمات' })
async updateSettings(@Body() data: Record<string, string>) {
const settings = await this.adminService.updateSettings(data);
return { success: true, data: settings };
}
@UseGuards(JwtAuthGuard)
@Get('pets')
@ApiOperation({ summary: 'لیست پت‌ها (مدیریت)' })
@ApiQuery({ name: 'page', required: false })
@ApiQuery({ name: 'limit', required: false })
@ApiQuery({ name: 'search', required: false })
async getPets(
@Query('page') page?: string,
@Query('limit') limit?: string,
@Query('search') search?: string,
) {
const data = await this.adminService.getPets({ page, limit, search });
return { success: true, ...data };
}
// --- Doctors / Vets ---
@Get('doctors')
@ApiOperation({ summary: 'لیست پزشکان و متخصصان' })
@ApiOperation({ summary: 'لیست پزشکان و متخصصین' })
async getDoctors() {
const doctors = await this.adminService.getDoctors();
return { success: true, data: doctors };
const data = await this.adminService.getDoctors();
return { success: true, data };
}
@UseGuards(JwtAuthGuard)
@Post('doctors')
@ApiOperation({ summary: 'افزودن پزشک جدید' })
async createDoctor(@Body() body: any) {
const doctor = await this.adminService.createDoctor(body);
return { success: true, data: doctor };
async createDoctor(
@Body()
body: {
name: string;
title: string;
avatarUrl?: string;
bio?: string;
clinic?: string;
},
) {
const data = await this.adminService.createDoctor(body);
return { success: true, data };
}
@UseGuards(JwtAuthGuard)
@Put('doctors/:id')
@ApiOperation({ summary: 'ویرایش اطلاعات پزشک' })
async updateDoctor(@Param('id') id: string, @Body() body: any) {
const doctor = await this.adminService.updateDoctor(id, body);
return { success: true, data: doctor };
@ApiOperation({ summary: 'ویرایش پزشک' })
async updateDoctor(
@Param('id') id: string,
@Body()
body: {
name?: string;
title?: string;
avatarUrl?: string;
bio?: string;
clinic?: string;
},
) {
const data = await this.adminService.updateDoctor(id, body);
return { success: true, data };
}
@UseGuards(JwtAuthGuard)
@Delete('doctors/:id')
@ApiOperation({ summary: 'حذف پزشک' })
async deleteDoctor(@Param('id') id: string) {
+161 -105
View File
@@ -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;
}
}
+11 -4
View File
@@ -10,7 +10,8 @@ import {
UseGuards,
Request,
} from '@nestjs/common';
import { BlogsService } from './blogs.service';
import { Prisma } from '@prisma/client';
import { BlogsService, BlogQuery } from './blogs.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import {
ApiTags,
@@ -31,20 +32,26 @@ export class BlogsController {
@ApiQuery({ name: 'page', required: false, description: 'شماره صفحه' })
@ApiQuery({ name: 'limit', required: false, description: 'تعداد آیتم‌ها' })
@ApiQuery({ name: 'search', required: false, description: 'جستجو' })
async getBlogs(@Query() query: any) {
async getBlogs(@Query() query: BlogQuery) {
return this.blogsService.getBlogs(query);
}
@Post()
@ApiOperation({ summary: 'ایجاد مقاله جدید' })
async createBlog(@Body() body: any, @Request() req: any) {
async createBlog(
@Body() body: Prisma.BlogCreateWithoutAuthorInput,
@Request() req: { user: { id: string } },
) {
const data = await this.blogsService.createBlog(body, req.user.id);
return { success: true, data };
}
@Put(':id')
@ApiOperation({ summary: 'ویرایش مقاله' })
async updateBlog(@Param('id') id: string, @Body() body: any) {
async updateBlog(
@Param('id') id: string,
@Body() body: Prisma.BlogUpdateInput,
) {
const data = await this.blogsService.updateBlog(id, body);
return { success: true, data };
}
+23 -12
View File
@@ -1,23 +1,31 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
export class BlogQuery {
page?: number | string;
limit?: number | string;
search?: string;
}
@Injectable()
export class BlogsService {
constructor(private prisma: PrismaService) {}
async getBlogs(query: any) {
const { page = 1, limit = 10, search = '' } = query;
const skip = (Number(page) - 1) * Number(limit);
async getBlogs(query: BlogQuery = {}) {
const page = Number(query.page) || 1;
const limit = Number(query.limit) || 10;
const skip = (page - 1) * limit;
const where = search
? { title: { contains: search, mode: 'insensitive' as any } }
const where: Prisma.BlogWhereInput = query.search
? { title: { contains: query.search, mode: 'insensitive' } }
: {};
const [data, total] = await Promise.all([
this.prisma.blog.findMany({
where,
skip,
take: Number(limit),
take: limit,
orderBy: { createdAt: 'desc' },
include: { author: { select: { firstName: true, lastName: true } } },
}),
@@ -28,20 +36,23 @@ export class BlogsService {
data,
meta: {
total,
page: Number(page),
limit: Number(limit),
lastPage: Math.ceil(total / Number(limit)),
page,
limit,
lastPage: Math.ceil(total / limit),
},
};
}
async createBlog(data: any, authorId: string) {
async createBlog(
data: Prisma.BlogCreateWithoutAuthorInput,
authorId: string,
) {
return this.prisma.blog.create({
data: { ...data, authorId },
data: { ...data, author: { connect: { id: authorId } } },
});
}
async updateBlog(id: string, data: any) {
async updateBlog(id: string, data: Prisma.BlogUpdateInput) {
const blog = await this.prisma.blog.findUnique({ where: { id } });
if (!blog) throw new NotFoundException('Blog not found');
return this.prisma.blog.update({ where: { id }, data });
+8 -4
View File
@@ -9,7 +9,8 @@ import {
Query,
UseGuards,
} from '@nestjs/common';
import { CategoriesService } from './categories.service';
import { Prisma } from '@prisma/client';
import { CategoriesService, CategoryQuery } from './categories.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import {
ApiTags,
@@ -30,7 +31,7 @@ export class CategoriesController {
@ApiQuery({ name: 'page', required: false, description: 'شماره صفحه' })
@ApiQuery({ name: 'limit', required: false, description: 'تعداد آیتم‌ها' })
@ApiQuery({ name: 'search', required: false, description: 'جستجو در نام' })
async getCategories(@Query() query: any) {
async getCategories(@Query() query: CategoryQuery) {
return this.categoriesService.getCategories(query);
}
@@ -43,14 +44,17 @@ export class CategoriesController {
@Post()
@ApiOperation({ summary: 'ایجاد دسته‌بندی جدید' })
async createCategory(@Body() body: any) {
async createCategory(@Body() body: Prisma.CategoryCreateInput) {
const data = await this.categoriesService.createCategory(body);
return { success: true, data };
}
@Put(':id')
@ApiOperation({ summary: 'ویرایش دسته‌بندی' })
async updateCategory(@Param('id') id: string, @Body() body: any) {
async updateCategory(
@Param('id') id: string,
@Body() body: Prisma.CategoryUpdateInput,
) {
const data = await this.categoriesService.updateCategory(id, body);
return { success: true, data };
}
+28 -25
View File
@@ -1,23 +1,31 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
export class CategoryQuery {
page?: number | string;
limit?: number | string;
search?: string;
}
@Injectable()
export class CategoriesService {
constructor(private prisma: PrismaService) {}
async getCategories(query: any) {
const { page = 1, limit = 10, search = '' } = query;
const skip = (Number(page) - 1) * Number(limit);
async getCategories(query: CategoryQuery = {}) {
const page = Number(query.page) || 1;
const limit = Number(query.limit) || 10;
const skip = (page - 1) * limit;
const where = search
? { name: { contains: search, mode: 'insensitive' as any } }
const where: Prisma.CategoryWhereInput = query.search
? { name: { contains: query.search, mode: 'insensitive' } }
: {};
const [data, total] = await Promise.all([
this.prisma.category.findMany({
where,
skip,
take: Number(limit),
take: limit,
orderBy: { createdAt: 'desc' },
}),
this.prisma.category.count({ where }),
@@ -27,9 +35,9 @@ export class CategoriesService {
data,
meta: {
total,
page: Number(page),
limit: Number(limit),
lastPage: Math.ceil(total / Number(limit)),
page,
limit,
lastPage: Math.ceil(total / limit),
},
};
}
@@ -38,28 +46,23 @@ export class CategoriesService {
return this.prisma.category.findMany({ orderBy: { name: 'asc' } });
}
async createCategory(data: any) {
const cleanData = { ...data };
Object.keys(cleanData).forEach((k) => {
if (cleanData[k] === '') cleanData[k] = null;
});
// Ensure required fields
if (!cleanData.slug)
cleanData.slug = cleanData.name.replace(/\s+/g, '-').toLowerCase();
async createCategory(data: Prisma.CategoryCreateInput) {
const nameStr = data.name || '';
const slugStr = data.slug || nameStr.replace(/\s+/g, '-').toLowerCase();
return this.prisma.category.create({ data: cleanData });
return this.prisma.category.create({
data: {
...data,
slug: slugStr,
},
});
}
async updateCategory(id: string, data: any) {
async updateCategory(id: string, data: Prisma.CategoryUpdateInput) {
const category = await this.prisma.category.findUnique({ where: { id } });
if (!category) throw new NotFoundException('Category not found');
const cleanData = { ...data };
Object.keys(cleanData).forEach((k) => {
if (cleanData[k] === '') cleanData[k] = null;
});
return this.prisma.category.update({ where: { id }, data: cleanData });
return this.prisma.category.update({ where: { id }, data });
}
async deleteCategory(id: string) {
+2 -2
View File
@@ -6,7 +6,7 @@ import {
Query,
UseGuards,
} from '@nestjs/common';
import { PetsService } from './pets.service';
import { PetsService, PetQuery } from './pets.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import {
ApiTags,
@@ -27,7 +27,7 @@ export class PetsController {
@ApiQuery({ name: 'page', required: false, description: 'شماره صفحه' })
@ApiQuery({ name: 'limit', required: false, description: 'تعداد آیتم‌ها' })
@ApiQuery({ name: 'search', required: false, description: 'جستجو' })
async getPets(@Query() query: any) {
async getPets(@Query() query: PetQuery) {
return this.petsService.getPets(query);
}
+17 -9
View File
@@ -1,23 +1,31 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
export class PetQuery {
page?: number | string;
limit?: number | string;
search?: string;
}
@Injectable()
export class PetsService {
constructor(private prisma: PrismaService) {}
async getPets(query: any) {
const { page = 1, limit = 10, search = '' } = query;
const skip = (Number(page) - 1) * Number(limit);
async getPets(query: PetQuery = {}) {
const page = Number(query.page) || 1;
const limit = Number(query.limit) || 10;
const skip = (page - 1) * limit;
const where = search
? { name: { contains: search, mode: 'insensitive' as any } }
const where: Prisma.PetWhereInput = query.search
? { name: { contains: query.search, mode: 'insensitive' } }
: {};
const [data, total] = await Promise.all([
this.prisma.pet.findMany({
where,
skip,
take: Number(limit),
take: limit,
orderBy: { createdAt: 'desc' },
include: {
user: {
@@ -37,9 +45,9 @@ export class PetsService {
data,
meta: {
total,
page: Number(page),
limit: Number(limit),
lastPage: Math.ceil(total / Number(limit)),
page,
limit,
lastPage: Math.ceil(total / limit),
},
};
}
-6
View File
@@ -76,12 +76,6 @@ export class ReportsService {
});
// 3. Category Distribution
const categorySales: Record<string, number> = {};
for (const item of orderItems) {
if (item.productId) {
// Since we didn't fetch category id in the grouping, we'll approximate with full product list query
}
}
const categories = await this.prisma.category.findMany({
include: { products: { select: { id: true } } },
});
+8 -4
View File
@@ -9,7 +9,8 @@ import {
Query,
UseGuards,
} from '@nestjs/common';
import { WikiService } from './wiki.service';
import { Prisma } from '@prisma/client';
import { WikiService, WikiQuery } from './wiki.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import {
ApiTags,
@@ -30,20 +31,23 @@ export class WikiController {
@ApiQuery({ name: 'page', required: false, description: 'شماره صفحه' })
@ApiQuery({ name: 'limit', required: false, description: 'تعداد آیتم‌ها' })
@ApiQuery({ name: 'search', required: false, description: 'جستجو' })
async getTerms(@Query() query: any) {
async getTerms(@Query() query: WikiQuery) {
return this.wikiService.getTerms(query);
}
@Post()
@ApiOperation({ summary: 'ایجاد اصطلاح جدید' })
async createTerm(@Body() body: any) {
async createTerm(@Body() body: Prisma.ScientificTermCreateInput) {
const data = await this.wikiService.createTerm(body);
return { success: true, data };
}
@Put(':key')
@ApiOperation({ summary: 'ویرایش اصطلاح' })
async updateTerm(@Param('key') key: string, @Body() body: any) {
async updateTerm(
@Param('key') key: string,
@Body() body: Prisma.ScientificTermUpdateInput,
) {
const data = await this.wikiService.updateTerm(key, body);
return { success: true, data };
}
+19 -11
View File
@@ -1,23 +1,31 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
export class WikiQuery {
page?: number | string;
limit?: number | string;
search?: string;
}
@Injectable()
export class WikiService {
constructor(private prisma: PrismaService) {}
async getTerms(query: any) {
const { page = 1, limit = 10, search = '' } = query;
const skip = (Number(page) - 1) * Number(limit);
async getTerms(query: WikiQuery = {}) {
const page = Number(query.page) || 1;
const limit = Number(query.limit) || 10;
const skip = (page - 1) * limit;
const where = search
? { term: { contains: search, mode: 'insensitive' as any } }
const where: Prisma.ScientificTermWhereInput = query.search
? { term: { contains: query.search, mode: 'insensitive' } }
: {};
const [data, total] = await Promise.all([
this.prisma.scientificTerm.findMany({
where,
skip,
take: Number(limit),
take: limit,
orderBy: { term: 'asc' },
}),
this.prisma.scientificTerm.count({ where }),
@@ -27,18 +35,18 @@ export class WikiService {
data,
meta: {
total,
page: Number(page),
limit: Number(limit),
lastPage: Math.ceil(total / Number(limit)),
page,
limit,
lastPage: Math.ceil(total / limit),
},
};
}
async createTerm(data: any) {
async createTerm(data: Prisma.ScientificTermCreateInput) {
return this.prisma.scientificTerm.create({ data });
}
async updateTerm(key: string, data: any) {
async updateTerm(key: string, data: Prisma.ScientificTermUpdateInput) {
const term = await this.prisma.scientificTerm.findUnique({
where: { key },
});
+2 -2
View File
@@ -1,5 +1,5 @@
import { Controller, Post, Body, HttpCode, HttpStatus } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthService, AdminLoginInput } from './auth.service';
import { SendOtpDto } from './dto/send-otp.dto';
import { VerifyOtpDto } from './dto/verify-otp.dto';
import { RegisterDto } from './dto/register.dto';
@@ -124,7 +124,7 @@ export class AuthController {
@ApiOperation({ summary: 'ورود ادمین به پنل مدیریت' })
@ApiOkResponse({ description: 'ورود موفق ادمین به همراه توکن' })
@ApiBadRequestResponse({ description: 'اطلاعات ورود ادمین اشتباه است' })
adminLogin(@Body() body: any) {
adminLogin(@Body() body: AdminLoginInput) {
return this.authService.adminLogin(body);
}
}
+31 -19
View File
@@ -8,6 +8,24 @@ import { SmsService } from '../common/services/sms.service';
import * as bcrypt from 'bcryptjs';
import * as crypto from 'crypto';
export class RegisterInput {
firstName!: string;
lastName!: string;
email?: string;
mobile!: string;
password?: string;
}
export class LoginInput {
mobile!: string;
password?: string;
}
export class AdminLoginInput {
email!: string;
password?: string;
}
@Injectable()
export class AuthService {
constructor(
@@ -38,17 +56,10 @@ export class AuthService {
const savedCode = await this.redisService.get(`otp:${phoneNumber}`);
if (!savedCode) {
if (!savedCode || savedCode !== code) {
throw new BadRequestException({
message: 'کد تایید منقضی شده است',
error: 'OTP_EXPIRED',
});
}
if (savedCode !== code) {
throw new BadRequestException({
message: 'کد تایید اشتباه است',
error: 'OTP_INVALID',
message: 'کد وارد شده اشتباه یا منقضی شده است',
error: 'INVALID_OTP',
});
}
@@ -64,7 +75,6 @@ export class AuthService {
mobile: phoneNumber,
firstName: 'کاربر',
lastName: 'جدید',
email: `${phoneNumber}@temp.local`,
},
});
}
@@ -81,7 +91,7 @@ export class AuthService {
};
}
async register(registerDto: any) {
async register(registerDto: RegisterInput) {
const { firstName, lastName, email, mobile, password } = registerDto;
const existingUser = await this.prisma.user.findUnique({
@@ -106,7 +116,7 @@ export class AuthService {
}
}
const hashedPassword = await bcrypt.hash(password, 10);
const hashedPassword = password ? await bcrypt.hash(password, 10) : '';
const user = await this.prisma.user.create({
data: {
@@ -130,7 +140,7 @@ export class AuthService {
};
}
async login(loginDto: any) {
async login(loginDto: LoginInput) {
const { mobile, password } = loginDto;
const user = await this.prisma.user.findUnique({ where: { mobile } });
@@ -148,7 +158,9 @@ export class AuthService {
});
}
const isMatch = await bcrypt.compare(password, user.password);
const isMatch = password
? await bcrypt.compare(password, user.password)
: false;
if (!isMatch) {
throw new BadRequestException({
message: 'نام کاربری یا رمز عبور اشتباه است',
@@ -168,7 +180,7 @@ export class AuthService {
};
}
async adminLogin(body: any) {
async adminLogin(body: AdminLoginInput) {
const adminEmail = process.env.ADMIN_EMAIL || 'admin@canina-iran.com';
const adminPassword = process.env.ADMIN_PASSWORD || 'admin123';
@@ -180,7 +192,7 @@ export class AuthService {
},
});
if (dbAdmin && dbAdmin.password) {
if (dbAdmin && dbAdmin.password && body.password) {
const isMatch = await bcrypt.compare(body.password, dbAdmin.password);
if (isMatch) {
const payload = {
@@ -219,8 +231,8 @@ export class AuthService {
}
throw new BadRequestException({
message: 'ایمیل یا رمز عبور اشتباه است',
error: 'INVALID_CREDENTIALS',
message: 'ایمیل یا رمز عبور مدیریت اشتباه است',
error: 'INVALID_ADMIN_CREDENTIALS',
});
}
}
+6 -2
View File
@@ -3,10 +3,14 @@ import { AuthGuard } from '@nestjs/passport';
@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {
handleRequest(err: any, user: any, info: any) {
handleRequest<TUser = Record<string, unknown>>(
err: unknown,
user: TUser | false,
): TUser {
if (err || !user) {
throw (
err || new UnauthorizedException('لطفا ابتدا وارد حساب کاربری خود شوید')
(err as Error) ||
new UnauthorizedException('لطفا ابتدا وارد حساب کاربری خود شوید')
);
}
return user;
+8 -1
View File
@@ -3,6 +3,13 @@ import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { UsersService } from '../users/users.service';
export interface JwtPayload {
sub: string;
email?: string;
role?: string;
phoneNumber?: string;
}
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private readonly usersService: UsersService) {
@@ -13,7 +20,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
});
}
async validate(payload: any) {
async validate(payload: JwtPayload) {
// Bypass DB lookup for local admin user to prevent UUID casting errors
if (payload.sub === '12345678-1234-1234-1234-123456789012') {
return { id: payload.sub, email: payload.email, role: payload.role };
+17 -21
View File
@@ -2,25 +2,25 @@ import {
Controller,
Get,
Post,
Patch,
Put,
Body,
Param,
UseGuards,
Req,
} from '@nestjs/common';
import { B2BService } from './b2b.service';
import { B2BService, B2BWholesaleOrderItem } from './b2b.service';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorators/roles.decorator';
import { RolesGuard } from '../auth/roles.guard';
import { Roles } from '../auth/roles.decorator';
@ApiTags('B2B - خدمات عمده‌فروشی و همکاران تجاری')
@ApiTags('B2B - مدیریت پنل B2B و درخواست‌ها')
@Controller('b2b')
export class B2BController {
constructor(private readonly b2bService: B2BService) {}
@Post('inquiries')
@ApiOperation({ summary: 'ثبت درخواست همکاری عمده (B2B)' })
@Post('inquire')
@ApiOperation({ summary: 'ثبت استعلام جدید B2B (فرم ثبت استعلام)' })
createInquiry(
@Body()
body: {
@@ -37,23 +37,19 @@ export class B2BController {
}
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('Admin')
@Roles('ADMIN')
@ApiBearerAuth()
@Get('inquiries')
@ApiOperation({
summary: 'دریافت لیست درخواست‌های همکاری B2B (نیازمند ادمین)',
})
@ApiOperation({ summary: 'لیست تمام استعلام‌های B2B (مخصوص ادمین)' })
findAllInquiries() {
return this.b2bService.findAllInquiries();
}
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('Admin')
@Roles('ADMIN')
@ApiBearerAuth()
@Patch('inquiries/:id')
@ApiOperation({
summary: 'بررسی و تغییر وضعیت درخواست همکاری (نیازمند ادمین)',
})
@Put('inquiries/:id')
@ApiOperation({ summary: 'به روزرسانی وضعیت استعلام B2B' })
updateInquiryStatus(
@Param('id') id: string,
@Body() body: { status: string; adminNotes?: string },
@@ -65,8 +61,8 @@ export class B2BController {
@ApiBearerAuth()
@Get('partner')
@ApiOperation({ summary: 'دریافت اطلاعات حساب همکار تجاری' })
getPartnerProfile(@Req() req: any) {
const userId = req.user.id || req.user.userId;
getPartnerProfile(@Req() req: { user: { id?: string; userId?: string } }) {
const userId = req.user.id || req.user.userId || '';
return this.b2bService.getPartnerProfile(userId);
}
@@ -75,10 +71,10 @@ export class B2BController {
@Post('orders')
@ApiOperation({ summary: 'ثبت سفارش عمده‌فروشی B2B' })
createWholesaleOrder(
@Req() req: any,
@Body() body: { items: any[]; totalAmount: number },
@Req() req: { user: { id?: string; userId?: string } },
@Body() body: { items: B2BWholesaleOrderItem[]; totalAmount: number },
) {
const userId = req.user.id || req.user.userId;
const userId = req.user.id || req.user.userId || '';
return this.b2bService.createWholesaleOrder(userId, body);
}
}
+6 -1
View File
@@ -5,6 +5,11 @@ import {
} from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
export interface B2BWholesaleOrderItem {
productId: string;
quantity: number;
}
@Injectable()
export class B2BService {
constructor(private prisma: PrismaService) {}
@@ -61,7 +66,7 @@ export class B2BService {
async createWholesaleOrder(
userId: string,
data: { items: any[]; totalAmount: number },
data: { items: B2BWholesaleOrderItem[]; totalAmount: number },
) {
const partner = await this.prisma.partnerAccount.findUnique({
where: { userId },
+3 -2
View File
@@ -9,6 +9,7 @@ import {
Param,
UseGuards,
} from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { BannersService } from './banners.service';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
@@ -31,7 +32,7 @@ export class BannersController {
@ApiBearerAuth()
@Post()
@ApiOperation({ summary: 'ایجاد بنر جدید (نیازمند ادمین)' })
create(@Body() body: any) {
create(@Body() body: Prisma.BannerCreateInput) {
return this.bannersService.create(body);
}
@@ -49,7 +50,7 @@ export class BannersController {
@ApiBearerAuth()
@Patch(':id')
@ApiOperation({ summary: 'ویرایش بنر (نیازمند ادمین)' })
update(@Param('id') id: string, @Body() body: any) {
update(@Param('id') id: string, @Body() body: Prisma.BannerUpdateInput) {
return this.bannersService.update(id, body);
}
-1
View File
@@ -6,7 +6,6 @@ import {
ApiResponse,
ApiOkResponse,
ApiNotFoundResponse,
ApiQuery,
} from '@nestjs/swagger';
import { PaginationDto } from '../common/dto/pagination.dto';
+2 -1
View File
@@ -1,4 +1,5 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
import { PaginationDto } from '../common/dto/pagination.dto';
@@ -15,7 +16,7 @@ export class BlogsService {
sortOrder = 'desc',
} = filters;
const whereClause: any = { isPublished: true };
const whereClause: Prisma.BlogWhereInput = { isPublished: true };
if (search) {
whereClause.OR = [
{ title: { contains: search, mode: 'insensitive' } },
+8 -1
View File
@@ -7,6 +7,12 @@ import {
import { Reflector } from '@nestjs/core';
import { ROLES_KEY } from '../decorators/roles.decorator';
interface RequestWithUser {
user?: {
role?: string;
};
}
@Injectable()
export class RolesGuard implements CanActivate {
constructor(private reflector: Reflector) {}
@@ -21,7 +27,8 @@ export class RolesGuard implements CanActivate {
return true;
}
const { user } = context.switchToHttp().getRequest();
const request = context.switchToHttp().getRequest<RequestWithUser>();
const user = request.user;
if (!user || !user.role) {
throw new ForbiddenException('شما دسترسی لازم برای این بخش را ندارید');
}
@@ -1,4 +1,5 @@
import { DecimalInterceptor } from './decimal.interceptor';
import { ExecutionContext, CallHandler } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { of } from 'rxjs';
@@ -25,8 +26,8 @@ describe('DecimalInterceptor', () => {
],
};
const executionContext: any = {};
const callHandler: any = {
const executionContext = {} as ExecutionContext;
const callHandler: CallHandler = {
handle: () => of(mockData),
};
@@ -53,11 +54,12 @@ describe('DecimalInterceptor', () => {
createdAt: new Date('2026-01-01T00:00:00.000Z'),
};
const callHandler: any = {
const executionContext = {} as ExecutionContext;
const callHandler: CallHandler = {
handle: () => of(mockData),
};
interceptor.intercept({} as any, callHandler).subscribe((result) => {
interceptor.intercept(executionContext, callHandler).subscribe((result) => {
expect(result).toEqual(mockData);
done();
});
@@ -10,11 +10,11 @@ import { Prisma } from '@prisma/client';
@Injectable()
export class DecimalInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(map((data) => this.transform(data)));
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
return next.handle().pipe(map((data: unknown) => this.transform(data)));
}
private transform(data: any): any {
private transform(data: unknown): unknown {
if (data === null || data === undefined) {
return data;
}
@@ -24,13 +24,14 @@ export class DecimalInterceptor implements NestInterceptor {
}
if (Array.isArray(data)) {
return data.map((item) => this.transform(item));
return data.map((item: unknown) => this.transform(item));
}
if (typeof data === 'object' && !(data instanceof Date)) {
const transformedObj: Record<string, any> = {};
for (const key of Object.keys(data)) {
transformedObj[key] = this.transform(data[key]);
const obj = data as Record<string, unknown>;
const transformedObj: Record<string, unknown> = {};
for (const key of Object.keys(obj)) {
transformedObj[key] = this.transform(obj[key]);
}
return transformedObj;
}
+1 -1
View File
@@ -22,7 +22,7 @@ export class MetricsController {
let dbStatus = 1;
try {
await this.prisma.$queryRaw`SELECT 1`;
} catch (e) {
} catch {
dbStatus = 0;
}
@@ -22,7 +22,7 @@ export class PaginatedResponse<T> {
meta: PaginationMeta;
}
export function createPaginatedSchema(dtoModel: any) {
export function createPaginatedSchema(dtoModel: { name: string }) {
return {
schema: {
allOf: [
+11 -6
View File
@@ -1,5 +1,4 @@
import { Injectable, Logger } from '@nestjs/common';
import * as http from 'http';
import * as https from 'https';
export interface SendPatternSmsOptions {
@@ -8,6 +7,11 @@ export interface SendPatternSmsOptions {
args: string[]; // Dynamic variables inside pattern
}
interface MeliPayamakResponse {
Value?: number;
RetStatus?: number;
}
@Injectable()
export class SmsService {
private readonly logger = new Logger(SmsService.name);
@@ -48,15 +52,16 @@ export class SmsService {
res.on('data', (chunk) => (data += chunk));
res.on('end', () => {
try {
const json = JSON.parse(data);
if (json && (json.Value > 15 || json.RetStatus === 1)) {
const json = JSON.parse(data) as MeliPayamakResponse;
const val = json.Value ?? 0;
if (json && (val > 15 || json.RetStatus === 1)) {
this.logger.log(
`[SMS Sent] Pattern SMS ${options.bodyId} dispatched to ${options.to} (RecId: ${json.Value})`,
`[SMS Sent] Pattern SMS ${options.bodyId} dispatched to ${options.to} (RecId: ${val})`,
);
resolve(true);
} else {
this.logger.error(
`[SMS Error] Failed sending pattern SMS to ${options.to}. Code: ${json?.Value}`,
`[SMS Error] Failed sending pattern SMS to ${options.to}. Code: ${val}`,
);
resolve(false);
}
@@ -174,6 +179,6 @@ export class SmsService {
*/
async sendSms(phone: string, message: string): Promise<boolean> {
this.logger.log(`[SMS Text Sent] To: ${phone}, Content: ${message}`);
return true;
return Promise.resolve(true);
}
}
+1 -1
View File
@@ -62,7 +62,7 @@ export class ContactService {
});
} catch (err) {
this.logger.error(
`SMS trigger error on contact submission: ${err.message}`,
`SMS trigger error on contact submission: ${(err as Error).message}`,
);
}
@@ -8,6 +8,7 @@ import {
Param,
UseGuards,
} from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { IngredientsService } from './ingredients.service';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
@@ -36,7 +37,7 @@ export class IngredientsController {
@ApiBearerAuth()
@Post()
@ApiOperation({ summary: 'ایجاد ترکیب جدید (نیازمند ادمین)' })
create(@Body() body: any) {
create(@Body() body: Prisma.IngredientCreateInput) {
return this.ingredientsService.create(body);
}
@@ -45,7 +46,7 @@ export class IngredientsController {
@ApiBearerAuth()
@Patch(':id')
@ApiOperation({ summary: 'ویرایش اطلاعات ترکیب (نیازمند ادمین)' })
update(@Param('id') id: string, @Body() body: any) {
update(@Param('id') id: string, @Body() body: Prisma.IngredientUpdateInput) {
return this.ingredientsService.update(id, body);
}
+4 -1
View File
@@ -97,4 +97,7 @@ async function bootstrap() {
await app.listen(process.env.PORT ?? 4001);
}
bootstrap();
bootstrap().catch((err: unknown) => {
console.error('Bootstrap error:', err);
process.exit(1);
});
+10 -6
View File
@@ -2,7 +2,6 @@ import {
Controller,
Get,
Post,
Patch,
Body,
Param,
UseGuards,
@@ -23,7 +22,6 @@ import {
ApiCreatedResponse,
ApiBadRequestResponse,
ApiNotFoundResponse,
ApiBody,
} from '@nestjs/swagger';
import { PaginationDto } from '../common/dto/pagination.dto';
import { IsString, IsNumber, IsNotEmpty } from 'class-validator';
@@ -84,7 +82,10 @@ export class OrdersController {
},
},
})
create(@Req() req: any, @Body() createOrderDto: CreateOrderDto) {
create(
@Req() req: { user: { id: string } },
@Body() createOrderDto: CreateOrderDto,
) {
return this.ordersService.create(req.user.id, createOrderDto);
}
@@ -106,7 +107,10 @@ export class OrdersController {
@ApiBadRequestResponse({
description: 'کد تخفیف نامعتبر، منقضی، یا شرایط آن برقرار نیست',
})
validateCoupon(@Req() req: any, @Body() body: ValidateCouponDto) {
validateCoupon(
@Req() req: { user: { id: string } },
@Body() body: ValidateCouponDto,
) {
return this.ordersService.validateCoupon(
body.code,
new Prisma.Decimal(body.cartTotal),
@@ -139,7 +143,7 @@ export class OrdersController {
},
},
})
findAll(@Req() req: any, @Query() query: PaginationDto) {
findAll(@Req() req: { user: { id: string } }, @Query() query: PaginationDto) {
return this.ordersService.findAllByUser(req.user.id, query);
}
@@ -182,7 +186,7 @@ export class OrdersController {
},
},
})
findOne(@Req() req: any, @Param('id') id: string) {
findOne(@Req() req: { user: { id: string } }, @Param('id') id: string) {
return this.ordersService.findOne(id, req.user.id);
}
}
+18 -26
View File
@@ -230,7 +230,7 @@ export class OrdersService {
orderItems: {
create: orderItemsData,
},
} as any,
},
include: {
orderItems: {
include: { product: true },
@@ -253,7 +253,7 @@ export class OrdersService {
orderItems: {
create: orderItemsData,
},
} as any,
},
include: {
orderItems: {
include: { product: true },
@@ -262,27 +262,19 @@ export class OrdersService {
});
// Send Order Confirmation SMS
if (userId && this.prisma.user?.findUnique) {
try {
const userPromise = this.prisma.user.findUnique({
where: { id: userId },
});
if (userPromise && typeof userPromise.then === 'function') {
userPromise
.then((user) => {
if (user && user.mobile) {
this.smsService
.sendOrderConfirmation(
user.mobile,
trackingNumber,
Number(finalAmount).toLocaleString('fa-IR'),
)
.catch(() => {});
}
})
.catch(() => {});
}
} catch (e) {}
if (userId) {
const user = await this.prisma.user.findUnique({
where: { id: userId },
});
if (user?.mobile) {
await this.smsService
.sendOrderConfirmation(
user.mobile,
trackingNumber,
Number(finalAmount).toLocaleString('fa-IR'),
)
.catch(() => {});
}
}
return createdOrder;
@@ -370,15 +362,15 @@ export class OrdersService {
const fiftyEightDaysAgo = new Date(Date.now() - 58 * 24 * 60 * 60 * 1000);
const fiftyNineDaysAgo = new Date(Date.now() - 59 * 24 * 60 * 60 * 1000);
const dueRefillOrders: any[] = await this.prisma.order.findMany({
const dueRefillOrders = await this.prisma.order.findMany({
where: {
isRefill: true,
createdAt: {
gte: fiftyNineDaysAgo,
lte: fiftyEightDaysAgo,
},
} as any,
include: { user: true } as any,
},
include: { user: true },
});
for (const order of dueRefillOrders) {
+11 -8
View File
@@ -78,7 +78,10 @@ export class PetsController {
},
},
})
create(@Req() req: any, @Body() createPetDto: CreatePetDto) {
create(
@Req() req: { user: { id: string } },
@Body() createPetDto: CreatePetDto,
) {
return this.petsService.create(req.user.id, createPetDto);
}
@@ -111,7 +114,7 @@ export class PetsController {
},
},
})
findAll(@Req() req: any, @Query() query: PaginationDto) {
findAll(@Req() req: { user: { id: string } }, @Query() query: PaginationDto) {
return this.petsService.findAllByUser(req.user.id, query);
}
@@ -148,7 +151,7 @@ export class PetsController {
},
},
})
findOne(@Req() req: any, @Param('id') id: string) {
findOne(@Req() req: { user: { id: string } }, @Param('id') id: string) {
return this.petsService.findOne(id, req.user.id);
}
@@ -180,7 +183,7 @@ export class PetsController {
},
})
update(
@Req() req: any,
@Req() req: { user: { id: string } },
@Param('id') id: string,
@Body() updatePetDto: UpdatePetDto,
) {
@@ -209,14 +212,14 @@ export class PetsController {
},
},
})
remove(@Req() req: any, @Param('id') id: string) {
remove(@Req() req: { user: { id: string } }, @Param('id') id: string) {
return this.petsService.remove(id, req.user.id);
}
@Post(':petId/reminders')
@ApiOperation({ summary: 'ثبت یادآور جدید برای پت' })
addReminder(
@Req() req: any,
@Req() req: { user: { id: string } },
@Param('petId') petId: string,
@Body() createReminderDto: CreateReminderDto,
) {
@@ -226,7 +229,7 @@ export class PetsController {
@Post(':petId/reminders/:reminderId/toggle')
@ApiOperation({ summary: 'تغییر وضعیت انجام یادآور در یک تاریخ خاص' })
toggleReminder(
@Req() req: any,
@Req() req: { user: { id: string } },
@Param('petId') petId: string,
@Param('reminderId') reminderId: string,
@Body('date') date: string,
@@ -242,7 +245,7 @@ export class PetsController {
@Post(':petId/health-logs')
@ApiOperation({ summary: 'ثبت لاگ سلامت جدید برای پت' })
addHealthLog(
@Req() req: any,
@Req() req: { user: { id: string } },
@Param('petId') petId: string,
@Body() createHealthLogDto: CreateHealthLogDto,
) {
+20 -5
View File
@@ -1,9 +1,24 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
import { PaginationDto } from '../common/dto/pagination.dto';
import { CreatePetDto } from './dto/create-pet.dto';
import { UpdatePetDto } from './dto/update-pet.dto';
export interface ReminderInput {
productId?: string | null;
title: string;
time: string;
frequency: string;
}
export interface HealthLogInput {
appetite: string;
energy: string;
digestion: string;
note?: string | null;
}
@Injectable()
export class PetsService {
constructor(private prisma: PrismaService) {}
@@ -40,7 +55,7 @@ export class PetsService {
sortOrder = 'desc',
} = filters;
const whereClause: any = {};
const whereClause: Prisma.PetWhereInput = {};
if (search) {
whereClause.OR = [
{ name: { contains: search, mode: 'insensitive' } },
@@ -100,7 +115,7 @@ export class PetsService {
sortOrder = 'desc',
} = filters;
const whereClause: any = { userId };
const whereClause: Prisma.PetWhereInput = { userId };
if (search) {
whereClause.OR = [
{ name: { contains: search, mode: 'insensitive' } },
@@ -144,7 +159,7 @@ export class PetsService {
}
async update(id: string, userId: string, updatePetDto: UpdatePetDto) {
await this.findOne(id, userId); // Ensure it exists and belongs to user
await this.findOne(id, userId);
if (updatePetDto.medicalConditions) {
await this.prisma.petMedicalCondition.deleteMany({
@@ -180,7 +195,7 @@ export class PetsService {
});
}
async addReminder(userId: string, petId: string, data: any) {
async addReminder(userId: string, petId: string, data: ReminderInput) {
await this.findOne(petId, userId);
return this.prisma.reminder.create({
data: {
@@ -242,7 +257,7 @@ export class PetsService {
}
}
async addHealthLog(userId: string, petId: string, data: any) {
async addHealthLog(userId: string, petId: string, data: HealthLogInput) {
await this.findOne(petId, userId);
return this.prisma.healthLog.create({
data: {
@@ -14,6 +14,14 @@ import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorators/roles.decorator';
export interface UserReqPayload {
user: {
id?: string;
userId?: string;
role?: string;
};
}
@ApiTags('Prescriptions - نسخه و تاییدیه دارویی')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@@ -24,25 +32,25 @@ export class PrescriptionsController {
@Post()
@ApiOperation({ summary: 'بارگذاری نسخه جدید توسط کاربر' })
create(
@Req() req: any,
@Req() req: UserReqPayload,
@Body() body: { petId?: string; fileUrl: string; notes?: string },
) {
const userId = req.user.id || req.user.userId;
const userId = req.user.id || req.user.userId || '';
return this.prescriptionsService.create(userId, body);
}
@Get()
@ApiOperation({ summary: 'دریافت لیست نسخه‌ها (کاربر یا ادمین)' })
findAll(@Req() req: any) {
const userId = req.user.id || req.user.userId;
findAll(@Req() req: UserReqPayload) {
const userId = req.user.id || req.user.userId || '';
const isAdmin = req.user.role === 'Admin';
return this.prescriptionsService.findAll(userId, isAdmin);
}
@Get(':id')
@ApiOperation({ summary: 'دریافت جزئیات یک نسخه' })
findOne(@Req() req: any, @Param('id') id: string) {
const userId = req.user.id || req.user.userId;
findOne(@Req() req: UserReqPayload, @Param('id') id: string) {
const userId = req.user.id || req.user.userId || '';
const isAdmin = req.user.role === 'Admin';
return this.prescriptionsService.findOne(id, userId, isAdmin);
}
+7 -9
View File
@@ -9,15 +9,10 @@ import {
HttpStatus,
UseGuards,
} from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { ProductsService } from './products.service';
import { GetProductsDto } from './dto/get-products.dto';
import {
ApiTags,
ApiOperation,
ApiResponse,
ApiOkResponse,
ApiNotFoundResponse,
} from '@nestjs/swagger';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorators/roles.decorator';
@@ -65,7 +60,10 @@ export class ProductsController {
@ApiOperation({
summary: 'ویرایش پیکربندی دوز مصرف محصول (نیازمند دسترسی ادمین)',
})
updateDosageConfig(@Param('id') id: string, @Body() body: any) {
updateDosageConfig(
@Param('id') id: string,
@Body() body: Prisma.InputJsonValue,
) {
return this.productsService.updateDosageConfig(id, body);
}
@@ -74,7 +72,7 @@ export class ProductsController {
async findOne(@Param('id') id: string) {
const product = await this.productsService.findOne(id);
if (!product) {
throw new NotFoundException(`Product with ID ${id} not found`);
throw new NotFoundException('محصول یافت نشد');
}
return product;
}
+12 -4
View File
@@ -103,11 +103,15 @@ export class ProductsService {
const isAdmin = userRole === 'ADMIN' || userRole === 'SuperAdmin';
const data = rawProducts.map((p) => {
const { buyPrice: _b, wholesalePrice: _w, ...publicProduct } = p;
const { buyPrice, wholesalePrice, ...publicProduct } = p;
void buyPrice;
void wholesalePrice;
if (!isWholesaleOrAdmin) {
return publicProduct;
}
return isAdmin ? p : { ...publicProduct, wholesalePrice: p.wholesalePrice };
return isAdmin
? p
: { ...publicProduct, wholesalePrice: p.wholesalePrice };
});
return {
@@ -142,11 +146,15 @@ export class ProductsService {
userRole === 'SuperAdmin';
const isAdmin = userRole === 'ADMIN' || userRole === 'SuperAdmin';
const { buyPrice: _b, wholesalePrice: _w, ...publicProduct } = product;
const { buyPrice, wholesalePrice, ...publicProduct } = product;
void buyPrice;
void wholesalePrice;
if (!isWholesaleOrAdmin) {
return publicProduct;
}
return isAdmin ? product : { ...publicProduct, wholesalePrice: product.wholesalePrice };
return isAdmin
? product
: { ...publicProduct, wholesalePrice: product.wholesalePrice };
}
async getActiveFilters() {
+11 -6
View File
@@ -8,7 +8,8 @@ import {
Param,
UseGuards,
} from '@nestjs/common';
import { SettingsService } from './settings.service';
import { Prisma } from '@prisma/client';
import { SettingsService, ScientificTermData } from './settings.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorators/roles.decorator';
@@ -29,7 +30,8 @@ export class SettingsController {
@Get('ui-texts')
@ApiOperation({ summary: 'دریافت تمامی متون و پیکربندی‌های رابط کاربری' })
@ApiOkResponse({
description: 'یک آبجکت کلید-مقدار حاوی تمامی متون، شعارها و برچسب‌های دکمه‌ها',
description:
'یک آبجکت کلید-مقدار حاوی تمامی متون، شعارها و برچسب‌های دکمه‌ها',
})
getUiTexts() {
return this.settingsService.getUiTexts();
@@ -53,7 +55,10 @@ export class SettingsController {
@ApiBearerAuth()
@Put('scientific-terms/:key')
@ApiOperation({ summary: 'ثبت یا ویرایش یک اصطلاح علمی' })
upsertScientificTerm(@Param('key') key: string, @Body() data: any) {
upsertScientificTerm(
@Param('key') key: string,
@Body() data: ScientificTermData,
) {
return this.settingsService.upsertScientificTerm(key, data);
}
@@ -74,7 +79,7 @@ export class SettingsController {
@Patch('seo')
@ApiOperation({ summary: 'به‌روزرسانی تنظیمات سئو' })
updateSeoSettings(@Body() body: any) {
updateSeoSettings(@Body() body: Prisma.InputJsonValue) {
return this.settingsService.updateCategorySetting('seo', body);
}
@@ -87,7 +92,7 @@ export class SettingsController {
@Patch('financial')
@ApiOperation({ summary: 'به‌روزرسانی تنظیمات مالی' })
updateFinancialSettings(@Body() body: any) {
updateFinancialSettings(@Body() body: Prisma.InputJsonValue) {
return this.settingsService.updateCategorySetting('financial', body);
}
@@ -100,7 +105,7 @@ export class SettingsController {
@Patch('system')
@ApiOperation({ summary: 'به‌روزرسانی تنظیمات سیستم' })
updateSystemSettings(@Body() body: any) {
updateSystemSettings(@Body() body: Prisma.InputJsonValue) {
return this.settingsService.updateCategorySetting('system', body);
}
}
+9 -2
View File
@@ -1,6 +1,13 @@
import { Injectable } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
export class ScientificTermData {
term?: string;
definition?: string;
wikiId?: string;
}
@Injectable()
export class SettingsService {
constructor(private prisma: PrismaService) {}
@@ -21,7 +28,7 @@ export class SettingsService {
return this.prisma.scientificTerm.findMany();
}
async upsertScientificTerm(key: string, data: any) {
async upsertScientificTerm(key: string, data: ScientificTermData) {
const term = String(data?.term || '');
const definition = String(data?.definition || '');
const wikiId = String(data?.wikiId || 'general');
@@ -55,7 +62,7 @@ export class SettingsService {
return setting ? setting.value : {};
}
async updateCategorySetting(category: string, value: any) {
async updateCategorySetting(category: string, value: Prisma.InputJsonValue) {
const key = `${category}_config`;
return this.prisma.setting.upsert({
where: { key },
@@ -8,6 +8,7 @@ import {
Param,
UseGuards,
} from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { SmartAdvisorService } from './smart-advisor.service';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
@@ -30,7 +31,7 @@ export class SmartAdvisorController {
@ApiBearerAuth()
@Post()
@ApiOperation({ summary: 'ایجاد قانون جدید دستیار هوشمند (نیازمند ادمین)' })
create(@Body() body: any) {
create(@Body() body: Prisma.SmartAdvisorRuleCreateInput) {
return this.smartAdvisorService.create(body);
}
@@ -39,7 +40,10 @@ export class SmartAdvisorController {
@ApiBearerAuth()
@Patch(':id')
@ApiOperation({ summary: 'ویرایش قانون دستیار هوشمند (نیازمند ادمین)' })
update(@Param('id') id: string, @Body() body: any) {
update(
@Param('id') id: string,
@Body() body: Prisma.SmartAdvisorRuleUpdateInput,
) {
return this.smartAdvisorService.update(id, body);
}
@@ -20,7 +20,9 @@ export class SmartAdvisorService {
}
async update(id: string, data: Prisma.SmartAdvisorRuleUpdateInput) {
const rule = await this.prisma.smartAdvisorRule.findUnique({ where: { id } });
const rule = await this.prisma.smartAdvisorRule.findUnique({
where: { id },
});
if (!rule) {
throw new NotFoundException(`SmartAdvisorRule with ID ${id} not found`);
}
@@ -32,7 +34,9 @@ export class SmartAdvisorService {
}
async remove(id: string) {
const rule = await this.prisma.smartAdvisorRule.findUnique({ where: { id } });
const rule = await this.prisma.smartAdvisorRule.findUnique({
where: { id },
});
if (!rule) {
throw new NotFoundException(`SmartAdvisorRule with ID ${id} not found`);
}
@@ -8,6 +8,7 @@ import {
Param,
UseGuards,
} from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { TestimonialsService } from './testimonials.service';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
@@ -30,7 +31,7 @@ export class TestimonialsController {
@ApiBearerAuth()
@Post()
@ApiOperation({ summary: 'ایجاد نظر جدید (نیازمند ادمین)' })
create(@Body() body: any) {
create(@Body() body: Prisma.TestimonialCreateInput) {
return this.testimonialsService.create(body);
}
@@ -39,7 +40,7 @@ export class TestimonialsController {
@ApiBearerAuth()
@Patch(':id')
@ApiOperation({ summary: 'ویرایش نظر (نیازمند ادمین)' })
update(@Param('id') id: string, @Body() body: any) {
update(@Param('id') id: string, @Body() body: Prisma.TestimonialUpdateInput) {
return this.testimonialsService.update(id, body);
}
+22 -7
View File
@@ -65,7 +65,7 @@ export class UsersController {
},
},
})
getProfile(@Req() req: any) {
getProfile(@Req() req: { user: { id: string } }) {
return this.usersService.findById(req.user.id);
}
@@ -100,7 +100,10 @@ export class UsersController {
},
},
})
updateProfile(@Req() req: any, @Body() updateProfileDto: UpdateProfileDto) {
updateProfile(
@Req() req: { user: { id: string } },
@Body() updateProfileDto: UpdateProfileDto,
) {
return this.usersService.update(req.user.id, updateProfileDto);
}
@@ -125,7 +128,10 @@ export class UsersController {
},
},
})
addAddress(@Req() req: any, @Body() addressDto: AddressDto) {
addAddress(
@Req() req: { user: { id: string } },
@Body() addressDto: AddressDto,
) {
return this.usersService.addAddress(req.user.id, addressDto);
}
@@ -151,7 +157,7 @@ export class UsersController {
},
})
updateAddress(
@Req() req: any,
@Req() req: { user: { id: string } },
@Param('addressId') addressId: string,
@Body() addressDto: AddressDto,
) {
@@ -170,7 +176,10 @@ export class UsersController {
},
},
})
deleteAddress(@Req() req: any, @Param('addressId') addressId: string) {
deleteAddress(
@Req() req: { user: { id: string } },
@Param('addressId') addressId: string,
) {
return this.usersService.deleteAddress(req.user.id, addressId);
}
@@ -186,7 +195,10 @@ export class UsersController {
},
},
})
setDefaultAddress(@Req() req: any, @Param('addressId') addressId: string) {
setDefaultAddress(
@Req() req: { user: { id: string } },
@Param('addressId') addressId: string,
) {
return this.usersService.setDefaultAddress(req.user.id, addressId);
}
@@ -208,7 +220,10 @@ export class UsersController {
},
})
@ApiBadRequestResponse({ description: 'مبلغ نامعتبر است' })
async topUpWallet(@Req() req: any, @Body() body: { amount: number }) {
async topUpWallet(
@Req() req: { user: { id: string } },
@Body() body: { amount: number },
) {
const amount = Number(body.amount);
if (!amount || amount <= 0) {
throw new BadRequestException('مبلغ شارژ باید بزرگتر از صفر باشد');
+20 -2
View File
@@ -56,7 +56,16 @@ describe('UsersService', () => {
});
it('should addAddress', async () => {
const addressData = { title: 'Home', isDefault: true };
const addressData = {
title: 'Home',
receptorName: 'Ali',
phone: '09123456789',
province: 'Tehran',
city: 'Tehran',
detail: 'Street 1',
zipCode: '1234567890',
isDefault: true,
};
mockPrisma.userAddress.create.mockResolvedValue({
id: 'addr-id',
...addressData,
@@ -72,7 +81,16 @@ describe('UsersService', () => {
});
it('should updateAddress', async () => {
const addressData = { title: 'Work', isDefault: true };
const addressData = {
title: 'Work',
receptorName: 'Ali',
phone: '09123456789',
province: 'Tehran',
city: 'Tehran',
detail: 'Street 2',
zipCode: '1234567890',
isDefault: true,
};
mockPrisma.userAddress.update.mockResolvedValue({
id: 'addr-id',
...addressData,
+19 -3
View File
@@ -1,6 +1,18 @@
import { Injectable } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
export interface UserAddressInput {
title: string;
receptorName: string;
phone: string;
province: string;
city: string;
detail: string;
zipCode: string;
isDefault?: boolean;
}
@Injectable()
export class UsersService {
constructor(private prisma: PrismaService) {}
@@ -37,7 +49,7 @@ export class UsersService {
});
}
async update(id: string, data: any) {
async update(id: string, data: Prisma.UserUpdateInput) {
return this.prisma.user.update({
where: { id },
data,
@@ -56,7 +68,7 @@ export class UsersService {
});
}
async addAddress(userId: string, data: any) {
async addAddress(userId: string, data: UserAddressInput) {
if (data.isDefault) {
await this.prisma.userAddress.updateMany({
where: { userId },
@@ -78,7 +90,11 @@ export class UsersService {
});
}
async updateAddress(userId: string, addressId: string, data: any) {
async updateAddress(
userId: string,
addressId: string,
data: UserAddressInput,
) {
if (data.isDefault) {
await this.prisma.userAddress.updateMany({
where: { userId, NOT: { id: addressId } },
+2 -2
View File
@@ -9,7 +9,7 @@ import {
Query,
UseGuards,
} from '@nestjs/common';
import { VideosService } from './videos.service';
import { VideosService, VideoQuery } from './videos.service';
import { CreateVideoDto } from './dto/create-video.dto';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import {
@@ -30,7 +30,7 @@ export class VideosController {
@ApiQuery({ name: 'limit', required: false })
@ApiQuery({ name: 'search', required: false })
@ApiQuery({ name: 'featured', required: false })
findAll(@Query() query: any) {
findAll(@Query() query: VideoQuery) {
return this.videosService.findAll(query);
}
+38 -44
View File
@@ -1,21 +1,26 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
import { CreateVideoDto } from './dto/create-video.dto';
export class VideoQuery {
page?: number | string;
limit?: number | string;
search?: string;
featured?: boolean | string;
}
@Injectable()
export class VideosService {
constructor(private readonly prisma: PrismaService) {}
private get video() {
return (this.prisma as any).video;
}
async findAll(query: any) {
async findAll(query: VideoQuery = {}) {
const page = Number(query.page) || 1;
const limit = Number(query.limit) || 20;
const skip = (page - 1) * limit;
const where: any = {};
const where: Prisma.VideoWhereInput = {};
if (query.search) {
where.OR = [
{ title: { contains: query.search, mode: 'insensitive' } },
@@ -23,82 +28,71 @@ export class VideosService {
{ description: { contains: query.search, mode: 'insensitive' } },
];
}
if (query.featured !== undefined) {
where.isFeatured = query.featured === 'true' || query.featured === true;
}
const [data, total] = await Promise.all([
this.video.findMany({
const [videos, total] = await Promise.all([
this.prisma.video.findMany({
where,
skip,
take: limit,
orderBy: [{ isFeatured: 'desc' }, { createdAt: 'desc' }],
orderBy: { createdAt: 'desc' },
}),
this.video.count({ where }),
this.prisma.video.count({ where }),
]);
return {
data,
videos,
meta: {
total,
page,
limit,
lastPage: Math.ceil(total / limit),
totalPages: Math.ceil(total / limit),
},
};
}
async findOne(id: string, incrementView = false) {
const video = await this.video.findUnique({
async findOne(id: string) {
const video = await this.prisma.video.findUnique({
where: { id },
});
if (!video) {
throw new NotFoundException('ویدئوی مورد نظر یافت نشد');
}
if (incrementView) {
await this.video
.update({
where: { id },
data: { viewsCount: { increment: 1 } },
})
.catch(() => {});
throw new NotFoundException(`Video with ID ${id} not found`);
}
return video;
}
async create(dto: CreateVideoDto) {
return this.video.create({
async create(createVideoDto: CreateVideoDto) {
return this.prisma.video.create({
data: {
title: dto.title,
doctor: dto.doctor,
duration: dto.duration || '۰۲:۰۰',
thumbnail: dto.thumbnail,
videoUrl: dto.videoUrl,
description: dto.description,
isFeatured: dto.isFeatured ?? false,
title: createVideoDto.title,
doctor: createVideoDto.doctor,
duration: createVideoDto.duration || '00:00',
videoUrl: createVideoDto.videoUrl,
thumbnail: createVideoDto.thumbnail || '',
description: createVideoDto.description || null,
isFeatured: createVideoDto.isFeatured || false,
},
});
}
async update(id: string, dto: Partial<CreateVideoDto>) {
async update(id: string, updateVideoDto: Partial<CreateVideoDto>) {
await this.findOne(id);
return this.video.update({
return this.prisma.video.update({
where: { id },
data: {
title: dto.title,
doctor: dto.doctor,
duration: dto.duration,
thumbnail: dto.thumbnail,
videoUrl: dto.videoUrl,
description: dto.description,
isFeatured: dto.isFeatured,
},
data: updateVideoDto,
});
}
async remove(id: string) {
await this.findOne(id);
return this.video.delete({
return this.prisma.video.delete({
where: { id },
});
}
@@ -26,7 +26,10 @@ export class WholesaleController {
@ApiOperation({
summary: 'ثبت درخواست همکاری عمده‌فروشی (ارسال پروانه کلینیک/داروخانه)',
})
applyForWholesale(@Request() req: any, @Body() dto: WholesaleApplyDto) {
applyForWholesale(
@Request() req: { user: { id: string } },
@Body() dto: WholesaleApplyDto,
) {
return this.wholesaleService.applyForWholesale(req.user.id, dto);
}
+2 -1
View File
@@ -1,4 +1,5 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
import { PaginationDto } from '../common/dto/pagination.dto';
@@ -17,7 +18,7 @@ export class WikiService {
const allowedSortFields = ['key', 'term', 'wikiId'];
const sortBy = allowedSortFields.includes(rawSortBy) ? rawSortBy : 'term';
const whereClause: any = {};
const whereClause: Prisma.ScientificTermWhereInput = {};
if (search) {
whereClause.OR = [
{ term: { contains: search, mode: 'insensitive' } },