Kantoor. Bookings
(Kantoor v1.0.0)
Booking links: "send my availability" as a public page, /book/<token>.
The page and the visitor. A signed-in person has one Kantoor.Bookings.Page
(the public token, working hours, durations, buffer, notice and how far ahead).
A visitor needs no account: they choose a duration, a day and a free slot, and
give a name and an e-mail address. Nothing a visitor may see comes from the
calendar except the slots that are free.
Availability. Free slots are computed at the moment they are asked for and
again at the moment they are booked (slots/3, book/2): the host's working
hours for that weekday, minus the busy events on every calendar the host can
see (shared and subscribed ones too; an all-day event closes the day), with
the buffer kept free on both sides, the minimum notice and the horizon. A
booking is written with Kantoor.Events.create/2 into the host's own
calendar, so invitations, CalDAV and the rest of the product treat it like any
other event. Nothing is ever written into a shared calendar.
Double booking. book/2 and reschedule/2 take a PostgreSQL advisory lock
for the page and check the slot again inside the transaction.
The manage link. Each booking gets a 256-bit token; only its SHA-256 hash is stored. The e-mail carries the plain token in one link to a page that shows the appointment, moves it or cancels it. The booking's event is deleted on cancel and changed on a move, and the booker is told in each case.
Writes are the host's. Every write uses a scope made for the page's owner
(Kantoor.Accounts.Scope.for_user/1), so Kantoor.Events checks the same
permissions it checks for the person at the keyboard.
Summary
Functions
Books a slot. attrs (string keys): "date" (YYYY-MM-DD), "time"
(HH:MM), "duration" (minutes), "name", "email", "note" and the
optional honeypot "website" (must be empty).
A booking by its manage token (only the hash is stored).
Every buffer a page may keep, in minutes.
Cancels a booking by its manage token: the event is deleted.
Cancels one of the owner's own bookings (from the settings page).
A changeset for the page's form.
The next days a visitor may pick, with how many free slots each has for the
shortest duration offered. %{date:, count:, closed?:} in date order.
The days with the free-slot count for the duration the visitor chose.
The hours a page starts with: Monday to Friday, 09:00 to 17:00.
Every length a page may offer.
The host's address the e-mails are sent from.
Whether the page offers this date at all (within its horizon).
Every notice a page may ask, in minutes.
What the clock of the host's zone reads now, as a naive date-time.
A page regardless of the switch: for the owner's own preview.
The page of the signed-in person, made with sensible defaults the first time.
The public page behind a token, when it is switched on.
Moves a booking by its manage token to another slot. attrs are "date",
"time" and optionally "duration" (the booking's own when absent).
Replaces the page's public token. The old link stops working at once; the appointments already made keep their own manage links.
The free slots of date for duration (minutes), earliest first. The
duration must be one the page offers; anything else answers [].
The same, leaving one event out of the busy time (a booking that is being
moved: its own event must not block the new time). Option :except.
The title a visitor sees.
The confirmed bookings from today on, soonest first, for the owner's settings page.
Saves the page. attrs carries the Page changeset fields plus
"working_hours" as %{weekday => %{"start" => ..., "end" => ...}} (an empty
pair closes the day).
The public URL of the page.
The weekday keys, Monday first.
Types
Functions
@spec book(Kantoor.Bookings.Page.t(), map()) :: {:ok, Kantoor.Bookings.Booking.t(), String.t(), :sent | :failed} | {:error, :taken | :invalid | :closed | {:limited, pos_integer()}}
Books a slot. attrs (string keys): "date" (YYYY-MM-DD), "time"
(HH:MM), "duration" (minutes), "name", "email", "note" and the
optional honeypot "website" (must be empty).
Returns {:ok, booking, token, :sent | :failed} or {:error, reason} where
reason is :taken (the slot is no longer free), :invalid (a field is
wrong), :closed or {:limited, seconds}.
@spec booking_by_token(String.t()) :: {:ok, Kantoor.Bookings.Booking.t()} | :error
A booking by its manage token (only the hash is stored).
Every buffer a page may keep, in minutes.
@spec cancel(String.t()) :: {:ok, Kantoor.Bookings.Booking.t()} | {:error, :not_found | :already_cancelled}
Cancels a booking by its manage token: the event is deleted.
@spec cancel_by_host(Kantoor.Accounts.Scope.t(), Ecto.UUID.t()) :: {:ok, Kantoor.Bookings.Booking.t()} | {:error, :not_found}
Cancels one of the owner's own bookings (from the settings page).
A changeset for the page's form.
@spec days(Kantoor.Bookings.Page.t()) :: [map()]
The next days a visitor may pick, with how many free slots each has for the
shortest duration offered. %{date:, count:, closed?:} in date order.
@spec days(Kantoor.Bookings.Page.t(), integer()) :: [map()]
The days with the free-slot count for the duration the visitor chose.
The hours a page starts with: Monday to Friday, 09:00 to 17:00.
Every length a page may offer.
@spec host_address(Kantoor.Accounts.User.t()) :: String.t() | nil
The host's address the e-mails are sent from.
Whether the page offers this date at all (within its horizon).
Every notice a page may ask, in minutes.
@spec now_local(Kantoor.Bookings.Page.t()) :: NaiveDateTime.t()
What the clock of the host's zone reads now, as a naive date-time.
A page regardless of the switch: for the owner's own preview.
@spec page_for(Kantoor.Accounts.Scope.t()) :: Kantoor.Bookings.Page.t()
The page of the signed-in person, made with sensible defaults the first time.
@spec public_page(String.t()) :: {:ok, Kantoor.Bookings.Page.t(), Kantoor.Accounts.User.t()} | :error
The public page behind a token, when it is switched on.
@spec reschedule(String.t(), map()) :: {:ok, Kantoor.Bookings.Booking.t()} | {:error, :not_found | :already_cancelled | :taken | :invalid}
Moves a booking by its manage token to another slot. attrs are "date",
"time" and optionally "duration" (the booking's own when absent).
@spec rotate_token(Kantoor.Accounts.Scope.t()) :: Kantoor.Bookings.Page.t()
Replaces the page's public token. The old link stops working at once; the appointments already made keep their own manage links.
@spec slots(Kantoor.Bookings.Page.t(), Date.t(), integer()) :: [slot()]
The free slots of date for duration (minutes), earliest first. The
duration must be one the page offers; anything else answers [].
@spec slots(Kantoor.Bookings.Page.t(), Date.t(), integer(), keyword()) :: [slot()]
The same, leaving one event out of the busy time (a booking that is being
moved: its own event must not block the new time). Option :except.
The title a visitor sees.
@spec upcoming(Kantoor.Accounts.Scope.t()) :: [Kantoor.Bookings.Booking.t()]
The confirmed bookings from today on, soonest first, for the owner's settings page.
@spec update_page(Kantoor.Accounts.Scope.t(), map()) :: {:ok, Kantoor.Bookings.Page.t()} | {:error, Ecto.Changeset.t()}
Saves the page. attrs carries the Page changeset fields plus
"working_hours" as %{weekday => %{"start" => ..., "end" => ...}} (an empty
pair closes the day).
The public URL of the page.
The weekday keys, Monday first.