Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 49x 5x 8x 2x 53x 5x 48x 53x 53x 4x 47x 47x 1x 46x 46x 46x 9x 9x 1x 8x 8x 33x | import { InvalidDoctorNameError } from './errors/invalid-doctor-name.error';
import { FutureVisitRecordError } from './errors/future-visit-record.error';
import { MeasurementBeforeVisitError } from './errors/measurement-before-visit.error';
import { PastAppointmentDateError } from './errors/past-appointment-date.error';
export type AppointmentStatus = 'SCHEDULED' | 'COMPLETED' | 'CANCELLED';
export interface AppointmentProps {
id: string;
babyId: string;
scheduledAt: Date;
doctorName: string;
specialty: string | null;
location: string | null;
reason: string | null;
notes: string | null;
status: AppointmentStatus;
specialistId: string | null;
weightGrams: number | null;
heightMillimeters: number | null;
createdAt: Date;
}
export interface ScheduleAppointmentProps {
id: string;
babyId: string;
scheduledAt: Date;
doctorName: string;
specialty?: string | null;
location?: string | null;
reason?: string | null;
specialistId?: string | null;
weightGrams?: number | null;
heightMillimeters?: number | null;
createdAt?: Date;
referenceDate?: Date;
}
export class Appointment {
readonly id: string;
readonly babyId: string;
readonly scheduledAt: Date;
readonly doctorName: string;
readonly specialty: string | null;
readonly location: string | null;
readonly reason: string | null;
readonly notes: string | null;
readonly status: AppointmentStatus;
/**
* Optional link to the saved professional, **beside** `doctorName` and never instead of it.
*
* The name stays as it was typed on the day, so deleting a specialist cannot rewrite a visit
* that happened — the schema nulls this column and leaves the name alone.
*/
readonly specialistId: string | null;
readonly weightGrams: number | null;
readonly heightMillimeters: number | null;
readonly createdAt: Date;
private constructor(props: AppointmentProps) {
this.id = props.id;
this.babyId = props.babyId;
this.scheduledAt = props.scheduledAt;
this.doctorName = props.doctorName;
this.specialty = props.specialty;
this.location = props.location;
this.reason = props.reason;
this.notes = props.notes;
this.status = props.status;
this.specialistId = props.specialistId;
this.weightGrams = props.weightGrams;
this.heightMillimeters = props.heightMillimeters;
this.createdAt = props.createdAt;
}
// Only enforced here (creation/explicit reschedule), never on plain reconstruction,
// so completing or cancelling an appointment whose time has since passed still works.
static assertNotInThePast(scheduledAt: Date, referenceDate: Date = new Date()): void {
if (scheduledAt.getTime() < referenceDate.getTime()) {
throw new PastAppointmentDateError();
}
}
// The mirror of assertNotInThePast, and the reason recording a past visit is its own factory
// rather than a `schedule` with the check relaxed. A visit entered as already done cannot be in
// the future, exactly as one entered as upcoming cannot be in the past; both are data-entry
// mistakes, and one lenient path that accepted any date would catch neither.
static assertNotInTheFuture(scheduledAt: Date, referenceDate: Date = new Date()): void {
if (scheduledAt.getTime() > referenceDate.getTime()) {
throw new FutureVisitRecordError();
}
}
/**
* A weight or a height is something a scale and a stadiometer produced during the visit, so a
* visit still in the future cannot carry either.
*
* This is not tidiness. These rows are the growth series, and a measurement attached to an
* appointment that has not happened would plot a point in the future — visibly wrong on a chart,
* and impossible to tell apart from a real one once stored.
*/
static assertMeasurementsFollowTheVisit(
status: AppointmentStatus,
weightGrams: number | null | undefined,
heightMillimeters: number | null | undefined,
): void {
if (status !== 'SCHEDULED') {
return;
}
const hasWeight = weightGrams !== null && weightGrams !== undefined;
const hasHeight = heightMillimeters !== null && heightMillimeters !== undefined;
if (hasWeight || hasHeight) {
throw new MeasurementBeforeVisitError();
}
}
static schedule(props: ScheduleAppointmentProps): Appointment {
const doctorName = props.doctorName.trim();
if (doctorName.length === 0) {
throw new InvalidDoctorNameError();
}
Appointment.assertNotInThePast(props.scheduledAt, props.referenceDate);
Appointment.assertMeasurementsFollowTheVisit('SCHEDULED', props.weightGrams, props.heightMillimeters);
return new Appointment({
id: props.id,
babyId: props.babyId,
scheduledAt: props.scheduledAt,
doctorName,
specialty: props.specialty ?? null,
location: props.location ?? null,
reason: props.reason ?? null,
notes: null,
status: 'SCHEDULED',
specialistId: props.specialistId ?? null,
weightGrams: null,
heightMillimeters: null,
createdAt: props.createdAt ?? new Date(),
});
}
/**
* A consultation that already happened, entered after the fact.
*
* Not a variant of scheduling: the appointment is born COMPLETED, because there was never a
* moment when it was upcoming. Everything a parent actually wants to keep — who they saw, when,
* where, and what was said — is the same, which is why it is the same entity and not a second
* one beside it.
*/
static record(props: ScheduleAppointmentProps): Appointment {
const doctorName = props.doctorName.trim();
if (doctorName.length === 0) {
throw new InvalidDoctorNameError();
}
Appointment.assertNotInTheFuture(props.scheduledAt, props.referenceDate);
return new Appointment({
id: props.id,
babyId: props.babyId,
scheduledAt: props.scheduledAt,
doctorName,
specialty: props.specialty ?? null,
location: props.location ?? null,
reason: props.reason ?? null,
notes: null,
status: 'COMPLETED',
specialistId: props.specialistId ?? null,
weightGrams: props.weightGrams ?? null,
heightMillimeters: props.heightMillimeters ?? null,
createdAt: props.createdAt ?? new Date(),
});
}
static restore(props: AppointmentProps): Appointment {
return new Appointment(props);
}
}
|