@simplelayer/graniteer-core 0.3.0 → 0.3.2

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.
@@ -73,16 +73,16 @@ export async function sendAgreementPortalEmail(supabase, { email, name, portalUr
73
73
  const office = (officeNumber || '').trim();
74
74
  const officeClause = office ? ` for office ${esc(office)}` : '';
75
75
  const subject = `Your ${operator.shortName} private office agreement is ready — review and pay your retainer`;
76
- const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
77
- <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">Your ${operator.shortName} private office agreement is ready</h2>
78
- ${p(`Hi ${esc(greetingName)},`)}
79
- ${p(`Your ${operator.name} private office agreement${officeClause} is ready to review. There are two quick steps to get you moved in.`)}
80
- ${p('<strong>1. Review your terms and pay your service retainer.</strong> Open your secure agreement portal:')}
81
- ${p(`<a href="${esc(portalUrl)}" style="color:#C98A5E">Review and pay your retainer →</a>`)}
82
- ${p('<strong>2. Sign your agreement.</strong> BoldSign is sending you a separate email with the signing link — watch for it in your inbox (check spam if you don\'t see it shortly).')}
83
- ${p('Your monthly billing starts only after both steps are complete. This portal link is unique to your agreement, so please don\'t forward it.')}
84
- ${p(`Questions in the meantime? Just reply to this email or reach us at <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
85
- ${p(`Welcome aboard,<br />The ${operator.name} team`)}
76
+ const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
77
+ <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">Your ${operator.shortName} private office agreement is ready</h2>
78
+ ${p(`Hi ${esc(greetingName)},`)}
79
+ ${p(`Your ${operator.name} private office agreement${officeClause} is ready to review. There are two quick steps to get you moved in.`)}
80
+ ${p('<strong>1. Review your terms and pay your service retainer.</strong> Open your secure agreement portal:')}
81
+ ${p(`<a href="${esc(portalUrl)}" style="color:#C98A5E">Review and pay your retainer →</a>`)}
82
+ ${p('<strong>2. Sign your agreement.</strong> BoldSign is sending you a separate email with the signing link — watch for it in your inbox (check spam if you don\'t see it shortly).')}
83
+ ${p('Your monthly billing starts only after both steps are complete. This portal link is unique to your agreement, so please don\'t forward it.')}
84
+ ${p(`Questions in the meantime? Just reply to this email or reach us at <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
85
+ ${p(`Welcome aboard,<br />The ${operator.name} team`)}
86
86
  </td></tr></table></td></tr></table></div>`;
87
87
  const emailRes = await fetch(`${GHL_BASE}/conversations/messages`, {
88
88
  method: 'POST', headers,
@@ -66,14 +66,14 @@ export async function sendBillingPortalLinkEmail(supabase, { email, name, portal
66
66
  if (!contactId)
67
67
  throw new Error('ghl_upsert_no_contact_id');
68
68
  const p = (t) => `<p style="margin:0 0 8px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:14px;line-height:22px">${t}</p>`;
69
- const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
70
- <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">Update your ${operator.shortName} billing details</h2>
71
- ${p(`Hi ${esc(greetingName)},`)}
72
- ${p(`Our team wanted to make it easy for you to update the payment method on your ${operator.name} membership. Use the secure link below:`)}
73
- ${p(`<a href="${esc(portalUrl)}" style="color:#C98A5E">Update payment method →</a>`)}
74
- ${p("This link is unique to your account and takes you straight to our payment processor, Stripe, where you can update your card, view invoices, and manage your subscription. Please don't forward it.")}
75
- ${p(`Questions? Just reply to this email or reach us at <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
76
- ${p(`Thanks,<br />The ${operator.name} team`)}
69
+ const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
70
+ <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">Update your ${operator.shortName} billing details</h2>
71
+ ${p(`Hi ${esc(greetingName)},`)}
72
+ ${p(`Our team wanted to make it easy for you to update the payment method on your ${operator.name} membership. Use the secure link below:`)}
73
+ ${p(`<a href="${esc(portalUrl)}" style="color:#C98A5E">Update payment method →</a>`)}
74
+ ${p("This link is unique to your account and takes you straight to our payment processor, Stripe, where you can update your card, view invoices, and manage your subscription. Please don't forward it.")}
75
+ ${p(`Questions? Just reply to this email or reach us at <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
76
+ ${p(`Thanks,<br />The ${operator.name} team`)}
77
77
  </td></tr></table></td></tr></table></div>`;
78
78
  const emailRes = await fetch(`${GHL_BASE}/conversations/messages`, {
79
79
  method: 'POST', headers,
@@ -57,9 +57,9 @@ async function sendViaGhl(supabase, opts) {
57
57
  const contactId = upJson.contact?.id || upJson.id || null;
58
58
  if (!contactId)
59
59
  throw new Error('ghl_upsert_no_contact_id');
60
- const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
61
- <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">${esc(opts.subject)}</h2>
62
- ${opts.body(esc(greetingName))}
60
+ const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
61
+ <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">${esc(opts.subject)}</h2>
62
+ ${opts.body(esc(greetingName))}
63
63
  </td></tr></table></td></tr></table></div>`;
64
64
  const emailRes = await fetch(`${GHL_BASE}/conversations/messages`, {
65
65
  method: 'POST', headers,
@@ -128,9 +128,9 @@ async function sendViaGhl(supabase, opts) {
128
128
  const contactId = upJson.contact?.id || upJson.id || null;
129
129
  if (!contactId)
130
130
  throw new Error('ghl_upsert_no_contact_id');
131
- const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
132
- <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">${esc(opts.subject)}</h2>
133
- ${opts.body(esc(greetingName))}
131
+ const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
132
+ <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">${esc(opts.subject)}</h2>
133
+ ${opts.body(esc(greetingName))}
134
134
  </td></tr></table></td></tr></table></div>`;
135
135
  const emailRes = await fetch(`${GHL_BASE}/conversations/messages`, {
136
136
  method: 'POST', headers,
@@ -153,13 +153,13 @@ export async function sendDayPassConfirmation(supabase, purchase, operator) {
153
153
  p(wifiLine),
154
154
  p(`Hosts are on site ${esc(hostHours)} Pacific — outside those hours, check in whenever the front desk is staffed.`),
155
155
  ].join(''));
156
- const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
157
- <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">Your day pass is confirmed</h2>
158
- ${p(`Hi ${esc(greetingName)},`)}
159
- ${p(`Thanks for booking a day pass at ${esc(operator.name)}.`)}
160
- <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;margin-top:12px">${detailsCard}${checkinCard}</table>
161
- ${p(`Questions before you come in? Reply to this email or write to <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
162
- <p style="margin:24px 0 0 0;color:#748272;font-family:Inter,Arial,sans-serif;font-size:13px;line-height:20px">${operator.name}</p>
156
+ const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
157
+ <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">Your day pass is confirmed</h2>
158
+ ${p(`Hi ${esc(greetingName)},`)}
159
+ ${p(`Thanks for booking a day pass at ${esc(operator.name)}.`)}
160
+ <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;margin-top:12px">${detailsCard}${checkinCard}</table>
161
+ ${p(`Questions before you come in? Reply to this email or write to <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
162
+ <p style="margin:24px 0 0 0;color:#748272;font-family:Inter,Arial,sans-serif;font-size:13px;line-height:20px">${operator.name}</p>
163
163
  </td></tr></table></td></tr></table></div>`;
164
164
  const emailRes = await fetch(`${GHL_BASE}/conversations/messages`, {
165
165
  method: 'POST', headers,
@@ -150,11 +150,11 @@ export async function sendDunningEmail(supabase, email, membershipId, stage, gra
150
150
  })
151
151
  : 'the end of your grace period';
152
152
  const { subject, bodyLines } = stageCopy(stage, endLabel, p);
153
- const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
154
- <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">${esc(subject)}</h2>
155
- ${bodyLines.join('')}
156
- ${p(`Questions? Just reply to this email — it reaches a real person here. You can also write to <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
157
- <p style="margin:24px 0 0 0;color:#748272;font-family:Inter,Arial,sans-serif;font-size:13px;line-height:20px">${operator.name}</p>
153
+ const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
154
+ <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">${esc(subject)}</h2>
155
+ ${bodyLines.join('')}
156
+ ${p(`Questions? Just reply to this email — it reaches a real person here. You can also write to <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
157
+ <p style="margin:24px 0 0 0;color:#748272;font-family:Inter,Arial,sans-serif;font-size:13px;line-height:20px">${operator.name}</p>
158
158
  </td></tr></table></td></tr></table></div>`;
159
159
  const emailRes = await fetch(`${GHL_BASE}/conversations/messages`, {
160
160
  method: 'POST', headers,
@@ -151,20 +151,48 @@ export async function handle(req, operator, deps) {
151
151
  const net = Math.max(0, price_cents - dynamic_discount_cents);
152
152
  // Reclaim stale holds overlapping the range (same as create-booking).
153
153
  await reclaimStaleHoldsExceptCompletedTier3(admin, room_id, rangeLiteral);
154
- let creditEligible = false;
155
- if (membership) {
156
- let tier = null;
154
+ // Contract tier lookup: needed both for member-credit eligibility (Tier 3
155
+ // rooms never draw member credit - they always go through the Commons
156
+ // contract/deposit flow) and, new here, for the contract-skip audit trail
157
+ // below. Root cause + rationale: claude/admin-create-booking-agreement-gap-scope.md
158
+ // (this function never runs create-booking.js's tiered agreement gate or
159
+ // the audited admin-skip-commons-contract path - every booking it settles
160
+ // to 'paid' does so with no agreement, previously with no audit trail
161
+ // either).
162
+ let contractTier = null;
163
+ try {
164
+ contractTier = await resolveRoomContractTier(admin, room_id);
165
+ }
166
+ catch (e) {
167
+ console.warn('[admin-create-booking] contract tier lookup failed', String(e));
157
168
  try {
158
- tier = await resolveRoomContractTier(admin, room_id);
159
- }
160
- catch (e) {
161
- console.warn('[admin-create-booking] contract tier lookup failed', String(e));
169
+ await logOps(admin, 'admin_create_booking_tier_lookup_failed', { message: String(e), room_id }, { severity: 'warning' });
162
170
  }
163
- creditEligible = room.booking_mode !== 'quote' && tier !== null && tier !== 3;
171
+ catch { /* ignore */ }
172
+ }
173
+ let creditEligible = false;
174
+ if (membership) {
175
+ creditEligible = room.booking_mode !== 'quote' && contractTier !== null && contractTier !== 3;
164
176
  }
165
177
  const useCredit = !!membership && creditEligible;
166
178
  const nowIso = new Date().toISOString();
167
179
  const isOffline = mode === 'offline';
180
+ // Audit-only contract-skip marking (landed 2026-09-18, see scope doc
181
+ // above): every booking this function settles straight to 'paid' bypasses
182
+ // the tiered agreement gate self-serve bookings go through. This does NOT
183
+ // block or gate the booking - it only makes the skip self-documenting via
184
+ // the same contract_skipped_* columns admin-skip-commons-contract uses for
185
+ // an explicit staff skip. NOTE: private.on_booking_paid_transition() still
186
+ // fires and still inserts its own booking_contract_violations row on this
187
+ // same write -- this stamp is in ADDITION to that row, not a replacement
188
+ // for it; the violations table keeps growing exactly as before.
189
+ const CONTRACT_SKIP_REASON = `admin-create-booking: no agreement flow in this function (tier ${contractTier ?? 'unmapped'}; ` +
190
+ 'known gap, audit-only fix landed 2026-09-18 - see admin-create-booking-agreement-gap-scope.md)';
191
+ const contractSkipFields = {
192
+ contract_skipped_at: nowIso,
193
+ contract_skipped_by_email: callerEmail,
194
+ contract_skip_reason: CONTRACT_SKIP_REASON,
195
+ };
168
196
  const insertRow = {
169
197
  room_id,
170
198
  during: rangeLiteral,
@@ -190,6 +218,7 @@ export async function handle(req, operator, deps) {
190
218
  insertRow.charged_amount_cents = net;
191
219
  insertRow.credit_applied_cents = 0;
192
220
  insertRow.hold_expires_at = null;
221
+ Object.assign(insertRow, contractSkipFields);
193
222
  }
194
223
  else {
195
224
  insertRow.status = 'pending';
@@ -300,6 +329,7 @@ export async function handle(req, operator, deps) {
300
329
  credit_applied_cents: credit_applied,
301
330
  discount_applied_cents: discount_applied,
302
331
  hold_expires_at: null,
332
+ ...contractSkipFields,
303
333
  })
304
334
  .eq('id', booking_id);
305
335
  await runPaidSideEffects();
@@ -312,10 +312,10 @@ export async function handle(req, operator) {
312
312
  try {
313
313
  const { data: row, error: rowErr } = await admin
314
314
  .from('signature_documents')
315
- .select(`id,status,boldsign_document_id,boldsign_template_id,document_type,signer_name,signer_email,field_values,flow,start_date,
316
- stripe_checkout_url,stripe_checkout_session_id,stripe_customer_id,stripe_issued_at,stripe_subscription_id,
317
- retainer_paid_at,retainer_payment_intent_id,retainer_terms_accepted_at,retainer_setup_intent_id,retainer_intent_kind,
318
- retainer_attempt_count,subscription_attempt_count,subscription_error,client_signed_at,payment_link_email_sent_at,
315
+ .select(`id,status,boldsign_document_id,boldsign_template_id,document_type,signer_name,signer_email,field_values,flow,start_date,
316
+ stripe_checkout_url,stripe_checkout_session_id,stripe_customer_id,stripe_issued_at,stripe_subscription_id,
317
+ retainer_paid_at,retainer_payment_intent_id,retainer_terms_accepted_at,retainer_setup_intent_id,retainer_intent_kind,
318
+ retainer_attempt_count,subscription_attempt_count,subscription_error,client_signed_at,payment_link_email_sent_at,
319
319
  portal_issued_at,portal_token_revoked_at,activation_started_at`)
320
320
  .eq('id', docId)
321
321
  .maybeSingle();
@@ -1634,7 +1634,26 @@ export function createStripeWebhookHandler(config) {
1634
1634
  break;
1635
1635
  }
1636
1636
  const booking = await findBooking(session);
1637
- if (booking && booking.status !== 'paid' && booking.status !== 'deposit_paid') {
1637
+ // A replayed/out-of-order webhook must never resurrect a booking the
1638
+ // customer or staff already cancelled. booking.status is untyped
1639
+ // (findBooking's select is not run through generated DB types), so
1640
+ // nullish-coalesce before .startsWith: a null/absent status must not
1641
+ // throw here -- that would reach the outer catch, delete the
1642
+ // stripe_events ledger row, and 500 (Stripe retries ~16x, paging
1643
+ // each time, booking never provisions). Preserves the pre-fix
1644
+ // behavior for that case (null !== 'paid' -> proceed) exactly.
1645
+ const bkStatus = booking?.status ?? '';
1646
+ // All three cancelled_* terminal states (see manage-booking.ts's
1647
+ // `b.status.startsWith('cancelled_')` idiom and stripe-db-reconcile.ts's
1648
+ // BOOKING_POST_PAID_OK set) are excluded, not just 'cancelled_credited'.
1649
+ // Hoisted (rather than inlined in the condition below) so the
1650
+ // else-if branch further down -- which handles the actual
1651
+ // cancelled_* case -- can reuse it without recomputing.
1652
+ const bkIsCancelled = bkStatus.startsWith('cancelled_');
1653
+ if (booking &&
1654
+ bkStatus !== 'paid' &&
1655
+ bkStatus !== 'deposit_paid' &&
1656
+ !bkIsCancelled) {
1638
1657
  const paymentIntent = typeof session.payment_intent === 'string'
1639
1658
  ? session.payment_intent
1640
1659
  : session.payment_intent?.id ?? null;
@@ -1653,6 +1672,16 @@ export function createStripeWebhookHandler(config) {
1653
1672
  // status='pending', because this generic path legitimately serves
1654
1673
  // other non-pending recoveries (e.g. a re-paid expired hold).
1655
1674
  .neq('status', 'deposit_paid')
1675
+ // Never resurrect an already-cancelled booking on a replayed/
1676
+ // out-of-order webhook (all 3 terminal cancelled_* states --
1677
+ // see stripe-db-reconcile.ts's BOOKING_POST_PAID_OK). Explicit
1678
+ // .neq() chain to match this file's existing idiom, rather than
1679
+ // a LIKE pattern -- this file elsewhere notes '_' is a live SQL
1680
+ // wildcard, not just a literal underscore, so an unescaped
1681
+ // 'cancelled_%' LIKE could match unintended values.
1682
+ .neq('status', 'cancelled_credited')
1683
+ .neq('status', 'cancelled_forfeited')
1684
+ .neq('status', 'cancelled_refunded')
1656
1685
  .select('id');
1657
1686
  if (!flipErr && flipped && flipped.length > 0) {
1658
1687
  // Post-payment side effects — MUST NOT break the webhook.
@@ -1769,6 +1798,60 @@ export function createStripeWebhookHandler(config) {
1769
1798
  console.error('[stripe-webhook] post-paid side effects error', e);
1770
1799
  }
1771
1800
  }
1801
+ else if (flipErr) {
1802
+ console.error('[stripe-webhook] booking paid-flip update failed', flipErr);
1803
+ const persisted = await logOps(supabase, 'booking_paid_flip_failed', { session_id: session.id, message: flipErr.message }, { severity: 'error', bookingId: booking.id });
1804
+ if (!persisted.ok) {
1805
+ console.error('[stripe-webhook] booking-paid-flip-failed alarm failed to persist', { bookingId: booking.id, sessionId: session.id, error: persisted.error });
1806
+ }
1807
+ }
1808
+ else {
1809
+ // Guard blocked the flip even though the JS-level read saw this
1810
+ // booking as flippable (not paid/deposit_paid/cancelled_*) --
1811
+ // a TOCTOU race: something else (another webhook delivery,
1812
+ // manage-booking, the reconciler) changed the row's status
1813
+ // between our read and this UPDATE's WHERE clause evaluating.
1814
+ // `read_status` below is that STALE pre-UPDATE value, not the
1815
+ // row's current status -- never rename it back to `db_status`,
1816
+ // which would wrongly imply it reflects what's in the DB right
1817
+ // now. Always 'warning' (never conditionally 'info'): whatever
1818
+ // the row's status became, Stripe says this session is paid
1819
+ // and nothing got flipped or provisioned for it here -- that's
1820
+ // never a routine no-op (project invariant: "divergence
1821
+ // alarms, not silence").
1822
+ const raced = await logOps(supabase, 'paid_session_flip_raced', {
1823
+ session_id: session.id,
1824
+ read_status: booking.status,
1825
+ amount_total: session.amount_total ?? null,
1826
+ }, {
1827
+ severity: 'warning',
1828
+ bookingId: booking.id,
1829
+ });
1830
+ if (!raced.ok) {
1831
+ console.error('[stripe-webhook] paid-session-flip-raced alarm failed to persist', { bookingId: booking.id, sessionId: session.id, error: raced.error });
1832
+ }
1833
+ }
1834
+ }
1835
+ else if (booking && bkIsCancelled) {
1836
+ // The actual bug this fix exists to catch: a Stripe payment
1837
+ // landed on a booking that is cancelled_* right now, per the
1838
+ // JS-level read above (not a TOCTOU race -- see the `else`
1839
+ // branch above for that case). Must never be silent (project
1840
+ // invariant: "divergence alarms, not silence"). dedupeKey so a
1841
+ // replayed/retried webhook for the same session doesn't page
1842
+ // Slack a second time for the same collision.
1843
+ const flagged = await logOps(supabase, 'paid_session_for_cancelled_booking', {
1844
+ session_id: session.id,
1845
+ booking_status: bkStatus,
1846
+ amount_total: session.amount_total ?? null,
1847
+ }, {
1848
+ severity: 'error',
1849
+ bookingId: booking.id,
1850
+ dedupeKey: `paid_session_for_cancelled_booking:${booking.id}:${session.id}`,
1851
+ });
1852
+ if (!flagged.ok) {
1853
+ console.error('[stripe-webhook] cancelled-booking paid-session alarm failed to persist', { bookingId: booking.id, sessionId: session.id, error: flagged.error });
1854
+ }
1772
1855
  }
1773
1856
  break;
1774
1857
  }
@@ -74,12 +74,12 @@ export async function sendMailArrivalEmail(supabase, email, mailItemId, descript
74
74
  const detailLine = trimmed
75
75
  ? p(`Staff noted: <strong>${esc(trimmed)}</strong>`)
76
76
  : '';
77
- const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
78
- <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">${esc(subject)}</h2>
79
- ${p(`An item arrived for you and is being held at the front desk. Stop by during staffed hours to pick it up.`)}
80
- ${detailLine}
81
- ${p(`Questions? Just reply to this email — it reaches a real person here. You can also write to <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
82
- <p style="margin:24px 0 0 0;color:#748272;font-family:Inter,Arial,sans-serif;font-size:13px;line-height:20px">${operator.name}</p>
77
+ const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
78
+ <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">${esc(subject)}</h2>
79
+ ${p(`An item arrived for you and is being held at the front desk. Stop by during staffed hours to pick it up.`)}
80
+ ${detailLine}
81
+ ${p(`Questions? Just reply to this email — it reaches a real person here. You can also write to <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
82
+ <p style="margin:24px 0 0 0;color:#748272;font-family:Inter,Arial,sans-serif;font-size:13px;line-height:20px">${operator.name}</p>
83
83
  </td></tr></table></td></tr></table></div>`;
84
84
  const emailRes = await fetch(`${GHL_BASE}/conversations/messages`, {
85
85
  method: 'POST', headers,
@@ -238,18 +238,18 @@ export async function sendMembershipWelcome(supabase, email, planId, customerNam
238
238
  : '';
239
239
  // Host hours come from app_settings, never hardcoded here.
240
240
  const hostHoursCard = await buildHostHoursCard(supabase, groups, card, p, config.hostHours);
241
- const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
242
- <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">Welcome to ${operator.shortName} — your access is being set up</h2>
243
- ${p(`Hi ${esc(greetingName)},`)}
244
- ${p(`Thanks for joining us. You're all set on <strong>${esc(planLine)}</strong>.`)}
241
+ const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
242
+ <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">Welcome to ${operator.shortName} — your access is being set up</h2>
243
+ ${p(`Hi ${esc(greetingName)},`)}
244
+ ${p(`Thanks for joining us. You're all set on <strong>${esc(planLine)}</strong>.`)}
245
245
  ${startsOnLabel
246
246
  ? p(`<strong>Your membership starts ${esc(startsOnLabel)}.</strong> Nothing is charged today, and your door and WiFi access will be set up on that date — not before.`)
247
- : p('<strong>Your door and WiFi access is being set up right now — it usually takes a few minutes.</strong> Nothing is wrong if you don\'t have access the moment you finish reading this.')}
248
- ${p('<strong>You\'ll get a separate email inviting you to an app called UniFi Endpoint. That invitation is from us — it\'s how you unlock the door with your phone.</strong> It arrives with Ubiquiti branding, so please don\'t delete it as spam.')}
249
- <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;margin-top:12px">${doorCard}${wifiCard}${hostHoursCard}</table>
250
- ${p(`We're at ${esc(address)}.`)}
251
- ${p(`Questions? Just reply to this email — it reaches a real person here. You can also write to <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
252
- <p style="margin:24px 0 0 0;color:#748272;font-family:Inter,Arial,sans-serif;font-size:13px;line-height:20px">${operator.name} · ${esc(address)}</p>
247
+ : p('<strong>Your door and WiFi access is being set up right now — it usually takes a few minutes.</strong> Nothing is wrong if you don\'t have access the moment you finish reading this.')}
248
+ ${p('<strong>You\'ll get a separate email inviting you to an app called UniFi Endpoint. That invitation is from us — it\'s how you unlock the door with your phone.</strong> It arrives with Ubiquiti branding, so please don\'t delete it as spam.')}
249
+ <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;margin-top:12px">${doorCard}${wifiCard}${hostHoursCard}</table>
250
+ ${p(`We're at ${esc(address)}.`)}
251
+ ${p(`Questions? Just reply to this email — it reaches a real person here. You can also write to <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
252
+ <p style="margin:24px 0 0 0;color:#748272;font-family:Inter,Arial,sans-serif;font-size:13px;line-height:20px">${operator.name} · ${esc(address)}</p>
253
253
  </td></tr></table></td></tr></table></div>`;
254
254
  const emailRes = await fetch(`${GHL_BASE}/conversations/messages`, {
255
255
  method: 'POST', headers,
@@ -376,14 +376,14 @@ export async function sendPrivateOfficeWelcome(supabase, email, name, plan, conf
376
376
  }
377
377
  billingCard = card('Your billing', lines.join(''));
378
378
  }
379
- const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
380
- <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">Welcome to ${operator.name} — here's what's next</h2>
381
- ${p(`Hi ${esc(greetingName)},`)}
382
- ${p(`You're all set — welcome to ${operator.name}. We're glad to have you as part of the community.`)}
383
- <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;margin-top:12px">${nextCard}${billingCard}</table>
384
-
385
- ${p(`If anything's unclear or you need something before you move in, just reply here or write to <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a> — we're happy to help.`)}
386
- ${p(`Welcome again,<br>The ${operator.name} team`)}
379
+ const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
380
+ <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">Welcome to ${operator.name} — here's what's next</h2>
381
+ ${p(`Hi ${esc(greetingName)},`)}
382
+ ${p(`You're all set — welcome to ${operator.name}. We're glad to have you as part of the community.`)}
383
+ <table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;margin-top:12px">${nextCard}${billingCard}</table>
384
+
385
+ ${p(`If anything's unclear or you need something before you move in, just reply here or write to <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a> — we're happy to help.`)}
386
+ ${p(`Welcome again,<br>The ${operator.name} team`)}
387
387
  </td></tr></table></td></tr></table></div>`;
388
388
  const emailRes = await fetch(`${GHL_BASE}/conversations/messages`, {
389
389
  method: 'POST', headers,
@@ -465,14 +465,14 @@ export async function sendTeamSeatWelcome(supabase, email, name, companyName, pl
465
465
  const companyLine = companyName && companyName.trim()
466
466
  ? `You've been added as a team member on <strong>${esc(companyName.trim())}</strong>'s membership at ${esc(operator.shortName)}.`
467
467
  : `You've been added as a team member on your company's membership at ${esc(operator.shortName)}.`;
468
- const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
469
- <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">You've been added as a team member</h2>
470
- ${p(`Hi ${esc(greetingName)},`)}
471
- ${p(companyLine)}
472
- ${p(`<strong>Setting up your login:</strong> go to <a href="https://${operator.domain}/portal/login" style="color:#C98A5E">${operator.domain}/portal/login</a> and enter this email address (${esc(email)}). We'll email you a sign-in link — there's no password to remember.`)}
473
- ${p('<strong>Door and WiFi access:</strong> if your seat includes it, access is being set up now and can take a little while to go live. You may also receive a separate invite to an app called <strong>UniFi Endpoint</strong> — that\'s from us, and it\'s how you unlock the door with your phone.')}
474
- ${p(`Questions? Just reply to this email — it reaches a real person here. You can also write to <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
475
- <p style="margin:24px 0 0 0;color:#748272;font-family:Inter,Arial,sans-serif;font-size:13px;line-height:20px">${operator.name}</p>
468
+ const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
469
+ <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">You've been added as a team member</h2>
470
+ ${p(`Hi ${esc(greetingName)},`)}
471
+ ${p(companyLine)}
472
+ ${p(`<strong>Setting up your login:</strong> go to <a href="https://${operator.domain}/portal/login" style="color:#C98A5E">${operator.domain}/portal/login</a> and enter this email address (${esc(email)}). We'll email you a sign-in link — there's no password to remember.`)}
473
+ ${p('<strong>Door and WiFi access:</strong> if your seat includes it, access is being set up now and can take a little while to go live. You may also receive a separate invite to an app called <strong>UniFi Endpoint</strong> — that\'s from us, and it\'s how you unlock the door with your phone.')}
474
+ ${p(`Questions? Just reply to this email — it reaches a real person here. You can also write to <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
475
+ <p style="margin:24px 0 0 0;color:#748272;font-family:Inter,Arial,sans-serif;font-size:13px;line-height:20px">${operator.name}</p>
476
476
  </td></tr></table></td></tr></table></div>`;
477
477
  const emailRes = await fetch(`${GHL_BASE}/conversations/messages`, {
478
478
  method: 'POST', headers,
@@ -69,15 +69,15 @@ export async function sendPaymentLinkEmail(supabase, { email, name, checkoutUrl,
69
69
  const p = (t) => `<p style="margin:0 0 8px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:14px;line-height:22px">${t}</p>`;
70
70
  const office = (officeNumber || '').trim();
71
71
  const officeClause = office ? ` for office ${esc(office)}` : '';
72
- const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
73
- <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">Your ${operator.shortName} office agreement is signed — set up payment</h2>
74
- ${p(`Hi ${esc(greetingName)},`)}
75
- ${p(`Thanks for signing your ${operator.name} office agreement${officeClause} — you're one step from locked in.`)}
76
- ${p('The last step is setting up your monthly payment. Use the link below to add a payment method and start your subscription:')}
77
- ${p(`<a href="${esc(checkoutUrl)}" style="color:#C98A5E">Set up payment →</a>`)}
78
- ${p('This link is unique to your agreement, so please don\'t forward it. Once checkout is complete, we\'ll follow up with move-in details and everything else you need to get started.')}
79
- ${p(`Questions in the meantime? Just reply to this email or reach us at <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
80
- ${p(`Welcome aboard,<br />The ${operator.name} team`)}
72
+ const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
73
+ <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">Your ${operator.shortName} office agreement is signed — set up payment</h2>
74
+ ${p(`Hi ${esc(greetingName)},`)}
75
+ ${p(`Thanks for signing your ${operator.name} office agreement${officeClause} — you're one step from locked in.`)}
76
+ ${p('The last step is setting up your monthly payment. Use the link below to add a payment method and start your subscription:')}
77
+ ${p(`<a href="${esc(checkoutUrl)}" style="color:#C98A5E">Set up payment →</a>`)}
78
+ ${p('This link is unique to your agreement, so please don\'t forward it. Once checkout is complete, we\'ll follow up with move-in details and everything else you need to get started.')}
79
+ ${p(`Questions in the meantime? Just reply to this email or reach us at <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
80
+ ${p(`Welcome aboard,<br />The ${operator.name} team`)}
81
81
  </td></tr></table></td></tr></table></div>`;
82
82
  const emailRes = await fetch(`${GHL_BASE}/conversations/messages`, {
83
83
  method: 'POST', headers,
@@ -78,15 +78,15 @@ export async function sendSignedUnpaidReminderEmail(supabase, { email, name, por
78
78
  const office = (officeNumber || '').trim();
79
79
  const officeClause = office ? ` for office ${esc(office)}` : '';
80
80
  const subject = `Almost there — your ${operator.shortName} agreement is signed, just the retainer left`;
81
- const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
82
- <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">Your signature is in — one step left</h2>
83
- ${p(`Hi ${esc(greetingName)},`)}
84
- ${p(`Thanks for signing your ${operator.name} private office agreement${officeClause}. We just need your service retainer to finish setting things up.`)}
85
- ${p('<strong>Review your terms and pay your retainer</strong> in your secure agreement portal:')}
86
- ${p(`<a href="${esc(portalUrl)}" style="color:#C98A5E">Review and pay your retainer →</a>`)}
87
- ${p('Your monthly billing starts only after the retainer is paid, so nothing happens until you complete this step. This portal link is unique to your agreement, so please don\'t forward it.')}
88
- ${p(`Questions in the meantime? Just reply to this email or reach us at <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
89
- ${p(`Thanks,<br />The ${operator.name} team`)}
81
+ const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
82
+ <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">Your signature is in — one step left</h2>
83
+ ${p(`Hi ${esc(greetingName)},`)}
84
+ ${p(`Thanks for signing your ${operator.name} private office agreement${officeClause}. We just need your service retainer to finish setting things up.`)}
85
+ ${p('<strong>Review your terms and pay your retainer</strong> in your secure agreement portal:')}
86
+ ${p(`<a href="${esc(portalUrl)}" style="color:#C98A5E">Review and pay your retainer →</a>`)}
87
+ ${p('Your monthly billing starts only after the retainer is paid, so nothing happens until you complete this step. This portal link is unique to your agreement, so please don\'t forward it.')}
88
+ ${p(`Questions in the meantime? Just reply to this email or reach us at <a href="mailto:${operator.supportEmail}" style="color:#C98A5E">${operator.supportEmail}</a>.`)}
89
+ ${p(`Thanks,<br />The ${operator.name} team`)}
90
90
  </td></tr></table></td></tr></table></div>`;
91
91
  const emailRes = await fetch(`${GHL_BASE}/conversations/messages`, {
92
92
  method: 'POST', headers,
@@ -64,13 +64,13 @@ export async function sendSupportTicketEmail(supabase, { ticketId, submitterName
64
64
  if (!contactId)
65
65
  throw new Error('ghl_upsert_no_contact_id');
66
66
  const p = (t) => `<p style="margin:0 0 8px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:14px;line-height:22px">${t}</p>`;
67
- const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
68
- <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">New support ticket — ${esc(operator.name)}</h2>
69
- ${p(`<strong>From:</strong> ${esc(submitterName)} &lt;${esc(submitterEmail)}&gt;`)}
70
- ${p(`<strong>Subject:</strong> ${esc(subject)}`)}
71
- ${p(`<strong>Ticket:</strong> ${esc(ticketId)}`)}
72
- <div style="margin:12px 0;padding:12px;background:#F6F6F6;border-radius:6px;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:14px;line-height:22px;white-space:pre-wrap">${esc(message)}</div>
73
- ${p(`Manage this ticket at <a href="https://${esc(operator.domain)}/admin/support-tickets/" style="color:#C98A5E">/admin/support-tickets/</a>.`)}
67
+ const html = `<div style="background:#F6F6F6;padding:24px 0;margin:0"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;background:#F6F6F6"><tr><td align="center" style="padding:0 12px"><table role="presentation" width="560" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;max-width:560px;width:100%;background:#FFFFFF;border-radius:10px"><tr><td style="padding:24px">
68
+ <h2 style="margin:0 0 12px 0;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:22px">New support ticket — ${esc(operator.name)}</h2>
69
+ ${p(`<strong>From:</strong> ${esc(submitterName)} &lt;${esc(submitterEmail)}&gt;`)}
70
+ ${p(`<strong>Subject:</strong> ${esc(subject)}`)}
71
+ ${p(`<strong>Ticket:</strong> ${esc(ticketId)}`)}
72
+ <div style="margin:12px 0;padding:12px;background:#F6F6F6;border-radius:6px;color:#22263B;font-family:Inter,Arial,sans-serif;font-size:14px;line-height:22px;white-space:pre-wrap">${esc(message)}</div>
73
+ ${p(`Manage this ticket at <a href="https://${esc(operator.domain)}/admin/support-tickets/" style="color:#C98A5E">/admin/support-tickets/</a>.`)}
74
74
  </td></tr></table></td></tr></table></div>`;
75
75
  const emailRes = await fetch(`${GHL_BASE}/conversations/messages`, {
76
76
  method: 'POST', headers,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simplelayer/graniteer-core",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Shared platform core for Graniteer",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",