@somewhere-cloud/cli 0.38.155 → 0.38.156
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/CHANGELOG.md +4 -1
- package/dist/commands/docs.js +1 -1
- package/dist/lib/bundle.js +276 -188
- package/dist/lib/bundle.js.map +3 -3
- package/npm-shrinkwrap.json +2 -2
- package/package.json +2 -3
- package/dist/docs/adopt.md +0 -39
- package/dist/docs/analytics.md +0 -161
- package/dist/docs/api-reference.md +0 -339
- package/dist/docs/api-surface.md +0 -2067
- package/dist/docs/app-mcp.md +0 -162
- package/dist/docs/architecture-patterns.md +0 -334
- package/dist/docs/auth-client.md +0 -531
- package/dist/docs/billing.md +0 -76
- package/dist/docs/browser.md +0 -639
- package/dist/docs/calendar.md +0 -298
- package/dist/docs/changes.md +0 -323
- package/dist/docs/cli.md +0 -407
- package/dist/docs/common-mistakes.md +0 -199
- package/dist/docs/connect-agent.md +0 -72
- package/dist/docs/cors.md +0 -114
- package/dist/docs/cron.md +0 -167
- package/dist/docs/database-engine.md +0 -167
- package/dist/docs/declared-data.md +0 -1656
- package/dist/docs/declared-files.md +0 -475
- package/dist/docs/deploy-intelligence.md +0 -188
- package/dist/docs/deploy.md +0 -733
- package/dist/docs/design-system.md +0 -273
- package/dist/docs/dev-environments.md +0 -496
- package/dist/docs/domain-ownership.md +0 -31
- package/dist/docs/domains.md +0 -370
- package/dist/docs/feedback.md +0 -88
- package/dist/docs/file-tools.md +0 -116
- package/dist/docs/functions.md +0 -489
- package/dist/docs/getting-started.md +0 -238
- package/dist/docs/github.md +0 -95
- package/dist/docs/google-calendar.md +0 -212
- package/dist/docs/groups.md +0 -836
- package/dist/docs/guarantees.md +0 -58
- package/dist/docs/handled-for-you.md +0 -224
- package/dist/docs/inspect.md +0 -171
- package/dist/docs/limits.md +0 -68
- package/dist/docs/live-data.md +0 -197
- package/dist/docs/live.md +0 -92
- package/dist/docs/local-dev.md +0 -30
- package/dist/docs/migration-supabase.md +0 -58
- package/dist/docs/payments.md +0 -1102
- package/dist/docs/portability.md +0 -194
- package/dist/docs/postgres.md +0 -301
- package/dist/docs/pricing-comparison.md +0 -42
- package/dist/docs/pricing.md +0 -39
- package/dist/docs/project-groups.md +0 -107
- package/dist/docs/projects.md +0 -380
- package/dist/docs/recipe-ai-agent.md +0 -112
- package/dist/docs/recipe-ai-features.md +0 -227
- package/dist/docs/recipe-ai-receipts.md +0 -783
- package/dist/docs/recipe-background-jobs.md +0 -369
- package/dist/docs/recipe-booking.md +0 -348
- package/dist/docs/recipe-csv-import.md +0 -390
- package/dist/docs/recipe-ecommerce.md +0 -127
- package/dist/docs/recipe-email.md +0 -282
- package/dist/docs/recipe-escalation.md +0 -133
- package/dist/docs/recipe-file-uploads.md +0 -159
- package/dist/docs/recipe-import-app.md +0 -110
- package/dist/docs/recipe-login.md +0 -457
- package/dist/docs/recipe-multi-chat.md +0 -198
- package/dist/docs/recipe-paid-api.md +0 -161
- package/dist/docs/recipe-pdfs.md +0 -205
- package/dist/docs/recipe-push-notifications.md +0 -107
- package/dist/docs/recipe-seo.md +0 -200
- package/dist/docs/recipe-signed-in-app.md +0 -330
- package/dist/docs/render.md +0 -99
- package/dist/docs/rooms.md +0 -554
- package/dist/docs/sdk.md +0 -287
- package/dist/docs/sdks.md +0 -24
- package/dist/docs/search.md +0 -199
- package/dist/docs/security-model.md +0 -168
- package/dist/docs/setup.md +0 -212
- package/dist/docs/smoke.md +0 -102
- package/dist/docs/speed.md +0 -298
- package/dist/docs/sql-compatibility.md +0 -96
- package/dist/docs/sw.agent.md +0 -248
- package/dist/docs/sw.ai.md +0 -1291
- package/dist/docs/sw.auth.md +0 -1811
- package/dist/docs/sw.billing.md +0 -171
- package/dist/docs/sw.db.md +0 -2128
- package/dist/docs/sw.email.md +0 -661
- package/dist/docs/sw.endpoint.md +0 -187
- package/dist/docs/sw.env.md +0 -303
- package/dist/docs/sw.fetch.md +0 -115
- package/dist/docs/sw.fs.md +0 -424
- package/dist/docs/sw.groups.md +0 -836
- package/dist/docs/sw.image.md +0 -69
- package/dist/docs/sw.jobs.md +0 -520
- package/dist/docs/sw.logs.md +0 -61
- package/dist/docs/sw.mail.md +0 -106
- package/dist/docs/sw.notifications.md +0 -78
- package/dist/docs/sw.push.md +0 -161
- package/dist/docs/sw.rateLimit.md +0 -67
- package/dist/docs/sw.web.md +0 -128
- package/dist/docs/tasks.md +0 -269
- package/dist/docs/test.md +0 -220
- package/dist/docs/tool-reference-account.md +0 -7
- package/dist/docs/tool-reference-ai.md +0 -121
- package/dist/docs/tool-reference-analytics.md +0 -37
- package/dist/docs/tool-reference-api.md +0 -19
- package/dist/docs/tool-reference-artifacts.md +0 -35
- package/dist/docs/tool-reference-auth.md +0 -163
- package/dist/docs/tool-reference-browser.md +0 -114
- package/dist/docs/tool-reference-code.md +0 -26
- package/dist/docs/tool-reference-connect.md +0 -21
- package/dist/docs/tool-reference-cron.md +0 -48
- package/dist/docs/tool-reference-db.md +0 -282
- package/dist/docs/tool-reference-domains.md +0 -107
- package/dist/docs/tool-reference-email.md +0 -55
- package/dist/docs/tool-reference-env.md +0 -19
- package/dist/docs/tool-reference-feedback.md +0 -56
- package/dist/docs/tool-reference-fs.md +0 -265
- package/dist/docs/tool-reference-github.md +0 -57
- package/dist/docs/tool-reference-groups.md +0 -85
- package/dist/docs/tool-reference-help.md +0 -29
- package/dist/docs/tool-reference-jobs.md +0 -39
- package/dist/docs/tool-reference-live.md +0 -48
- package/dist/docs/tool-reference-logs.md +0 -16
- package/dist/docs/tool-reference-payments.md +0 -44
- package/dist/docs/tool-reference-project.md +0 -706
- package/dist/docs/tool-reference-push.md +0 -78
- package/dist/docs/tool-reference-rate-limit.md +0 -18
- package/dist/docs/tool-reference-search.md +0 -68
- package/dist/docs/tool-reference-security.md +0 -42
- package/dist/docs/tool-reference-smoke.md +0 -17
- package/dist/docs/tool-reference-stock-photos.md +0 -20
- package/dist/docs/tool-reference-tasks.md +0 -73
- package/dist/docs/tool-reference-telegram.md +0 -32
- package/dist/docs/tool-reference-usage.md +0 -9
- package/dist/docs/tool-reference-web.md +0 -71
- package/dist/docs/tool-reference-webhooks.md +0 -9
- package/dist/docs/troubleshooting.md +0 -411
- package/dist/docs/typed-functions.md +0 -94
- package/dist/docs/verify-before-deploy.md +0 -67
- package/dist/docs/vs-supabase.md +0 -75
- package/dist/docs/vs-vercel.md +0 -103
|
@@ -1,348 +0,0 @@
|
|
|
1
|
-
# Recipe — Booking / calendar app
|
|
2
|
-
|
|
3
|
-
Calendar slot reservation with availability hints and payment checkout. Each
|
|
4
|
-
appointment locks a provider calendar range with `sw.calendar` and binds that
|
|
5
|
-
hold to checkout. This recipe stops before app-level fulfillment.
|
|
6
|
-
|
|
7
|
-
Pick the booking shape first:
|
|
8
|
-
|
|
9
|
-
- **Paid time slots, no customer account.** The first part of this recipe:
|
|
10
|
-
`sw.calendar` prevents double booking, checkout confirms the slot, and the
|
|
11
|
-
customer is identified by their checkout email.
|
|
12
|
-
- **One business whose staff all see every booking.** Store bookings in your
|
|
13
|
-
own table with `anyOf(owner(), appRole(...))`: signed-in customers see
|
|
14
|
-
their own bookings and every staff member sees all of them. Without payment,
|
|
15
|
-
follow "One business, no payment" below; `sw.groups` → `app-wide-roles`
|
|
16
|
-
grants staff.
|
|
17
|
-
- **Independent providers who each see only their own bookings.** Give each
|
|
18
|
-
booking row a per-booking provider access row, not an app-wide provider
|
|
19
|
-
role (`sw.db` → `provider-or-booker-and-owner-or-staff-recipes`).
|
|
20
|
-
|
|
21
|
-
## Database schema
|
|
22
|
-
|
|
23
|
-
Only provider configuration is server-only here: your functions own it, and
|
|
24
|
-
no customer or staff member ever reads it under their own name. Rows a
|
|
25
|
-
customer or a staff member reads (appointments, bookings) take a declared
|
|
26
|
-
rule: "One business, no payment" below, or the slots-and-bookings shape in
|
|
27
|
-
`sw.db` → `provider-or-booker-and-owner-or-staff-recipes`.
|
|
28
|
-
|
|
29
|
-
```ts
|
|
30
|
-
// db/schema.ts — trusted functions own provider configuration; browsers
|
|
31
|
-
// reach only the narrow availability and booking handlers below.
|
|
32
|
-
import { schema, table, id, text, integer, timestamp, serverOnly } from 'somewhere/db'
|
|
33
|
-
|
|
34
|
-
export default schema({
|
|
35
|
-
providers: table({
|
|
36
|
-
id: id({ uuid: true }),
|
|
37
|
-
user_id: text(),
|
|
38
|
-
name: text(),
|
|
39
|
-
slot_minutes: integer({ default: 60 }),
|
|
40
|
-
price_cents: integer({ default: 0 }),
|
|
41
|
-
timezone: text({ default: 'UTC' }),
|
|
42
|
-
created_at: timestamp({ default: 'now' }),
|
|
43
|
-
}, { scope: serverOnly() }),
|
|
44
|
-
})
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
## Function: api/availability.ts — list open slots
|
|
48
|
-
|
|
49
|
-
Set each provider's weekly hours once with `sw.calendar.setPolicy(resource,
|
|
50
|
-
{ hours: { timezone, weekly } })` from your provider-setup function
|
|
51
|
-
(`calendar` → `appointments-hours-and-slots`). `slots` then returns the
|
|
52
|
-
starts inside those hours, in absolute time, clear of every hold and booking.
|
|
53
|
-
|
|
54
|
-
```ts
|
|
55
|
-
// api/availability.ts — list open slots for the next two weeks
|
|
56
|
-
export default sw.endpoint({
|
|
57
|
-
auth: 'none',
|
|
58
|
-
handler: async ({ request }, sw) => {
|
|
59
|
-
const url = new URL(request.url)
|
|
60
|
-
const providerId = url.searchParams.get('provider_id')
|
|
61
|
-
|
|
62
|
-
// provider ids identify the public booking calendar; expose only timing.
|
|
63
|
-
const { data: providers } = await sw.db.from('providers', {
|
|
64
|
-
columns: ['slot_minutes'], where: { id: providerId }, limit: 1, asServer: true,
|
|
65
|
-
})
|
|
66
|
-
if (!providers[0]) return Response.json({ error: 'not_found' }, { status: 404 })
|
|
67
|
-
|
|
68
|
-
const now = Date.now()
|
|
69
|
-
const { slots } = await sw.calendar.slots({
|
|
70
|
-
resource: `provider:${providerId}`,
|
|
71
|
-
range: { start: now, end: now + 14 * 24 * 3600 * 1000 },
|
|
72
|
-
duration_minutes: providers[0].slot_minutes,
|
|
73
|
-
})
|
|
74
|
-
// Start and end times only: never forward reservation ids to the browser.
|
|
75
|
-
return { slots: slots.map(s => ({ starts_at: s.start_ms, ends_at: s.end_ms })) }
|
|
76
|
-
},
|
|
77
|
-
})
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
## Function: api/book.ts — reserve + checkout
|
|
81
|
-
|
|
82
|
-
```ts
|
|
83
|
-
// api/book.ts — reserve + checkout
|
|
84
|
-
export default sw.endpoint({
|
|
85
|
-
auth: 'none',
|
|
86
|
-
body: { provider_id: 'string', customer_email: 'email', starts_at: 'number' },
|
|
87
|
-
// Each call holds a slot for 45 minutes without payment: cap holds per caller.
|
|
88
|
-
rateLimit: '5/hour',
|
|
89
|
-
handler: async ({ body, request }, sw) => {
|
|
90
|
-
const { provider_id, customer_email, starts_at } = body
|
|
91
|
-
if (customer_email.length > 254 || !Number.isSafeInteger(starts_at) || starts_at <= Date.now() ||
|
|
92
|
-
starts_at > Date.now() + 90 * 24 * 3600 * 1000) {
|
|
93
|
-
return Response.json({ error: 'bad_request' }, { status: 400 })
|
|
94
|
-
}
|
|
95
|
-
// This public endpoint deliberately reads the selected provider's booking
|
|
96
|
-
// terms, then exposes only the checkout URL and hold expiry.
|
|
97
|
-
const { data: providers } = await sw.db.from('providers', {
|
|
98
|
-
columns: ['slot_minutes', 'price_cents', 'timezone'],
|
|
99
|
-
where: { id: provider_id }, limit: 1, asServer: true,
|
|
100
|
-
})
|
|
101
|
-
const provider = providers[0]
|
|
102
|
-
if (!provider) return Response.json({ error: 'not_found' }, { status: 404 })
|
|
103
|
-
|
|
104
|
-
// hold refuses a range outside the provider's hours (CALENDAR_OUTSIDE_HOURS).
|
|
105
|
-
const ends_at = starts_at + provider.slot_minutes * 60 * 1000
|
|
106
|
-
|
|
107
|
-
let hold
|
|
108
|
-
try {
|
|
109
|
-
hold = await sw.calendar.hold({
|
|
110
|
-
resource: `provider:${provider_id}`,
|
|
111
|
-
range: {
|
|
112
|
-
start: starts_at,
|
|
113
|
-
end: ends_at,
|
|
114
|
-
timezone: provider.timezone,
|
|
115
|
-
},
|
|
116
|
-
ttl_seconds: 45 * 60,
|
|
117
|
-
})
|
|
118
|
-
} catch (err) {
|
|
119
|
-
// A caught value is `unknown`: narrow it before reading its code.
|
|
120
|
-
if (typeof err === 'object' && err !== null && 'code' in err && err.code === 'CALENDAR_CONFLICT') {
|
|
121
|
-
return Response.json({ error: 'slot_taken' }, { status: 409 })
|
|
122
|
-
}
|
|
123
|
-
if (typeof err === 'object' && err !== null && 'code' in err &&
|
|
124
|
-
(err.code === 'CALENDAR_OUTSIDE_HOURS' || err.code === 'CALENDAR_ADVANCE_NOTICE')) {
|
|
125
|
-
return Response.json({ error: 'not_a_slot' }, { status: 400 })
|
|
126
|
-
}
|
|
127
|
-
throw err
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
try {
|
|
131
|
-
const checkout = await sw.payments.checkout({
|
|
132
|
-
mode: 'payment',
|
|
133
|
-
calendar_hold_token: hold.hold_token,
|
|
134
|
-
line_items: [{ amount: provider.price_cents, currency: 'usd', name: 'Appointment' }],
|
|
135
|
-
success_url: new URL('/checkout-return', request.url).toString(),
|
|
136
|
-
cancel_url: new URL('/book', request.url).toString(),
|
|
137
|
-
customer_email,
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
return {
|
|
141
|
-
reservation_id: hold.reservation.id,
|
|
142
|
-
status: 'pending_payment',
|
|
143
|
-
checkout_url: checkout.url,
|
|
144
|
-
hold_expires_at: hold.expires_at,
|
|
145
|
-
}
|
|
146
|
-
} catch (err) {
|
|
147
|
-
await sw.calendar.release(hold.hold_token, { release_reason: 'checkout_setup_failed' }).catch(() => {})
|
|
148
|
-
throw err
|
|
149
|
-
}
|
|
150
|
-
},
|
|
151
|
-
})
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
Checkout uses test payments in development and live payments in production.
|
|
155
|
-
Live checkout requires completed payment onboarding. A preview can't start a
|
|
156
|
-
checkout with a calendar hold (`PREVIEW_PAYMENT_SHARED_RECORD_REFUSED`),
|
|
157
|
-
because holds are shared with the live app; test this flow in development.
|
|
158
|
-
|
|
159
|
-
The checkout binds the hold to the session. The platform confirms the
|
|
160
|
-
reservation on checkout completion and releases the hold when the session
|
|
161
|
-
expires. Read the reservation with `sw.calendar.get(reservation_id)` in a
|
|
162
|
-
function to show its current status on a return page.
|
|
163
|
-
|
|
164
|
-
To receive calendar checkout events, set your project's webhook URL with
|
|
165
|
-
`auth_webhook_set`; the same URL receives account events. See
|
|
166
|
-
[Payment events](/docs/payments#webhook-events-what-each-flow-actually-emits)
|
|
167
|
-
and [Webhook signatures](/docs/sw.auth#lifecycle-webhook-clean-up-your-own-tables-on-user-delete).
|
|
168
|
-
`payment.completed` reports checkout completion. Delayed payment methods
|
|
169
|
-
can still be unpaid at that point, so keep fulfillment behind a verified paid
|
|
170
|
-
payment status. This recipe ends at checkout and reservation status.
|
|
171
|
-
|
|
172
|
-
## One business, no payment
|
|
173
|
-
|
|
174
|
-
Signed-in customers book a slot, see their own appointments, and every staff
|
|
175
|
-
member sees all of them. Your `appointments` table is the record;
|
|
176
|
-
`sw.calendar` only stops two customers taking the same slot.
|
|
177
|
-
|
|
178
|
-
The calendar and the database are separate services: no call writes both
|
|
179
|
-
atomically, and a failed call does not always mean nothing happened. Order the
|
|
180
|
-
steps so that every failure leaves a state you can see and settle:
|
|
181
|
-
|
|
182
|
-
1. **Hold** the slot. `CALENDAR_CONFLICT` means it is taken; nothing was
|
|
183
|
-
written. A hold that is never confirmed ends by itself at its TTL.
|
|
184
|
-
2. **Record** the appointment row with the hold's reservation id and status
|
|
185
|
-
`held`. Only `DATA_CONFLICT` proves the row was not written: release the
|
|
186
|
-
hold then. Any other failure, such as `DB_WRITE_OUTCOME_UNKNOWN` or
|
|
187
|
-
`DB_COMMITTED_READ_FAILED`, may have saved the row, so keep the hold and
|
|
188
|
-
answer "unknown". If nothing was saved the hold runs out by itself; a saved
|
|
189
|
-
row is settled in step 4. Do not book again automatically: the hold still
|
|
190
|
-
owns the slot.
|
|
191
|
-
3. **Confirm** the hold. Confirming an already confirmed hold returns it
|
|
192
|
-
again. `CALENDAR_HOLD_EXPIRED` or `CALENDAR_HOLD_NOT_ACTIVE` means the slot
|
|
193
|
-
is no longer held. Any other failure is an unknown outcome: leave the row
|
|
194
|
-
`held`.
|
|
195
|
-
4. **Settle** a row only from `held`: `confirmed` when the reservation is
|
|
196
|
-
confirmed, `released` when its hold has ended. Make the update conditional
|
|
197
|
-
on `status: 'held'` and re-read the row, so a status someone else set in
|
|
198
|
-
the meantime (for example staff marking it done) is never overwritten and
|
|
199
|
-
the answer is what is stored.
|
|
200
|
-
|
|
201
|
-
```ts
|
|
202
|
-
// db/schema.ts — customers read their own appointments; staff read all of them
|
|
203
|
-
import { anyOf, appRole, id, integer, owner, schema, table, text } from 'somewhere/db'
|
|
204
|
-
|
|
205
|
-
export default schema({
|
|
206
|
-
appointments: table({
|
|
207
|
-
id: id(),
|
|
208
|
-
reservation_id: text({ unique: true }),
|
|
209
|
-
starts_at: integer(),
|
|
210
|
-
ends_at: integer(),
|
|
211
|
-
status: text({ default: 'held' }), // held, then confirmed or released; staff: done or no_show
|
|
212
|
-
}, {
|
|
213
|
-
scope: anyOf(owner(), appRole({ roles: { read: ['staff'], update: ['staff'] } })),
|
|
214
|
-
client: { read: ['id', 'starts_at', 'ends_at', 'status'] },
|
|
215
|
-
}),
|
|
216
|
-
}, { appRoles: { staff: { grantableBy: [] } } })
|
|
217
|
-
```
|
|
218
|
-
|
|
219
|
-
```ts
|
|
220
|
-
// api/appointments/book.ts — hold the slot, record it, then confirm
|
|
221
|
-
const SLOT_MS = 30 * 60 * 1000
|
|
222
|
-
|
|
223
|
-
function errorCode(err: unknown): unknown {
|
|
224
|
-
return typeof err === 'object' && err !== null && 'code' in err ? err.code : null
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// Moves a still-held row to its settled status, then answers with the row as stored.
|
|
228
|
-
async function settle(sw: SomewhereRuntimeContext, reservationId: string, status: 'confirmed' | 'released') {
|
|
229
|
-
await sw.db.update('appointments', { set: { status }, where: { reservation_id: reservationId, status: 'held' } })
|
|
230
|
-
const { data } = await sw.db.from('appointments', { where: { reservation_id: reservationId }, limit: 1 })
|
|
231
|
-
return data[0] ?? null
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
export default sw.endpoint({
|
|
235
|
-
auth: 'required',
|
|
236
|
-
body: { starts_at: 'number' },
|
|
237
|
-
rateLimit: '10/hour',
|
|
238
|
-
handler: async ({ body }, sw) => {
|
|
239
|
-
const startsAt = body.starts_at
|
|
240
|
-
if (!Number.isSafeInteger(startsAt) || startsAt % SLOT_MS !== 0 || startsAt <= Date.now()) {
|
|
241
|
-
return Response.json({ error: 'not_a_slot' }, { status: 400 })
|
|
242
|
-
}
|
|
243
|
-
// 1. Hold: the calendar refuses an overlapping hold or booking.
|
|
244
|
-
let hold
|
|
245
|
-
try {
|
|
246
|
-
hold = await sw.calendar.hold({
|
|
247
|
-
resource: 'salon',
|
|
248
|
-
range: { start: startsAt, end: startsAt + SLOT_MS, timezone: 'UTC' },
|
|
249
|
-
ttl_seconds: 5 * 60,
|
|
250
|
-
})
|
|
251
|
-
} catch (err) {
|
|
252
|
-
if (errorCode(err) === 'CALENDAR_CONFLICT') return Response.json({ error: 'slot_taken' }, { status: 409 })
|
|
253
|
-
throw err
|
|
254
|
-
}
|
|
255
|
-
const reservationId = hold.reservation.id
|
|
256
|
-
// 2. Record: the signed-in customer becomes the row's owner.
|
|
257
|
-
try {
|
|
258
|
-
await sw.db.insert('appointments', {
|
|
259
|
-
reservation_id: reservationId, starts_at: startsAt, ends_at: startsAt + SLOT_MS, status: 'held',
|
|
260
|
-
})
|
|
261
|
-
} catch (err) {
|
|
262
|
-
if (errorCode(err) === 'DATA_CONFLICT') {
|
|
263
|
-
// Refused, so nothing was written: free the slot now.
|
|
264
|
-
await sw.calendar.release(hold.hold_token, { release_reason: 'record_refused' }).catch(() => {})
|
|
265
|
-
return Response.json({ error: 'not_recorded' }, { status: 409 })
|
|
266
|
-
}
|
|
267
|
-
// The row may have been saved. Keep the hold: it runs out if nothing was.
|
|
268
|
-
return Response.json({ reservation_id: reservationId, status: 'unknown' }, { status: 503 })
|
|
269
|
-
}
|
|
270
|
-
// 3. Confirm.
|
|
271
|
-
try {
|
|
272
|
-
await sw.calendar.confirm(hold.hold_token)
|
|
273
|
-
} catch (err) {
|
|
274
|
-
const code = errorCode(err)
|
|
275
|
-
if (code === 'CALENDAR_HOLD_EXPIRED' || code === 'CALENDAR_HOLD_NOT_ACTIVE') {
|
|
276
|
-
return Response.json({ error: 'hold_expired', appointment: await settle(sw, reservationId, 'released') }, { status: 409 })
|
|
277
|
-
}
|
|
278
|
-
// Unknown outcome: the row stays held until its reservation says otherwise.
|
|
279
|
-
return Response.json({ reservation_id: reservationId, status: 'pending' }, { status: 202 })
|
|
280
|
-
}
|
|
281
|
-
// 4. Settle.
|
|
282
|
-
return { appointment: await settle(sw, reservationId, 'confirmed') }
|
|
283
|
-
},
|
|
284
|
-
})
|
|
285
|
-
```
|
|
286
|
-
|
|
287
|
-
```ts
|
|
288
|
-
// api/appointments/[id].ts — one appointment; a held row is settled from its reservation
|
|
289
|
-
export default sw.endpoint({
|
|
290
|
-
auth: 'required',
|
|
291
|
-
handler: async ({ params }, sw) => {
|
|
292
|
-
const read = async () => (await sw.db.from('appointments', { where: { id: params.id }, limit: 1 })).data[0]
|
|
293
|
-
const row = await read()
|
|
294
|
-
if (!row) return Response.json({ error: 'not_found' }, { status: 404 })
|
|
295
|
-
if (row.status !== 'held') return row
|
|
296
|
-
const { reservation } = await sw.calendar.get(row.reservation_id)
|
|
297
|
-
const active = (reservation.status === 'hold' || reservation.status === 'pending_payment')
|
|
298
|
-
&& reservation.expires_at !== null && reservation.expires_at > Date.now()
|
|
299
|
-
if (active) return row // still held: it is confirmed or runs out later
|
|
300
|
-
const status = reservation.status === 'confirmed' ? 'confirmed' : 'released'
|
|
301
|
-
// Only a row that is still held moves; a status set meanwhile stands.
|
|
302
|
-
await sw.db.update('appointments', { set: { status }, where: { id: row.id, status: 'held' } })
|
|
303
|
-
return (await read()) ?? Response.json({ error: 'not_found' }, { status: 404 })
|
|
304
|
-
},
|
|
305
|
-
})
|
|
306
|
-
```
|
|
307
|
-
|
|
308
|
-
A customer may not change an appointment's status, and a browser `client`
|
|
309
|
-
grant applies to the whole table, not to one branch of its scope. So status
|
|
310
|
-
changes go through a function. Because the `owner()` branch also lets a
|
|
311
|
-
customer update their own row, the role check, not the scope, is what keeps
|
|
312
|
-
this function staff-only (`sw.groups` → `app-wide-roles`):
|
|
313
|
-
|
|
314
|
-
```ts
|
|
315
|
-
// api/appointments/status.ts — staff mark an appointment done or no-show
|
|
316
|
-
export default sw.endpoint({
|
|
317
|
-
auth: 'required',
|
|
318
|
-
body: { id: 'string', status: 'string' },
|
|
319
|
-
handler: async ({ body, request }, sw) => {
|
|
320
|
-
if (body.status !== 'done' && body.status !== 'no_show') return Response.json({ error: 'bad_status' }, { status: 400 })
|
|
321
|
-
let staff = false
|
|
322
|
-
let cursor: string | undefined
|
|
323
|
-
do {
|
|
324
|
-
const { data, error } = await sw.groups.appRoles.list(request, undefined, cursor ? { cursor } : undefined)
|
|
325
|
-
// A failed read is never treated as "not staff": report it.
|
|
326
|
-
if (error) return Response.json({ error: error.code }, { status: error.status })
|
|
327
|
-
staff = data.items.some((grant) => grant.role === 'staff')
|
|
328
|
-
cursor = staff ? undefined : data.next_cursor ?? undefined
|
|
329
|
-
} while (cursor)
|
|
330
|
-
if (!staff) return Response.json({ error: 'forbidden' }, { status: 403 })
|
|
331
|
-
// Only a confirmed appointment can be marked; a held one is still being settled.
|
|
332
|
-
const { changes } = await sw.db.update('appointments', { set: { status: body.status }, where: { id: body.id, status: 'confirmed' } })
|
|
333
|
-
if (changes) return { ok: true }
|
|
334
|
-
const { data } = await sw.db.from('appointments', { where: { id: body.id }, limit: 1 })
|
|
335
|
-
return data[0]
|
|
336
|
-
? Response.json({ error: 'not_confirmed', status: data[0].status }, { status: 409 })
|
|
337
|
-
: Response.json({ error: 'not_found' }, { status: 404 })
|
|
338
|
-
},
|
|
339
|
-
})
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
A customer cancelling a confirmed appointment calls `sw.calendar.cancel`
|
|
343
|
-
with its reservation id, then moves the row from `confirmed` the same
|
|
344
|
-
conditional way; `release` applies only to a hold that is not yet confirmed. Grant the first staff member with
|
|
345
|
-
`somewhere auth roles grant <address> staff` (MCP `app_role_grant`); `sw.groups` → `app-wide-roles`
|
|
346
|
-
has the in-app path.
|
|
347
|
-
|
|
348
|
-
Related topics: `calendar`, `sw.db`, `sw.payments`, `sw.groups`.
|