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

# Booking system with notifications

> Guide: a booking backend that rejects double bookings with a beforeInsert hook, emails confirmations with an afterInsert hook and template.

You'll combine hooks and messaging to automate a booking flow.

```text theme={null}
POST /collections/bookings
  └─ beforeInsert: slot free?  no → reject "This time slot is already booked"
  └─ saved
  └─ afterInsert: email "Booking confirmed" to the customer
```

**1. Collections**

`bookings` with fields: `room` (Select), `starts_at` (DateTime, required, `timezone_required`), `customer_name` (Text), `customer_email` (Text, format `email`), `status` (Select: pending, confirmed, cancelled).

**2. Email channel and template**

Create an SMTP [channel](https://docs.bludia.com/messaging/channels.md) and a [template](https://docs.bludia.com/messaging/templates.md) named `Booking confirmed`:

```html theme={null}
Subject: Your booking on {{date}} is confirmed
<p>Hi {{name}}, your booking in {{room}} on {{date}} is confirmed.</p>
```

**3. beforeInsert: block double bookings**

```go theme={null}
package main

import "bludia"

func main() {
	ctx := bludia.GetContext()
	rec, _ := ctx.Record()

	taken, err := ctx.Collection("bookings").
		Where("room", bludia.Eq, rec["room"]).
		Where("starts_at", bludia.Eq, rec["starts_at"]).
		Where("status", bludia.Neq, "cancelled").
		Exists()
	if err != nil {
		bludia.Abort("Could not check availability, please try again")
		return
	}
	if taken {
		bludia.Abort("This time slot is already booked")
		return
	}

	rec["status"] = "confirmed"
	ctx.SetRecord(rec)
}
```

**4. afterInsert: send the confirmation**

```go theme={null}
package main

import "bludia"

func main() {
	ctx := bludia.GetContext()
	rec, _ := ctx.Record()

	to, _ := rec["customer_email"].(string)
	if to == "" {
		return
	}

	res, err := ctx.Msg().Send(bludia.MsgSendRequest{
		Channel:  "email",
		Template: "Booking confirmed",
		To:       to,
		Data: map[string]any{
			"name": rec["customer_name"],
			"room": rec["room"],
			"date": rec["starts_at"],
		},
	})
	if err != nil || !res.Accepted {
		bludia.LogWith(bludia.Error, "Confirmation not queued", map[string]any{"to": to})
	}
}
```

**5. Test**

Create two bookings for the same room and time. The second must fail with *"This time slot is already booked"*. Check the hook's [executions](https://docs.bludia.com/hooks/logs-and-debugging.md) and your inbox.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.