71 lines
1.7 KiB
TypeScript
71 lines
1.7 KiB
TypeScript
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: PetQuery = {}) {
|
|
const page = Number(query.page) || 1;
|
|
const limit = Number(query.limit) || 10;
|
|
const skip = (page - 1) * limit;
|
|
|
|
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: limit,
|
|
orderBy: { createdAt: 'desc' },
|
|
include: {
|
|
user: {
|
|
select: {
|
|
firstName: true,
|
|
lastName: true,
|
|
email: true,
|
|
mobile: true,
|
|
},
|
|
},
|
|
medicalConditions: true,
|
|
reminders: true,
|
|
healthLogs: {
|
|
orderBy: { createdAt: 'desc' },
|
|
take: 10,
|
|
},
|
|
prescriptions: {
|
|
orderBy: { createdAt: 'desc' },
|
|
take: 5,
|
|
},
|
|
},
|
|
}),
|
|
this.prisma.pet.count({ where }),
|
|
]);
|
|
|
|
return {
|
|
data,
|
|
meta: {
|
|
total,
|
|
page,
|
|
limit,
|
|
lastPage: Math.ceil(total / limit),
|
|
},
|
|
};
|
|
}
|
|
|
|
async deletePet(id: string) {
|
|
const pet = await this.prisma.pet.findUnique({ where: { id } });
|
|
if (!pet) throw new NotFoundException('Pet not found');
|
|
return this.prisma.pet.delete({ where: { id } });
|
|
}
|
|
}
|