improvement: Nothing done is related to subscriptions!!!
This commit is contained in:
@@ -21,7 +21,13 @@ export class PatientsService {
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
return { success: true, data: existing, existing: true as const };
|
||||
if (
|
||||
!existing.isWalkIn &&
|
||||
existing.createdByOrganizationId === organizationId
|
||||
) {
|
||||
return { success: true, data: existing, existing: true as const };
|
||||
}
|
||||
throw new AppException(ErrorCode.PATIENT_MOBILE_UNAVAILABLE, HttpStatus.CONFLICT);
|
||||
}
|
||||
|
||||
const patient = await this.prisma.patient.create({
|
||||
@@ -39,12 +45,13 @@ export class PatientsService {
|
||||
return { success: true, data: patient, existing: false as const };
|
||||
}
|
||||
|
||||
async findAll(query: ListPatientsDto) {
|
||||
async findAll(query: ListPatientsDto, organizationId: string) {
|
||||
const { page = 1, limit = 10, q } = query;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const where = {
|
||||
isWalkIn: false,
|
||||
createdByOrganizationId: organizationId,
|
||||
...(q?.trim() ? this.buildSearchWhere(q.trim()) : {}),
|
||||
};
|
||||
|
||||
@@ -72,27 +79,13 @@ export class PatientsService {
|
||||
};
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const patient = await this.prisma.patient.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!patient || patient.isWalkIn) {
|
||||
throw new AppException(ErrorCode.PATIENT_NOT_FOUND, HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
async findOne(id: string, organizationId: string) {
|
||||
const patient = await this.findNamedPatientInOrg(id, organizationId);
|
||||
return { success: true, data: patient };
|
||||
}
|
||||
|
||||
async update(id: string, updatePatientDto: UpdatePatientDto) {
|
||||
await this.ensurePatient(id);
|
||||
const patient = await this.prisma.patient.findUnique({
|
||||
where: { id },
|
||||
select: { isWalkIn: true },
|
||||
});
|
||||
if (patient?.isWalkIn) {
|
||||
throw new AppException(ErrorCode.PATIENT_NOT_FOUND, HttpStatus.NOT_FOUND);
|
||||
}
|
||||
async update(id: string, updatePatientDto: UpdatePatientDto, organizationId: string) {
|
||||
await this.findNamedPatientInOrg(id, organizationId);
|
||||
|
||||
const data: {
|
||||
firstName?: string;
|
||||
@@ -110,7 +103,15 @@ export class PatientsService {
|
||||
data.lastName = this.requireNonEmptyName(updatePatientDto.lastName, 'lastName');
|
||||
}
|
||||
if (updatePatientDto.mobile !== undefined) {
|
||||
data.mobile = this.resolveMobile(updatePatientDto.mobile);
|
||||
const mobile = this.resolveMobile(updatePatientDto.mobile);
|
||||
const taken = await this.prisma.patient.findUnique({
|
||||
where: { mobile },
|
||||
select: { id: true, createdByOrganizationId: true, isWalkIn: true },
|
||||
});
|
||||
if (taken && taken.id !== id) {
|
||||
throw new AppException(ErrorCode.PATIENT_MOBILE_UNAVAILABLE, HttpStatus.CONFLICT);
|
||||
}
|
||||
data.mobile = mobile;
|
||||
}
|
||||
if (updatePatientDto.email !== undefined) {
|
||||
data.email = updatePatientDto.email?.trim() || null;
|
||||
@@ -138,7 +139,7 @@ export class PatientsService {
|
||||
actorUserId: string,
|
||||
) {
|
||||
await this.assertCanViewPatients(actorUserId, organizationId);
|
||||
await this.ensurePatient(patientId);
|
||||
await this.findNamedPatientInOrg(patientId, organizationId);
|
||||
|
||||
const items = await this.prisma.appointment.findMany({
|
||||
where: { organizationId, patientId },
|
||||
@@ -241,14 +242,18 @@ export class PatientsService {
|
||||
}
|
||||
}
|
||||
|
||||
private async ensurePatient(id: string) {
|
||||
const patient = await this.prisma.patient.findUnique({
|
||||
where: { id },
|
||||
select: { id: true },
|
||||
private async findNamedPatientInOrg(id: string, organizationId: string) {
|
||||
const patient = await this.prisma.patient.findFirst({
|
||||
where: {
|
||||
id,
|
||||
isWalkIn: false,
|
||||
createdByOrganizationId: organizationId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!patient) {
|
||||
throw new AppException(ErrorCode.PATIENT_NOT_FOUND, HttpStatus.NOT_FOUND);
|
||||
}
|
||||
return patient;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user