iClinic Developers

Recipes

Mark an appointment “Arrived” or “Cancelled”

  1. Get recent encounters (visits) from the iClinic Integration API. Use status=all so that cancelled encounters are included.
  2. Match encounters to the appointment by phone number, with a visitDate within ±5 days of the appointment date. Skip encounters where phone = null.
  3. At least one matching encounter is not CANCELLED → Arrived. All matching encounters are CANCELLED → Cancelled. No matching encounters → leave the appointment unchanged.

Follow-up call list after a visit (day 0 / 3 / 6 / 30)

  1. Get encounters with status=FINISHED and from = today − 30 days (a range of exactly 31 days, enough for the day-30 call).
  2. Calculate the call date as visitDate + 0, 3, 6 and 30 days. Keep the encounters whose call date is today.
  3. Show patientName, phone and diagnosis. patientType = NEW means a new patient; OLD and RE_EXAMINATION mean returning patients.
Match appointments
const DAY = 86_400_000;
const dayOf = (date) => Date.parse(`${date}T00:00:00Z`);

// appointment: { phone, date: 'YYYY-MM-DD' }; encounters: fetched with status=all
export function appointmentOutcome(appointment, encounters) {
  const near = encounters.filter(
    (e) =>
      e.phone &&
      e.phone === appointment.phone &&
      Math.abs(dayOf(e.visitDate) - dayOf(appointment.date)) <= 5 * DAY,
  );
  if (near.length === 0) return null; // leave unchanged
  return near.some((e) => e.status !== 'CANCELLED') ? 'Arrived' : 'Cancelled';
}
Call list
const OFFSETS = [0, 3, 6, 30];
const addDays = (date, n) =>
  new Date(Date.parse(`${date}T00:00:00Z`) + n * 86_400_000).toISOString().slice(0, 10);

// encounters: status=FINISHED, from = today - 30 days
export function callsDueOn(today, encounters) {
  return encounters.flatMap((e) =>
    OFFSETS.filter((n) => addDays(e.visitDate, n) === today).map((n) => ({
      day: n,
      uuid: e.encounterUuid,
      name: e.patientName,
      phone: e.phone,
      kind: e.patientType === 'NEW' ? 'New patient' : 'Returning patient',
      diagnosis: e.diagnosis,
    })),
  );
}