@composable-os/pack-ticketing 0.0.0-stage → 0.1.0-beta.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +109 -0
- package/dist/automations.d.ts +72 -0
- package/dist/automations.js +347 -0
- package/dist/blocks/ticketing-checkout/ticket-picker.d.ts +25 -0
- package/dist/blocks/ticketing-checkout/ticket-picker.js +77 -0
- package/dist/blocks/ticketing-events/event-list.d.ts +15 -0
- package/dist/blocks/ticketing-events/event-list.js +26 -0
- package/dist/blocks/ticketing-my-tickets/my-tickets.d.ts +9 -0
- package/dist/blocks/ticketing-my-tickets/my-tickets.js +13 -0
- package/dist/blocks/ticketing-native-my-tickets/my-tickets-screen.d.ts +10 -0
- package/dist/blocks/ticketing-native-my-tickets/my-tickets-screen.js +17 -0
- package/dist/blocks/ticketing-native-scanner/door-scanner-screen.d.ts +18 -0
- package/dist/blocks/ticketing-native-scanner/door-scanner-screen.js +42 -0
- package/dist/blocks/ticketing-native-ticket/ticket-screen.d.ts +21 -0
- package/dist/blocks/ticketing-native-ticket/ticket-screen.js +48 -0
- package/dist/blocks/ticketing-order/order-confirmation.d.ts +12 -0
- package/dist/blocks/ticketing-order/order-confirmation.js +11 -0
- package/dist/client/index.d.ts +804 -0
- package/dist/client/index.js +288 -0
- package/dist/dashboards.d.ts +5 -0
- package/dist/dashboards.js +91 -0
- package/dist/index.d.ts +656 -0
- package/dist/index.js +341 -0
- package/dist/tools.d.ts +184 -0
- package/dist/tools.js +591 -0
- package/package.json +31 -4
- package/README.md +0 -3
package/dist/tools.js
ADDED
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
// The Ticketing tools. They become operations x.ticketing.<tool>, run in the sandbox, and reach
|
|
2
|
+
// the host only through ctx.ops. None of them holds a rule: prices, the sale window, the most per
|
|
3
|
+
// order, places, who may see a ticket and that a ticket is used once are all the host's. A tool
|
|
4
|
+
// asks, and reports what the host answered.
|
|
5
|
+
import { defineTool, OpsCallError, z } from "@composable-os/sdk";
|
|
6
|
+
const EVENT = "ticketing.event";
|
|
7
|
+
const TICKET_TYPE = "ticketing.tickettype";
|
|
8
|
+
const TICKET = "ticketing.ticket";
|
|
9
|
+
const text = (value) => value === null || value === undefined ? null : String(value);
|
|
10
|
+
const num = (value) => typeof value === "number" ? value : value == null ? null : Number(value);
|
|
11
|
+
// --- On sale -------------------------------------------------------------------------------------
|
|
12
|
+
const availability = z.object({
|
|
13
|
+
status: z.enum(["available", "low", "sold_out"]),
|
|
14
|
+
available: z.number().nullable().optional().describe("Places left, when the type says so"),
|
|
15
|
+
});
|
|
16
|
+
const currency = z
|
|
17
|
+
.string()
|
|
18
|
+
.nullable()
|
|
19
|
+
.describe("The currency the prices are in (ISO 4217): the deployment's one currency, from the host. " +
|
|
20
|
+
"Null until an admin sets it, and until then nothing can be bought.");
|
|
21
|
+
/** An event's own time zone: its days and times are this zone's (ADR 0460). */
|
|
22
|
+
const timeZone = z
|
|
23
|
+
.string()
|
|
24
|
+
.nullable()
|
|
25
|
+
.describe("The event's IANA time zone, e.g. Asia/Bangkok: show its start and end, and count its days, " +
|
|
26
|
+
"in this zone. Null only for an event made before events had one.");
|
|
27
|
+
export const eventsOnSale = defineTool({
|
|
28
|
+
description: "The events on sale now, soonest first, each with its ticket types (name, price and its " +
|
|
29
|
+
"currency, sale window, most per order, deposit) and whether places are left. Prices and " +
|
|
30
|
+
"deposits are in the major unit of `currency` (45.5 is 45.50). For an event page or a list " +
|
|
31
|
+
"of events. Only what the public may read: an event that is not on sale is not here. " +
|
|
32
|
+
"Read-only.",
|
|
33
|
+
input: z.object({
|
|
34
|
+
event: z.string().min(1).max(64).optional().describe("Only this event"),
|
|
35
|
+
limit: z.number().int().min(1).max(50).default(20),
|
|
36
|
+
}),
|
|
37
|
+
output: z.object({
|
|
38
|
+
events: z.array(z.object({
|
|
39
|
+
id: z.string(),
|
|
40
|
+
name: z.string().nullable(),
|
|
41
|
+
startsAt: z.string().nullable().describe("When it starts (ISO 8601, UTC)"),
|
|
42
|
+
endsAt: z.string().nullable(),
|
|
43
|
+
timeZone,
|
|
44
|
+
venue: z.string().nullable(),
|
|
45
|
+
description: z.string().nullable(),
|
|
46
|
+
image: z.string().nullable(),
|
|
47
|
+
availability,
|
|
48
|
+
ticketTypes: z.array(z.object({
|
|
49
|
+
id: z.string(),
|
|
50
|
+
name: z.string().nullable(),
|
|
51
|
+
description: z.string().nullable(),
|
|
52
|
+
price: z.number().nullable(),
|
|
53
|
+
currency,
|
|
54
|
+
onSaleFrom: z.string().nullable(),
|
|
55
|
+
onSaleUntil: z.string().nullable(),
|
|
56
|
+
maxPerOrder: z.number().nullable(),
|
|
57
|
+
deposit: z.number().nullable(),
|
|
58
|
+
balanceDue: z.string().nullable(),
|
|
59
|
+
availability,
|
|
60
|
+
})),
|
|
61
|
+
})),
|
|
62
|
+
}),
|
|
63
|
+
risk: "read",
|
|
64
|
+
audiences: ["customer", "public"],
|
|
65
|
+
async handler(input, ctx) {
|
|
66
|
+
// The seam decides which events and which fields: the public scope shows an event only
|
|
67
|
+
// while it is on sale, and a ticket type only while its event is (ADR 0438).
|
|
68
|
+
const events = await ctx.ops.call("public.list", {
|
|
69
|
+
object: EVENT,
|
|
70
|
+
...(input.event ? { filter: { field: "id", op: "eq", value: input.event } } : {}),
|
|
71
|
+
sort: { field: "startsAt", direction: "asc" },
|
|
72
|
+
limit: input.limit,
|
|
73
|
+
});
|
|
74
|
+
const places = async (object, id) => {
|
|
75
|
+
const view = await ctx.ops.call("capacity.get", { object, id });
|
|
76
|
+
return {
|
|
77
|
+
status: view.status,
|
|
78
|
+
...(view.available === undefined ? {} : { available: view.available }),
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
const out = [];
|
|
82
|
+
for (const event of events.items) {
|
|
83
|
+
const types = await ctx.ops.call("public.list", {
|
|
84
|
+
object: TICKET_TYPE,
|
|
85
|
+
filter: { field: "event", op: "eq", value: event.id },
|
|
86
|
+
limit: 50,
|
|
87
|
+
});
|
|
88
|
+
const v = event.values;
|
|
89
|
+
out.push({
|
|
90
|
+
id: event.id,
|
|
91
|
+
name: text(v.name),
|
|
92
|
+
startsAt: text(v.startsAt),
|
|
93
|
+
endsAt: text(v.endsAt),
|
|
94
|
+
timeZone: text(v.timezone),
|
|
95
|
+
venue: text(v.venue),
|
|
96
|
+
description: text(v.description),
|
|
97
|
+
image: text(v.image),
|
|
98
|
+
availability: await places(EVENT, event.id),
|
|
99
|
+
ticketTypes: await Promise.all(types.items.map(async (type) => ({
|
|
100
|
+
id: type.id,
|
|
101
|
+
name: text(type.values.name),
|
|
102
|
+
description: text(type.values.description),
|
|
103
|
+
price: num(type.values.price),
|
|
104
|
+
// What currency the prices are in is the host's to say, beside them (ADR 0435).
|
|
105
|
+
currency: types.currency ?? null,
|
|
106
|
+
onSaleFrom: text(type.values.onSaleFrom),
|
|
107
|
+
onSaleUntil: text(type.values.onSaleUntil),
|
|
108
|
+
maxPerOrder: num(type.values.maxPerOrder),
|
|
109
|
+
deposit: num(type.values.depositAmount),
|
|
110
|
+
balanceDue: text(type.values.balanceDue),
|
|
111
|
+
availability: await places(TICKET_TYPE, type.id),
|
|
112
|
+
}))),
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
return { events: out };
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
// --- Buying -------------------------------------------------------------------------------------
|
|
119
|
+
const orderLine = z.object({
|
|
120
|
+
id: z.string(),
|
|
121
|
+
ticketTypeId: z.string(),
|
|
122
|
+
title: z.string(),
|
|
123
|
+
quantity: z.number(),
|
|
124
|
+
unitPrice: z.number(),
|
|
125
|
+
total: z.number(),
|
|
126
|
+
});
|
|
127
|
+
const orderOutput = z.object({
|
|
128
|
+
id: z.string(),
|
|
129
|
+
state: z.string(),
|
|
130
|
+
currency: z.string(),
|
|
131
|
+
total: z.number().describe("In the currency's minor unit (cents)"),
|
|
132
|
+
paid: z.number(),
|
|
133
|
+
balance: z.number(),
|
|
134
|
+
pay: z.string(),
|
|
135
|
+
deposit: z.number().nullable(),
|
|
136
|
+
balanceDueAt: z.string().nullable(),
|
|
137
|
+
expiresAt: z.string().nullable(),
|
|
138
|
+
lines: z.array(orderLine),
|
|
139
|
+
token: z
|
|
140
|
+
.string()
|
|
141
|
+
.nullable()
|
|
142
|
+
.describe("A guest's key to this order, shown once (order.checkout and order.view take it)"),
|
|
143
|
+
});
|
|
144
|
+
export const orderStart = defineTool({
|
|
145
|
+
description: "Start an order for tickets: one or more ticket types and how many of each, paid in full or " +
|
|
146
|
+
"by deposit when every ticket type offers one. A signed-in customer buys as themselves; a " +
|
|
147
|
+
"guest gives their email address. The host prices the order from the ticket types, refuses " +
|
|
148
|
+
"a ticket type outside its sale window, over its most per order or without enough places " +
|
|
149
|
+
"left (with details naming the rule), and holds the places while the order is unpaid. " +
|
|
150
|
+
"Nothing is paid here: the buyer then calls order.checkout with the order's id (and a " +
|
|
151
|
+
"guest's token) to pay on the provider's page. Amounts are in the minor unit.",
|
|
152
|
+
input: z.object({
|
|
153
|
+
lines: z
|
|
154
|
+
.array(z.object({
|
|
155
|
+
ticketTypeId: z.string().min(1).max(64).describe("The ticket type (its record id)"),
|
|
156
|
+
quantity: z.number().int().min(1).max(100).default(1),
|
|
157
|
+
}))
|
|
158
|
+
.min(1)
|
|
159
|
+
.max(20),
|
|
160
|
+
pay: z.enum(["full", "deposit"]).default("full"),
|
|
161
|
+
email: z
|
|
162
|
+
.string()
|
|
163
|
+
.trim()
|
|
164
|
+
.max(320)
|
|
165
|
+
.optional()
|
|
166
|
+
.describe("A guest's email address. A signed-in customer passes none."),
|
|
167
|
+
}),
|
|
168
|
+
output: orderOutput,
|
|
169
|
+
risk: "write",
|
|
170
|
+
audiences: ["customer", "public"],
|
|
171
|
+
async handler(input, ctx) {
|
|
172
|
+
const order = await ctx.ops.call("order.start", {
|
|
173
|
+
lines: input.lines.map((line) => ({
|
|
174
|
+
object: TICKET_TYPE,
|
|
175
|
+
id: line.ticketTypeId,
|
|
176
|
+
quantity: line.quantity,
|
|
177
|
+
})),
|
|
178
|
+
pay: input.pay,
|
|
179
|
+
...(input.email === undefined ? {} : { email: input.email }),
|
|
180
|
+
});
|
|
181
|
+
return {
|
|
182
|
+
id: order.id,
|
|
183
|
+
state: order.state,
|
|
184
|
+
currency: order.currency,
|
|
185
|
+
total: order.total,
|
|
186
|
+
paid: order.paid,
|
|
187
|
+
balance: order.balance,
|
|
188
|
+
pay: order.pay,
|
|
189
|
+
deposit: order.deposit ?? null,
|
|
190
|
+
balanceDueAt: order.balanceDueAt ?? null,
|
|
191
|
+
expiresAt: order.expiresAt ?? null,
|
|
192
|
+
lines: order.lines.map((line) => ({
|
|
193
|
+
id: line.id,
|
|
194
|
+
ticketTypeId: line.recordId,
|
|
195
|
+
title: line.title,
|
|
196
|
+
quantity: line.quantity,
|
|
197
|
+
unitPrice: line.unitPrice,
|
|
198
|
+
total: line.total,
|
|
199
|
+
})),
|
|
200
|
+
token: order.token ?? null,
|
|
201
|
+
};
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
// --- A customer's tickets -------------------------------------------------------------------------
|
|
205
|
+
export const myTickets = defineTool({
|
|
206
|
+
description: "The signed-in customer's own tickets: each with its event (id, name, start, end, venue " +
|
|
207
|
+
"and state, as they are now, whether or not the event is still on sale), its ticket type " +
|
|
208
|
+
"(id and name), its code and its state (valid, used or void). Only the caller's tickets " +
|
|
209
|
+
"exist here; a code that is not theirs finds nothing. Filter by event or by code. Read-only.",
|
|
210
|
+
input: z.object({
|
|
211
|
+
event: z.string().min(1).max(64).optional().describe("Only tickets for this event"),
|
|
212
|
+
code: z.string().trim().min(1).max(64).optional().describe("Only the ticket with this code"),
|
|
213
|
+
state: z.enum(["valid", "used", "void"]).optional(),
|
|
214
|
+
cursor: z.string().max(1000).optional(),
|
|
215
|
+
limit: z.number().int().min(1).max(100).default(50),
|
|
216
|
+
}),
|
|
217
|
+
output: z.object({
|
|
218
|
+
items: z.array(z.object({
|
|
219
|
+
id: z.string(),
|
|
220
|
+
event: z.string().nullable(),
|
|
221
|
+
ticketType: z.string().nullable(),
|
|
222
|
+
code: z.string().nullable(),
|
|
223
|
+
state: z.string().nullable(),
|
|
224
|
+
eventName: z.string().nullable(),
|
|
225
|
+
startsAt: z.string().nullable().describe("When the event starts (ISO 8601, UTC)"),
|
|
226
|
+
endsAt: z.string().nullable(),
|
|
227
|
+
timeZone,
|
|
228
|
+
venue: z.string().nullable(),
|
|
229
|
+
eventStatus: z
|
|
230
|
+
.string()
|
|
231
|
+
.nullable()
|
|
232
|
+
.describe("The event's state now: draft, on_sale, ended or cancelled"),
|
|
233
|
+
ticketTypeName: z.string().nullable(),
|
|
234
|
+
})),
|
|
235
|
+
nextCursor: z.string().nullable(),
|
|
236
|
+
}),
|
|
237
|
+
risk: "read",
|
|
238
|
+
audiences: ["customer"],
|
|
239
|
+
async handler(input, ctx) {
|
|
240
|
+
const conditions = [
|
|
241
|
+
...(input.event ? [{ field: "event", op: "eq", value: input.event }] : []),
|
|
242
|
+
...(input.code ? [{ field: "code", op: "eq", value: input.code }] : []),
|
|
243
|
+
...(input.state ? [{ field: "state", op: "eq", value: input.state }] : []),
|
|
244
|
+
];
|
|
245
|
+
// The host's seam gives a customer their own tickets and the fields the ticket declares for
|
|
246
|
+
// them, and nothing else: no filter here decides whose tickets these are.
|
|
247
|
+
const page = await ctx.ops.call("my.list", {
|
|
248
|
+
object: TICKET,
|
|
249
|
+
...(conditions.length === 0
|
|
250
|
+
? {}
|
|
251
|
+
: { filter: conditions.length === 1 ? conditions[0] : { and: conditions } }),
|
|
252
|
+
limit: input.limit,
|
|
253
|
+
...(input.cursor ? { cursor: input.cursor } : {}),
|
|
254
|
+
});
|
|
255
|
+
// Their tickets' events and ticket types, as they are now: the seam lets a holder read the
|
|
256
|
+
// fields those objects declare for them (customerVia, ADR 0439), whatever the event's state,
|
|
257
|
+
// and only for records one of their own tickets points to.
|
|
258
|
+
const pointed = async (object, field) => {
|
|
259
|
+
const ids = [...new Set(page.items.map((t) => text(t.values[field])).filter(Boolean))];
|
|
260
|
+
if (!ids.length)
|
|
261
|
+
return new Map();
|
|
262
|
+
const found = await ctx.ops.call("my.list", {
|
|
263
|
+
object,
|
|
264
|
+
filter: { field: "id", op: "in", value: ids },
|
|
265
|
+
limit: ids.length,
|
|
266
|
+
});
|
|
267
|
+
return new Map(found.items.map((item) => [item.id, item.values]));
|
|
268
|
+
};
|
|
269
|
+
const events = await pointed(EVENT, "event");
|
|
270
|
+
const types = await pointed(TICKET_TYPE, "tickettype");
|
|
271
|
+
return {
|
|
272
|
+
items: page.items.map((ticket) => {
|
|
273
|
+
const eventId = text(ticket.values.event);
|
|
274
|
+
const typeId = text(ticket.values.tickettype);
|
|
275
|
+
const event = (eventId && events.get(eventId)) || {};
|
|
276
|
+
const type = (typeId && types.get(typeId)) || {};
|
|
277
|
+
return {
|
|
278
|
+
id: ticket.id,
|
|
279
|
+
event: eventId,
|
|
280
|
+
ticketType: typeId,
|
|
281
|
+
code: text(ticket.values.code),
|
|
282
|
+
state: text(ticket.values.state),
|
|
283
|
+
eventName: text(event.name),
|
|
284
|
+
startsAt: text(event.startsAt),
|
|
285
|
+
endsAt: text(event.endsAt),
|
|
286
|
+
timeZone: text(event.timezone),
|
|
287
|
+
venue: text(event.venue),
|
|
288
|
+
eventStatus: text(event.status),
|
|
289
|
+
ticketTypeName: text(type.name),
|
|
290
|
+
};
|
|
291
|
+
}),
|
|
292
|
+
nextCursor: page.nextCursor ?? null,
|
|
293
|
+
};
|
|
294
|
+
},
|
|
295
|
+
});
|
|
296
|
+
// --- Sales ----------------------------------------------------------------------------------------
|
|
297
|
+
export const salesSummary = defineTool({
|
|
298
|
+
description: "How one event is selling, per ticket type: places sold (taken), held by orders being paid, " +
|
|
299
|
+
"remaining (null when unlimited), and revenue (the line totals of orders paid in full or by " +
|
|
300
|
+
"deposit, in the currency's minor unit), with the event's totals and the currency. Prices " +
|
|
301
|
+
"are in the major unit. For the dashboard and for questions such as 'how are sales going'. " +
|
|
302
|
+
"Read-only.",
|
|
303
|
+
input: z.object({ eventId: z.string().min(1).max(64) }),
|
|
304
|
+
output: z.object({
|
|
305
|
+
event: z.object({ id: z.string(), name: z.string().nullable(), status: z.string().nullable() }),
|
|
306
|
+
currency,
|
|
307
|
+
ticketTypes: z.array(z.object({
|
|
308
|
+
id: z.string(),
|
|
309
|
+
name: z.string().nullable(),
|
|
310
|
+
price: z.number().nullable(),
|
|
311
|
+
sold: z.number(),
|
|
312
|
+
held: z.number(),
|
|
313
|
+
remaining: z.number().nullable(),
|
|
314
|
+
revenue: z.number(),
|
|
315
|
+
})),
|
|
316
|
+
totals: z.object({
|
|
317
|
+
sold: z.number(),
|
|
318
|
+
held: z.number(),
|
|
319
|
+
remaining: z.number().nullable(),
|
|
320
|
+
revenue: z.number(),
|
|
321
|
+
}),
|
|
322
|
+
}),
|
|
323
|
+
risk: "read",
|
|
324
|
+
async handler(input, ctx) {
|
|
325
|
+
const event = await ctx.ops.call("record.get", {
|
|
326
|
+
object: EVENT,
|
|
327
|
+
id: input.eventId,
|
|
328
|
+
});
|
|
329
|
+
const types = await ctx.ops.call("record.list", {
|
|
330
|
+
object: TICKET_TYPE,
|
|
331
|
+
filter: { field: "event", op: "eq", value: event.id },
|
|
332
|
+
sort: { field: "name", direction: "asc" },
|
|
333
|
+
limit: 200,
|
|
334
|
+
});
|
|
335
|
+
const revenue = new Map();
|
|
336
|
+
if (types.items.length) {
|
|
337
|
+
// Revenue from the order lines the host snapshotted: paid in full, or by deposit.
|
|
338
|
+
const ids = types.items.map((t) => t.id);
|
|
339
|
+
const result = await ctx.ops.call("query.sql", {
|
|
340
|
+
sql: "SELECT l.record_id AS id, SUM(l.line_total) AS revenue FROM rec_order_line l " +
|
|
341
|
+
"JOIN rec_order o ON o.id = l.order_id WHERE l.object_type = ? " +
|
|
342
|
+
`AND o.state IN ('paid', 'part_paid') AND l.record_id IN (${ids.map(() => "?").join(", ")}) ` +
|
|
343
|
+
"GROUP BY l.record_id",
|
|
344
|
+
params: [TICKET_TYPE, ...ids],
|
|
345
|
+
});
|
|
346
|
+
for (const row of result.rows)
|
|
347
|
+
revenue.set(row.id, Number(row.revenue ?? 0));
|
|
348
|
+
}
|
|
349
|
+
const ticketTypes = types.items.map((type) => ({
|
|
350
|
+
id: type.id,
|
|
351
|
+
name: text(type.values.name),
|
|
352
|
+
price: num(type.values.price),
|
|
353
|
+
sold: num(type.values.taken) ?? 0,
|
|
354
|
+
held: num(type.values.held) ?? 0,
|
|
355
|
+
remaining: num(type.values.available),
|
|
356
|
+
revenue: revenue.get(type.id) ?? 0,
|
|
357
|
+
}));
|
|
358
|
+
const eventLeft = num(event.values.available);
|
|
359
|
+
const typesLeft = ticketTypes.every((t) => t.remaining !== null)
|
|
360
|
+
? ticketTypes.reduce((sum, t) => sum + (t.remaining ?? 0), 0)
|
|
361
|
+
: null;
|
|
362
|
+
const settings = await ctx.ops.call("order.settings.get", {});
|
|
363
|
+
return {
|
|
364
|
+
event: { id: event.id, name: text(event.values.name), status: text(event.values.status) },
|
|
365
|
+
currency: settings.currency ?? null,
|
|
366
|
+
ticketTypes,
|
|
367
|
+
totals: {
|
|
368
|
+
sold: ticketTypes.reduce((sum, t) => sum + t.sold, 0),
|
|
369
|
+
held: ticketTypes.reduce((sum, t) => sum + t.held, 0),
|
|
370
|
+
// The fewer of the event's places left and the ticket types' own.
|
|
371
|
+
remaining: eventLeft === null
|
|
372
|
+
? typesLeft
|
|
373
|
+
: typesLeft === null
|
|
374
|
+
? eventLeft
|
|
375
|
+
: Math.min(eventLeft, typesLeft),
|
|
376
|
+
revenue: ticketTypes.reduce((sum, t) => sum + t.revenue, 0),
|
|
377
|
+
},
|
|
378
|
+
};
|
|
379
|
+
},
|
|
380
|
+
});
|
|
381
|
+
// --- Check-in -------------------------------------------------------------------------------------
|
|
382
|
+
/** A granted ticket's code: 26 characters of Crockford base32 (ADR 0435). */
|
|
383
|
+
const CODE_RE = /^[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
384
|
+
/** A code as a scanner or a person typed it: case, spaces and dashes do not matter. */
|
|
385
|
+
export function normaliseCode(code) {
|
|
386
|
+
return code.toUpperCase().replace(/[\s-]/g, "");
|
|
387
|
+
}
|
|
388
|
+
const ticketOutput = z.object({
|
|
389
|
+
id: z.string(),
|
|
390
|
+
name: z.string().nullable(),
|
|
391
|
+
event: z.string().nullable(),
|
|
392
|
+
ticketType: z.string().nullable(),
|
|
393
|
+
holder: z.object({ id: z.string(), name: z.string().nullable() }).nullable(),
|
|
394
|
+
state: z.string().nullable(),
|
|
395
|
+
usedAt: z.string().nullable(),
|
|
396
|
+
usedBy: z.object({ id: z.string(), name: z.string().nullable() }).nullable(),
|
|
397
|
+
});
|
|
398
|
+
const checkInOutput = z.object({
|
|
399
|
+
result: z
|
|
400
|
+
.enum(["accepted", "already_used", "void", "unknown"])
|
|
401
|
+
.describe("accepted: let them in. already_used: this code was checked in before (see usedAt and " +
|
|
402
|
+
"usedBy). void: the ticket was cancelled or refunded. unknown: no ticket has this code."),
|
|
403
|
+
accepted: z.boolean(),
|
|
404
|
+
message: z.string(),
|
|
405
|
+
ticket: ticketOutput.nullable(),
|
|
406
|
+
balanceOwing: z
|
|
407
|
+
.object({
|
|
408
|
+
amount: z.number().describe("In the currency's minor unit"),
|
|
409
|
+
currency: z.string(),
|
|
410
|
+
dueAt: z.string().nullable(),
|
|
411
|
+
})
|
|
412
|
+
.nullable()
|
|
413
|
+
.describe("Set when the ticket's order still has a balance to pay. It does not refuse entry."),
|
|
414
|
+
});
|
|
415
|
+
async function personName(ctx, id) {
|
|
416
|
+
try {
|
|
417
|
+
const person = await ctx.ops.call("person.get", { id });
|
|
418
|
+
return person?.displayName ?? null;
|
|
419
|
+
}
|
|
420
|
+
catch (error) {
|
|
421
|
+
if (error instanceof OpsCallError && error.code === "not_found")
|
|
422
|
+
return null;
|
|
423
|
+
throw error;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
async function describeTicket(ctx, ticket) {
|
|
427
|
+
const v = ticket.values;
|
|
428
|
+
const holder = text(v.holder);
|
|
429
|
+
const usedBy = text(v.usedBy);
|
|
430
|
+
return {
|
|
431
|
+
id: ticket.id,
|
|
432
|
+
name: text(v.name),
|
|
433
|
+
event: text(v.event),
|
|
434
|
+
ticketType: text(v.tickettype),
|
|
435
|
+
holder: holder ? { id: holder, name: await personName(ctx, holder) } : null,
|
|
436
|
+
state: text(v.state),
|
|
437
|
+
usedAt: text(v.usedAt),
|
|
438
|
+
usedBy: usedBy ? { id: usedBy, name: text(v.usedByName) } : null,
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
/** What a ticket that cannot be let in is reported as. */
|
|
442
|
+
function refusedFor(ticket) {
|
|
443
|
+
if (ticket.state === "void") {
|
|
444
|
+
return {
|
|
445
|
+
result: "void",
|
|
446
|
+
accepted: false,
|
|
447
|
+
message: "This ticket was cancelled or refunded. It is not valid.",
|
|
448
|
+
ticket,
|
|
449
|
+
balanceOwing: null,
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
if (ticket.state === "used") {
|
|
453
|
+
const who = ticket.usedBy?.name ?? "someone";
|
|
454
|
+
return {
|
|
455
|
+
result: "already_used",
|
|
456
|
+
accepted: false,
|
|
457
|
+
message: `Already checked in${ticket.usedAt ? ` at ${ticket.usedAt}` : ""} by ${who}.`,
|
|
458
|
+
ticket,
|
|
459
|
+
balanceOwing: null,
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
return null;
|
|
463
|
+
}
|
|
464
|
+
async function balanceOwing(ctx, orderId) {
|
|
465
|
+
if (!orderId)
|
|
466
|
+
return null;
|
|
467
|
+
const order = await ctx.ops.call("order.get", { id: orderId });
|
|
468
|
+
if (order.state !== "part_paid" || order.balance <= 0)
|
|
469
|
+
return null;
|
|
470
|
+
return { amount: order.balance, currency: order.currency, dueAt: order.balanceDueAt ?? null };
|
|
471
|
+
}
|
|
472
|
+
const checkInInput = z.object({
|
|
473
|
+
code: z.string().trim().min(1).max(64).describe("The code on the ticket, as scanned"),
|
|
474
|
+
});
|
|
475
|
+
async function checkInHandler(input, ctx) {
|
|
476
|
+
const code = normaliseCode(input.code);
|
|
477
|
+
const unknown = {
|
|
478
|
+
result: "unknown",
|
|
479
|
+
accepted: false,
|
|
480
|
+
message: "No ticket has this code. Check it was scanned whole, or look the buyer up.",
|
|
481
|
+
ticket: null,
|
|
482
|
+
balanceOwing: null,
|
|
483
|
+
};
|
|
484
|
+
if (!CODE_RE.test(code))
|
|
485
|
+
return unknown;
|
|
486
|
+
const found = await ctx.ops.call("record.list", {
|
|
487
|
+
object: TICKET,
|
|
488
|
+
filter: { field: "code", op: "eq", value: code },
|
|
489
|
+
limit: 1,
|
|
490
|
+
});
|
|
491
|
+
const record = found.items[0];
|
|
492
|
+
if (!record)
|
|
493
|
+
return unknown;
|
|
494
|
+
const before = await describeTicket(ctx, record);
|
|
495
|
+
const refused = refusedFor(before);
|
|
496
|
+
if (refused)
|
|
497
|
+
return refused;
|
|
498
|
+
const usedAt = new Date().toISOString();
|
|
499
|
+
const staff = ctx.caller?.kind === "staff" ? ctx.caller : null;
|
|
500
|
+
const usedBy = staff?.userId ?? null;
|
|
501
|
+
try {
|
|
502
|
+
// Moved out of `valid` only if it is still valid when the host writes it (ADR 0437): of two
|
|
503
|
+
// scanners at the same moment, one is let in and the other told who was first.
|
|
504
|
+
await ctx.ops.call("record.update", {
|
|
505
|
+
object: TICKET,
|
|
506
|
+
id: record.id,
|
|
507
|
+
from: "valid",
|
|
508
|
+
values: { state: "used", usedAt, usedBy, usedByName: staff?.name || null },
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
catch (error) {
|
|
512
|
+
if (!(error instanceof OpsCallError) || error.code !== "conflict")
|
|
513
|
+
throw error;
|
|
514
|
+
const now = await ctx.ops.call("record.get", { object: TICKET, id: record.id });
|
|
515
|
+
const ticket = await describeTicket(ctx, now);
|
|
516
|
+
return (refusedFor(ticket) ?? {
|
|
517
|
+
// Moved by someone and back again in the meantime: say so, and let them scan again.
|
|
518
|
+
result: "unknown",
|
|
519
|
+
accepted: false,
|
|
520
|
+
message: "The ticket changed while it was being checked in. Scan it again.",
|
|
521
|
+
ticket,
|
|
522
|
+
balanceOwing: null,
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
const owing = await balanceOwing(ctx, text(record.values.order));
|
|
526
|
+
const name = before.holder?.name;
|
|
527
|
+
return {
|
|
528
|
+
result: "accepted",
|
|
529
|
+
accepted: true,
|
|
530
|
+
message: `Valid${name ? `: ${name}` : ""}. Let them in.` +
|
|
531
|
+
(owing ? ` A balance of ${(owing.amount / 100).toFixed(2)} ${owing.currency} is owing.` : ""),
|
|
532
|
+
ticket: {
|
|
533
|
+
...before,
|
|
534
|
+
state: "used",
|
|
535
|
+
usedAt,
|
|
536
|
+
usedBy: usedBy ? { id: usedBy, name: staff?.name || null } : null,
|
|
537
|
+
},
|
|
538
|
+
balanceOwing: owing,
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
export const checkIn = defineTool({
|
|
542
|
+
description: "Check a ticket in at the door by its code. A valid ticket is marked used and accepted; a " +
|
|
543
|
+
"used one is refused with who checked it in and when; a void (cancelled or refunded) one " +
|
|
544
|
+
"and an unknown code are refused with their own reasons. A ticket whose order still has a " +
|
|
545
|
+
"balance owing is accepted, and the result says how much. Safe to call twice: the second " +
|
|
546
|
+
"scan reports the first. Two scanners at once: one is accepted, the other told.",
|
|
547
|
+
input: checkInInput,
|
|
548
|
+
output: checkInOutput,
|
|
549
|
+
risk: "write",
|
|
550
|
+
permission: "ticketing:checkin",
|
|
551
|
+
handler: checkInHandler,
|
|
552
|
+
});
|
|
553
|
+
export const undoCheckIn = defineTool({
|
|
554
|
+
description: "Undo a check-in made by mistake: a used ticket becomes valid again and its check-in time " +
|
|
555
|
+
"and person are cleared. A ticket that is not used is left as it is and the result says why.",
|
|
556
|
+
input: z.object({ ticketId: z.string().min(1).max(64) }),
|
|
557
|
+
output: z.object({ undone: z.boolean(), message: z.string(), ticket: ticketOutput }),
|
|
558
|
+
risk: "write",
|
|
559
|
+
permission: "ticketing:checkin",
|
|
560
|
+
async handler(input, ctx) {
|
|
561
|
+
try {
|
|
562
|
+
const after = await ctx.ops.call("record.update", {
|
|
563
|
+
object: TICKET,
|
|
564
|
+
id: input.ticketId,
|
|
565
|
+
from: "used",
|
|
566
|
+
values: { state: "valid", usedAt: null, usedBy: null, usedByName: null },
|
|
567
|
+
});
|
|
568
|
+
return {
|
|
569
|
+
undone: true,
|
|
570
|
+
message: "Check-in undone. The ticket is valid again.",
|
|
571
|
+
ticket: await describeTicket(ctx, after),
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
catch (error) {
|
|
575
|
+
if (!(error instanceof OpsCallError) || error.code !== "conflict")
|
|
576
|
+
throw error;
|
|
577
|
+
const now = await ctx.ops.call("record.get", {
|
|
578
|
+
object: TICKET,
|
|
579
|
+
id: input.ticketId,
|
|
580
|
+
});
|
|
581
|
+
const ticket = await describeTicket(ctx, now);
|
|
582
|
+
return {
|
|
583
|
+
undone: false,
|
|
584
|
+
message: ticket.state === "void"
|
|
585
|
+
? "This ticket is void; there is no check-in to undo."
|
|
586
|
+
: "This ticket is not checked in; there is nothing to undo.",
|
|
587
|
+
ticket,
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
},
|
|
591
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@composable-os/pack-ticketing",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-beta.11",
|
|
4
|
+
"license": "FSL-1.1-ALv2",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/timoconnellaus/composable-os.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://composableos.dev",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"provenance": true
|
|
14
|
+
},
|
|
15
|
+
"description": "Ticketing: events, ticket types for sale and the tickets a paid order grants, check-in at the door and the door role (stage 27). Installed in every deployment of a product that lists it. Bundled as source into cos-runner.",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./client": {
|
|
22
|
+
"types": "./dist/client/index.d.ts",
|
|
23
|
+
"default": "./dist/client/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@composable-os/client": "0.1.0-beta.11",
|
|
31
|
+
"@composable-os/sdk": "0.1.0-beta.11"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/README.md
DELETED