style: apply standard ESLint & Prettier formatting across backend
Deploy Canina / deploy (push) Successful in 4m58s
Deploy Canina / deploy (push) Successful in 4m58s
This commit is contained in:
1 parent
ae636c8b03
commit
62bd8811fa
66 files changed
+1398
-607
No files matched your search
@@ -17,7 +17,10 @@ export class CreateHealthLogDto {
|
||||
@IsString()
|
||||
digestion: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'یادداشت یا توضیح اضافی', example: 'امروز فعالیت خوبی داشت.' })
|
||||
@ApiPropertyOptional({
|
||||
description: 'یادداشت یا توضیح اضافی',
|
||||
example: 'امروز فعالیت خوبی داشت.',
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
note?: string;
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString, IsOptional, IsNumber, IsArray } from 'class-validator';
|
||||
import {
|
||||
IsNotEmpty,
|
||||
IsString,
|
||||
IsOptional,
|
||||
IsNumber,
|
||||
IsArray,
|
||||
} from 'class-validator';
|
||||
|
||||
export class CreatePetDto {
|
||||
@ApiProperty({ description: 'نام حیوان خانگی', example: 'بادی' })
|
||||
|
||||
@@ -12,12 +12,18 @@ export class CreateReminderDto {
|
||||
@IsString()
|
||||
time: string;
|
||||
|
||||
@ApiProperty({ description: 'دوره زمانی (روزانه / هفتگی)', example: 'روزانه' })
|
||||
@ApiProperty({
|
||||
description: 'دوره زمانی (روزانه / هفتگی)',
|
||||
example: 'روزانه',
|
||||
})
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
frequency: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'شناسه محصول مربوطه', example: 'a1b2c3d4-1234-5678-abcd-ef1234567890' })
|
||||
@ApiPropertyOptional({
|
||||
description: 'شناسه محصول مربوطه',
|
||||
example: 'a1b2c3d4-1234-5678-abcd-ef1234567890',
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
productId?: string;
|
||||
|
||||
@@ -8,7 +8,9 @@ describe('PetsController', () => {
|
||||
|
||||
const mockPetsService = {
|
||||
create: jest.fn().mockResolvedValue({ id: 'pet-id', name: 'Buddy' }),
|
||||
findAllByUser: jest.fn().mockResolvedValue([{ id: 'pet-id', name: 'Buddy' }]),
|
||||
findAllByUser: jest
|
||||
.fn()
|
||||
.mockResolvedValue([{ id: 'pet-id', name: 'Buddy' }]),
|
||||
findOne: jest.fn().mockResolvedValue({ id: 'pet-id', name: 'Buddy' }),
|
||||
update: jest.fn().mockResolvedValue({ id: 'pet-id', name: 'Buddy New' }),
|
||||
remove: jest.fn().mockResolvedValue({ success: true }),
|
||||
@@ -17,9 +19,7 @@ describe('PetsController', () => {
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [PetsController],
|
||||
providers: [
|
||||
{ provide: PetsService, useValue: mockPetsService },
|
||||
],
|
||||
providers: [{ provide: PetsService, useValue: mockPetsService }],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<PetsController>(PetsController);
|
||||
|
||||
@@ -1,11 +1,32 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Req, HttpStatus, Query } from '@nestjs/common';
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
UseGuards,
|
||||
Req,
|
||||
HttpStatus,
|
||||
Query,
|
||||
} from '@nestjs/common';
|
||||
import { PetsService } from './pets.service';
|
||||
import { CreatePetDto } from './dto/create-pet.dto';
|
||||
import { UpdatePetDto } from './dto/update-pet.dto';
|
||||
import { CreateReminderDto } from './dto/create-reminder.dto';
|
||||
import { CreateHealthLogDto } from './dto/create-health-log.dto';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation, ApiResponse, ApiOkResponse, ApiCreatedResponse, ApiBadRequestResponse, ApiNotFoundResponse } from '@nestjs/swagger';
|
||||
import {
|
||||
ApiTags,
|
||||
ApiBearerAuth,
|
||||
ApiOperation,
|
||||
ApiResponse,
|
||||
ApiOkResponse,
|
||||
ApiCreatedResponse,
|
||||
ApiBadRequestResponse,
|
||||
ApiNotFoundResponse,
|
||||
} from '@nestjs/swagger';
|
||||
import { PaginationDto } from '../common/dto/pagination.dto';
|
||||
|
||||
@ApiTags('Pets - مدیریت حیوانات خانگی')
|
||||
@@ -20,9 +41,9 @@ import { PaginationDto } from '../common/dto/pagination.dto';
|
||||
success: false,
|
||||
message: 'Unauthorized',
|
||||
code: 'UNAUTHORIZED',
|
||||
details: {}
|
||||
}
|
||||
}
|
||||
details: {},
|
||||
},
|
||||
},
|
||||
})
|
||||
export class PetsController {
|
||||
constructor(private readonly petsService: PetsService) {}
|
||||
@@ -42,9 +63,9 @@ export class PetsController {
|
||||
weight: '25.50',
|
||||
activityLevel: 'متوسط',
|
||||
imageUrl: null,
|
||||
createdAt: '2026-05-26T18:10:00.000Z'
|
||||
}
|
||||
}
|
||||
createdAt: '2026-05-26T18:10:00.000Z',
|
||||
},
|
||||
},
|
||||
})
|
||||
@ApiBadRequestResponse({
|
||||
description: 'خطا در صحتسنجی فیلدهای ورودی',
|
||||
@@ -53,9 +74,9 @@ export class PetsController {
|
||||
success: false,
|
||||
message: 'نوع حیوان خانگی اجباری است',
|
||||
code: 'BAD_REQUEST',
|
||||
details: {}
|
||||
}
|
||||
}
|
||||
details: {},
|
||||
},
|
||||
},
|
||||
})
|
||||
create(@Req() req: any, @Body() createPetDto: CreatePetDto) {
|
||||
return this.petsService.create(req.user.id, createPetDto);
|
||||
@@ -78,17 +99,17 @@ export class PetsController {
|
||||
weight: '25.50',
|
||||
activityLevel: 'متوسط',
|
||||
imageUrl: null,
|
||||
createdAt: '2026-05-26T18:10:00.000Z'
|
||||
}
|
||||
createdAt: '2026-05-26T18:10:00.000Z',
|
||||
},
|
||||
],
|
||||
meta: {
|
||||
total: 1,
|
||||
page: 1,
|
||||
lastPage: 1,
|
||||
limit: 10
|
||||
}
|
||||
}
|
||||
}
|
||||
limit: 10,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
findAll(@Req() req: any, @Query() query: PaginationDto) {
|
||||
return this.petsService.findAllByUser(req.user.id, query);
|
||||
@@ -112,9 +133,9 @@ export class PetsController {
|
||||
medicalConditions: [],
|
||||
reminders: [],
|
||||
healthLogs: [],
|
||||
createdAt: '2026-05-26T18:10:00.000Z'
|
||||
}
|
||||
}
|
||||
createdAt: '2026-05-26T18:10:00.000Z',
|
||||
},
|
||||
},
|
||||
})
|
||||
@ApiNotFoundResponse({
|
||||
description: 'حیوان خانگی پیدا نشد یا متعلق به کاربر جاری نیست',
|
||||
@@ -123,9 +144,9 @@ export class PetsController {
|
||||
success: false,
|
||||
message: 'Pet not found or unauthorized',
|
||||
code: 'NOT_FOUND',
|
||||
details: {}
|
||||
}
|
||||
}
|
||||
details: {},
|
||||
},
|
||||
},
|
||||
})
|
||||
findOne(@Req() req: any, @Param('id') id: string) {
|
||||
return this.petsService.findOne(id, req.user.id);
|
||||
@@ -143,9 +164,9 @@ export class PetsController {
|
||||
breed: 'ژرمن شپرد',
|
||||
age: 4,
|
||||
weight: '26.00',
|
||||
activityLevel: 'زیاد'
|
||||
}
|
||||
}
|
||||
activityLevel: 'زیاد',
|
||||
},
|
||||
},
|
||||
})
|
||||
@ApiNotFoundResponse({
|
||||
description: 'حیوان خانگی یافت نشد',
|
||||
@@ -154,11 +175,15 @@ export class PetsController {
|
||||
success: false,
|
||||
message: 'Pet not found',
|
||||
code: 'NOT_FOUND',
|
||||
details: {}
|
||||
}
|
||||
}
|
||||
details: {},
|
||||
},
|
||||
},
|
||||
})
|
||||
update(@Req() req: any, @Param('id') id: string, @Body() updatePetDto: UpdatePetDto) {
|
||||
update(
|
||||
@Req() req: any,
|
||||
@Param('id') id: string,
|
||||
@Body() updatePetDto: UpdatePetDto,
|
||||
) {
|
||||
return this.petsService.update(id, req.user.id, updatePetDto);
|
||||
}
|
||||
|
||||
@@ -169,9 +194,9 @@ export class PetsController {
|
||||
schema: {
|
||||
example: {
|
||||
success: true,
|
||||
message: 'حیوان خانگی با موفقیت حذف شد'
|
||||
}
|
||||
}
|
||||
message: 'حیوان خانگی با موفقیت حذف شد',
|
||||
},
|
||||
},
|
||||
})
|
||||
@ApiNotFoundResponse({
|
||||
description: 'حیوان خانگی یافت نشد',
|
||||
@@ -180,9 +205,9 @@ export class PetsController {
|
||||
success: false,
|
||||
message: 'Pet not found',
|
||||
code: 'NOT_FOUND',
|
||||
details: {}
|
||||
}
|
||||
}
|
||||
details: {},
|
||||
},
|
||||
},
|
||||
})
|
||||
remove(@Req() req: any, @Param('id') id: string) {
|
||||
return this.petsService.remove(id, req.user.id);
|
||||
@@ -206,7 +231,12 @@ export class PetsController {
|
||||
@Param('reminderId') reminderId: string,
|
||||
@Body('date') date: string,
|
||||
) {
|
||||
return this.petsService.toggleReminder(req.user.id, petId, reminderId, date);
|
||||
return this.petsService.toggleReminder(
|
||||
req.user.id,
|
||||
petId,
|
||||
reminderId,
|
||||
date,
|
||||
);
|
||||
}
|
||||
|
||||
@Post(':petId/health-logs')
|
||||
@@ -216,6 +246,10 @@ export class PetsController {
|
||||
@Param('petId') petId: string,
|
||||
@Body() createHealthLogDto: CreateHealthLogDto,
|
||||
) {
|
||||
return this.petsService.addHealthLog(req.user.id, petId, createHealthLogDto);
|
||||
return this.petsService.addHealthLog(
|
||||
req.user.id,
|
||||
petId,
|
||||
createHealthLogDto,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -58,7 +58,9 @@ describe('PetsService', () => {
|
||||
describe('findOne', () => {
|
||||
it('should throw NotFoundException if pet not found', async () => {
|
||||
mockPrisma.pet.findFirst.mockResolvedValue(null);
|
||||
await expect(service.findOne('pet-id', 'user-id')).rejects.toThrow(NotFoundException);
|
||||
await expect(service.findOne('pet-id', 'user-id')).rejects.toThrow(
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
|
||||
it('should return pet if found', async () => {
|
||||
|
||||
@@ -9,7 +9,8 @@ export class PetsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async create(userId: string, createPetDto: CreatePetDto) {
|
||||
const { name, type, breed, weight, age, activityLevel, medicalConditions } = createPetDto;
|
||||
const { name, type, breed, weight, age, activityLevel, medicalConditions } =
|
||||
createPetDto;
|
||||
|
||||
return this.prisma.pet.create({
|
||||
data: {
|
||||
@@ -20,16 +21,24 @@ export class PetsService {
|
||||
age: age || 1,
|
||||
weight: weight || 0,
|
||||
userId,
|
||||
medicalConditions: medicalConditions?.length ? {
|
||||
create: medicalConditions.map(condition => ({ condition }))
|
||||
} : undefined,
|
||||
medicalConditions: medicalConditions?.length
|
||||
? {
|
||||
create: medicalConditions.map((condition) => ({ condition })),
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
include: { medicalConditions: true, reminders: true, healthLogs: true },
|
||||
});
|
||||
}
|
||||
|
||||
async findAllByUser(userId: string, filters: PaginationDto) {
|
||||
const { search, page = 1, limit = 10, sortBy = 'createdAt', sortOrder = 'desc' } = filters;
|
||||
const {
|
||||
search,
|
||||
page = 1,
|
||||
limit = 10,
|
||||
sortBy = 'createdAt',
|
||||
sortOrder = 'desc',
|
||||
} = filters;
|
||||
|
||||
const whereClause: any = { userId };
|
||||
if (search) {
|
||||
@@ -48,7 +57,7 @@ export class PetsService {
|
||||
take: limit,
|
||||
orderBy: { [sortBy]: sortOrder },
|
||||
}),
|
||||
this.prisma.pet.count({ where: whereClause })
|
||||
this.prisma.pet.count({ where: whereClause }),
|
||||
]);
|
||||
|
||||
return {
|
||||
@@ -58,7 +67,7 @@ export class PetsService {
|
||||
page,
|
||||
lastPage: Math.ceil(total / limit),
|
||||
limit,
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -77,7 +86,9 @@ export class PetsService {
|
||||
await this.findOne(id, userId); // Ensure it exists and belongs to user
|
||||
|
||||
if (updatePetDto.medicalConditions) {
|
||||
await this.prisma.petMedicalCondition.deleteMany({ where: { petId: id } });
|
||||
await this.prisma.petMedicalCondition.deleteMany({
|
||||
where: { petId: id },
|
||||
});
|
||||
}
|
||||
|
||||
return this.prisma.pet.update({
|
||||
@@ -89,9 +100,13 @@ export class PetsService {
|
||||
weight: updatePetDto.weight,
|
||||
age: updatePetDto.age,
|
||||
activityLevel: updatePetDto.activityLevel,
|
||||
medicalConditions: updatePetDto.medicalConditions ? {
|
||||
create: updatePetDto.medicalConditions.map(condition => ({ condition }))
|
||||
} : undefined,
|
||||
medicalConditions: updatePetDto.medicalConditions
|
||||
? {
|
||||
create: updatePetDto.medicalConditions.map((condition) => ({
|
||||
condition,
|
||||
})),
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
include: { medicalConditions: true, reminders: true, healthLogs: true },
|
||||
});
|
||||
@@ -117,7 +132,12 @@ export class PetsService {
|
||||
});
|
||||
}
|
||||
|
||||
async toggleReminder(userId: string, petId: string, reminderId: string, dateStr: string) {
|
||||
async toggleReminder(
|
||||
userId: string,
|
||||
petId: string,
|
||||
reminderId: string,
|
||||
dateStr: string,
|
||||
) {
|
||||
await this.findOne(petId, userId);
|
||||
|
||||
const reminder = await this.prisma.reminder.findUnique({
|
||||
@@ -128,7 +148,13 @@ export class PetsService {
|
||||
}
|
||||
|
||||
const dateParts = dateStr.split('-');
|
||||
const completedDate = new Date(Date.UTC(Number(dateParts[0]), Number(dateParts[1]) - 1, Number(dateParts[2])));
|
||||
const completedDate = new Date(
|
||||
Date.UTC(
|
||||
Number(dateParts[0]),
|
||||
Number(dateParts[1]) - 1,
|
||||
Number(dateParts[2]),
|
||||
),
|
||||
);
|
||||
|
||||
const existingCompletion = await this.prisma.reminderCompletion.findUnique({
|
||||
where: {
|
||||
|
||||
Reference in new issue
Block a user