@simplelayer/graniteer-core 0.0.1 → 0.1.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.
- package/index.js +2 -2
- package/package.json +98 -15
- package/src/adminBookingCredit.test.ts +451 -0
- package/src/adminBookingCredit.ts +239 -0
- package/src/availability.test.ts +25 -0
- package/src/availability.ts +201 -0
- package/src/bookingHoldReclaim.ts +94 -0
- package/src/checkoutSessionParams.ts +56 -0
- package/src/commonsContract.ts +175 -0
- package/src/conductorAdmin.ts +141 -0
- package/src/email-templates/email-change.tsx +92 -0
- package/src/email-templates/invite.tsx +79 -0
- package/src/email-templates/reauthentication.tsx +60 -0
- package/src/functions/admin-cancel-membership.ts +311 -0
- package/src/functions/admin-change-membership-plan.ts +308 -0
- package/src/functions/admin-charge-guest-print.ts +229 -0
- package/src/functions/admin-correct-booking.ts +333 -0
- package/src/functions/admin-create-commons-event.ts +244 -0
- package/src/functions/admin-debug-boldsign-doc.ts +93 -0
- package/src/functions/admin-door-write.ts +272 -0
- package/src/functions/admin-mark-booking-paid.ts +203 -0
- package/src/functions/admin-photo-upload.ts +128 -0
- package/src/functions/admin-remove-team-member.ts +150 -0
- package/src/functions/admin-reset-mfa.ts +138 -0
- package/src/functions/admin-reverse-coupon-redemption.ts +98 -0
- package/src/functions/admin-send-commons-agreement.ts +214 -0
- package/src/functions/admin-set-credit-amount.ts +123 -0
- package/src/functions/admin-skip-commons-contract.ts +165 -0
- package/src/functions/admin-undo-membership-cancel.ts +144 -0
- package/src/functions/admin-wall-photo-write.ts +141 -0
- package/src/functions/boldsign-sender-identity.ts +114 -0
- package/src/functions/booking-availability.ts +419 -0
- package/src/functions/booking-member-preview.ts +136 -0
- package/src/functions/day-pass-status.ts +89 -0
- package/src/functions/dayStatus.ts +71 -0
- package/src/functions/diag-webpush-test.ts +45 -0
- package/src/functions/event-access-store.ts +309 -0
- package/src/functions/ezeep-print-webhook.ts +213 -0
- package/src/functions/ezeep-usage-reconcile.ts +299 -0
- package/src/functions/get-signing-link.ts +211 -0
- package/src/functions/guest-print-setup-confirm.ts +128 -0
- package/src/functions/guest-print-submit.ts +149 -0
- package/src/functions/luma-webhook.ts +165 -0
- package/src/functions/migration-collision-alert.copy.ts +232 -0
- package/src/functions/migration-collision-alert.test.ts +349 -0
- package/src/functions/migration-collision-alert.ts +232 -0
- package/src/functions/office-assignment-end.ts +146 -0
- package/src/functions/portal-booking-access.ts +95 -0
- package/src/functions/portal-list-invoices.ts +99 -0
- package/src/functions/print-billing-recover.ts +97 -0
- package/src/functions/print-billing-run.ts +183 -0
- package/src/functions/public-facts.ts +168 -0
- package/src/functions/space-availability-range.acceptance.test.ts +161 -0
- package/src/functions/space-availability-range.ts +142 -0
- package/src/functions/sync-gcal-blackouts.ts +298 -0
- package/src/functions/timetable-event-register.ts +162 -0
- package/src/functions/timetable-event-status.ts +90 -0
- package/src/functions/timetable-spaces.ts +219 -0
- package/src/functions/touch-pass-reconciler.ts +689 -0
- package/src/functions/virtual-onboarding-purge.ts +134 -0
- package/src/functions/virtual-onboarding-submit.ts +315 -0
- package/src/functions/voice-inquiry.test.ts +301 -0
- package/src/functions/voice-inquiry.ts +198 -0
- package/src/functions/voice-tour-availability.ts +204 -0
- package/src/gcalSync.ts +142 -0
- package/src/ghlNote.ts +34 -0
- package/src/ghlTask.ts +48 -0
- package/src/ghlVoiceInquiry.ts +90 -0
- package/src/guestPrintCapture.test.ts +722 -0
- package/src/guestPrintCapture.ts +538 -0
- package/src/lumaGhl.ts +195 -0
- package/src/membership.ts +109 -0
- package/src/money.ts +36 -0
- package/src/ops.ts +97 -0
- package/src/pacificDate.ts +34 -0
- package/src/policyAcknowledgment.test.ts +50 -0
- package/src/policyAcknowledgment.ts +96 -0
- package/src/portalSession.ts +46 -0
- package/src/pricing.test.ts +660 -0
- package/src/pricing.ts +1025 -0
- package/src/printBilling.test.ts +750 -0
- package/src/printBilling.ts +891 -0
- package/src/printBillingAdapters.ts +169 -0
- package/src/printCorrections.test.ts +348 -0
- package/src/printCorrections.ts +496 -0
- package/src/rateLimit.ts +82 -0
- package/src/referralCredits.test.ts +31 -0
- package/src/referralCredits.ts +34 -0
- package/src/referralGrant.ts +321 -0
- package/src/rescheduleCredit.test.ts +521 -0
- package/src/rescheduleCredit.ts +337 -0
- package/src/reservationWifiCode.test.ts +69 -0
- package/src/reservationWifiCode.ts +81 -0
- package/src/retellCallAnalyzed.test.ts +167 -0
- package/src/retellCallAnalyzed.ts +113 -0
- package/src/retellSignature.test.ts +137 -0
- package/src/retellSignature.ts +82 -0
- package/src/slackAlert.ts +38 -0
- package/src/supabaseTestFakes.ts +131 -0
- package/src/tier1Agreement.ts +125 -0
package/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
// Spike/smoke-test placeholder for graniteer-core. Real code lands in Phase 2+.
|
|
2
|
-
module.exports = { ping: () => "pong" };
|
|
1
|
+
// Spike/smoke-test placeholder for graniteer-core. Real code lands in Phase 2+.
|
|
2
|
+
module.exports = { ping: () => "pong" };
|
package/package.json
CHANGED
|
@@ -1,15 +1,98 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@simplelayer/graniteer-core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Shared platform core for Graniteer
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"types": "index.js",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@simplelayer/graniteer-core",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Shared platform core for Graniteer",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
"./adminBookingCredit": "./src/adminBookingCredit.ts",
|
|
9
|
+
"./availability": "./src/availability.ts",
|
|
10
|
+
"./bookingHoldReclaim": "./src/bookingHoldReclaim.ts",
|
|
11
|
+
"./checkoutSessionParams": "./src/checkoutSessionParams.ts",
|
|
12
|
+
"./commonsContract": "./src/commonsContract.ts",
|
|
13
|
+
"./conductorAdmin": "./src/conductorAdmin.ts",
|
|
14
|
+
"./email-change": "./src/email-templates/email-change.tsx",
|
|
15
|
+
"./invite": "./src/email-templates/invite.tsx",
|
|
16
|
+
"./reauthentication": "./src/email-templates/reauthentication.tsx",
|
|
17
|
+
"./gcalSync": "./src/gcalSync.ts",
|
|
18
|
+
"./ghlNote": "./src/ghlNote.ts",
|
|
19
|
+
"./ghlTask": "./src/ghlTask.ts",
|
|
20
|
+
"./ghlVoiceInquiry": "./src/ghlVoiceInquiry.ts",
|
|
21
|
+
"./guestPrintCapture": "./src/guestPrintCapture.ts",
|
|
22
|
+
"./lumaGhl": "./src/lumaGhl.ts",
|
|
23
|
+
"./membership": "./src/membership.ts",
|
|
24
|
+
"./money": "./src/money.ts",
|
|
25
|
+
"./ops": "./src/ops.ts",
|
|
26
|
+
"./pacificDate": "./src/pacificDate.ts",
|
|
27
|
+
"./policyAcknowledgment": "./src/policyAcknowledgment.ts",
|
|
28
|
+
"./portalSession": "./src/portalSession.ts",
|
|
29
|
+
"./pricing": "./src/pricing.ts",
|
|
30
|
+
"./printBilling": "./src/printBilling.ts",
|
|
31
|
+
"./printBillingAdapters": "./src/printBillingAdapters.ts",
|
|
32
|
+
"./printCorrections": "./src/printCorrections.ts",
|
|
33
|
+
"./rateLimit": "./src/rateLimit.ts",
|
|
34
|
+
"./referralCredits": "./src/referralCredits.ts",
|
|
35
|
+
"./supabaseTestFakes": "./src/supabaseTestFakes.ts",
|
|
36
|
+
"./referralGrant": "./src/referralGrant.ts",
|
|
37
|
+
"./rescheduleCredit": "./src/rescheduleCredit.ts",
|
|
38
|
+
"./reservationWifiCode": "./src/reservationWifiCode.ts",
|
|
39
|
+
"./retellCallAnalyzed": "./src/retellCallAnalyzed.ts",
|
|
40
|
+
"./retellSignature": "./src/retellSignature.ts",
|
|
41
|
+
"./slackAlert": "./src/slackAlert.ts",
|
|
42
|
+
"./tier1Agreement": "./src/tier1Agreement.ts",
|
|
43
|
+
"./functions/admin-cancel-membership": "./src/functions/admin-cancel-membership.ts",
|
|
44
|
+
"./functions/admin-change-membership-plan": "./src/functions/admin-change-membership-plan.ts",
|
|
45
|
+
"./functions/admin-charge-guest-print": "./src/functions/admin-charge-guest-print.ts",
|
|
46
|
+
"./functions/admin-correct-booking": "./src/functions/admin-correct-booking.ts",
|
|
47
|
+
"./functions/admin-create-commons-event": "./src/functions/admin-create-commons-event.ts",
|
|
48
|
+
"./functions/admin-debug-boldsign-doc": "./src/functions/admin-debug-boldsign-doc.ts",
|
|
49
|
+
"./functions/admin-door-write": "./src/functions/admin-door-write.ts",
|
|
50
|
+
"./functions/admin-mark-booking-paid": "./src/functions/admin-mark-booking-paid.ts",
|
|
51
|
+
"./functions/admin-photo-upload": "./src/functions/admin-photo-upload.ts",
|
|
52
|
+
"./functions/admin-remove-team-member": "./src/functions/admin-remove-team-member.ts",
|
|
53
|
+
"./functions/admin-reset-mfa": "./src/functions/admin-reset-mfa.ts",
|
|
54
|
+
"./functions/admin-reverse-coupon-redemption": "./src/functions/admin-reverse-coupon-redemption.ts",
|
|
55
|
+
"./functions/admin-send-commons-agreement": "./src/functions/admin-send-commons-agreement.ts",
|
|
56
|
+
"./functions/admin-set-credit-amount": "./src/functions/admin-set-credit-amount.ts",
|
|
57
|
+
"./functions/admin-skip-commons-contract": "./src/functions/admin-skip-commons-contract.ts",
|
|
58
|
+
"./functions/admin-undo-membership-cancel": "./src/functions/admin-undo-membership-cancel.ts",
|
|
59
|
+
"./functions/admin-wall-photo-write": "./src/functions/admin-wall-photo-write.ts",
|
|
60
|
+
"./functions/boldsign-sender-identity": "./src/functions/boldsign-sender-identity.ts",
|
|
61
|
+
"./functions/booking-availability": "./src/functions/booking-availability.ts",
|
|
62
|
+
"./functions/booking-member-preview": "./src/functions/booking-member-preview.ts",
|
|
63
|
+
"./functions/day-pass-status": "./src/functions/day-pass-status.ts",
|
|
64
|
+
"./functions/diag-webpush-test": "./src/functions/diag-webpush-test.ts",
|
|
65
|
+
"./functions/event-access-store": "./src/functions/event-access-store.ts",
|
|
66
|
+
"./functions/ezeep-print-webhook": "./src/functions/ezeep-print-webhook.ts",
|
|
67
|
+
"./functions/ezeep-usage-reconcile": "./src/functions/ezeep-usage-reconcile.ts",
|
|
68
|
+
"./functions/get-signing-link": "./src/functions/get-signing-link.ts",
|
|
69
|
+
"./functions/guest-print-setup-confirm": "./src/functions/guest-print-setup-confirm.ts",
|
|
70
|
+
"./functions/guest-print-submit": "./src/functions/guest-print-submit.ts",
|
|
71
|
+
"./functions/luma-webhook": "./src/functions/luma-webhook.ts",
|
|
72
|
+
"./functions/migration-collision-alert": "./src/functions/migration-collision-alert.ts",
|
|
73
|
+
"./functions/office-assignment-end": "./src/functions/office-assignment-end.ts",
|
|
74
|
+
"./functions/portal-booking-access": "./src/functions/portal-booking-access.ts",
|
|
75
|
+
"./functions/portal-list-invoices": "./src/functions/portal-list-invoices.ts",
|
|
76
|
+
"./functions/print-billing-recover": "./src/functions/print-billing-recover.ts",
|
|
77
|
+
"./functions/print-billing-run": "./src/functions/print-billing-run.ts",
|
|
78
|
+
"./functions/public-facts": "./src/functions/public-facts.ts",
|
|
79
|
+
"./functions/space-availability-range": "./src/functions/space-availability-range.ts",
|
|
80
|
+
"./functions/sync-gcal-blackouts": "./src/functions/sync-gcal-blackouts.ts",
|
|
81
|
+
"./functions/timetable-event-register": "./src/functions/timetable-event-register.ts",
|
|
82
|
+
"./functions/timetable-event-status": "./src/functions/timetable-event-status.ts",
|
|
83
|
+
"./functions/timetable-spaces": "./src/functions/timetable-spaces.ts",
|
|
84
|
+
"./functions/touch-pass-reconciler": "./src/functions/touch-pass-reconciler.ts",
|
|
85
|
+
"./functions/virtual-onboarding-purge": "./src/functions/virtual-onboarding-purge.ts",
|
|
86
|
+
"./functions/virtual-onboarding-submit": "./src/functions/virtual-onboarding-submit.ts",
|
|
87
|
+
"./functions/voice-inquiry": "./src/functions/voice-inquiry.ts",
|
|
88
|
+
"./functions/voice-tour-availability": "./src/functions/voice-tour-availability.ts"
|
|
89
|
+
},
|
|
90
|
+
"publishConfig": {
|
|
91
|
+
"access": "public"
|
|
92
|
+
},
|
|
93
|
+
"repository": {
|
|
94
|
+
"type": "git",
|
|
95
|
+
"url": "git+https://github.com/simplelayer/graniteer-core.git"
|
|
96
|
+
},
|
|
97
|
+
"license": "UNLICENSED"
|
|
98
|
+
}
|
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
// Fully mocked. NO network: the Supabase client and the Stripe client are both
|
|
2
|
+
// hand-rolled stubs holding in-memory credit_periods state, so this can run
|
|
3
|
+
// against live-only credentials with zero risk of touching them.
|
|
4
|
+
//
|
|
5
|
+
// What it pins down is the money math on the staff booking path
|
|
6
|
+
// (admin-create-booking): a member's credit balance must never be
|
|
7
|
+
// oversubscribed, and Stripe must be asked for EXACTLY the remainder credit
|
|
8
|
+
// did not cover — no more, no less, and not at all when there is no remainder.
|
|
9
|
+
|
|
10
|
+
import { assertEquals, assert } from 'https://deno.land/std@0.224.0/assert/mod.ts';
|
|
11
|
+
import {
|
|
12
|
+
availableCredit,
|
|
13
|
+
buildAdminCheckoutSessionParams,
|
|
14
|
+
consumeMemberCredit,
|
|
15
|
+
holdMemberCredit,
|
|
16
|
+
releaseMemberCredit,
|
|
17
|
+
splitCharge,
|
|
18
|
+
STRIPE_MIN_CHARGE_CENTS,
|
|
19
|
+
type CreditRpcClient,
|
|
20
|
+
} from './adminBookingCredit.ts';
|
|
21
|
+
|
|
22
|
+
const MEMBERSHIP_ID = '11111111-1111-1111-1111-111111111111';
|
|
23
|
+
const BOOKING_ID = '22222222-2222-2222-2222-222222222222';
|
|
24
|
+
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
// Fakes
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
|
|
29
|
+
interface PeriodState {
|
|
30
|
+
granted_cents: number;
|
|
31
|
+
consumed_cents: number;
|
|
32
|
+
held_cents: number;
|
|
33
|
+
expired_cents: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type RpcCall = { fn: string; args: Record<string, unknown> };
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* In-memory stand-in for credit_get_or_create_period / credit_hold /
|
|
40
|
+
* credit_consume / credit_release. Enforces the same invariant the SQL
|
|
41
|
+
* functions do: a hold larger than the spendable balance is rejected.
|
|
42
|
+
*/
|
|
43
|
+
function makeCreditSupabase(initial: Partial<PeriodState> & { granted_cents: number }) {
|
|
44
|
+
const period: PeriodState = {
|
|
45
|
+
consumed_cents: 0,
|
|
46
|
+
held_cents: 0,
|
|
47
|
+
expired_cents: 0,
|
|
48
|
+
...initial,
|
|
49
|
+
};
|
|
50
|
+
const calls: RpcCall[] = [];
|
|
51
|
+
const client: CreditRpcClient & { calls: RpcCall[]; period: PeriodState } = {
|
|
52
|
+
calls,
|
|
53
|
+
period,
|
|
54
|
+
rpc(fn: string, args: Record<string, unknown>) {
|
|
55
|
+
calls.push({ fn, args });
|
|
56
|
+
if (fn === 'credit_get_or_create_period') {
|
|
57
|
+
return Promise.resolve({ data: { ...period }, error: null });
|
|
58
|
+
}
|
|
59
|
+
if (fn === 'credit_hold') {
|
|
60
|
+
const amount = Number(args.p_amount_cents);
|
|
61
|
+
const avail = period.granted_cents - period.consumed_cents
|
|
62
|
+
- period.held_cents - period.expired_cents;
|
|
63
|
+
if (amount > avail) {
|
|
64
|
+
return Promise.resolve({ data: null, error: { message: 'INSUFFICIENT_CREDIT' } });
|
|
65
|
+
}
|
|
66
|
+
period.held_cents += amount;
|
|
67
|
+
return Promise.resolve({ data: null, error: null });
|
|
68
|
+
}
|
|
69
|
+
if (fn === 'credit_consume') {
|
|
70
|
+
period.consumed_cents += period.held_cents;
|
|
71
|
+
period.held_cents = 0;
|
|
72
|
+
return Promise.resolve({ data: null, error: null });
|
|
73
|
+
}
|
|
74
|
+
if (fn === 'credit_release') {
|
|
75
|
+
period.held_cents = 0;
|
|
76
|
+
return Promise.resolve({ data: null, error: null });
|
|
77
|
+
}
|
|
78
|
+
if (fn === 'resolve_room_contract_tier') {
|
|
79
|
+
return Promise.resolve({ data: 1, error: null });
|
|
80
|
+
}
|
|
81
|
+
throw new Error(`unexpected rpc ${fn}`);
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
return client;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Records every Checkout Session it is asked to create. Never calls Stripe. */
|
|
88
|
+
function makeStripe() {
|
|
89
|
+
const created: Record<string, unknown>[] = [];
|
|
90
|
+
return {
|
|
91
|
+
created,
|
|
92
|
+
checkout: {
|
|
93
|
+
sessions: {
|
|
94
|
+
// deno-lint-ignore require-await
|
|
95
|
+
create: async (params: Record<string, unknown>) => {
|
|
96
|
+
created.push(params);
|
|
97
|
+
return {
|
|
98
|
+
id: `cs_test_fake_${created.length}`,
|
|
99
|
+
url: `https://checkout.stripe.test/pay/cs_test_fake_${created.length}`,
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ---------------------------------------------------------------------------
|
|
108
|
+
// The unit under test: the exact credit + checkout sequence
|
|
109
|
+
// admin-create-booking/index.ts runs, in the same order, against the fakes.
|
|
110
|
+
// ---------------------------------------------------------------------------
|
|
111
|
+
|
|
112
|
+
interface RunResult {
|
|
113
|
+
credit_applied: number;
|
|
114
|
+
discount_applied: number;
|
|
115
|
+
charged: number;
|
|
116
|
+
checkout_url: string | null;
|
|
117
|
+
/** What the booking row would end up carrying. */
|
|
118
|
+
row: {
|
|
119
|
+
credit_applied_cents: number;
|
|
120
|
+
discount_applied_cents: number;
|
|
121
|
+
charged_amount_cents: number | null;
|
|
122
|
+
status: 'pending' | 'paid';
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function runStaffBooking(opts: {
|
|
127
|
+
membershipId: string | null;
|
|
128
|
+
net: number;
|
|
129
|
+
supabase: CreditRpcClient;
|
|
130
|
+
stripe: ReturnType<typeof makeStripe>;
|
|
131
|
+
mode?: 'payment_link' | 'offline';
|
|
132
|
+
}): Promise<RunResult> {
|
|
133
|
+
const { membershipId, net, supabase, stripe } = opts;
|
|
134
|
+
const mode = opts.mode ?? 'payment_link';
|
|
135
|
+
|
|
136
|
+
let credit_applied = 0;
|
|
137
|
+
let discount_applied = 0;
|
|
138
|
+
let charged = net;
|
|
139
|
+
|
|
140
|
+
if (membershipId) {
|
|
141
|
+
credit_applied = await holdMemberCredit(supabase, {
|
|
142
|
+
membershipId,
|
|
143
|
+
bookingId: BOOKING_ID,
|
|
144
|
+
net,
|
|
145
|
+
});
|
|
146
|
+
const split = splitCharge(net, credit_applied);
|
|
147
|
+
charged = split.charged;
|
|
148
|
+
discount_applied = split.discount_applied;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (mode === 'offline') {
|
|
152
|
+
if (membershipId && credit_applied > 0) await consumeMemberCredit(supabase, BOOKING_ID);
|
|
153
|
+
return {
|
|
154
|
+
credit_applied, discount_applied, charged, checkout_url: null,
|
|
155
|
+
row: {
|
|
156
|
+
credit_applied_cents: credit_applied,
|
|
157
|
+
discount_applied_cents: discount_applied,
|
|
158
|
+
charged_amount_cents: charged,
|
|
159
|
+
status: 'paid',
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Fully covered by credit — no Stripe at all.
|
|
165
|
+
if (membershipId && charged === 0) {
|
|
166
|
+
await consumeMemberCredit(supabase, BOOKING_ID);
|
|
167
|
+
return {
|
|
168
|
+
credit_applied, discount_applied, charged, checkout_url: null,
|
|
169
|
+
row: {
|
|
170
|
+
credit_applied_cents: credit_applied,
|
|
171
|
+
discount_applied_cents: discount_applied,
|
|
172
|
+
charged_amount_cents: 0,
|
|
173
|
+
status: 'paid',
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const session = await stripe.checkout.sessions.create(
|
|
179
|
+
buildAdminCheckoutSessionParams({
|
|
180
|
+
roomName: 'Cove',
|
|
181
|
+
roomSlug: 'cove',
|
|
182
|
+
human: 'Thu, Sep 3 · 9:00 AM – 11:00 AM',
|
|
183
|
+
customerEmail: 'member@example.com',
|
|
184
|
+
bookingId: BOOKING_ID,
|
|
185
|
+
amountCents: charged,
|
|
186
|
+
base: 'https://admin.granitecity.io',
|
|
187
|
+
}),
|
|
188
|
+
);
|
|
189
|
+
return {
|
|
190
|
+
credit_applied, discount_applied, charged, checkout_url: session.url,
|
|
191
|
+
row: {
|
|
192
|
+
credit_applied_cents: credit_applied,
|
|
193
|
+
discount_applied_cents: discount_applied,
|
|
194
|
+
// Left null while pending; the webhook writes it on payment.
|
|
195
|
+
charged_amount_cents: null,
|
|
196
|
+
status: 'pending',
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** bookings_amount_math_check, asserted the way Postgres asserts it. */
|
|
202
|
+
function assertAmountMath(net: number, r: RunResult) {
|
|
203
|
+
if (r.row.charged_amount_cents === null) return; // constraint allows NULL
|
|
204
|
+
assertEquals(
|
|
205
|
+
net - r.row.credit_applied_cents - r.row.discount_applied_cents,
|
|
206
|
+
r.row.charged_amount_cents,
|
|
207
|
+
'bookings_amount_math_check would fail',
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// ---------------------------------------------------------------------------
|
|
212
|
+
// (a) Member with MORE than enough credit -> no checkout session at all
|
|
213
|
+
// ---------------------------------------------------------------------------
|
|
214
|
+
|
|
215
|
+
Deno.test('(a) full credit available: booking fully covered, NO checkout session', async () => {
|
|
216
|
+
const supabase = makeCreditSupabase({ granted_cents: 20000 });
|
|
217
|
+
const stripe = makeStripe();
|
|
218
|
+
|
|
219
|
+
const r = await runStaffBooking({ membershipId: MEMBERSHIP_ID, net: 12000, supabase, stripe });
|
|
220
|
+
|
|
221
|
+
assertEquals(r.credit_applied, 12000);
|
|
222
|
+
assertEquals(r.charged, 0);
|
|
223
|
+
assertEquals(r.checkout_url, null);
|
|
224
|
+
assertEquals(stripe.created.length, 0, 'Stripe must not be called when credit covers it');
|
|
225
|
+
// Hold was placed then consumed; balance dropped by exactly the net.
|
|
226
|
+
assertEquals(supabase.period.consumed_cents, 12000);
|
|
227
|
+
assertEquals(supabase.period.held_cents, 0);
|
|
228
|
+
assertEquals(availableCredit(supabase.period), 8000);
|
|
229
|
+
assertEquals(r.row.status, 'paid');
|
|
230
|
+
assertAmountMath(12000, r);
|
|
231
|
+
assertEquals(
|
|
232
|
+
supabase.calls.map((c) => c.fn),
|
|
233
|
+
['credit_get_or_create_period', 'credit_hold', 'credit_consume'],
|
|
234
|
+
);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
Deno.test('(a2) credit exactly equal to net is still fully covered', async () => {
|
|
238
|
+
const supabase = makeCreditSupabase({ granted_cents: 12000 });
|
|
239
|
+
const stripe = makeStripe();
|
|
240
|
+
const r = await runStaffBooking({ membershipId: MEMBERSHIP_ID, net: 12000, supabase, stripe });
|
|
241
|
+
assertEquals(r.credit_applied, 12000);
|
|
242
|
+
assertEquals(r.charged, 0);
|
|
243
|
+
assertEquals(stripe.created.length, 0);
|
|
244
|
+
assertEquals(availableCredit(supabase.period), 0);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
// ---------------------------------------------------------------------------
|
|
248
|
+
// (b) Partial credit -> checkout session for EXACTLY the remainder
|
|
249
|
+
// ---------------------------------------------------------------------------
|
|
250
|
+
|
|
251
|
+
Deno.test('(b) partial credit: checkout session for exactly the uncovered remainder', async () => {
|
|
252
|
+
const supabase = makeCreditSupabase({ granted_cents: 5000 });
|
|
253
|
+
const stripe = makeStripe();
|
|
254
|
+
|
|
255
|
+
const r = await runStaffBooking({ membershipId: MEMBERSHIP_ID, net: 12000, supabase, stripe });
|
|
256
|
+
|
|
257
|
+
// Math.min(net, avail) — never more than the member actually has.
|
|
258
|
+
assertEquals(r.credit_applied, 5000);
|
|
259
|
+
assertEquals(r.charged, 7000);
|
|
260
|
+
assertEquals(stripe.created.length, 1);
|
|
261
|
+
const params = stripe.created[0] as {
|
|
262
|
+
line_items: [{ price_data: { unit_amount: number } }];
|
|
263
|
+
metadata: { booking_id: string };
|
|
264
|
+
};
|
|
265
|
+
assertEquals(params.line_items[0].price_data.unit_amount, 7000, 'Stripe charged the remainder');
|
|
266
|
+
assertEquals(params.metadata.booking_id, BOOKING_ID, 'webhook needs this to consume the hold');
|
|
267
|
+
assert(r.checkout_url?.startsWith('https://checkout.stripe.test/'));
|
|
268
|
+
// Credit stays HELD while checkout is open — never consumed early.
|
|
269
|
+
assertEquals(supabase.period.held_cents, 5000);
|
|
270
|
+
assertEquals(supabase.period.consumed_cents, 0);
|
|
271
|
+
assertEquals(availableCredit(supabase.period), 0);
|
|
272
|
+
assertEquals(supabase.calls.map((c) => c.fn), ['credit_get_or_create_period', 'credit_hold']);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
Deno.test('(b2) partly-spent period: only the UNSPENT balance is drawn', async () => {
|
|
276
|
+
// 10000 granted, 6000 already consumed, 1000 held elsewhere -> 3000 spendable.
|
|
277
|
+
const supabase = makeCreditSupabase({
|
|
278
|
+
granted_cents: 10000, consumed_cents: 6000, held_cents: 1000,
|
|
279
|
+
});
|
|
280
|
+
const stripe = makeStripe();
|
|
281
|
+
|
|
282
|
+
const r = await runStaffBooking({ membershipId: MEMBERSHIP_ID, net: 12000, supabase, stripe });
|
|
283
|
+
|
|
284
|
+
assertEquals(r.credit_applied, 3000, 'must not draw against consumed/held cents');
|
|
285
|
+
assertEquals(r.charged, 9000);
|
|
286
|
+
assertEquals(availableCredit(supabase.period), 0);
|
|
287
|
+
assert(availableCredit(supabase.period) >= 0, 'balance can never go negative');
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
Deno.test('(b3) sub-$0.50 remainder is absorbed, not sent to Stripe', async () => {
|
|
291
|
+
const supabase = makeCreditSupabase({ granted_cents: 11970 });
|
|
292
|
+
const stripe = makeStripe();
|
|
293
|
+
|
|
294
|
+
const r = await runStaffBooking({ membershipId: MEMBERSHIP_ID, net: 12000, supabase, stripe });
|
|
295
|
+
|
|
296
|
+
assertEquals(r.credit_applied, 11970);
|
|
297
|
+
assertEquals(r.charged, 0, `remainder of 30c is under ${STRIPE_MIN_CHARGE_CENTS}`);
|
|
298
|
+
assertEquals(r.discount_applied, 30);
|
|
299
|
+
assertEquals(stripe.created.length, 0);
|
|
300
|
+
assertAmountMath(12000, r);
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
// ---------------------------------------------------------------------------
|
|
304
|
+
// (c) Zero credit -> checkout for the full amount, nothing consumed
|
|
305
|
+
// ---------------------------------------------------------------------------
|
|
306
|
+
|
|
307
|
+
Deno.test('(c) zero credit: checkout for the full amount, no credit consumed', async () => {
|
|
308
|
+
const supabase = makeCreditSupabase({ granted_cents: 0 });
|
|
309
|
+
const stripe = makeStripe();
|
|
310
|
+
|
|
311
|
+
const r = await runStaffBooking({ membershipId: MEMBERSHIP_ID, net: 12000, supabase, stripe });
|
|
312
|
+
|
|
313
|
+
assertEquals(r.credit_applied, 0);
|
|
314
|
+
assertEquals(r.charged, 12000);
|
|
315
|
+
assertEquals(stripe.created.length, 1);
|
|
316
|
+
const params = stripe.created[0] as { line_items: [{ price_data: { unit_amount: number } }] };
|
|
317
|
+
assertEquals(params.line_items[0].price_data.unit_amount, 12000);
|
|
318
|
+
assertEquals(supabase.period.held_cents, 0);
|
|
319
|
+
assertEquals(supabase.period.consumed_cents, 0);
|
|
320
|
+
// Period is read, but with nothing available no hold is even attempted.
|
|
321
|
+
assertEquals(supabase.calls.map((c) => c.fn), ['credit_get_or_create_period']);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
Deno.test('(c2) fully-exhausted period behaves like zero credit', async () => {
|
|
325
|
+
const supabase = makeCreditSupabase({
|
|
326
|
+
granted_cents: 10000, consumed_cents: 7000, held_cents: 2000, expired_cents: 1000,
|
|
327
|
+
});
|
|
328
|
+
const stripe = makeStripe();
|
|
329
|
+
const r = await runStaffBooking({ membershipId: MEMBERSHIP_ID, net: 12000, supabase, stripe });
|
|
330
|
+
assertEquals(availableCredit(supabase.period), 0);
|
|
331
|
+
assertEquals(r.credit_applied, 0);
|
|
332
|
+
assertEquals(r.charged, 12000);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
Deno.test('(c3) an RPC failure never fails the booking — member just pays full', async () => {
|
|
336
|
+
const failing: CreditRpcClient = {
|
|
337
|
+
rpc: () => Promise.resolve({ data: null, error: { message: 'MEMBERSHIP_NOT_FOUND' } }),
|
|
338
|
+
};
|
|
339
|
+
const stripe = makeStripe();
|
|
340
|
+
const r = await runStaffBooking({
|
|
341
|
+
membershipId: MEMBERSHIP_ID, net: 12000, supabase: failing, stripe,
|
|
342
|
+
});
|
|
343
|
+
assertEquals(r.credit_applied, 0);
|
|
344
|
+
assertEquals(r.charged, 12000);
|
|
345
|
+
assertEquals(stripe.created.length, 1);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
// ---------------------------------------------------------------------------
|
|
349
|
+
// (d) No membership attached -> free-form path completely untouched
|
|
350
|
+
// ---------------------------------------------------------------------------
|
|
351
|
+
|
|
352
|
+
Deno.test('(d) no membershipId: zero credit RPCs, Stripe charged the full net', async () => {
|
|
353
|
+
const supabase = makeCreditSupabase({ granted_cents: 50000 });
|
|
354
|
+
const stripe = makeStripe();
|
|
355
|
+
|
|
356
|
+
const r = await runStaffBooking({ membershipId: null, net: 12000, supabase, stripe });
|
|
357
|
+
|
|
358
|
+
assertEquals(supabase.calls.length, 0, 'the credit ledger must not be touched at all');
|
|
359
|
+
assertEquals(r.credit_applied, 0);
|
|
360
|
+
assertEquals(r.discount_applied, 0);
|
|
361
|
+
assertEquals(r.charged, 12000);
|
|
362
|
+
assertEquals(stripe.created.length, 1);
|
|
363
|
+
// The Checkout params must be exactly what this function built before member
|
|
364
|
+
// attachment existed.
|
|
365
|
+
assertEquals(stripe.created[0], {
|
|
366
|
+
mode: 'payment',
|
|
367
|
+
customer_email: 'member@example.com',
|
|
368
|
+
line_items: [{
|
|
369
|
+
quantity: 1,
|
|
370
|
+
price_data: {
|
|
371
|
+
currency: 'usd',
|
|
372
|
+
unit_amount: 12000,
|
|
373
|
+
product_data: { name: 'Cove — Thu, Sep 3 · 9:00 AM – 11:00 AM' },
|
|
374
|
+
},
|
|
375
|
+
}],
|
|
376
|
+
metadata: { booking_id: BOOKING_ID },
|
|
377
|
+
success_url: 'https://admin.granitecity.io/booking/confirmation?session_id={CHECKOUT_SESSION_ID}',
|
|
378
|
+
cancel_url: 'https://admin.granitecity.io/meeting-rooms/cove?canceled=1',
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
Deno.test('(d2) no membershipId, offline mode: nothing charged, nothing held', async () => {
|
|
383
|
+
const supabase = makeCreditSupabase({ granted_cents: 50000 });
|
|
384
|
+
const stripe = makeStripe();
|
|
385
|
+
const r = await runStaffBooking({
|
|
386
|
+
membershipId: null, net: 12000, supabase, stripe, mode: 'offline',
|
|
387
|
+
});
|
|
388
|
+
assertEquals(supabase.calls.length, 0);
|
|
389
|
+
assertEquals(stripe.created.length, 0);
|
|
390
|
+
assertEquals(r.row.charged_amount_cents, 12000);
|
|
391
|
+
assertEquals(r.row.credit_applied_cents, 0);
|
|
392
|
+
assertAmountMath(12000, r);
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
// ---------------------------------------------------------------------------
|
|
396
|
+
// Offline + member
|
|
397
|
+
// ---------------------------------------------------------------------------
|
|
398
|
+
|
|
399
|
+
Deno.test('offline + member: credit consumed immediately, remainder collected offline', async () => {
|
|
400
|
+
const supabase = makeCreditSupabase({ granted_cents: 5000 });
|
|
401
|
+
const stripe = makeStripe();
|
|
402
|
+
|
|
403
|
+
const r = await runStaffBooking({
|
|
404
|
+
membershipId: MEMBERSHIP_ID, net: 12000, supabase, stripe, mode: 'offline',
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
assertEquals(r.credit_applied, 5000);
|
|
408
|
+
assertEquals(r.row.charged_amount_cents, 7000);
|
|
409
|
+
assertEquals(stripe.created.length, 0);
|
|
410
|
+
assertEquals(supabase.period.consumed_cents, 5000);
|
|
411
|
+
assertEquals(supabase.period.held_cents, 0);
|
|
412
|
+
assertAmountMath(12000, r);
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
// ---------------------------------------------------------------------------
|
|
416
|
+
// Abandon path
|
|
417
|
+
// ---------------------------------------------------------------------------
|
|
418
|
+
|
|
419
|
+
Deno.test('release gives the held balance back', async () => {
|
|
420
|
+
const supabase = makeCreditSupabase({ granted_cents: 5000 });
|
|
421
|
+
const stripe = makeStripe();
|
|
422
|
+
await runStaffBooking({ membershipId: MEMBERSHIP_ID, net: 12000, supabase, stripe });
|
|
423
|
+
assertEquals(availableCredit(supabase.period), 0);
|
|
424
|
+
|
|
425
|
+
const err = await releaseMemberCredit(supabase, BOOKING_ID, 'stripe_create_failed');
|
|
426
|
+
assertEquals(err, null);
|
|
427
|
+
assertEquals(availableCredit(supabase.period), 5000, 'member keeps their full balance');
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
// ---------------------------------------------------------------------------
|
|
431
|
+
// splitCharge / availableCredit in isolation
|
|
432
|
+
// ---------------------------------------------------------------------------
|
|
433
|
+
|
|
434
|
+
Deno.test('splitCharge never returns a negative charge', () => {
|
|
435
|
+
assertEquals(splitCharge(1000, 5000), { charged: 0, discount_applied: 0 });
|
|
436
|
+
assertEquals(splitCharge(1000, 0), { charged: 1000, discount_applied: 0 });
|
|
437
|
+
assertEquals(splitCharge(1000, 960), { charged: 0, discount_applied: 40 });
|
|
438
|
+
assertEquals(splitCharge(1000, 950), { charged: 50, discount_applied: 0 });
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
Deno.test('availableCredit floors at zero and subtracts every bucket', () => {
|
|
442
|
+
assertEquals(availableCredit({ granted_cents: 100 }), 100);
|
|
443
|
+
assertEquals(availableCredit({ granted_cents: 100, consumed_cents: 150 }), 0);
|
|
444
|
+
assertEquals(
|
|
445
|
+
availableCredit({
|
|
446
|
+
granted_cents: 1000, consumed_cents: 100, held_cents: 200, expired_cents: 300,
|
|
447
|
+
}),
|
|
448
|
+
400,
|
|
449
|
+
);
|
|
450
|
+
assertEquals(availableCredit(null), 0);
|
|
451
|
+
});
|