@simplelayer/graniteer-core 0.3.21 → 0.3.22
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.
|
@@ -10,5 +10,23 @@ export interface MembershipDoorSyncConfig {
|
|
|
10
10
|
membershipBusEnforce: (members: MembershipSnapshotMember[], supabase: SupabaseLike) => Promise<Record<string, unknown> | null>;
|
|
11
11
|
membershipBusRevoke: (members: MembershipSnapshotMember[], generatedAt: string, supabase: SupabaseLike) => Promise<Record<string, unknown> | null>;
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Loads membership_id -> add-on UniFi group ids for LIVE subscriptions to
|
|
15
|
+
* grants_access add-ons. Throws on any read error so the tick aborts before
|
|
16
|
+
* any Phase 2/3/4 call: falling back to "no add-on groups" could make the
|
|
17
|
+
* revoke phase strip real access.
|
|
18
|
+
*/
|
|
19
|
+
export declare function loadAddonGroupIds(supabase: SupabaseClient): Promise<Map<string, string[]>>;
|
|
20
|
+
/**
|
|
21
|
+
* Desired UniFi group ids = union of entitled rows' plan groups and those
|
|
22
|
+
* same rows' live add-on groups. Not entitled (no entitled rows) => [].
|
|
23
|
+
* Sorted + de-duplicated; empty/null ids ignored.
|
|
24
|
+
*/
|
|
25
|
+
export declare function computeDesiredGroupIds(entitledRows: Array<{
|
|
26
|
+
id: string;
|
|
27
|
+
membership_plans: {
|
|
28
|
+
unifi_group_ids: string[] | null;
|
|
29
|
+
} | null;
|
|
30
|
+
}>, addonGroupsByMembership: Map<string, string[]>): string[];
|
|
13
31
|
export declare function createMembershipDoorSyncHandler(config: MembershipDoorSyncConfig): (req: Request) => Promise<Response>;
|
|
14
32
|
export {};
|
|
@@ -83,6 +83,80 @@ function stateEquals(a, b) {
|
|
|
83
83
|
}
|
|
84
84
|
return true;
|
|
85
85
|
}
|
|
86
|
+
const LIVE_ADDON_STATUSES = ['active', 'past_due', 'pending_removal'];
|
|
87
|
+
function assertNotTruncated(table, res) {
|
|
88
|
+
const data = res.data ?? [];
|
|
89
|
+
const count = res.count;
|
|
90
|
+
if (typeof count !== 'number' || count !== data.length) {
|
|
91
|
+
throw new Error(`${table} read truncated: got ${data.length} of ${count} -- aborting tick before any Phase 2/3/4 call`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Loads membership_id -> add-on UniFi group ids for LIVE subscriptions to
|
|
96
|
+
* grants_access add-ons. Throws on any read error so the tick aborts before
|
|
97
|
+
* any Phase 2/3/4 call: falling back to "no add-on groups" could make the
|
|
98
|
+
* revoke phase strip real access.
|
|
99
|
+
*/
|
|
100
|
+
export async function loadAddonGroupIds(supabase) {
|
|
101
|
+
const result = new Map();
|
|
102
|
+
const subsRes = (await supabase
|
|
103
|
+
.from('member_addon_subscriptions')
|
|
104
|
+
.select('membership_id,addon_id,status', { count: 'exact' })
|
|
105
|
+
.in('status', LIVE_ADDON_STATUSES)
|
|
106
|
+
.order('id'));
|
|
107
|
+
if (subsRes.error)
|
|
108
|
+
throw new Error('member_addon_subscriptions read failed: ' + subsRes.error.message);
|
|
109
|
+
assertNotTruncated('member_addon_subscriptions', subsRes);
|
|
110
|
+
// Re-check status locally so correctness never depends solely on the filter.
|
|
111
|
+
const subs = (subsRes.data ?? [])
|
|
112
|
+
.filter((s) => s.membership_id && s.addon_id && LIVE_ADDON_STATUSES.includes(s.status));
|
|
113
|
+
if (subs.length === 0)
|
|
114
|
+
return result;
|
|
115
|
+
const addonIds = Array.from(new Set(subs.map((s) => s.addon_id)));
|
|
116
|
+
const addonsRes = (await supabase
|
|
117
|
+
.from('member_addons')
|
|
118
|
+
.select('id,unifi_group_ids,grants_access', { count: 'exact' })
|
|
119
|
+
.in('id', addonIds)
|
|
120
|
+
.eq('grants_access', true)
|
|
121
|
+
.order('id'));
|
|
122
|
+
if (addonsRes.error)
|
|
123
|
+
throw new Error('member_addons read failed: ' + addonsRes.error.message);
|
|
124
|
+
assertNotTruncated('member_addons', addonsRes);
|
|
125
|
+
const groupsByAddon = new Map();
|
|
126
|
+
for (const a of (addonsRes.data ?? [])) {
|
|
127
|
+
if (a.grants_access !== true)
|
|
128
|
+
continue;
|
|
129
|
+
groupsByAddon.set(a.id, a.unifi_group_ids ?? []);
|
|
130
|
+
}
|
|
131
|
+
for (const s of subs) {
|
|
132
|
+
const gids = groupsByAddon.get(s.addon_id);
|
|
133
|
+
if (!gids)
|
|
134
|
+
continue;
|
|
135
|
+
const list = result.get(s.membership_id) ?? [];
|
|
136
|
+
for (const g of gids)
|
|
137
|
+
if (g)
|
|
138
|
+
list.push(g);
|
|
139
|
+
result.set(s.membership_id, list);
|
|
140
|
+
}
|
|
141
|
+
return result;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Desired UniFi group ids = union of entitled rows' plan groups and those
|
|
145
|
+
* same rows' live add-on groups. Not entitled (no entitled rows) => [].
|
|
146
|
+
* Sorted + de-duplicated; empty/null ids ignored.
|
|
147
|
+
*/
|
|
148
|
+
export function computeDesiredGroupIds(entitledRows, addonGroupsByMembership) {
|
|
149
|
+
const set = new Set();
|
|
150
|
+
for (const r of entitledRows) {
|
|
151
|
+
for (const g of r.membership_plans?.unifi_group_ids ?? [])
|
|
152
|
+
if (g)
|
|
153
|
+
set.add(g);
|
|
154
|
+
for (const g of addonGroupsByMembership.get(r.id) ?? [])
|
|
155
|
+
if (g)
|
|
156
|
+
set.add(g);
|
|
157
|
+
}
|
|
158
|
+
return Array.from(set).sort();
|
|
159
|
+
}
|
|
86
160
|
/**
|
|
87
161
|
* Door-access start-date gate, separate from (and layered AFTER) pricing
|
|
88
162
|
* entitlement. An UNGATED row (door_access_not_before === null) is never
|
|
@@ -134,6 +208,7 @@ export function createMembershipDoorSyncHandler(config) {
|
|
|
134
208
|
if (rows.length > 0 && !('door_access_not_before' in rows[0])) {
|
|
135
209
|
throw new Error('shape_guard_failed: door_access_not_before missing from memberships select result -- aborting tick before any Phase 2/3/4 call');
|
|
136
210
|
}
|
|
211
|
+
const addonGroupsByMembership = await loadAddonGroupIds(supabase);
|
|
137
212
|
// Group by lowercased email. Rows with no email are ignored (nothing to
|
|
138
213
|
// key door/WiFi state to). resolveIsMember() likewise treats "" as "not
|
|
139
214
|
// a member" — keep that behavior aligned.
|
|
@@ -184,16 +259,11 @@ export function createMembershipDoorSyncHandler(config) {
|
|
|
184
259
|
let groupIds = [];
|
|
185
260
|
let wifiEnabled = false;
|
|
186
261
|
if (entitled) {
|
|
187
|
-
const set = new Set();
|
|
188
262
|
for (const r of entitledRows) {
|
|
189
|
-
const gids = r.membership_plans?.unifi_group_ids ?? [];
|
|
190
|
-
for (const g of gids)
|
|
191
|
-
if (g)
|
|
192
|
-
set.add(g);
|
|
193
263
|
if (r.membership_plans?.wifi_enabled === true)
|
|
194
264
|
wifiEnabled = true;
|
|
195
265
|
}
|
|
196
|
-
groupIds =
|
|
266
|
+
groupIds = computeDesiredGroupIds(entitledRows, addonGroupsByMembership);
|
|
197
267
|
}
|
|
198
268
|
const desired = { entitled, group_ids: groupIds, wifi_enabled: wifiEnabled };
|
|
199
269
|
// Every managed, non-protected email goes into the snapshot -- entitled or not.
|