Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ exact tag (`ghcr.io/calnode/calnode:0.1.0`) if you need stability between upgrad
was degraded, rather than implying there is none. It appears on the booking and reschedule
pages and in the embed widget. Proposed in discussion #254.

- **Booking outside availability through MCP (`skip_availability`).** A workspace owner or
admin can pass `skip_availability` to the `create_booking` and `reschedule_booking` MCP
tools to book, or move a booking to, a time the
public calendar would not offer: outside the hosts' working hours, inside the minimum
notice or beyond the booking window. It is for the appointment agreed on the phone that
still needs its calendar event and confirmation. The double-booking check is not affected;
a time that collides with an existing booking is refused either way, and so is a start that
has already passed. Anyone else sending
the flag gets an explicit error and no booking, as does the stdio transport, which has no
user to record. The acting user is stored in the new `bookings.availability_override_by`
column (migration `00080`), which is cleared again when a normal reschedule, by anyone,
moves the booking into a regular slot. It is recorded only when it was needed: with the
flag on a time that is bookable anyway, the column stays empty. For a round-robin event
type this includes the case where the hosts within their hours are all taken: with the
flag the booking goes to a rotation host outside their hours, and that is recorded. While a
host within their hours is free, the rotation stays with them, also when the flag waived
the minimum notice or the booking window. `reschedule_booking` now also says so when a
target is refused for being outside availability, instead of reporting it as taken.
MCP only: REST, the booking page and the admin UI do not
accept the flag. Requested in #270.

### Fixed

- **Buttons and inputs in the booking UI now use the surrounding font.** Form controls
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ the API, not buried in a UI.
**Native MCP server.** A Model Context Protocol server is compiled *into* the binary
(official Go SDK), exposing eight first-class tools — `list_event_types`,
`get_event_type`, `get_available_slots`, `create_booking`, `get_booking`,
`reschedule_booking`, `cancel_booking`, `list_bookings`. The MCP tools call the same internal services as
`reschedule_booking`, `cancel_booking`, `list_bookings`. Owners and admins can pass
`skip_availability` to `create_booking` and `reschedule_booking` for a time outside
normal availability (never past the double-booking check, never in the past). The MCP tools call the same internal services as
the REST API (no parallel code path), so booking side effects — calendar events,
confirmation emails, webhooks, reminders — fire identically.

Expand Down
82 changes: 79 additions & 3 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ partial unique index covers) — no TOCTOU between concurrent bookings.
(the host-roles table), `event_type_questions` (intake form),
`event_type_reminders` (per-ET `hours_before`, UNIQUE).
- **Availability:** `availability_rules` (weekly), `availability_overrides` (dated).
- **Bookings:** `bookings` (primary `host_id`, `external_event_id`, status),
- **Bookings:** `bookings` (primary `host_id`, `external_event_id`, status,
`availability_override_by` - see §19),
`booking_hosts` (every attending host + `is_primary` + per-host
`external_event_id`), `booking_attendees` (organizer + invitees),
`booking_answers`, `booking_manage_tokens` (PK is the hashed token; TTL is
Expand Down Expand Up @@ -497,7 +498,9 @@ The `phone_outbound` location type ("we'll call you") is the outbound counterpar
- **Reschedule:** `RescheduleBooking` (admin) + `RescheduleByToken` share
`moveCalendarEvents` — `Service.UpdateEvent` moves **every** assigned host's event to
the new time. The overlap re-check covers **all** the booking's hosts, not just
the primary.
the primary. `Service.Reschedule` also resets `availability_override_by` on every
move (§19): these two paths always pass "no override", so a normal reschedule clears
it.
- **Reassign:** `ReassignHost` swaps the primary host (archive/resolve flow), moves
the calendar event old→new, and keeps `booking_hosts` in sync so later
cancel/notify fan-out targets the right person.
Expand Down Expand Up @@ -1085,9 +1088,82 @@ types that have required questions.
(`ListByHost`, host-scoped `Cancel`), admins/owner get the whole workspace
(`ListAll`/`CancelByID`). The stdio transport has no bound caller → full access (local
operator). `list_event_types`/`get_available_slots`/`create_booking` are the public
booking surface (unscoped). Tools translate between the slug they expose as
booking surface (unscoped, apart from `skip_availability` below). Tools translate between the slug they expose as
`event_type_id` and the internal booking id. Gap: `create_booking` does not yet honour
an `idempotency_key` (REST-only).
- **Availability override (`skip_availability`, #270).** `create_booking` and
`reschedule_booking` accept an
optional `skip_availability` for a time the public calendar would never have offered,
such as an appointment agreed on the phone. It is an admin action with a record, not
"skip validation":
- **What it skips:** minimum notice, `max_future_days` and the hosts' availability
windows (weekly rules and date overrides) - the three rules in `validateBookingTime`,
which takes the bypass as a parameter (`validateRescheduleTime` hands it through).
`filterAvailableHosts`, which re-checks the same windows per host on create, still
runs for round robin (see below) and is left out only for fixed and collective
under a waived refusal, where every candidate has a seat and filtering would drop
the host outside their hours from the booking instead of booking them.
**Nothing else:** the overlap checks in `booking.Service.Create` and `Reschedule`,
host resolution, intake answers, the paid-event refusal and, on create, the
external calendar busy check (`calendarFreeHosts`) all still run. Reschedule has
never had a calendar busy check, with or without the flag, so nothing is lost there. A time that collides with an existing booking is refused with the
flag exactly as without it.
- **Not in the past, flag or not.** Waiving minimum notice does not waive the floor
underneath it: a start that has already passed is refused under the flag with
`errStartInPast`, on create and on reschedule, and is never treated as a waivable
availability refusal. Without the flag it stays the ordinary `errSlotUnavailable`
for every caller; only `reschedule_booking` answers with the same explicit message
in both cases, since "resend with `skip_availability`" would be wrong advice there.
- **Who:** owners and admins only. `mcpAvailabilityOverride` (`mcp_oauth.go`) reads the
caller bound by `MCPCallerMiddleware`; no tool argument can grant it. It runs before
anything is loaded (on reschedule, before the member's "booking not found" check,
so the refusal reads the same whether or not the booking exists), and anyone else
gets an explicit tool error and no booking or change - the flag is never silently
dropped, because that would book and look as if it had worked. The stdio transport
has no bound caller and is refused too: there is no user to record.
- **The record:** `bookings.availability_override_by` (migration 00080; nullable, no
foreign key, so removing a user neither is blocked by it nor erases it) holds the
acting user's id, written in the same INSERT as the booking, or in the same UPDATE
as the new times. Flag and actor travel as one value (`overrideBy`, empty for none)
from the tool down to the SQL, so the checks cannot be skipped without the id being
stored. The column is not exposed in `bookingJSON`, which also serves the public
create response, nor in webhooks.
- **It means "where this booking currently sits".** `booking.Service.Reschedule` takes
`overrideBy` and writes the column on every move: a reschedule under the override
names whoever made that move (replacing an earlier actor), and every ordinary
reschedule clears it to NULL - through MCP, the admin REST endpoint or the guest's
manage link alike, since all three go through `Reschedule`. The gate hangs on the
target time, not on the booking's history: a booking placed by an override can be
moved into a normal slot by anyone who could move it anyway.
- **Reschedule refusals are told apart.** Without the flag, a target that fails notice,
window or hours is refused by `reschedule_booking` with a message naming the reason
and the flag (`errRescheduleOutsideAvailability`); a collision keeps "that time slot
is no longer available". Only the MCP wording differs - REST and the manage page
keep their messages.
- **Recorded only when it was needed.** The rules are always evaluated;
`validateBookingTime` waives a refusal under the flag and reports that it did. For
a time that is normally bookable the flag changes nothing: the ordinary path runs
(including `filterAvailableHosts` on create) and the column stays NULL - or, on a
reschedule, is cleared like on any ordinary move - so it never claims an override
for a booking that sits in a regular slot.
- **Round robin keeps the host filter under the flag.** A waived refusal may have been
minimum notice or the booking window, which say nothing about hours, so the
rotation still gets only the hosts within theirs and cannot land on an out-of-hours
host while an in-hours one is free. Only when the filter finds nobody within hours
(the hours rule itself was waived) does the rotation pick among all candidates. The
booking is recorded either way, because a refusal was waived.
- **Round robin: the one case decided after the rules.** There the slot "exists" as
soon as one rotation host is within hours, so the rules can pass while every such
host is already taken (overlap or calendar busy). Under the flag, and only when
`filterAvailableHosts` actually removed someone, `createBookingForSlug` then tries
once more with exactly the hosts the filter removed - through the same calendar
busy and overlap checks. That pool holds nobody within their hours, so the booking
lands on a rotation host outside their hours by construction, and is recorded as
an override. Without the flag the refusal stands as before.
- **Where:** the MCP tools only. REST, the public booking page, the manage page, the
assistant and the admin UI pass "no override" explicitly; the new parameters on
`validateBookingTime`, `validateRescheduleTime`, `createBookingForSlug` and
`Service.Reschedule` make every caller say so.
- **Authorization — the "Connect" flow** (`mcp_oauth.go`, `mcp_oauth_authorize.go`):
Calnode is its own **OAuth 2.1 authorization server** for the `/mcp` resource, so an
agent (Claude, ChatGPT) adds the server by URL and clicks **Connect** instead of
Expand Down
9 changes: 9 additions & 0 deletions internal/booking/booking.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ type Booking struct {
// InviteDelivery is who sent this booking's invite (InviteByCalendar/InviteByCalnode/
// InviteByNone), fixed at creation; reschedule, cancel and reassign follow it.
InviteDelivery string
// AvailabilityOverrideBy is the id of the owner or admin who placed this booking at
// a time outside normal availability (migration 00080); empty when it sits in a
// normally bookable slot. Internal audit data, not part of any API response.
AvailabilityOverrideBy string
}

// Attendee is a participant in a booking (the person who made the booking).
Expand Down Expand Up @@ -117,4 +121,9 @@ type CreateParams struct {
// Stored on the booking so its reschedule and cancel follow the mode it was created
// with, not whatever the event type is set to by then.
InviteDelivery string
// AvailabilityOverrideBy is the id of the owner or admin who is placing this booking
// outside normal availability, recorded in the same INSERT as the booking. Empty
// (the default, and what every public path passes) records nothing. It never
// relaxes the overlap checks in Create.
AvailabilityOverrideBy string
}
95 changes: 91 additions & 4 deletions internal/booking/manage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package booking_test
import (
"context"
"crypto/sha256"
"database/sql"
"encoding/hex"
"errors"
"testing"
"time"

Expand Down Expand Up @@ -181,7 +183,7 @@ func TestReschedule_success(t *testing.T) {

newStart := slot(10, 0)
newEnd := slot(10, 30)
updated, err := svc.Reschedule(context.Background(), b.ID, newStart, newEnd)
updated, err := svc.Reschedule(context.Background(), b.ID, newStart, newEnd, "")
if err != nil {
t.Fatalf("Reschedule: %v", err)
}
Expand All @@ -205,6 +207,91 @@ func TestReschedule_success(t *testing.T) {
}
}

// The availability override (#270) follows the booking's current position: Reschedule
// writes it with the new times. An override names whoever made that move, and an
// ordinary move - the path the admin REST endpoint and the guest's manage link take -
// clears it.
func TestReschedule_setsAndClearsAvailabilityOverride(t *testing.T) {
database := newTestDB(t)
svc := booking.New(database)
hostID := seedHost(t, database)
etID := seedEventType(t, database, hostID)

b, err := svc.Create(context.Background(), booking.CreateParams{
EventTypeID: etID,
HostIDs: []string{hostID},
StartAt: slot(22, 0),
EndAt: slot(22, 30),
Organizer: booking.Attendee{Name: "Alice", Email: "alice@example.com"},
AvailabilityOverrideBy: "admin-one",
})
if err != nil {
t.Fatalf("Create: %v", err)
}
stored := func() sql.NullString {
t.Helper()
var v sql.NullString
if err := database.QueryRow(`SELECT availability_override_by FROM bookings WHERE id = ?`, b.ID).Scan(&v); err != nil {
t.Fatalf("read column: %v", err)
}
return v
}

// Moved again under the override by someone else: the column names them.
updated, err := svc.Reschedule(context.Background(), b.ID, slot(23, 0), slot(23, 30), "admin-two")
if err != nil {
t.Fatalf("Reschedule with override: %v", err)
}
if updated.AvailabilityOverrideBy != "admin-two" {
t.Errorf("returned AvailabilityOverrideBy = %q; want %q", updated.AvailabilityOverrideBy, "admin-two")
}
if got := stored(); !got.Valid || got.String != "admin-two" {
t.Errorf("stored availability_override_by = %+v; want %q", got, "admin-two")
}

// An ordinary move clears it, to NULL.
updated, err = svc.Reschedule(context.Background(), b.ID, slot(10, 0), slot(10, 30), "")
if err != nil {
t.Fatalf("Reschedule without override: %v", err)
}
if updated.AvailabilityOverrideBy != "" {
t.Errorf("returned AvailabilityOverrideBy = %q; want empty", updated.AvailabilityOverrideBy)
}
if got := stored(); got.Valid {
t.Errorf("stored availability_override_by after a normal move = %q; want NULL", got.String)
}
got, err := svc.Get(context.Background(), b.ID)
if err != nil {
t.Fatalf("Get: %v", err)
}
if got.AvailabilityOverrideBy != "" {
t.Errorf("Get AvailabilityOverrideBy = %q; want empty", got.AvailabilityOverrideBy)
}

// A refused move (overlap) leaves the record as it was.
other, err := svc.Create(context.Background(), booking.CreateParams{
EventTypeID: etID,
HostIDs: []string{hostID},
StartAt: slot(20, 0),
EndAt: slot(20, 30),
Organizer: booking.Attendee{Name: "Bob", Email: "bob@example.com"},
AvailabilityOverrideBy: "admin-one",
})
if err != nil {
t.Fatalf("Create second: %v", err)
}
if _, err := svc.Reschedule(context.Background(), other.ID, slot(10, 0), slot(10, 30), ""); !errors.Is(err, booking.ErrDoubleBooked) {
t.Fatalf("overlapping Reschedule: err = %v; want ErrDoubleBooked", err)
}
after, err := svc.Get(context.Background(), other.ID)
if err != nil {
t.Fatalf("Get after refused move: %v", err)
}
if after.AvailabilityOverrideBy != "admin-one" {
t.Errorf("after refused move: AvailabilityOverrideBy = %q; want %q", after.AvailabilityOverrideBy, "admin-one")
}
}

func TestReschedule_sameTime(t *testing.T) {
// Rescheduling to the same time must succeed (self-exclusion in overlap check).
database := newTestDB(t)
Expand All @@ -223,7 +310,7 @@ func TestReschedule_sameTime(t *testing.T) {
t.Fatalf("Create: %v", err)
}

_, err = svc.Reschedule(context.Background(), b.ID, slot(9, 0), slot(9, 30))
_, err = svc.Reschedule(context.Background(), b.ID, slot(9, 0), slot(9, 30), "")
if err != nil {
t.Errorf("Reschedule to same time: got %v; want nil", err)
}
Expand Down Expand Up @@ -258,7 +345,7 @@ func TestReschedule_doubleBooked(t *testing.T) {
}

// Try to reschedule to 10:00-10:30 which is already taken.
_, err = svc.Reschedule(context.Background(), b.ID, slot(10, 0), slot(10, 30))
_, err = svc.Reschedule(context.Background(), b.ID, slot(10, 0), slot(10, 30), "")
if err != booking.ErrDoubleBooked {
t.Errorf("Reschedule to conflicting slot: got %v; want ErrDoubleBooked", err)
}
Expand All @@ -284,7 +371,7 @@ func TestReschedule_alreadyCancelled(t *testing.T) {
t.Fatalf("Cancel: %v", err)
}

_, err = svc.Reschedule(context.Background(), b.ID, slot(11, 0), slot(11, 30))
_, err = svc.Reschedule(context.Background(), b.ID, slot(11, 0), slot(11, 30), "")
if err != booking.ErrAlreadyCancelled {
t.Errorf("Reschedule of cancelled booking: got %v; want ErrAlreadyCancelled", err)
}
Expand Down
Loading
Loading