2026-04-29 13:32:22 +03:30
|
|
|
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
|
|
|
|
import { PrismaService } from '../../../prisma/prisma.service';
|
2026-06-28 14:49:37 +03:30
|
|
|
import { isValidMobile, mobileSearchDigits, normalizeMobile } from '../../common/phone';
|
2026-04-29 13:32:22 +03:30
|
|
|
import { CreatePatientDto } from './dto/create-patient.dto';
|
|
|
|
|
import { ListPatientsDto } from './dto/list-patients.dto';
|
|
|
|
|
import { UpdatePatientDto } from './dto/update-patient.dto';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class PatientsService {
|
|
|
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
|
|
|
|
|
|
async create(createPatientDto: CreatePatientDto, organizationId: string) {
|
2026-06-28 14:49:37 +03:30
|
|
|
const mobile = this.resolveMobile(createPatientDto.mobile);
|
|
|
|
|
|
|
|
|
|
const existing = await this.prisma.patient.findUnique({
|
|
|
|
|
where: { mobile },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (existing) {
|
|
|
|
|
return { success: true, data: existing, existing: true as const };
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-29 13:32:22 +03:30
|
|
|
const patient = await this.prisma.patient.create({
|
|
|
|
|
data: {
|
2026-06-28 14:49:37 +03:30
|
|
|
firstName: createPatientDto.firstName.trim(),
|
|
|
|
|
lastName: createPatientDto.lastName.trim(),
|
|
|
|
|
mobile,
|
|
|
|
|
email: createPatientDto.email?.trim() || null,
|
|
|
|
|
notes: createPatientDto.notes?.trim() || null,
|
2026-04-29 13:32:22 +03:30
|
|
|
dateOfBirth: createPatientDto.dateOfBirth ? new Date(createPatientDto.dateOfBirth) : null,
|
2026-06-28 14:49:37 +03:30
|
|
|
createdByOrganizationId: organizationId,
|
2026-04-29 13:32:22 +03:30
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-28 14:49:37 +03:30
|
|
|
return { success: true, data: patient, existing: false as const };
|
2026-04-29 13:32:22 +03:30
|
|
|
}
|
|
|
|
|
|
2026-06-28 14:49:37 +03:30
|
|
|
async findAll(query: ListPatientsDto) {
|
2026-04-29 13:32:22 +03:30
|
|
|
const { page = 1, limit = 10, q } = query;
|
|
|
|
|
const skip = (page - 1) * limit;
|
|
|
|
|
|
2026-06-28 14:49:37 +03:30
|
|
|
const where = q?.trim()
|
|
|
|
|
? this.buildSearchWhere(q.trim())
|
|
|
|
|
: {};
|
2026-04-29 13:32:22 +03:30
|
|
|
|
|
|
|
|
const [items, total] = await Promise.all([
|
|
|
|
|
this.prisma.patient.findMany({
|
|
|
|
|
where,
|
|
|
|
|
skip,
|
|
|
|
|
take: limit,
|
|
|
|
|
orderBy: [{ updatedAt: 'desc' }],
|
|
|
|
|
}),
|
|
|
|
|
this.prisma.patient.count({ where }),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
data: {
|
|
|
|
|
items,
|
|
|
|
|
pagination: {
|
|
|
|
|
page,
|
|
|
|
|
limit,
|
|
|
|
|
total,
|
|
|
|
|
totalPages: Math.max(1, Math.ceil(total / limit)),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-28 14:49:37 +03:30
|
|
|
async findOne(id: string) {
|
|
|
|
|
const patient = await this.prisma.patient.findUnique({
|
|
|
|
|
where: { id },
|
2026-04-29 13:32:22 +03:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!patient) {
|
|
|
|
|
throw new NotFoundException('Patient not found');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { success: true, data: patient };
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-28 14:49:37 +03:30
|
|
|
async update(id: string, updatePatientDto: UpdatePatientDto) {
|
|
|
|
|
await this.ensurePatient(id);
|
|
|
|
|
|
|
|
|
|
const data: {
|
|
|
|
|
firstName?: string;
|
|
|
|
|
lastName?: string;
|
|
|
|
|
mobile?: string;
|
|
|
|
|
email?: string | null;
|
|
|
|
|
notes?: string | null;
|
|
|
|
|
dateOfBirth?: Date | null;
|
|
|
|
|
} = {};
|
|
|
|
|
|
|
|
|
|
if (updatePatientDto.firstName !== undefined) {
|
|
|
|
|
data.firstName = updatePatientDto.firstName.trim();
|
|
|
|
|
}
|
|
|
|
|
if (updatePatientDto.lastName !== undefined) {
|
|
|
|
|
data.lastName = updatePatientDto.lastName.trim();
|
|
|
|
|
}
|
|
|
|
|
if (updatePatientDto.mobile !== undefined) {
|
|
|
|
|
data.mobile = this.resolveMobile(updatePatientDto.mobile);
|
|
|
|
|
}
|
|
|
|
|
if (updatePatientDto.email !== undefined) {
|
|
|
|
|
data.email = updatePatientDto.email?.trim() || null;
|
|
|
|
|
}
|
|
|
|
|
if (updatePatientDto.notes !== undefined) {
|
|
|
|
|
data.notes = updatePatientDto.notes?.trim() || null;
|
|
|
|
|
}
|
|
|
|
|
if (updatePatientDto.dateOfBirth !== undefined) {
|
|
|
|
|
data.dateOfBirth = updatePatientDto.dateOfBirth
|
|
|
|
|
? new Date(updatePatientDto.dateOfBirth)
|
|
|
|
|
: null;
|
|
|
|
|
}
|
2026-04-29 13:32:22 +03:30
|
|
|
|
|
|
|
|
const patient = await this.prisma.patient.update({
|
|
|
|
|
where: { id },
|
2026-06-28 14:49:37 +03:30
|
|
|
data,
|
2026-04-29 13:32:22 +03:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return { success: true, data: patient };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getOrganizationIdFromUser(user: { organizationId?: string }) {
|
|
|
|
|
if (!user?.organizationId) {
|
|
|
|
|
throw new BadRequestException('Organization is not selected');
|
|
|
|
|
}
|
|
|
|
|
return user.organizationId;
|
|
|
|
|
}
|
2026-06-28 14:49:37 +03:30
|
|
|
|
|
|
|
|
private buildSearchWhere(q: string) {
|
|
|
|
|
const orConditions: Array<Record<string, unknown>> = [
|
|
|
|
|
{ firstName: { contains: q, mode: 'insensitive' as const } },
|
|
|
|
|
{ lastName: { contains: q, mode: 'insensitive' as const } },
|
|
|
|
|
{ email: { contains: q, mode: 'insensitive' as const } },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const normalized = normalizeMobile(q);
|
|
|
|
|
if (normalized) {
|
|
|
|
|
orConditions.push({ mobile: normalized });
|
|
|
|
|
} else {
|
|
|
|
|
const digits = mobileSearchDigits(q);
|
|
|
|
|
if (digits.length >= 3) {
|
|
|
|
|
orConditions.push({ mobile: { contains: digits } });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { OR: orConditions };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private resolveMobile(raw: string): string {
|
|
|
|
|
const mobile = normalizeMobile(raw);
|
|
|
|
|
if (!mobile || !isValidMobile(mobile)) {
|
|
|
|
|
throw new BadRequestException(
|
|
|
|
|
'Invalid mobile number. Use a valid Iran mobile (e.g. 09121234567 or +989121234567).',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return mobile;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async ensurePatient(id: string) {
|
|
|
|
|
const patient = await this.prisma.patient.findUnique({
|
|
|
|
|
where: { id },
|
|
|
|
|
select: { id: true },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!patient) {
|
|
|
|
|
throw new NotFoundException('Patient not found');
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-29 13:32:22 +03:30
|
|
|
}
|