@simplelayer/graniteer-core 0.3.2 → 0.3.4

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,
@@ -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();
@@ -163,7 +163,18 @@ export async function handle(req) {
163
163
  const visitorId = str(args.visitor_id);
164
164
  if (!grantId)
165
165
  return await fail('invalid_args', {});
166
- const r = await cas(grantId, 'pending', 'active', { visitor_id: visitorId });
166
+ const extra = { visitor_id: visitorId };
167
+ // D35 (two-pin organizer unlock/relock): unlock_visitor_id/relock_visitor_id
168
+ // are only ever sent by ftw-middleware for a door configured for two-pin
169
+ // grants; every other caller omits them, so this CAS patch is byte-identical
170
+ // to before for every existing single-visitor door.
171
+ const unlockVisitorId = str(args.unlock_visitor_id);
172
+ const relockVisitorId = str(args.relock_visitor_id);
173
+ if (unlockVisitorId)
174
+ extra.unlock_visitor_id = unlockVisitorId;
175
+ if (relockVisitorId)
176
+ extra.relock_visitor_id = relockVisitorId;
177
+ const r = await cas(grantId, 'pending', 'active', extra);
167
178
  if ('err' in r)
168
179
  return await fail('db_error', { message: r.err });
169
180
  return json(200, { ok: true, result: r.row });
@@ -206,6 +217,15 @@ export async function handle(req) {
206
217
  };
207
218
  if (visitorId)
208
219
  patch.visitor_id = visitorId; // COALESCE semantics
220
+ // D35 (two-pin organizer unlock/relock): same COALESCE-on-presence
221
+ // semantics for the two new columns -- an omitted arg leaves whatever
222
+ // is already in the DB column untouched.
223
+ const unlockVisitorId = str(args.unlock_visitor_id);
224
+ const relockVisitorId = str(args.relock_visitor_id);
225
+ if (unlockVisitorId)
226
+ patch.unlock_visitor_id = unlockVisitorId;
227
+ if (relockVisitorId)
228
+ patch.relock_visitor_id = relockVisitorId;
209
229
  const { data, error } = await supabase
210
230
  .from('booking_access_grants').update(patch).eq('id', grantId).select('*').maybeSingle();
211
231
  if (error)
@@ -264,6 +284,20 @@ export async function handle(req) {
264
284
  return await fail('db_error', { message: error.message });
265
285
  return json(200, { ok: true, result: data ?? [] });
266
286
  }
287
+ case 'get_active_two_pin_grants': {
288
+ // D35 (event_access_pin_tick.py, DRY-RUN ONLY poller): grants whose door
289
+ // was provisioned with two separate Unlock/Relock PIN visitor credentials
290
+ // rather than a single visitor_id. No time-window filter here -- the
291
+ // poller itself checks the matched log entry's timestamp against the
292
+ // grant's own access_start/access_end.
293
+ const { data, error } = await supabase
294
+ .from('booking_access_grants').select('*')
295
+ .eq('status', 'active')
296
+ .or('unlock_visitor_id.not.is.null,relock_visitor_id.not.is.null');
297
+ if (error)
298
+ return await fail('db_error', { message: error.message });
299
+ return json(200, { ok: true, result: data ?? [] });
300
+ }
267
301
  case 'try_transition_reconciling_to_active': {
268
302
  const grantId = reqUuid(args, 'grant_id');
269
303
  const visitorId = str(args.visitor_id);
@@ -332,6 +366,72 @@ export async function handle(req) {
332
366
  return await fail('db_error', { message: error.message });
333
367
  return json(200, { ok: true, result: { event_id: eventId } });
334
368
  }
369
+ case 'get_pin_tick_cursor': {
370
+ // D35 (S-A/S1): persisted high-water-mark for event_access_pin_tick.py's
371
+ // poller, replacing its in-memory-only cursor so a container restart
372
+ // doesn't replay or (worse) permanently drop an out-of-order log entry.
373
+ // One row per poller instance, keyed by a caller-chosen id (today there's
374
+ // only ever one: 'commons_pin_tick').
375
+ const cursorId = str(args.cursor_id);
376
+ if (!cursorId)
377
+ return await fail('invalid_args', {});
378
+ const { data, error } = await supabase
379
+ .from('event_access_pin_cursor').select('*').eq('id', cursorId).maybeSingle();
380
+ if (error)
381
+ return await fail('db_error', { message: error.message });
382
+ return json(200, { ok: true, result: data ?? null });
383
+ }
384
+ case 'set_pin_tick_cursor': {
385
+ // D35 (S-A/S1): upsert with a floor -- the poller should never regress
386
+ // its own high-water-mark by writing a strictly older created_time than
387
+ // what's already stored. An intersecting duplicate hwm_ids_at_time entry
388
+ // is still fine to resend as-is.
389
+ //
390
+ // 2026-09-22 (FINDINGS3.md S2-4): this used to READ the stored
391
+ // hwm_created_time, compare it here in JS, then upsert -- a read-then-
392
+ // write TOCTOU. The poller is a single logical writer today, but two
393
+ // writers exist transiently on any deploy where the old and new
394
+ // containers overlap (and would exist permanently if a future add_job
395
+ // forgot max_instances=1). Interleaved: A reads 100, B reads 100, B
396
+ // writes 200, A writes 150 -- the cursor regresses to 150 with A's id
397
+ // set, and NEITHER caller gets regressed:true, so neither alerts. The
398
+ // entries between 150 and 200 then replay on the next tick: duplicate
399
+ // audit rows today, a replayed unlock/relock pair once armed.
400
+ //
401
+ // The floor now lives inside a single statement, in the ON CONFLICT
402
+ // WHERE clause of set_pin_tick_cursor_atomic() (see
403
+ // D35_EDGE_FUNCTION_MIGRATION_DRAFT.sql) -- PostgREST's .upsert()
404
+ // cannot express that WHERE, which is why it has to be an RPC.
405
+ const cursorId = str(args.cursor_id);
406
+ if (!cursorId)
407
+ return await fail('invalid_args', {});
408
+ const hwmCreatedTime = typeof args.hwm_created_time === 'number' ? args.hwm_created_time : null;
409
+ const hwmIds = Array.isArray(args.hwm_ids_at_time)
410
+ ? args.hwm_ids_at_time.filter((v) => typeof v === 'string')
411
+ : [];
412
+ if (hwmCreatedTime === null)
413
+ return await fail('invalid_args', {});
414
+ const { data, error } = await supabase.rpc('set_pin_tick_cursor_atomic', {
415
+ p_cursor_id: cursorId,
416
+ p_hwm_created_time: hwmCreatedTime,
417
+ p_hwm_ids_at_time: hwmIds,
418
+ });
419
+ if (error)
420
+ return await fail('db_error', { message: error.message });
421
+ // The function returns the cursor row in BOTH outcomes: the row it just
422
+ // wrote, or -- when the floor rejected the write -- the untouched
423
+ // existing row. So `regressed` is derived from what came back (the
424
+ // stored value is not the value we sent), never from a separate
425
+ // pre-read that another writer could have invalidated in between.
426
+ const row = Array.isArray(data) ? data[0] : data;
427
+ if (!row)
428
+ return await fail('db_error', { message: 'set_pin_tick_cursor_atomic returned no row' });
429
+ const stored = typeof row.hwm_created_time === 'number' ? row.hwm_created_time : null;
430
+ if (stored === null || stored !== hwmCreatedTime) {
431
+ return json(200, { ok: true, result: { regressed: true, kept_hwm_created_time: stored } });
432
+ }
433
+ return json(200, { ok: true, result: row });
434
+ }
335
435
  default:
336
436
  return await fail('unknown_op', {});
337
437
  }
@@ -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,
@@ -45,22 +45,22 @@ export function gcCodeOf(e) {
45
45
  * minimum is applied on this side. v14 also had a per-period `>= 50` inside
46
46
  * M-11's claim UPDATE; that predicate is DELETED, because it silently billed
47
47
  * a member who owed $9.30 nothing at all, every month (T-85). */
48
- export const ELIGIBILITY_SQL = `
49
- select p.membership_id,
50
- array_agg(p.id order by p.period_month) as period_ids,
51
- array_agg(distinct p.period_month) as period_months,
52
- sum(p.charge_cents_total)::integer as amount_cents
53
- from public.print_usage_periods p
54
- where p.status = 'pending_bill'
55
- and p.billing_run_id is null
56
- and p.billed_run_id is null
57
- and p.charge_cents_total > 0
58
- group by p.membership_id
59
- having sum(p.charge_cents_total) >= ${STRIPE_MIN_CENTS}
60
- and not exists (
61
- select 1 from public.print_billing_runs r
62
- where r.membership_id = p.membership_id
63
- and r.completed_at is null
48
+ export const ELIGIBILITY_SQL = `
49
+ select p.membership_id,
50
+ array_agg(p.id order by p.period_month) as period_ids,
51
+ array_agg(distinct p.period_month) as period_months,
52
+ sum(p.charge_cents_total)::integer as amount_cents
53
+ from public.print_usage_periods p
54
+ where p.status = 'pending_bill'
55
+ and p.billing_run_id is null
56
+ and p.billed_run_id is null
57
+ and p.charge_cents_total > 0
58
+ group by p.membership_id
59
+ having sum(p.charge_cents_total) >= ${STRIPE_MIN_CENTS}
60
+ and not exists (
61
+ select 1 from public.print_billing_runs r
62
+ where r.membership_id = p.membership_id
63
+ and r.completed_at is null
64
64
  )`;
65
65
  /** [v18/B12-1] The baseline query. The ORDER BY and LIMIT MUST live in a
66
66
  * subquery: v6..v17 printed an ungrouped avg() with a bare ORDER BY, which
@@ -69,41 +69,41 @@ having sum(p.charge_cents_total) >= ${STRIPE_MIN_CENTS}
69
69
  * a 25x baseline error on the v18 fixture (T-46b). Returns BOTH operands rule 2
70
70
  * needs; the old form returned no count at all, so the rule was unimplementable
71
71
  * from its own stated input. */
72
- export const BASELINE_SQL = `
73
- select coalesce(avg(b.billed_amount_cents), 0)::numeric as baseline_cents,
74
- count(*)::integer as qualifying_periods
75
- from (
76
- select p.billed_amount_cents, p.period_month
77
- from public.print_usage_periods p
78
- where p.membership_id = $1
79
- and p.status = 'billed'
80
- and p.billed_amount_cents > 0
81
- order by p.period_month desc
82
- limit ${BASELINE_WINDOW}
72
+ export const BASELINE_SQL = `
73
+ select coalesce(avg(b.billed_amount_cents), 0)::numeric as baseline_cents,
74
+ count(*)::integer as qualifying_periods
75
+ from (
76
+ select p.billed_amount_cents, p.period_month
77
+ from public.print_usage_periods p
78
+ where p.membership_id = $1
79
+ and p.status = 'billed'
80
+ and p.billed_amount_cents > 0
81
+ order by p.period_month desc
82
+ limit ${BASELINE_WINDOW}
83
83
  ) b`;
84
84
  /** section 5.3's escalation ladder. A held run gets louder rather than expiring —
85
85
  * an automatic expiry that frees the periods is exactly what created the
86
86
  * double-charge hole in R7 F4 / R8 F2. */
87
- export const STUCK_ESCALATION_SQL = `
88
- insert into public.ops_events (kind, severity, detail, dedupe_key)
89
- select
90
- 'print_billing_run_stuck',
91
- case
92
- when r.updated_at < now() - interval '7 days' then 'critical'
93
- when r.updated_at < now() - interval '24 hours' then 'error'
94
- else 'warning'
95
- end,
96
- jsonb_build_object(
97
- 'run_id', r.id, 'membership_id', r.membership_id, 'status', r.status,
98
- 'hold_reason', r.hold_reason, 'amount_cents', r.amount_cents,
99
- 'stripe_invoice_id', r.stripe_invoice_id,
100
- 'stuck_for', now() - r.updated_at,
101
- 'blocking_future_runs_for_membership', true),
102
- 'print_billing_run_stuck:' || r.id::text || ':'
103
- || (now() at time zone 'UTC')::date::text
104
- from public.print_billing_runs r
105
- where r.completed_at is null
106
- and r.status = 'held'
87
+ export const STUCK_ESCALATION_SQL = `
88
+ insert into public.ops_events (kind, severity, detail, dedupe_key)
89
+ select
90
+ 'print_billing_run_stuck',
91
+ case
92
+ when r.updated_at < now() - interval '7 days' then 'critical'
93
+ when r.updated_at < now() - interval '24 hours' then 'error'
94
+ else 'warning'
95
+ end,
96
+ jsonb_build_object(
97
+ 'run_id', r.id, 'membership_id', r.membership_id, 'status', r.status,
98
+ 'hold_reason', r.hold_reason, 'amount_cents', r.amount_cents,
99
+ 'stripe_invoice_id', r.stripe_invoice_id,
100
+ 'stuck_for', now() - r.updated_at,
101
+ 'blocking_future_runs_for_membership', true),
102
+ 'print_billing_run_stuck:' || r.id::text || ':'
103
+ || (now() at time zone 'UTC')::date::text
104
+ from public.print_billing_runs r
105
+ where r.completed_at is null
106
+ and r.status = 'held'
107
107
  on conflict (dedupe_key) where dedupe_key is not null do nothing`;
108
108
  /** Rules 1-3 of section 4.2's gate. The two gates are INDEPENDENT and both are
109
109
  * evaluated — a run can trip the cap without the ratio and vice versa, and
@@ -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.2",
3
+ "version": "0.3.4",
4
4
  "description": "Shared platform core for Graniteer",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -645,7 +645,7 @@
645
645
  "react": "18.3.1",
646
646
  "stripe": "17.5.0",
647
647
  "web-push": "3.6.7",
648
- "zod": "3.23.8"
648
+ "zod": "^3.25.76"
649
649
  },
650
650
  "files": [
651
651
  "dist"