@simplelayer/graniteer-core 0.1.2 → 0.2.0
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/index.js +2 -2
- package/package.json +73 -2
- package/src/agreementCheckout.ts +250 -0
- package/src/agreementPortalEmail.ts +128 -0
- package/src/billingPortalLinkEmail.ts +117 -0
- package/src/bookingAgreementEmail.ts +149 -0
- package/src/bookingEffects.ts +388 -0
- package/src/busClient.ts +268 -0
- package/src/busSignature.ts +71 -0
- package/src/checkoutCoupon.test.ts +675 -0
- package/src/checkoutCoupon.ts +455 -0
- package/src/commonsDeposit.ts +298 -0
- package/src/commonsDepositWebhook.ts +237 -0
- package/src/dayPassConfirmation.ts +230 -0
- package/src/dunningEmails.test.ts +263 -0
- package/src/dunningEmails.ts +203 -0
- package/src/email-templates/brand.ts +93 -0
- package/src/email-templates/magic-link.tsx +59 -0
- package/src/email-templates/recovery.tsx +59 -0
- package/src/email-templates/signup.tsx +67 -0
- package/src/functions/admin-cancel-membership.ts +311 -311
- package/src/functions/admin-change-membership-plan.ts +312 -308
- package/src/functions/admin-charge-guest-print.ts +229 -229
- package/src/functions/admin-correct-booking.ts +333 -333
- package/src/functions/admin-create-booking.ts +460 -0
- package/src/functions/admin-create-commons-event.ts +244 -244
- package/src/functions/admin-debug-boldsign-doc.ts +93 -93
- package/src/functions/admin-door-write.ts +272 -272
- package/src/functions/admin-issue-day-pass.ts +231 -0
- package/src/functions/admin-mark-booking-paid.ts +203 -203
- package/src/functions/admin-mint-billing-portal-link.ts +168 -0
- package/src/functions/admin-mint-portal-link.ts +62 -0
- package/src/functions/admin-photo-upload.ts +128 -128
- package/src/functions/admin-remove-team-member.ts +150 -150
- package/src/functions/admin-request-trial.ts +366 -0
- package/src/functions/admin-resend-commons-deposit-link.ts +323 -0
- package/src/functions/admin-reset-mfa.ts +138 -138
- package/src/functions/admin-retry-invoice-payment.test.ts +567 -0
- package/src/functions/admin-retry-invoice-payment.ts +333 -0
- package/src/functions/admin-reverse-coupon-redemption.ts +98 -98
- package/src/functions/admin-revoke-agreement.ts +467 -0
- package/src/functions/admin-send-commons-agreement.ts +214 -214
- package/src/functions/admin-set-credit-amount.ts +123 -123
- package/src/functions/admin-skip-commons-contract.ts +165 -165
- package/src/functions/admin-undo-membership-cancel.ts +144 -144
- package/src/functions/admin-wall-photo-write.ts +141 -141
- package/src/functions/auth-email-hook.ts +259 -0
- package/src/functions/boldsign-sender-identity.ts +114 -114
- package/src/functions/booking-availability.ts +419 -419
- package/src/functions/booking-member-preview.ts +136 -136
- package/src/functions/booking-status.ts +149 -0
- package/src/functions/conductor.ts +505 -0
- package/src/functions/create-agreement.ts +537 -0
- package/src/functions/create-booking.ts +648 -0
- package/src/functions/create-day-pass-checkout.ts +218 -0
- package/src/functions/create-portal-session.ts +142 -0
- package/src/functions/create-subscription-checkout.test.ts +222 -0
- package/src/functions/create-subscription-checkout.ts +440 -0
- package/src/functions/day-pass-status.ts +89 -89
- package/src/functions/diag-webpush-test.ts +45 -45
- package/src/functions/event-access-admin.ts +277 -0
- package/src/functions/event-access-store.ts +309 -309
- package/src/functions/ezeep-print-webhook.ts +213 -213
- package/src/functions/ezeep-usage-reconcile.ts +299 -299
- package/src/functions/fetch-avatar-original.ts +457 -0
- package/src/functions/get-provider-sign-link.ts +155 -0
- package/src/functions/get-signing-link.ts +211 -211
- package/src/functions/guest-print-setup-confirm.ts +128 -128
- package/src/functions/guest-print-submit.ts +149 -149
- package/src/functions/luma-webhook.ts +165 -165
- package/src/functions/manage-booking.ts +472 -0
- package/src/functions/mcp.ts +67 -0
- package/src/functions/member-door-action.test.ts +1248 -0
- package/src/functions/member-door-action.ts +863 -0
- package/src/functions/member-unifi-mapping.test.ts +516 -0
- package/src/functions/member-unifi-mapping.ts +206 -0
- package/src/functions/membership-door-sync.ts +956 -0
- package/src/functions/migration-collision-alert.copy.ts +232 -232
- package/src/functions/migration-collision-alert.test.ts +349 -349
- package/src/functions/migration-collision-alert.ts +232 -232
- package/src/functions/office-assignment-end.ts +146 -146
- package/src/functions/portal-booking-access.ts +95 -95
- package/src/functions/portal-list-invoices.ts +99 -99
- package/src/functions/portal-request-login.ts +192 -0
- package/src/functions/portal-resend-invite.ts +248 -0
- package/src/functions/portal-submit-event.ts +168 -0
- package/src/functions/print-billing-recover.ts +97 -97
- package/src/functions/print-billing-run.ts +183 -183
- package/src/functions/public-facts.ts +168 -168
- package/src/functions/push-send.ts +348 -0
- package/src/functions/push-test.ts +234 -0
- package/src/functions/request-portal-link.ts +211 -0
- package/src/functions/reschedule-booking.ts +649 -0
- package/src/functions/review-ratings.ts +113 -0
- package/src/functions/space-availability-range.acceptance.test.ts +161 -161
- package/src/functions/space-availability-range.ts +142 -142
- package/src/functions/stripe-db-reconcile.ts +319 -0
- package/src/functions/stripe-webhook.moved-regression.test.ts +78 -0
- package/src/functions/stripe-webhook.ts +2595 -0
- package/src/functions/sync-gcal-blackouts.ts +298 -298
- package/src/functions/timetable-event-register.ts +162 -162
- package/src/functions/timetable-event-status.ts +90 -90
- package/src/functions/timetable-spaces.ts +219 -219
- package/src/functions/touch-pass-reconciler.ts +689 -689
- package/src/functions/touch-pass-signup.ts +478 -0
- package/src/functions/upload-booking-display-image.ts +179 -0
- package/src/functions/virtual-onboarding-purge.ts +134 -134
- package/src/functions/virtual-onboarding-submit.ts +315 -315
- package/src/functions/voice-booking.ts +453 -0
- package/src/functions/voice-day-pass.ts +346 -0
- package/src/functions/voice-inquiry.test.ts +301 -301
- package/src/functions/voice-inquiry.ts +198 -198
- package/src/functions/voice-membership-signup.ts +355 -0
- package/src/functions/voice-tour-availability.ts +204 -204
- package/src/functions/voice-tour-booking.ts +429 -0
- package/src/ghlEventInquiry.ts +143 -0
- package/src/mailArrivalEmail.ts +121 -0
- package/src/manageToken.ts +363 -0
- package/src/membershipBusClient.ts +537 -0
- package/src/membershipWelcome.ts +683 -0
- package/src/migrationCheckout.ts +366 -0
- package/src/pacificDate.ts +54 -0
- package/src/paymentLinkEmail.ts +123 -0
- package/src/signedUnpaidReminderEmail.test.ts +246 -0
- package/src/signedUnpaidReminderEmail.ts +132 -0
- package/src/stripeCoworkingPortal.ts +91 -0
- package/src/supabaseTestFakes.ts +131 -131
- package/src/supportTicketEmail.ts +119 -0
- package/src/unifiAccessPolicies.ts +228 -0
- package/src/unifiGroups.ts +129 -0
|
@@ -1,136 +1,136 @@
|
|
|
1
|
-
// Read-only member-recognition preview for the booking widget.
|
|
2
|
-
//
|
|
3
|
-
// Lets BookingWidget.tsx acknowledge, as someone types a contact email, that
|
|
4
|
-
// a monthly room credit will apply to their booking — WITHOUT ever quoting a
|
|
5
|
-
// dollar figure to an unauthenticated caller. Credit is matched server-side
|
|
6
|
-
// by email (same as create-booking), so a typed address alone is enough for
|
|
7
|
-
// anyone to probe whether it belongs to a member; the balance itself is not.
|
|
8
|
-
//
|
|
9
|
-
// This endpoint therefore NEVER returns a cents figure, full stop. The
|
|
10
|
-
// member's actual available credit is only ever exposed through the portal's
|
|
11
|
-
// own RLS-scoped session (portalSupabase / portalCredits.ts's
|
|
12
|
-
// fetchMonthlyCreditAllowance) — a completely separate client with its own
|
|
13
|
-
// isolated auth storage (see src/integrations/supabase/portalClient.ts). This
|
|
14
|
-
// function has no path to that session and must not grow one; if a caller
|
|
15
|
-
// ever needs the real number, it belongs in the portal, not here.
|
|
16
|
-
import { corsHeaders } from 'npm:@supabase/supabase-js@2/cors';
|
|
17
|
-
import { createClient } from 'npm:@supabase/supabase-js@2';
|
|
18
|
-
import { z } from 'npm:zod@3.23.8';
|
|
19
|
-
import { clientIp, checkRateLimit } from 'npm:@simplelayer/graniteer-core/rateLimit';
|
|
20
|
-
import { resolveMembershipContext } from 'npm:@simplelayer/graniteer-core/membership';
|
|
21
|
-
|
|
22
|
-
const Body = z.object({
|
|
23
|
-
email: z.string().email(),
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
export async function handle(req: Request): Promise<Response> {
|
|
27
|
-
if (req.method === 'OPTIONS') return new Response('ok', { headers: corsHeaders });
|
|
28
|
-
|
|
29
|
-
try {
|
|
30
|
-
const raw = await req.json().catch(() => ({}));
|
|
31
|
-
const parsed = Body.safeParse(raw);
|
|
32
|
-
if (!parsed.success) {
|
|
33
|
-
return new Response(JSON.stringify({ error: parsed.error.flatten() }), {
|
|
34
|
-
status: 400, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
const emailKey = parsed.data.email.trim().toLowerCase();
|
|
38
|
-
|
|
39
|
-
const supabase = createClient(
|
|
40
|
-
Deno.env.get('SUPABASE_URL')!,
|
|
41
|
-
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!,
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
const ip = clientIp(req);
|
|
45
|
-
const [ipOk, emailOk] = await Promise.all([
|
|
46
|
-
checkRateLimit(supabase, 'booking-member-preview-ip', ip, 60, 60),
|
|
47
|
-
checkRateLimit(supabase, 'booking-member-preview-email', emailKey, 20, 60),
|
|
48
|
-
]);
|
|
49
|
-
if (!ipOk || !emailOk) {
|
|
50
|
-
return new Response(JSON.stringify({ error: 'Too many requests — please wait a moment and try again.' }), {
|
|
51
|
-
status: 429, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const { isMember, membershipId } = await resolveMembershipContext(supabase, emailKey);
|
|
56
|
-
if (!isMember || !membershipId) {
|
|
57
|
-
return new Response(JSON.stringify({ is_member: false }), {
|
|
58
|
-
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
let plan_name: string | null = null;
|
|
63
|
-
const { data: membership } = await supabase
|
|
64
|
-
.from('memberships')
|
|
65
|
-
.select('plan_id')
|
|
66
|
-
.eq('id', membershipId)
|
|
67
|
-
.maybeSingle();
|
|
68
|
-
if (membership?.plan_id) {
|
|
69
|
-
const { data: plan } = await supabase
|
|
70
|
-
.from('membership_plans')
|
|
71
|
-
.select('name')
|
|
72
|
-
.eq('id', membership.plan_id)
|
|
73
|
-
.maybeSingle();
|
|
74
|
-
plan_name = plan?.name ?? null;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Being a member does NOT mean having room credit: several plans (Day
|
|
78
|
-
// Pass, Starter, Virtual Office) carry no monthly credit at all, and a
|
|
79
|
-
// member who already spent this month's allowance has none left. The
|
|
80
|
-
// widget must not promise a credit in those cases, so resolve a boolean
|
|
81
|
-
// (never a cents figure — see file header) the same way the portal does:
|
|
82
|
-
// override -> plan default for the allowance, then this period's ledger
|
|
83
|
-
// for what actually remains.
|
|
84
|
-
let has_room_credit = false;
|
|
85
|
-
try {
|
|
86
|
-
const [overrideRes, defaultRes] = await Promise.all([
|
|
87
|
-
supabase
|
|
88
|
-
.from('member_credit_overrides')
|
|
89
|
-
.select('monthly_cents')
|
|
90
|
-
.eq('membership_id', membershipId)
|
|
91
|
-
.is('effective_to', null)
|
|
92
|
-
.maybeSingle(),
|
|
93
|
-
membership?.plan_id
|
|
94
|
-
? supabase
|
|
95
|
-
.from('plan_credit_defaults')
|
|
96
|
-
.select('monthly_cents')
|
|
97
|
-
.eq('plan_id', membership.plan_id)
|
|
98
|
-
.is('effective_to', null)
|
|
99
|
-
.maybeSingle()
|
|
100
|
-
: Promise.resolve({ data: null }),
|
|
101
|
-
]);
|
|
102
|
-
const monthlyCents = overrideRes.data?.monthly_cents ?? defaultRes.data?.monthly_cents ?? 0;
|
|
103
|
-
|
|
104
|
-
if (monthlyCents > 0) {
|
|
105
|
-
// Read-only: never create a period row from a public preview.
|
|
106
|
-
const { data: periodMonth } = await supabase.rpc('current_period_month');
|
|
107
|
-
const { data: period } = await supabase
|
|
108
|
-
.from('credit_periods')
|
|
109
|
-
.select('granted_cents,consumed_cents,held_cents,expired_cents')
|
|
110
|
-
.eq('membership_id', membershipId)
|
|
111
|
-
.eq('period_month', periodMonth)
|
|
112
|
-
.maybeSingle();
|
|
113
|
-
has_room_credit = period
|
|
114
|
-
? (period.granted_cents ?? 0) - (period.consumed_cents ?? 0)
|
|
115
|
-
- (period.held_cents ?? 0) - (period.expired_cents ?? 0) > 0
|
|
116
|
-
// No period row yet this month — the grant happens at booking time,
|
|
117
|
-
// so an entitled plan still has its full allowance available.
|
|
118
|
-
: true;
|
|
119
|
-
}
|
|
120
|
-
} catch (_e) {
|
|
121
|
-
// Credit lookup is advisory for a display hint only — fall back to the
|
|
122
|
-
// conservative answer (no promise of a credit) rather than 500-ing.
|
|
123
|
-
has_room_credit = false;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// No cents field anywhere in this response — see file header.
|
|
127
|
-
return new Response(JSON.stringify({ is_member: true, plan_name, has_room_credit }), {
|
|
128
|
-
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
129
|
-
});
|
|
130
|
-
} catch (e) {
|
|
131
|
-
return new Response(
|
|
132
|
-
JSON.stringify({ error: e instanceof Error ? e.message : String(e) }),
|
|
133
|
-
{ status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } },
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
1
|
+
// Read-only member-recognition preview for the booking widget.
|
|
2
|
+
//
|
|
3
|
+
// Lets BookingWidget.tsx acknowledge, as someone types a contact email, that
|
|
4
|
+
// a monthly room credit will apply to their booking — WITHOUT ever quoting a
|
|
5
|
+
// dollar figure to an unauthenticated caller. Credit is matched server-side
|
|
6
|
+
// by email (same as create-booking), so a typed address alone is enough for
|
|
7
|
+
// anyone to probe whether it belongs to a member; the balance itself is not.
|
|
8
|
+
//
|
|
9
|
+
// This endpoint therefore NEVER returns a cents figure, full stop. The
|
|
10
|
+
// member's actual available credit is only ever exposed through the portal's
|
|
11
|
+
// own RLS-scoped session (portalSupabase / portalCredits.ts's
|
|
12
|
+
// fetchMonthlyCreditAllowance) — a completely separate client with its own
|
|
13
|
+
// isolated auth storage (see src/integrations/supabase/portalClient.ts). This
|
|
14
|
+
// function has no path to that session and must not grow one; if a caller
|
|
15
|
+
// ever needs the real number, it belongs in the portal, not here.
|
|
16
|
+
import { corsHeaders } from 'npm:@supabase/supabase-js@2/cors';
|
|
17
|
+
import { createClient } from 'npm:@supabase/supabase-js@2';
|
|
18
|
+
import { z } from 'npm:zod@3.23.8';
|
|
19
|
+
import { clientIp, checkRateLimit } from 'npm:@simplelayer/graniteer-core/rateLimit';
|
|
20
|
+
import { resolveMembershipContext } from 'npm:@simplelayer/graniteer-core/membership';
|
|
21
|
+
|
|
22
|
+
const Body = z.object({
|
|
23
|
+
email: z.string().email(),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export async function handle(req: Request): Promise<Response> {
|
|
27
|
+
if (req.method === 'OPTIONS') return new Response('ok', { headers: corsHeaders });
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
const raw = await req.json().catch(() => ({}));
|
|
31
|
+
const parsed = Body.safeParse(raw);
|
|
32
|
+
if (!parsed.success) {
|
|
33
|
+
return new Response(JSON.stringify({ error: parsed.error.flatten() }), {
|
|
34
|
+
status: 400, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
const emailKey = parsed.data.email.trim().toLowerCase();
|
|
38
|
+
|
|
39
|
+
const supabase = createClient(
|
|
40
|
+
Deno.env.get('SUPABASE_URL')!,
|
|
41
|
+
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!,
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const ip = clientIp(req);
|
|
45
|
+
const [ipOk, emailOk] = await Promise.all([
|
|
46
|
+
checkRateLimit(supabase, 'booking-member-preview-ip', ip, 60, 60),
|
|
47
|
+
checkRateLimit(supabase, 'booking-member-preview-email', emailKey, 20, 60),
|
|
48
|
+
]);
|
|
49
|
+
if (!ipOk || !emailOk) {
|
|
50
|
+
return new Response(JSON.stringify({ error: 'Too many requests — please wait a moment and try again.' }), {
|
|
51
|
+
status: 429, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const { isMember, membershipId } = await resolveMembershipContext(supabase, emailKey);
|
|
56
|
+
if (!isMember || !membershipId) {
|
|
57
|
+
return new Response(JSON.stringify({ is_member: false }), {
|
|
58
|
+
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let plan_name: string | null = null;
|
|
63
|
+
const { data: membership } = await supabase
|
|
64
|
+
.from('memberships')
|
|
65
|
+
.select('plan_id')
|
|
66
|
+
.eq('id', membershipId)
|
|
67
|
+
.maybeSingle();
|
|
68
|
+
if (membership?.plan_id) {
|
|
69
|
+
const { data: plan } = await supabase
|
|
70
|
+
.from('membership_plans')
|
|
71
|
+
.select('name')
|
|
72
|
+
.eq('id', membership.plan_id)
|
|
73
|
+
.maybeSingle();
|
|
74
|
+
plan_name = plan?.name ?? null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Being a member does NOT mean having room credit: several plans (Day
|
|
78
|
+
// Pass, Starter, Virtual Office) carry no monthly credit at all, and a
|
|
79
|
+
// member who already spent this month's allowance has none left. The
|
|
80
|
+
// widget must not promise a credit in those cases, so resolve a boolean
|
|
81
|
+
// (never a cents figure — see file header) the same way the portal does:
|
|
82
|
+
// override -> plan default for the allowance, then this period's ledger
|
|
83
|
+
// for what actually remains.
|
|
84
|
+
let has_room_credit = false;
|
|
85
|
+
try {
|
|
86
|
+
const [overrideRes, defaultRes] = await Promise.all([
|
|
87
|
+
supabase
|
|
88
|
+
.from('member_credit_overrides')
|
|
89
|
+
.select('monthly_cents')
|
|
90
|
+
.eq('membership_id', membershipId)
|
|
91
|
+
.is('effective_to', null)
|
|
92
|
+
.maybeSingle(),
|
|
93
|
+
membership?.plan_id
|
|
94
|
+
? supabase
|
|
95
|
+
.from('plan_credit_defaults')
|
|
96
|
+
.select('monthly_cents')
|
|
97
|
+
.eq('plan_id', membership.plan_id)
|
|
98
|
+
.is('effective_to', null)
|
|
99
|
+
.maybeSingle()
|
|
100
|
+
: Promise.resolve({ data: null }),
|
|
101
|
+
]);
|
|
102
|
+
const monthlyCents = overrideRes.data?.monthly_cents ?? defaultRes.data?.monthly_cents ?? 0;
|
|
103
|
+
|
|
104
|
+
if (monthlyCents > 0) {
|
|
105
|
+
// Read-only: never create a period row from a public preview.
|
|
106
|
+
const { data: periodMonth } = await supabase.rpc('current_period_month');
|
|
107
|
+
const { data: period } = await supabase
|
|
108
|
+
.from('credit_periods')
|
|
109
|
+
.select('granted_cents,consumed_cents,held_cents,expired_cents')
|
|
110
|
+
.eq('membership_id', membershipId)
|
|
111
|
+
.eq('period_month', periodMonth)
|
|
112
|
+
.maybeSingle();
|
|
113
|
+
has_room_credit = period
|
|
114
|
+
? (period.granted_cents ?? 0) - (period.consumed_cents ?? 0)
|
|
115
|
+
- (period.held_cents ?? 0) - (period.expired_cents ?? 0) > 0
|
|
116
|
+
// No period row yet this month — the grant happens at booking time,
|
|
117
|
+
// so an entitled plan still has its full allowance available.
|
|
118
|
+
: true;
|
|
119
|
+
}
|
|
120
|
+
} catch (_e) {
|
|
121
|
+
// Credit lookup is advisory for a display hint only — fall back to the
|
|
122
|
+
// conservative answer (no promise of a credit) rather than 500-ing.
|
|
123
|
+
has_room_credit = false;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// No cents field anywhere in this response — see file header.
|
|
127
|
+
return new Response(JSON.stringify({ is_member: true, plan_name, has_room_credit }), {
|
|
128
|
+
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
129
|
+
});
|
|
130
|
+
} catch (e) {
|
|
131
|
+
return new Response(
|
|
132
|
+
JSON.stringify({ error: e instanceof Error ? e.message : String(e) }),
|
|
133
|
+
{ status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } },
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { corsHeaders } from 'npm:@supabase/supabase-js@2/cors';
|
|
2
|
+
import { createClient } from 'npm:@supabase/supabase-js@2';
|
|
3
|
+
import { z } from 'npm:zod@3.23.8';
|
|
4
|
+
import { buildManageUrl, verifyManageToken } from 'npm:@simplelayer/graniteer-core/manageToken';
|
|
5
|
+
|
|
6
|
+
// Auth model: this endpoint is unauthenticated but MUST NOT return PII for a
|
|
7
|
+
// bare booking UUID (that was an IDOR). Callers must supply either:
|
|
8
|
+
// - session_id: the Stripe Checkout Session id (unguessable, only known to
|
|
9
|
+
// the customer who completed checkout — used by the confirmation page via
|
|
10
|
+
// Stripe's {CHECKOUT_SESSION_ID} success_url placeholder), OR
|
|
11
|
+
// - token: a signed manage-booking token (see _shared/manageToken.ts).
|
|
12
|
+
const Body = z.object({
|
|
13
|
+
session_id: z.string().min(10).max(200).optional(),
|
|
14
|
+
token: z.string().min(20).max(4096).optional(),
|
|
15
|
+
}).refine((v) => Boolean(v.session_id || v.token), {
|
|
16
|
+
message: 'session_id or token required',
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
function parsePgRange(r: string): { start: string; end: string } | null {
|
|
20
|
+
const inner = r.slice(1, -1);
|
|
21
|
+
const parts = inner.split(',').map((s) => s.replace(/^"|"$/g, ''));
|
|
22
|
+
if (parts.length !== 2 || !parts[0] || !parts[1]) return null;
|
|
23
|
+
const a = new Date(parts[0]);
|
|
24
|
+
const b = new Date(parts[1]);
|
|
25
|
+
if (isNaN(a.getTime()) || isNaN(b.getTime())) return null;
|
|
26
|
+
return { start: a.toISOString(), end: b.toISOString() };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function handle(req: Request, operator: { domain: string }): Promise<Response> {
|
|
30
|
+
if (req.method === 'OPTIONS') return new Response('ok', { headers: corsHeaders });
|
|
31
|
+
try {
|
|
32
|
+
// Accept lookup key via body OR query string.
|
|
33
|
+
let payload: Record<string, unknown> = {};
|
|
34
|
+
if (req.method === 'POST') {
|
|
35
|
+
payload = (await req.json().catch(() => ({}))) as Record<string, unknown>;
|
|
36
|
+
} else {
|
|
37
|
+
const url = new URL(req.url);
|
|
38
|
+
payload = {
|
|
39
|
+
session_id: url.searchParams.get('session_id') ?? undefined,
|
|
40
|
+
token: url.searchParams.get('token') ?? undefined,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
const parsed = Body.safeParse(payload);
|
|
44
|
+
if (!parsed.success) {
|
|
45
|
+
return new Response(JSON.stringify({ error: 'missing session_id or token' }), {
|
|
46
|
+
status: 400, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const supabase = createClient(
|
|
51
|
+
Deno.env.get('SUPABASE_URL')!,
|
|
52
|
+
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!,
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
// Resolve to a booking id via the caller's chosen credential.
|
|
56
|
+
let bookingId: string | null = null;
|
|
57
|
+
let tokenBoundEmail: string | null = null;
|
|
58
|
+
const manageSecret = Deno.env.get('MANAGE_TOKEN_SECRET') ?? '';
|
|
59
|
+
|
|
60
|
+
if (parsed.data.token) {
|
|
61
|
+
if (!manageSecret) {
|
|
62
|
+
return new Response(JSON.stringify({ error: 'not_configured' }), {
|
|
63
|
+
status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
const claims = await verifyManageToken(parsed.data.token, manageSecret);
|
|
67
|
+
if (!claims) {
|
|
68
|
+
return new Response(JSON.stringify({ error: 'invalid_or_expired_token' }), {
|
|
69
|
+
status: 401, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
bookingId = claims.bid;
|
|
73
|
+
tokenBoundEmail = claims.eml.toLowerCase();
|
|
74
|
+
} else if (parsed.data.session_id) {
|
|
75
|
+
const { data: match, error: sErr } = await supabase
|
|
76
|
+
.from('bookings')
|
|
77
|
+
.select('id')
|
|
78
|
+
.eq('stripe_session_id', parsed.data.session_id)
|
|
79
|
+
.maybeSingle();
|
|
80
|
+
if (sErr) throw sErr;
|
|
81
|
+
if (!match) {
|
|
82
|
+
return new Response(JSON.stringify({ error: 'not found' }), {
|
|
83
|
+
status: 404, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
bookingId = match.id as string;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!bookingId) {
|
|
90
|
+
return new Response(JSON.stringify({ error: 'not found' }), {
|
|
91
|
+
status: 404, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const { data, error } = await supabase
|
|
96
|
+
.from('bookings')
|
|
97
|
+
.select('id,during,status,list_amount_cents,charged_amount_cents,contact_name,contact_email,rooms(name,usage_instructions)')
|
|
98
|
+
.eq('id', bookingId)
|
|
99
|
+
.maybeSingle();
|
|
100
|
+
if (error) throw error;
|
|
101
|
+
if (!data) {
|
|
102
|
+
return new Response(JSON.stringify({ error: 'not found' }), {
|
|
103
|
+
status: 404, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
// Token must match the booking's contact_email; a token signed for a
|
|
107
|
+
// different booking id maps to the wrong row and is rejected here.
|
|
108
|
+
if (tokenBoundEmail && (data.contact_email as string ?? '').toLowerCase() !== tokenBoundEmail) {
|
|
109
|
+
return new Response(JSON.stringify({ error: 'invalid_or_expired_token' }), {
|
|
110
|
+
status: 401, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const range = parsePgRange(data.during as unknown as string);
|
|
115
|
+
// deno-lint-ignore no-explicit-any
|
|
116
|
+
const room = (data as any).rooms as { name: string; usage_instructions: string | null } | null;
|
|
117
|
+
|
|
118
|
+
// Build a manage-booking magic link the confirmation page can surface.
|
|
119
|
+
let manageUrl: string | null = null;
|
|
120
|
+
if (manageSecret && range?.end && data.contact_email) {
|
|
121
|
+
const exp = Math.floor(new Date(range.end).getTime() / 1000) + 30 * 24 * 60 * 60;
|
|
122
|
+
manageUrl = await buildManageUrl(
|
|
123
|
+
`https://${operator.domain}`,
|
|
124
|
+
{ bid: data.id as string, eml: (data.contact_email as string).toLowerCase(), exp },
|
|
125
|
+
manageSecret,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return new Response(JSON.stringify({
|
|
130
|
+
room_name: room?.name ?? null,
|
|
131
|
+
// "About this room" copy for the confirmation page. NULL for every room
|
|
132
|
+
// that has not been given usage instructions — the panel is simply not
|
|
133
|
+
// rendered then.
|
|
134
|
+
room_usage_instructions: room?.usage_instructions ?? null,
|
|
135
|
+
start: range?.start ?? null,
|
|
136
|
+
end: range?.end ?? null,
|
|
137
|
+
status: data.status,
|
|
138
|
+
list_amount_cents: data.list_amount_cents,
|
|
139
|
+
charged_amount_cents: data.charged_amount_cents,
|
|
140
|
+
contact_name: data.contact_name,
|
|
141
|
+
manage_url: manageUrl,
|
|
142
|
+
}), { headers: { ...corsHeaders, 'Content-Type': 'application/json' } });
|
|
143
|
+
} catch (e) {
|
|
144
|
+
console.error('booking-status error', e);
|
|
145
|
+
return new Response(JSON.stringify({ error: e instanceof Error ? e.message : String(e) }), {
|
|
146
|
+
status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|