> ## Documentation Index
> Fetch the complete documentation index at: https://docs.junction.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Order and Appointment Lifecycle

> Reference for all order and appointment lifecycle statuses in the at-home phlebotomy flow, from order creation through results delivery.

The **At-Home Phlebotomy** orders are composed of two different lifecycles, the **Order** lifecycle and
the **Appointment** lifecycle, detailed in the following sections.

## Order Lifecycle

As discussed in [`Lab Test Lifecycle - Statuses`](/lab/workflow/lab-test-lifecycle#statuses),
each lab testing modality has the following format:

`[HIGH-LEVEL STATUS].[TEST MODALITY].[LOW-LEVEL STATUS]`

For each modality, there can be multiple **low-level statuses**, for **At-Home Phlebotomy** the
possible low-level statuses are:

* `ordered`: Junction received the order, stored it into our system, and started processing it asynchronously.
* `requisition_created`: An order requisition form was validated and created with the partner laboratory, making the order available to be carried out.
* `requisition_bypassed`: An order requisition form wasn't created when the order was placed with us because it already existed.
* `appointment_pending`: An appointment was placed in Junction's system for the order, but doesn't have a scheduled date.
* `appointment_scheduled`: An appointment was scheduled or rescheduled for the order.
* `draw_completed`: The phlebotomy appointment was completed and the blood was drawn successfully.
* `appointment_cancelled`: The appointment was cancelled by either the patient, Junction, or you.
* `partial_results`: The laboratory has started making partial results available.
* `completed`: The laboratory processed the blood sample and final results are available.
* `sample_error`: The collected sample was unprocessable by the lab.
* `cancelled`: The order was cancelled by either the patient, Junction, or you.

The Finite State Machine that defines the possible transitions for the low-level statuses described above is illustrated
in the following diagram.

<Frame caption="Possible state transitions for the At-Home Phlebotomy Order low-level statuses.">
  <img src="https://mintcdn.com/vital/deJB3IUUpJEYmyW_/img/at_home_phleb_order_lifecycle.png?fit=max&auto=format&n=deJB3IUUpJEYmyW_&q=85&s=08b897ababc0f4bec5e2586e8077e4a3" width="2711" height="3645" data-path="img/at_home_phleb_order_lifecycle.png" />
</Frame>

<br />

<Info>In **sandbox**, there is no async transition from the `ordered` state to the `requisition_created` state. This must be **manually triggered** via the **Junction Dashboard**.</Info>

## Appointment Lifecycle

The appointment lifecycle is separate from the order lifecycle, and it corresponds to a single appointment.
The possible statuses are defined as follows:

* `pending`: An appointment was placed in the system, and is pending updates from the phlebotomy service.
* `reserved`: The time slot has been reserved for the appointment, but the scheduling confirmation is waiting on the creation of the order requisition.
* `scheduled`: The appointment has been scheduled or rescheduled.
* `in-progress`: The phlebotomist is on their way to the patient's address.
* `cancelled`: The appointment was cancelled by either the patient, Junction, or you.
* `completed`: The phlebotomy appointment was completed and the blood was drawn successfully.

The Finite State Machine that defines the possible transitions for the appointment statuses described above is illustrated
in the following diagram.

<Frame caption="Possible state transitions for the At-Home Phlebotomy Appointment statuses.">
  <img src="https://mintcdn.com/vital/deJB3IUUpJEYmyW_/img/at_home_phleb_appointment_lifecycle.png?fit=max&auto=format&n=deJB3IUUpJEYmyW_&q=85&s=7ae3c4bbbf85458e87c7d23b06c6f38f" width="2527" height="2310" data-path="img/at_home_phleb_appointment_lifecycle.png" />
</Frame>

The events are related to a single appointment. An order can have multiple existing appointments in Junction's system, although only one appointment will be considered active
and returned when using the [GET Appointment endpoint](/api-reference/lab-testing-at-home-phlebotomy/get-appointment).

## Scheduling Appointments Before A Requisition Has Been Created

<Card horizontal icon="person-digging" color="#57164A">
  This feature is in **closed beta**.

  Interested in this feature? Get in touch with your Customer Success Manager.
</Card>

When this feature is enabled on your Team, you can create an At-Home Phlebotomy appointment even before the requisition has been created for
the corresponding Order (i.e., reaching the `requisition_created` status).

This allows you to build an experience that can order the lab test *and* book an appointment consecutively, without having to wait on the
asynchronous requisition creation process that may take an indeterminate amount of time.

### API Journey

<Steps>
  <Step title="Creating an appointment">
    You create an Appointment for an Order which does not yet have a requisition.

    For an [Appointment Ready Tier](/lab/at-home-phlebotomy/service-tiers) service, the Appointment starts with the `pending` status and immediately moves to the `reserved` status.

    For an [Appointment Request Tier](/lab/at-home-phlebotomy/service-tiers) service, the Appointment starts with the `pending`
    status. Once the service has penciled in the time slot, the Appointment moves to the `reserved` status — if the order
    requisition still has not yet been created by then.

    <Note>
      If the order requisition has been created on or before the time slot confirmation, the Appointment moves directly to the
      `confirmed` status. The `reserved` status would be skipped.
    </Note>
  </Step>

  <Step title="Waiting for Requisition Creation">
    The requisition is being created asynchronously.
  </Step>

  <Step title="Requisition has been created">
    The requisition has now been created.

    For all types of Appointments:

    1. The Order will move through the `requisition_created` and `appointment_pending` Order statuses in quick succession.

    You will receive two `labtest.order.updated` events, one each for the two status transitions.

    For all [Appointment Ready Tier](/lab/at-home-phlebotomy/service-tiers) Appointments and all the penciled
    [Appointment Request Tier](/lab/at-home-phlebotomy/service-tiers) Appointments:

    1. The Order will move further to the `appointment_scheduled` Order status in quick succession.
    2. The Appointment will move to the `confirmed` status automatically, which corresponds to the `scheduled` appointment event.

    You will additionally receive a `labtest.order.updated` event and a `labtest.appointment.updated` event for the status transition.
  </Step>
</Steps>

## Cancelling An Appointment

If an Appointment is cancelled **before** a requisition has been created:

* The Appointment will move to the `cancelled` status; and
* There is no change to the Order, which will remain in the `ordered` status.

If an Appointment is cancelled **after** a requisition has been created:

1. The Appointment will move to the `cancelled` status; and
2. The Order will move from the `appointment_pending` or `appointment_scheduled` status to the `appointment_cancelled` status.

### Auto-cancellation of appointments without requisition

In cases where appointments can be scheduled before a requisition has been created, the appointment will be automatically cancelled by Junction if the following happens:

1. More than 8 hours have passed since the order was created, and no requisition has been received.
2. There are fewer than 2 hours until the appointment start time, and no requisition has been received.

When booking an appointment with Getlabs, we strongly recommend that the appointment is booked at least 48 hours in advance to allow sufficient time for the requisition to be received and sent to the phlebotomist.
Getlabs automatically reschedules appointments that do not have a requisition form around 24 hours before the appointment start time. They also inform the patient about the rescheduling, directly via SMS.
Restricting the scheduling of appointments to at least 48 hours in advance allows Junction to handle the auto-cancellation of the appointment, which triggers an appointment cancelled webhook.
You can then use this for patient communications, rather than relying on Getlabs to do so.
