From 813c96742dfbd723c28c041d395c832e60598f88 Mon Sep 17 00:00:00 2001 From: parsa aghaei Date: Mon, 17 Aug 2026 16:25:20 +0330 Subject: [PATCH] feat(doctors): add doctors management, image optimization, testimonials section and clean fallbacks --- backend/prisma/schema.prisma | 35 +- backend/src/app.module.ts | 2 + backend/src/doctors/doctors.controller.ts | 60 ++ backend/src/doctors/doctors.module.ts | 12 + backend/src/doctors/doctors.service.ts | 102 ++++ backend/src/videos/dto/create-video.dto.ts | 7 + backend/src/videos/videos.service.ts | 13 + .../admin-panel/src/components/Sidebar.tsx | 2 + .../admin-panel/src/pages/DoctorsManager.tsx | 538 ++++++++++++++++++ frontend/admin-panel/src/pages/Videos.tsx | 65 ++- .../admin-panel/src/routes/adminRoutes.tsx | 3 +- frontend/application/app/HomeClient.tsx | 2 + frontend/application/components/SafeImage.tsx | 2 +- .../components/TestimonialsSection.tsx | 151 +++++ .../application/components/VetGallery.tsx | 121 ++-- .../application/components/VideosPage.tsx | 182 +++--- frontend/application/next.config.ts | 8 + 17 files changed, 1135 insertions(+), 170 deletions(-) create mode 100644 backend/src/doctors/doctors.controller.ts create mode 100644 backend/src/doctors/doctors.module.ts create mode 100644 backend/src/doctors/doctors.service.ts create mode 100644 frontend/admin-panel/src/pages/DoctorsManager.tsx create mode 100644 frontend/application/components/TestimonialsSection.tsx diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma index aa417cc9..3bf0d29d 100644 --- a/backend/prisma/schema.prisma +++ b/backend/prisma/schema.prisma @@ -174,17 +174,7 @@ model Product { @@map("products") } -model Doctor { - id String @id @default(uuid()) @db.Uuid - name String @db.VarChar(150) - title String @db.VarChar(150) - avatarUrl String? @map("avatar_url") @db.Text - bio String? @db.Text - clinic String? @db.VarChar(200) - createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz() - @@map("doctors") -} model ProductIngredient { productId String @map("product_id") @db.Uuid @@ -463,10 +453,32 @@ model SmartAdvisorRule { @@map("smart_advisor_rules") } +model Doctor { + id String @id @default(uuid()) @db.Uuid + name String @db.VarChar(150) + title String? @db.VarChar(150) + clinicName String? @map("clinic_name") @db.VarChar(200) + avatarUrl String? @map("avatar_url") @db.Text + bio String? @db.Text + cv String? @db.Text + phone String? @db.VarChar(50) + email String? @db.VarChar(150) + instagram String? @db.VarChar(100) + isActive Boolean @default(true) @map("is_active") + order Int @default(0) + createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz() + updatedAt DateTime @default(now()) @updatedAt @map("updated_at") @db.Timestamptz() + + videos Video[] + + @@map("doctors") +} + model Video { id String @id @default(uuid()) @db.Uuid title String @db.VarChar(200) doctor String @db.VarChar(150) + doctorId String? @map("doctor_id") @db.Uuid duration String @default("02:00") @db.VarChar(20) thumbnail String? @db.Text videoUrl String @map("video_url") @db.Text @@ -476,6 +488,9 @@ model Video { createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz() updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz() + doctorRef Doctor? @relation(fields: [doctorId], references: [id], onDelete: SetNull) + + @@index([doctorId]) @@map("videos") } diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index 6030b759..1e8a5930 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -30,6 +30,7 @@ import { B2BModule } from './b2b/b2b.module'; import { PaymentModule } from './payment/payment.module'; import { TicketsModule } from './tickets/tickets.module'; import { ReviewsModule } from './reviews/reviews.module'; +import { DoctorsModule } from './doctors/doctors.module'; import { Request, Response, NextFunction } from 'express'; @@ -48,6 +49,7 @@ import { Request, Response, NextFunction } from 'express'; PaymentModule, TicketsModule, ReviewsModule, + DoctorsModule, ThrottlerModule.forRoot([ { ttl: 60000, diff --git a/backend/src/doctors/doctors.controller.ts b/backend/src/doctors/doctors.controller.ts new file mode 100644 index 00000000..b604757a --- /dev/null +++ b/backend/src/doctors/doctors.controller.ts @@ -0,0 +1,60 @@ +import { + Controller, + Get, + Post, + Put, + Delete, + Body, + Param, + Query, + UseGuards, +} from '@nestjs/common'; +import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger'; +import { DoctorsService, DoctorQuery } from './doctors.service'; +import { JwtAuthGuard } from '../auth/jwt-auth.guard'; +import { Prisma } from '@prisma/client'; + +@ApiTags('Doctors') +@Controller('doctors') +export class DoctorsController { + constructor(private readonly doctorsService: DoctorsService) {} + + @Get() + @ApiOperation({ summary: 'Get list of all doctors / specialists' }) + findAll(@Query() query: DoctorQuery) { + return this.doctorsService.findAll(query); + } + + @Get(':id') + @ApiOperation({ summary: 'Get doctor by ID with their videos and CV' }) + findOne(@Param('id') id: string) { + return this.doctorsService.findOne(id); + } + + @Post() + @UseGuards(JwtAuthGuard) + @ApiBearerAuth() + @ApiOperation({ summary: 'Create new doctor' }) + create(@Body() createDoctorDto: Prisma.DoctorCreateInput) { + return this.doctorsService.create(createDoctorDto); + } + + @Put(':id') + @UseGuards(JwtAuthGuard) + @ApiBearerAuth() + @ApiOperation({ summary: 'Update doctor' }) + update( + @Param('id') id: string, + @Body() updateDoctorDto: Prisma.DoctorUpdateInput, + ) { + return this.doctorsService.update(id, updateDoctorDto); + } + + @Delete(':id') + @UseGuards(JwtAuthGuard) + @ApiBearerAuth() + @ApiOperation({ summary: 'Delete doctor' }) + remove(@Param('id') id: string) { + return this.doctorsService.remove(id); + } +} diff --git a/backend/src/doctors/doctors.module.ts b/backend/src/doctors/doctors.module.ts new file mode 100644 index 00000000..63777f06 --- /dev/null +++ b/backend/src/doctors/doctors.module.ts @@ -0,0 +1,12 @@ +import { Module } from '@nestjs/common'; +import { DoctorsService } from './doctors.service'; +import { DoctorsController } from './doctors.controller'; +import { PrismaModule } from '../prisma/prisma.module'; + +@Module({ + imports: [PrismaModule], + controllers: [DoctorsController], + providers: [DoctorsService], + exports: [DoctorsService], +}) +export class DoctorsModule {} diff --git a/backend/src/doctors/doctors.service.ts b/backend/src/doctors/doctors.service.ts new file mode 100644 index 00000000..89ce6272 --- /dev/null +++ b/backend/src/doctors/doctors.service.ts @@ -0,0 +1,102 @@ +import { Injectable, NotFoundException } from '@nestjs/common'; +import { Prisma } from '@prisma/client'; +import { PrismaService } from '../prisma/prisma.service'; + +export class DoctorQuery { + page?: number | string; + limit?: number | string; + search?: string; + activeOnly?: boolean | string; +} + +@Injectable() +export class DoctorsService { + constructor(private readonly prisma: PrismaService) {} + + async findAll(query: DoctorQuery = {}) { + const page = Number(query.page) || 1; + const limit = Number(query.limit) || 50; + const skip = (page - 1) * limit; + + const where: Prisma.DoctorWhereInput = {}; + + if (query.activeOnly === 'true' || query.activeOnly === true) { + where.isActive = true; + } + + if (query.search) { + where.OR = [ + { name: { contains: query.search, mode: 'insensitive' } }, + { title: { contains: query.search, mode: 'insensitive' } }, + { clinicName: { contains: query.search, mode: 'insensitive' } }, + { bio: { contains: query.search, mode: 'insensitive' } }, + { cv: { contains: query.search, mode: 'insensitive' } }, + ]; + } + + const [doctors, total] = await Promise.all([ + this.prisma.doctor.findMany({ + where, + skip, + take: limit, + orderBy: [{ order: 'asc' }, { createdAt: 'desc' }], + include: { + _count: { + select: { videos: true }, + }, + }, + }), + this.prisma.doctor.count({ where }), + ]); + + return { + doctors, + meta: { + total, + page, + limit, + totalPages: Math.ceil(total / limit), + }, + }; + } + + async findOne(id: string) { + const doctor = await this.prisma.doctor.findUnique({ + where: { id }, + include: { + videos: { + orderBy: { createdAt: 'desc' }, + }, + }, + }); + + if (!doctor) { + throw new NotFoundException(`پزشک با شناسه ${id} یافت نشد`); + } + + return doctor; + } + + async create(data: Prisma.DoctorCreateInput) { + return this.prisma.doctor.create({ + data, + }); + } + + async update(id: string, data: Prisma.DoctorUpdateInput) { + await this.findOne(id); + + return this.prisma.doctor.update({ + where: { id }, + data, + }); + } + + async remove(id: string) { + await this.findOne(id); + + return this.prisma.doctor.delete({ + where: { id }, + }); + } +} diff --git a/backend/src/videos/dto/create-video.dto.ts b/backend/src/videos/dto/create-video.dto.ts index 9de7c3dc..3dabb9d3 100644 --- a/backend/src/videos/dto/create-video.dto.ts +++ b/backend/src/videos/dto/create-video.dto.ts @@ -18,6 +18,13 @@ export class CreateVideoDto { @IsNotEmpty() doctor: string; + @ApiPropertyOptional({ + description: 'شناسه پزشک مرتبط در سیستم', + }) + @IsString() + @IsOptional() + doctorId?: string; + @ApiPropertyOptional({ description: 'مدت زمان ویدئو', example: '۰۲:۳۰' }) @IsString() @IsOptional() diff --git a/backend/src/videos/videos.service.ts b/backend/src/videos/videos.service.ts index a40d22fa..90c18639 100644 --- a/backend/src/videos/videos.service.ts +++ b/backend/src/videos/videos.service.ts @@ -39,6 +39,9 @@ export class VideosService { skip, take: limit, orderBy: { createdAt: 'desc' }, + include: { + doctorRef: true, + }, }), this.prisma.video.count({ where }), ]); @@ -57,6 +60,9 @@ export class VideosService { async findOne(id: string) { const video = await this.prisma.video.findUnique({ where: { id }, + include: { + doctorRef: true, + }, }); if (!video) { @@ -71,12 +77,16 @@ export class VideosService { data: { title: createVideoDto.title, doctor: createVideoDto.doctor, + doctorId: createVideoDto.doctorId || null, duration: createVideoDto.duration || '00:00', videoUrl: createVideoDto.videoUrl, thumbnail: createVideoDto.thumbnail || '', description: createVideoDto.description || null, isFeatured: createVideoDto.isFeatured || false, }, + include: { + doctorRef: true, + }, }); } @@ -86,6 +96,9 @@ export class VideosService { return this.prisma.video.update({ where: { id }, data: updateVideoDto, + include: { + doctorRef: true, + }, }); } diff --git a/frontend/admin-panel/src/components/Sidebar.tsx b/frontend/admin-panel/src/components/Sidebar.tsx index 7c1b547e..b7b48e61 100644 --- a/frontend/admin-panel/src/components/Sidebar.tsx +++ b/frontend/admin-panel/src/components/Sidebar.tsx @@ -23,6 +23,7 @@ import { Settings, ShieldCheck, Languages, + UserCheck, DollarSign, Sliders, Globe, @@ -138,6 +139,7 @@ export default function Sidebar({ isOpen, setIsOpen }: SidebarProps) { { icon: FileText, label: 'وبلاگ و مقالات', path: '/blogs' }, { icon: FlaskConical, label: 'دانشنامه ترکیبات', path: '/ingredients' }, { icon: BookOpen, label: 'دانشنامه عمومی و علمی', path: '/wiki' }, + { icon: UserCheck, label: 'پزشکان و کادر علمی', path: '/doctors' }, { icon: MessageSquareQuote, label: 'نظرات و گواهی متخصصان', path: '/testimonials' }, { icon: Video, label: 'ویدئوها و آموزش‌ها', path: '/videos' }, { icon: Image, label: 'کتابخانه رسانه', path: '/media' }, diff --git a/frontend/admin-panel/src/pages/DoctorsManager.tsx b/frontend/admin-panel/src/pages/DoctorsManager.tsx new file mode 100644 index 00000000..98800307 --- /dev/null +++ b/frontend/admin-panel/src/pages/DoctorsManager.tsx @@ -0,0 +1,538 @@ +import React, { useState, useEffect, useCallback } from 'react'; +import { Plus, Search, Edit2, Trash2, UserCheck, Star, X, ImageIcon, Phone, Mail, FileText, CheckCircle2, ShieldCheck, Globe } from 'lucide-react'; +import { toast } from 'react-hot-toast'; +import api, { BASE_DOMAIN } from '../services/api'; +import ConfirmModal from '../components/ui/ConfirmModal'; +import MediaSelector from '../components/ui/MediaSelector'; +import Spinner from '../components/ui/Spinner'; + +export interface Doctor { + id: string; + name: string; + title?: string; + clinicName?: string; + avatarUrl?: string; + bio?: string; + cv?: string; + phone?: string; + email?: string; + instagram?: string; + isActive: boolean; + order: number; + _count?: { + videos: number; + }; + createdAt: string; +} + +export default function DoctorsManager() { + const [doctors, setDoctors] = useState([]); + const [isLoading, setIsLoading] = useState(true); + const [searchTerm, setSearchTerm] = useState(''); + const [isModalOpen, setIsModalOpen] = useState(false); + const [editingDoctor, setEditingDoctor] = useState(null); + const [deleteTargetId, setDeleteTargetId] = useState(null); + const [isAvatarMediaOpen, setIsAvatarMediaOpen] = useState(false); + const [isCvDetailsOpen, setIsCvDetailsOpen] = useState(null); + + const [formData, setFormData] = useState({ + name: '', + title: '', + clinicName: '', + avatarUrl: '', + bio: '', + cv: '', + phone: '', + email: '', + instagram: '', + isActive: true, + order: 0, + }); + + const fetchDoctors = useCallback(async () => { + try { + setIsLoading(true); + const res = await api.get('/doctors', { + params: { search: searchTerm, limit: 100 }, + }); + const raw = res.data; + const list = Array.isArray(raw?.doctors) + ? raw.doctors + : Array.isArray(raw?.data?.doctors) + ? raw.data.doctors + : Array.isArray(raw?.data) + ? raw.data + : Array.isArray(raw) + ? raw + : []; + setDoctors(list); + } catch (err) { + console.error('Failed to fetch doctors:', err); + toast.error('خطا در دریافت لیست پزشکان'); + setDoctors([]); + } finally { + setIsLoading(false); + } + }, [searchTerm]); + + useEffect(() => { + fetchDoctors(); + }, [fetchDoctors]); + + const handleOpenModal = (doc?: Doctor) => { + if (doc) { + setEditingDoctor(doc); + setFormData({ + name: doc.name || '', + title: doc.title || '', + clinicName: doc.clinicName || '', + avatarUrl: doc.avatarUrl || '', + bio: doc.bio || '', + cv: doc.cv || '', + phone: doc.phone || '', + email: doc.email || '', + instagram: doc.instagram || '', + isActive: doc.isActive !== false, + order: doc.order || 0, + }); + } else { + setEditingDoctor(null); + setFormData({ + name: '', + title: 'دامپزشک و متخصص تغذیه', + clinicName: '', + avatarUrl: '', + bio: '', + cv: '', + phone: '', + email: '', + instagram: '', + isActive: true, + order: doctors.length + 1, + }); + } + setIsModalOpen(true); + }; + + const handleSave = async (e: React.FormEvent) => { + e.preventDefault(); + if (!formData.name.trim()) { + toast.error('لطفاً نام پزشک را وارد کنید'); + return; + } + + try { + if (editingDoctor) { + await api.put(`/doctors/${editingDoctor.id}`, formData); + toast.success('اطلاعات پزشک با موفقیت بروزرسانی شد'); + } else { + await api.post('/doctors', formData); + toast.success('پزشک جدید با موفقیت افزوده شد'); + } + setIsModalOpen(false); + await fetchDoctors(); + } catch (err) { + console.error('Save failed:', err); + toast.error('خطا در ذخیره‌سازی اطلاعات پزشک'); + } + }; + + const handleDelete = async () => { + if (!deleteTargetId) return; + try { + await api.delete(`/doctors/${deleteTargetId}`); + toast.success('پزشک با موفقیت حذف شد'); + setDeleteTargetId(null); + await fetchDoctors(); + } catch (err) { + console.error('Delete failed:', err); + toast.error('خطا در حذف پزشک'); + } + }; + + return ( +
+ {/* Header */} +
+
+
+
+ +
+

مدیریت پزشکان و کادر علمی

+
+

+ ثبت اطلاعات و رزومه (CV) دامپزشکان جهت انتساب به ویدئوها، مشاوره‌ها و معرفی کادر تخصصی +

+
+ +
+ + {/* Filter / Search */} +
+
+ + setSearchTerm(e.target.value)} + className="w-full pl-4 pr-11 py-2.5 bg-gray-50 border border-gray-200 rounded-xl text-sm focus:outline-none focus:border-purple-600 transition-colors" + /> +
+
+ مجموع: {doctors.length} نفر +
+
+ + {/* Grid List */} + {isLoading ? ( +
+ +
+ ) : doctors.length === 0 ? ( +
+
+ +
+

پزشکی یافت نشد

+

اولین پزشک یا متخصص کادر علمی را به سامانه اضافه کنید.

+ +
+ ) : ( +
+ {doctors.map((doc) => ( +
+
+ {/* Status Badge */} +
+ + {doc.isActive ? 'فعال' : 'غیرفعال'} + +
+ + +
+
+ + {/* Profile Header */} +
+
+ {doc.avatarUrl ? ( + {doc.name} + ) : ( + + )} +
+
+

{doc.name}

+

{doc.title || 'دامپزشک'}

+ {doc.clinicName && ( +

{doc.clinicName}

+ )} +
+
+ + {/* Bio snippet */} + {doc.bio && ( +

+ {doc.bio} +

+ )} + + {/* CV Button */} + {doc.cv && ( + + )} +
+ + {/* Footer info */} +
+ {doc._count?.videos || 0} ویدئوی آموزشی + {doc.phone && {doc.phone}} +
+
+ ))} +
+ )} + + {/* Create / Edit Modal */} + {isModalOpen && ( +
+
+ {/* Modal Header */} +
+
+
+ +
+
+

+ {editingDoctor ? 'ویرایش اطلاعات پزشک' : 'افزودن پزشک جدید'} +

+

مشخصات هویتی، آواتار و رزومه علمی پزشک

+
+
+ +
+ + {/* Form */} +
+
+ {/* Name */} +
+ + setFormData({ ...formData, name: e.target.value })} + className="w-full px-4 py-2.5 bg-gray-50 border border-gray-200 rounded-xl text-sm focus:outline-none focus:border-purple-600" + /> +
+ + {/* Title */} +
+ + setFormData({ ...formData, title: e.target.value })} + className="w-full px-4 py-2.5 bg-gray-50 border border-gray-200 rounded-xl text-sm focus:outline-none focus:border-purple-600" + /> +
+
+ + {/* Clinic Name */} +
+ + setFormData({ ...formData, clinicName: e.target.value })} + className="w-full px-4 py-2.5 bg-gray-50 border border-gray-200 rounded-xl text-sm focus:outline-none focus:border-purple-600" + /> +
+ + {/* Avatar Selector */} +
+ +
+ setFormData({ ...formData, avatarUrl: e.target.value })} + className="flex-1 px-4 py-2.5 bg-gray-50 border border-gray-200 rounded-xl text-sm focus:outline-none focus:border-purple-600" + /> + +
+ {formData.avatarUrl && ( +
+ Preview +
+ )} +
+ + {/* Bio */} +
+ +