- input.ticketwhat the caller gives
- typea value it works out
- TicketTypea record type
- TicketTypean attribute of a record or an answer
- "open"a fixed text or number
- nowwhat it knows without being told
- fora part of an email or a payment
- refusewhere it says no
Find the TicketType whose id is the given ticket.When none matches, refuse with "No such ticket".
type is
Find the first TicketType where
id is input.ticket
when none refuse "No such ticket"
Find the Event whose id is that TicketType's event_id.When none matches, or its state is not open, or its starts_at is now or earlier, refuse with "Registration for this event is closed".
event is
Find the first Event where
id is type.event_id
when none refuse "Registration for this event is closed"
When event.state is not "open"
or event.starts_at is on or before now
Refuse "Registration for this event is closed"
The seats taken are the count of Registrations for that TicketType whose state is confirmed or checked_in, plus those whose state is pending and that were made in the last hour.When the seats taken are that TicketType's seats or more, refuse with "These tickets are sold out".
A payment page left open for an hour gives its seat back, so a guest who walks away does not keep a seat.
held is
Count Registrations where
state is one of "confirmed" or "checked_in"
ticket_type_id is type.id
paying is
Count Registrations where
created_at is after now - 1 hour
state is "pending"
ticket_type_id is type.id
When held + paying is at least type.seats
Refuse "These tickets are sold out"
Create a Registration with event_id and ticket_type_id set to that Event's and that TicketType's ids, name and email set to the given name and email, starts_at set to that Event's starts_at, a new code of 10 random letters and digits, and state set to pending when that TicketType is paid, and to confirmed when it is free.
Add a Registration
set code to a random code of 10 letters and digits
set email to input.email
set event_id to event.id
set name to input.name
set starts_at to event.starts_at
set state to "pending" when the type's paid is true, otherwise "confirmed"
set ticket_type_id to type.id
When that TicketType is paid, open a payment page for the Ticket product, with the registration's code as the reference and the given email as the payer's email, and answer with the code and the address of that payment page.
When type.paid
page is
Open a payment page for Ticket
for is input.email
reference is registration.code
Answer
code is registration.code
payment_url is page.url
Otherwise email the given email with the subject "You are registered", saying that Event's title, its start, its place and the registration's code.
Send an email
to input.email
template "Registration Email"
code registration.code
place event.place
starts_at date and time of event.starts_at
title event.title
Answer with the registration's code.
Answer
code is registration.code