Skip to content

Generate Slots when PractitionerRole is updated #263

@lamurian

Description

@lamurian

User Story

As a practitioner, I want the system to trigger a background worker to generate or update Slot resources every time my PractitionerRole resource is created or updated so that the system availability accurately and automatically reflects my current schedule.

Description

Currently, changes to a practitioner's availability stored in the PractitionerRole are not automatically reflected as Slot resources. This requires waiting for the daily cron job to execute or when unavailable time trigger is set. We need a reliable mechanism where any creation or update to PractitionerRole with active status emits an event that causes the worker to generate the appropriate Slot resources for that practitioner role.

Key requirements:

  • Changes to PractitionerRole (create, update, patch, delete, activation toggles) must cause slot generation.
  • Generated Slot resources must be idempotent, consistent, and auditable.
  • Existing appointment bookings must be respected (do not delete Slot which status is busy or busy-tentative).

Proposed Flow

  • Emit a domain event when PractitionerRole is created, updated, or deleted. The event payload should include practitionerRoleId, practitionerId, timestamp, and a diff/summary of changed availability-related fields (if available).
  • Worker consumes the event and performs the following steps atomically where possible:
    • Load the authoritative PractitionerRole and related Practitioner and Location resources.
    • Compute desired Slot set for a configurable window (e.g., next 90 days) using availability rules.
    • Compare computed slots against persisted Slot resources:
      • Upsert new Slots where missing.
      • Update changed Slot metadata (status, start/end) where safe.
      • Delete obsolete Slot only if the status is free.
      • Never delete or override Slots that are linked to confirmed appointments.
    • Persist changes using bulk/batched FHIR operations where available (e.g., transaction bundles).
    • Emit a completion event with counts (created/updated/retired/retained/conflicts) and diagnostics.
  • Add monitoring and alerts: Log detailed audit trails that capture inputs, computed slot list, and decisions for conflict cases.
  • Document the behavior and provide guidance for practitioners/admins on how changes in PractitionerRole translate into slots and how to request manual regeneration.

Notes:

  • Idempotency: ensure each run can be safely re-applied (e.g., include a generationRunId / sourceEventId in Slot metadata) to prevent duplicate slots.
  • Concurrency & Ordering: handle concurrent updates to the same PractitionerRole (use event ordering, versioning, or optimistic locking).
  • Timezone and DST: compute slots in the practitioner's primary timezone and store slot timestamps in UTC with timezone metadata.
  • Appointments: treat Slots with linked confirmed Appointments as immutable; do not remove or convert them without special reconciliation.
  • Performance: use batched FHIR transactions for writes and paginate comparisons to avoid memory pressure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions