@simplelayer/graniteer-core 0.3.0 → 0.3.1

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.
@@ -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();
@@ -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
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.1",
4
4
  "description": "Shared platform core for Graniteer",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",