feat: implement backend auth and fix pet profile payload
This commit is contained in:
1 parent
82ab454647
commit
6027e635db
13 files changed
+493
-114
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString, IsOptional, IsNumber, IsBoolean, IsDateString } from 'class-validator';
|
||||
import { IsNotEmpty, IsString, IsOptional, IsNumber, IsArray } from 'class-validator';
|
||||
|
||||
export class CreatePetDto {
|
||||
@ApiProperty({ description: 'نام حیوان خانگی', example: 'بادی' })
|
||||
@@ -17,28 +17,24 @@ export class CreatePetDto {
|
||||
@IsString()
|
||||
breed?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'جنسیت', example: 'نر' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
gender?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'وزن (کیلوگرم)', example: 25.5 })
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
weight?: number;
|
||||
|
||||
@ApiPropertyOptional({ description: 'تاریخ تولد' })
|
||||
@ApiPropertyOptional({ description: 'سن (سال)', example: 3 })
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
birthDate?: string;
|
||||
@IsNumber()
|
||||
age?: number;
|
||||
|
||||
@ApiPropertyOptional({ description: 'عقیم شده است؟', example: false })
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
isNeutered?: boolean;
|
||||
|
||||
@ApiPropertyOptional({ description: 'سوابق پزشکی مختصر' })
|
||||
@ApiPropertyOptional({ description: 'سطح فعالیت', example: 'متوسط' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
medicalHistory?: string;
|
||||
activityLevel?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'سوابق پزشکی', type: [String] })
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
medicalConditions?: string[];
|
||||
}
|
||||
@@ -8,16 +8,22 @@ export class PetsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async create(userId: string, createPetDto: CreatePetDto) {
|
||||
const { name, type, breed, weight, age, activityLevel, medicalConditions } = createPetDto;
|
||||
|
||||
return this.prisma.pet.create({
|
||||
data: {
|
||||
name: createPetDto.name,
|
||||
type: createPetDto.type,
|
||||
breed: createPetDto.breed || '',
|
||||
activityLevel: 'متوسط', // Default
|
||||
age: 1, // Default
|
||||
weight: createPetDto.weight || 0,
|
||||
name,
|
||||
type,
|
||||
breed: breed || '',
|
||||
activityLevel: activityLevel || 'متوسط',
|
||||
age: age || 1,
|
||||
weight: weight || 0,
|
||||
userId,
|
||||
medicalConditions: medicalConditions?.length ? {
|
||||
create: medicalConditions.map(condition => ({ condition }))
|
||||
} : undefined,
|
||||
},
|
||||
include: { medicalConditions: true, reminders: true, healthLogs: true },
|
||||
});
|
||||
}
|
||||
|
||||
@@ -31,7 +37,7 @@ export class PetsService {
|
||||
async findOne(id: string, userId: string) {
|
||||
const pet = await this.prisma.pet.findFirst({
|
||||
where: { id, userId },
|
||||
include: { reminders: true, healthLogs: true },
|
||||
include: { medicalConditions: true, reminders: true, healthLogs: true },
|
||||
});
|
||||
if (!pet) {
|
||||
throw new NotFoundException('حیوان خانگی یافت نشد');
|
||||
@@ -41,6 +47,11 @@ export class PetsService {
|
||||
|
||||
async update(id: string, userId: string, updatePetDto: UpdatePetDto) {
|
||||
await this.findOne(id, userId); // Ensure it exists and belongs to user
|
||||
|
||||
if (updatePetDto.medicalConditions) {
|
||||
await this.prisma.petMedicalCondition.deleteMany({ where: { petId: id } });
|
||||
}
|
||||
|
||||
return this.prisma.pet.update({
|
||||
where: { id },
|
||||
data: {
|
||||
@@ -48,7 +59,13 @@ export class PetsService {
|
||||
type: updatePetDto.type,
|
||||
breed: updatePetDto.breed,
|
||||
weight: updatePetDto.weight,
|
||||
age: updatePetDto.age,
|
||||
activityLevel: updatePetDto.activityLevel,
|
||||
medicalConditions: updatePetDto.medicalConditions ? {
|
||||
create: updatePetDto.medicalConditions.map(condition => ({ condition }))
|
||||
} : undefined,
|
||||
},
|
||||
include: { medicalConditions: true, reminders: true, healthLogs: true },
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user