@simplelayer/graniteer-core 0.0.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/package.json +169 -15
- package/src/adminBookingCredit.test.ts +451 -0
- package/src/adminBookingCredit.ts +239 -0
- package/src/agreementCheckout.ts +250 -0
- package/src/agreementPortalEmail.ts +128 -0
- package/src/availability.test.ts +25 -0
- package/src/availability.ts +201 -0
- package/src/billingPortalLinkEmail.ts +117 -0
- package/src/bookingAgreementEmail.ts +149 -0
- package/src/bookingEffects.ts +388 -0
- package/src/bookingHoldReclaim.ts +94 -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/checkoutSessionParams.ts +56 -0
- package/src/commonsContract.ts +175 -0
- package/src/commonsDeposit.ts +298 -0
- package/src/commonsDepositWebhook.ts +237 -0
- package/src/conductorAdmin.ts +141 -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/email-change.tsx +92 -0
- package/src/email-templates/invite.tsx +79 -0
- package/src/email-templates/magic-link.tsx +59 -0
- package/src/email-templates/reauthentication.tsx +60 -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 -0
- package/src/functions/admin-change-membership-plan.ts +312 -0
- package/src/functions/admin-charge-guest-print.ts +229 -0
- package/src/functions/admin-correct-booking.ts +333 -0
- package/src/functions/admin-create-booking.ts +460 -0
- package/src/functions/admin-create-commons-event.ts +244 -0
- package/src/functions/admin-debug-boldsign-doc.ts +93 -0
- package/src/functions/admin-door-write.ts +272 -0
- package/src/functions/admin-issue-day-pass.ts +231 -0
- package/src/functions/admin-mark-booking-paid.ts +203 -0
- 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 -0
- package/src/functions/admin-remove-team-member.ts +150 -0
- 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 -0
- 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 -0
- package/src/functions/admin-revoke-agreement.ts +467 -0
- package/src/functions/admin-send-commons-agreement.ts +214 -0
- package/src/functions/admin-set-credit-amount.ts +123 -0
- package/src/functions/admin-skip-commons-contract.ts +165 -0
- package/src/functions/admin-undo-membership-cancel.ts +144 -0
- package/src/functions/admin-wall-photo-write.ts +141 -0
- package/src/functions/auth-email-hook.ts +259 -0
- package/src/functions/boldsign-sender-identity.ts +114 -0
- package/src/functions/booking-availability.ts +419 -0
- package/src/functions/booking-member-preview.ts +136 -0
- 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 -0
- package/src/functions/dayStatus.ts +71 -0
- package/src/functions/diag-webpush-test.ts +45 -0
- package/src/functions/event-access-admin.ts +277 -0
- package/src/functions/event-access-store.ts +309 -0
- package/src/functions/ezeep-print-webhook.ts +213 -0
- package/src/functions/ezeep-usage-reconcile.ts +299 -0
- 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 -0
- package/src/functions/guest-print-setup-confirm.ts +128 -0
- package/src/functions/guest-print-submit.ts +149 -0
- package/src/functions/luma-webhook.ts +165 -0
- 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 -0
- package/src/functions/migration-collision-alert.test.ts +349 -0
- package/src/functions/migration-collision-alert.ts +232 -0
- package/src/functions/office-assignment-end.ts +146 -0
- package/src/functions/portal-booking-access.ts +95 -0
- package/src/functions/portal-list-invoices.ts +99 -0
- 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 -0
- package/src/functions/print-billing-run.ts +183 -0
- package/src/functions/public-facts.ts +168 -0
- 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 -0
- package/src/functions/space-availability-range.ts +142 -0
- 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 -0
- package/src/functions/timetable-event-register.ts +162 -0
- package/src/functions/timetable-event-status.ts +90 -0
- package/src/functions/timetable-spaces.ts +219 -0
- package/src/functions/touch-pass-reconciler.ts +689 -0
- 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 -0
- package/src/functions/virtual-onboarding-submit.ts +315 -0
- 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 -0
- package/src/functions/voice-inquiry.ts +198 -0
- package/src/functions/voice-membership-signup.ts +355 -0
- package/src/functions/voice-tour-availability.ts +204 -0
- package/src/functions/voice-tour-booking.ts +429 -0
- package/src/gcalSync.ts +142 -0
- package/src/ghlEventInquiry.ts +143 -0
- package/src/ghlNote.ts +34 -0
- package/src/ghlTask.ts +48 -0
- package/src/ghlVoiceInquiry.ts +90 -0
- package/src/guestPrintCapture.test.ts +722 -0
- package/src/guestPrintCapture.ts +538 -0
- package/src/lumaGhl.ts +195 -0
- package/src/mailArrivalEmail.ts +121 -0
- package/src/manageToken.ts +363 -0
- package/src/membership.ts +109 -0
- package/src/membershipBusClient.ts +537 -0
- package/src/membershipWelcome.ts +683 -0
- package/src/migrationCheckout.ts +366 -0
- package/src/money.ts +36 -0
- package/src/ops.ts +97 -0
- package/src/pacificDate.ts +88 -0
- package/src/paymentLinkEmail.ts +123 -0
- package/src/policyAcknowledgment.test.ts +50 -0
- package/src/policyAcknowledgment.ts +96 -0
- package/src/portalSession.ts +46 -0
- package/src/pricing.test.ts +660 -0
- package/src/pricing.ts +1025 -0
- package/src/printBilling.test.ts +750 -0
- package/src/printBilling.ts +891 -0
- package/src/printBillingAdapters.ts +169 -0
- package/src/printCorrections.test.ts +348 -0
- package/src/printCorrections.ts +496 -0
- package/src/rateLimit.ts +82 -0
- package/src/referralCredits.test.ts +31 -0
- package/src/referralCredits.ts +34 -0
- package/src/referralGrant.ts +321 -0
- package/src/rescheduleCredit.test.ts +521 -0
- package/src/rescheduleCredit.ts +337 -0
- package/src/reservationWifiCode.test.ts +69 -0
- package/src/reservationWifiCode.ts +81 -0
- package/src/retellCallAnalyzed.test.ts +167 -0
- package/src/retellCallAnalyzed.ts +113 -0
- package/src/retellSignature.test.ts +137 -0
- package/src/retellSignature.ts +82 -0
- package/src/signedUnpaidReminderEmail.test.ts +246 -0
- package/src/signedUnpaidReminderEmail.ts +132 -0
- package/src/slackAlert.ts +38 -0
- package/src/stripeCoworkingPortal.ts +91 -0
- package/src/supabaseTestFakes.ts +131 -0
- package/src/supportTicketEmail.ts +119 -0
- package/src/tier1Agreement.ts +125 -0
- package/src/unifiAccessPolicies.ts +228 -0
- package/src/unifiGroups.ts +129 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { createClient, type SupabaseClient } from 'npm:@supabase/supabase-js@2';
|
|
2
|
+
import { z } from 'npm:zod@3.23.8';
|
|
3
|
+
import { clientIp, checkRateLimit } from 'npm:@simplelayer/graniteer-core/rateLimit';
|
|
4
|
+
import { logOps } from 'npm:@simplelayer/graniteer-core/ops';
|
|
5
|
+
import { buildMemberManageUrl } from 'npm:@simplelayer/graniteer-core/manageToken';
|
|
6
|
+
|
|
7
|
+
const corsHeaders = {
|
|
8
|
+
'Access-Control-Allow-Origin': '*',
|
|
9
|
+
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
|
10
|
+
'Access-Control-Allow-Methods': 'POST, OPTIONS',
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const GHL_BASE = 'https://services.leadconnectorhq.com';
|
|
14
|
+
const TAG = 'Membership Portal Requested';
|
|
15
|
+
const GENERIC_OK = {
|
|
16
|
+
ok: true,
|
|
17
|
+
message: "If that email has an active membership, we've sent a manage link to it.",
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const Body = z.object({ email: z.string().email().max(255) });
|
|
21
|
+
|
|
22
|
+
function json(status: number, body: unknown) {
|
|
23
|
+
return new Response(JSON.stringify(body), {
|
|
24
|
+
status,
|
|
25
|
+
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function ghlHeaders(apiKey: string) {
|
|
30
|
+
return {
|
|
31
|
+
Authorization: `Bearer ${apiKey}`,
|
|
32
|
+
Version: '2021-07-28',
|
|
33
|
+
'Content-Type': 'application/json',
|
|
34
|
+
Accept: 'application/json',
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type Operator = { operatorName: string; operatorShortName: string; domain: string };
|
|
39
|
+
|
|
40
|
+
function portalEmailHtml(manageUrl: string, operator: Operator): string {
|
|
41
|
+
return [
|
|
42
|
+
'<p>Hi there,</p>',
|
|
43
|
+
`<p>Here's your secure link to manage your ${operator.operatorName} membership — update your card, view invoices, or make changes anytime:</p>`,
|
|
44
|
+
`<p><a href="${manageUrl}">Manage my membership →</a></p>`,
|
|
45
|
+
"<p>This secure link is unique to you and expires in 30 days — please don't forward it.</p>",
|
|
46
|
+
`<p>Questions? Just reply to this email.<br>— ${operator.operatorName}</p>`,
|
|
47
|
+
].join('');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function deliverPortalLink(
|
|
51
|
+
admin: SupabaseClient<any, 'public', any>,
|
|
52
|
+
email: string,
|
|
53
|
+
secret: string,
|
|
54
|
+
operator: Operator,
|
|
55
|
+
): Promise<void> {
|
|
56
|
+
const apiKey = Deno.env.get('GHL_API_KEY');
|
|
57
|
+
const locationId = Deno.env.get('GHL_LOCATION_ID');
|
|
58
|
+
if (!apiKey || !locationId) {
|
|
59
|
+
console.warn('[request-portal-link] GHL not configured — cannot deliver link');
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const headers = ghlHeaders(apiKey);
|
|
63
|
+
|
|
64
|
+
const manageUrl = await buildMemberManageUrl(
|
|
65
|
+
`https://${operator.domain}`,
|
|
66
|
+
{ mbr: email, exp: Math.floor(Date.now() / 1000) + 30 * 24 * 60 * 60 },
|
|
67
|
+
secret,
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
let contactId: string | null = null;
|
|
71
|
+
try {
|
|
72
|
+
const upRes = await fetch(`${GHL_BASE}/contacts/upsert`, {
|
|
73
|
+
method: 'POST',
|
|
74
|
+
headers,
|
|
75
|
+
body: JSON.stringify({
|
|
76
|
+
locationId,
|
|
77
|
+
email,
|
|
78
|
+
source: `${operator.operatorShortName} Website — Membership Portal`,
|
|
79
|
+
tags: [TAG, operator.domain],
|
|
80
|
+
}),
|
|
81
|
+
});
|
|
82
|
+
if (!upRes.ok) {
|
|
83
|
+
console.warn('[request-portal-link] GHL upsert failed', upRes.status, await upRes.text());
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const j = await upRes.json();
|
|
87
|
+
contactId = j.contact?.id || j.contact?.contact?.id || j.id || null;
|
|
88
|
+
} catch (e) {
|
|
89
|
+
console.warn('[request-portal-link] GHL upsert error', e);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (!contactId) return;
|
|
93
|
+
|
|
94
|
+
// Send the portal email directly via GHL Conversations API.
|
|
95
|
+
try {
|
|
96
|
+
const sendRes = await fetch(`${GHL_BASE}/conversations/messages`, {
|
|
97
|
+
method: 'POST',
|
|
98
|
+
headers,
|
|
99
|
+
body: JSON.stringify({
|
|
100
|
+
type: 'Email',
|
|
101
|
+
contactId,
|
|
102
|
+
emailTo: email,
|
|
103
|
+
subject: `Manage your ${operator.operatorName} membership`,
|
|
104
|
+
html: portalEmailHtml(manageUrl, operator),
|
|
105
|
+
}),
|
|
106
|
+
});
|
|
107
|
+
if (!sendRes.ok) {
|
|
108
|
+
const txt = await sendRes.text();
|
|
109
|
+
console.warn('[request-portal-link] GHL email send failed', sendRes.status, txt);
|
|
110
|
+
await logOps(
|
|
111
|
+
admin,
|
|
112
|
+
'portal_email_send_failed',
|
|
113
|
+
{ email, status: sendRes.status, body: txt.slice(0, 500) },
|
|
114
|
+
{ severity: 'warning' },
|
|
115
|
+
);
|
|
116
|
+
} else {
|
|
117
|
+
await logOps(admin, 'portal_email_sent', { email }, { severity: 'info' });
|
|
118
|
+
}
|
|
119
|
+
} catch (e) {
|
|
120
|
+
console.warn('[request-portal-link] GHL email send error', e);
|
|
121
|
+
await logOps(
|
|
122
|
+
admin,
|
|
123
|
+
'portal_email_send_failed',
|
|
124
|
+
{ email, message: e instanceof Error ? e.message : String(e) },
|
|
125
|
+
{ severity: 'warning' },
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Best-effort tag apply (harmless CRM marker; delivery no longer depends on it).
|
|
130
|
+
try {
|
|
131
|
+
await fetch(`${GHL_BASE}/contacts/${contactId}/tags`, {
|
|
132
|
+
method: 'POST',
|
|
133
|
+
headers,
|
|
134
|
+
body: JSON.stringify({ tags: [TAG] }),
|
|
135
|
+
});
|
|
136
|
+
} catch (e) {
|
|
137
|
+
console.warn('[request-portal-link] tag add error', e);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export async function handle(req: Request, operator: Operator): Promise<Response> {
|
|
142
|
+
if (req.method === 'OPTIONS') return new Response('ok', { headers: corsHeaders });
|
|
143
|
+
if (req.method !== 'POST') return json(405, { error: 'method_not_allowed' });
|
|
144
|
+
|
|
145
|
+
const SUPABASE_URL = Deno.env.get('SUPABASE_URL')!;
|
|
146
|
+
const SERVICE_ROLE_KEY = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!;
|
|
147
|
+
const secret = Deno.env.get('MANAGE_TOKEN_SECRET');
|
|
148
|
+
// Missing secret should never happen in prod; fail closed to generic OK so
|
|
149
|
+
// callers can't distinguish this from "no membership found".
|
|
150
|
+
if (!secret) {
|
|
151
|
+
console.warn('[request-portal-link] MANAGE_TOKEN_SECRET not configured');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const admin = createClient(SUPABASE_URL, SERVICE_ROLE_KEY, {
|
|
155
|
+
auth: { persistSession: false, autoRefreshToken: false },
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// Parse first so we can rate-limit by email; but rate-limit BEFORE any DB row lookup.
|
|
159
|
+
let email = '';
|
|
160
|
+
try {
|
|
161
|
+
const raw = await req.json();
|
|
162
|
+
const parsed = Body.safeParse(raw);
|
|
163
|
+
if (!parsed.success) return json(200, GENERIC_OK); // don't leak validation state
|
|
164
|
+
email = parsed.data.email.toLowerCase();
|
|
165
|
+
} catch {
|
|
166
|
+
return json(200, GENERIC_OK);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const ip = clientIp(req);
|
|
170
|
+
const [ipOk, emailOk] = await Promise.all([
|
|
171
|
+
checkRateLimit(admin, 'portal-link-ip', ip, 20, 300),
|
|
172
|
+
checkRateLimit(admin, 'portal-link-email', email, 10, 300),
|
|
173
|
+
]);
|
|
174
|
+
if (!ipOk || !emailOk) return json(429, { error: 'rate_limited' });
|
|
175
|
+
|
|
176
|
+
// PLURALITY: never .maybeSingle() here -- supabase-js returns data:null when
|
|
177
|
+
// >1 row matches, which would silently deny a portal link to exactly the
|
|
178
|
+
// member holding two memberships. Oldest-first + limit(1), matching
|
|
179
|
+
// _shared/membership.ts's "THE OLDEST ENTITLED MEMBERSHIP PAYS" intent.
|
|
180
|
+
const { data: membershipRows, error: membershipErr } = await admin
|
|
181
|
+
.from('memberships')
|
|
182
|
+
.select('id,email,status,stripe_customer_id')
|
|
183
|
+
.eq('email', email)
|
|
184
|
+
.in('status', ['active', 'trialing', 'past_due'])
|
|
185
|
+
.not('stripe_customer_id', 'is', null)
|
|
186
|
+
.order('created_at', { ascending: true })
|
|
187
|
+
.limit(1);
|
|
188
|
+
if (membershipErr) {
|
|
189
|
+
await logOps(admin, 'portal_link_membership_lookup_failed',
|
|
190
|
+
{ email, message: membershipErr.message }, { severity: 'error' });
|
|
191
|
+
// Same shape/timing as every other outcome (no existence leak).
|
|
192
|
+
return json(200, GENERIC_OK);
|
|
193
|
+
}
|
|
194
|
+
const membership = (membershipRows ?? [])[0];
|
|
195
|
+
|
|
196
|
+
if (!membership || !secret) {
|
|
197
|
+
// Same shape/timing as the found path (no existence leak).
|
|
198
|
+
return json(200, GENERIC_OK);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
try {
|
|
202
|
+
await deliverPortalLink(admin, email, secret, operator);
|
|
203
|
+
await logOps(admin, 'portal_link_requested', { email }, { severity: 'info' });
|
|
204
|
+
} catch (e) {
|
|
205
|
+
await logOps(admin, 'portal_link_delivery_failed',
|
|
206
|
+
{ message: e instanceof Error ? e.message : String(e), email },
|
|
207
|
+
{ severity: 'error' });
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return json(200, GENERIC_OK);
|
|
211
|
+
}
|