@7365admin1/core 3.129.0 → 3.129.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@7365admin1/core",
3
3
  "license": "MIT",
4
- "version": "3.129.0",
4
+ "version": "3.129.2",
5
5
  "author": "7365admin1",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -0,0 +1,175 @@
1
+ // Saving a role with the inventory Quick-set presets through the real route
2
+ // (`PATCH /api/roles/permissions/id/:id`), not the database.
3
+ //
4
+ // The 2026-09-28 inventory keys (record-usage, issue-receive-stock,
5
+ // view-reports) are new strings under the SAME module, `inventory`. A grant of
6
+ // the Inventory module - on the organisation, the site, or the owner role
7
+ // Seven365 chose on Invite Client - must cover every key of that module, so a
8
+ // role that held the old four keys can be moved to a preset. A module that was
9
+ // never granted is refused with 403 and a message naming it.
10
+ //
11
+ // In-process MongoDB, loopback Redis and a loopback mail sink only (harness.mjs).
12
+ // Run with: yarn test:e2e
13
+
14
+ import { after, before, describe, it } from "node:test";
15
+ import assert from "node:assert/strict";
16
+ import { ObjectId } from "mongodb";
17
+
18
+ import { startHarness } from "./harness.mjs";
19
+
20
+ const PASSWORD = "InvPresets-Passw0rd!";
21
+ const STAFF = "staff-invp@e2e.example.com";
22
+ const OWNER = "owner-invp@e2e.example.com";
23
+
24
+ const OLD = [
25
+ "inventory:view-inventory",
26
+ "inventory:request-stock",
27
+ "inventory:approve-requests",
28
+ "inventory:manage-inventory",
29
+ ];
30
+ const PRESETS = {
31
+ staff: ["inventory:view-inventory", "inventory:request-stock", "inventory:record-usage"],
32
+ supervisor: ["inventory:view-inventory", "inventory:approve-requests", "inventory:view-reports"],
33
+ storekeeper: ["inventory:view-inventory", "inventory:approve-requests", "inventory:issue-receive-stock"],
34
+ admin: [
35
+ "inventory:view-inventory", "inventory:request-stock", "inventory:record-usage",
36
+ "inventory:approve-requests", "inventory:issue-receive-stock", "inventory:view-reports",
37
+ "inventory:manage-inventory",
38
+ ],
39
+ };
40
+ const OTHER = ["visitor-mgmt:view-visitors", "members:view-members"];
41
+
42
+ describe("inventory presets save through the role route", { concurrency: 1 }, () => {
43
+ let h;
44
+ const id = {};
45
+ const sid = {};
46
+
47
+ before(async () => {
48
+ h = await startHarness();
49
+ Object.assign(id, await seed(h));
50
+ sid.staff = await h.login(STAFF, PASSWORD);
51
+ sid.owner = await h.login(OWNER, PASSWORD);
52
+ }, { timeout: 300000 });
53
+
54
+ after(async () => {
55
+ if (h) await h.stop();
56
+ });
57
+
58
+ const save = (who, roleId, permissions) =>
59
+ h.api(`/roles/permissions/id/${roleId}`, { method: "PATCH", sid: sid[who], body: { permissions } });
60
+
61
+ async function freshRole(org, site) {
62
+ const _id = new ObjectId();
63
+ await h.db.collection("roles").insertOne({
64
+ _id, name: `R-${_id}`, org, ...(site ? { site } : {}), type: "security_agency",
65
+ permissions: [...OTHER, ...OLD], status: "active",
66
+ });
67
+ return _id;
68
+ }
69
+
70
+ for (const [name, keys] of Object.entries(PRESETS)) {
71
+ it(`Seven365 staff saves the ${name} preset where Inventory is granted`, async () => {
72
+ const role = await freshRole(id.granted);
73
+ const res = await save("staff", role, [...OTHER, ...keys]);
74
+ assert.equal(res.status, 200, JSON.stringify(res.body));
75
+ const stored = await h.db.collection("roles").findOne({ _id: role });
76
+ assert.deepEqual(stored.permissions, [...OTHER, ...keys]);
77
+ });
78
+
79
+ it(`the client owner saves the ${name} preset under an owner grant holding the old inventory keys`, async () => {
80
+ const role = await freshRole(id.granted);
81
+ const res = await save("owner", role, [...OTHER, ...keys]);
82
+ assert.equal(res.status, 200, JSON.stringify(res.body));
83
+ });
84
+
85
+ it(`the ${name} preset saves on a site-scoped role at a site that offers Inventory`, async () => {
86
+ const role = await freshRole(id.granted, id.site);
87
+ const res = await save("owner", role, [...OTHER, ...keys]);
88
+ assert.equal(res.status, 200, JSON.stringify(res.body));
89
+ });
90
+ }
91
+
92
+ it("the old four keys are still accepted", async () => {
93
+ const role = await freshRole(id.granted);
94
+ const res = await save("owner", role, [...OTHER, "inventory:view-inventory"]);
95
+ assert.equal(res.status, 200, JSON.stringify(res.body));
96
+ const back = await save("owner", role, [...OTHER, ...OLD]);
97
+ assert.equal(back.status, 200, JSON.stringify(back.body));
98
+ });
99
+
100
+ it("a role that already holds inventory moves to a preset where the module list predates Inventory", async () => {
101
+ const role = await freshRole(id.notGranted);
102
+ const res = await save("staff", role, [...OTHER, ...PRESETS.staff]);
103
+ assert.equal(res.status, 200, JSON.stringify(res.body));
104
+ });
105
+
106
+ it("a preset is refused with 403 and a message where Inventory was never granted", async () => {
107
+ const role = new ObjectId();
108
+ await h.db.collection("roles").insertOne({
109
+ _id: role, name: "No inventory", org: id.notGranted, type: "security_agency",
110
+ permissions: [...OTHER], status: "active",
111
+ });
112
+ const res = await save("staff", role, [...OTHER, ...PRESETS.staff]);
113
+ assert.equal(res.status, 403, JSON.stringify(res.body));
114
+ assert.match(res.body.message, /inventory:view-inventory/);
115
+ assert.match(res.body.message, /has not been given/);
116
+ });
117
+
118
+ it("the owner grant still refuses a module Seven365 did not give", async () => {
119
+ const role = await freshRole(id.granted);
120
+ const res = await save("owner", role, [...OTHER, ...OLD, "vehicle-mgmt:view-vehicles"]);
121
+ assert.equal(res.status, 403, JSON.stringify(res.body));
122
+ assert.match(res.body.message, /vehicle-mgmt:view-vehicles/);
123
+ });
124
+
125
+ it("a caller with no reach into the organisation is still refused", async () => {
126
+ const role = new ObjectId();
127
+ await h.db.collection("roles").insertOne({
128
+ _id: role, name: "Elsewhere", org: id.notGranted, type: "security_agency",
129
+ permissions: [...OTHER], status: "active",
130
+ });
131
+ const res = await save("owner", role, [...OTHER]);
132
+ assert.ok([401, 403].includes(res.status), `got ${res.status}`);
133
+ const stored = await h.db.collection("roles").findOne({ _id: role });
134
+ assert.deepEqual(stored.permissions, OTHER);
135
+ });
136
+ });
137
+
138
+ async function seed(h) {
139
+ const now = new Date().toISOString();
140
+ const granted = new ObjectId();
141
+ const notGranted = new ObjectId();
142
+ const site = new ObjectId();
143
+ const staffRole = new ObjectId();
144
+ const ownerRole = new ObjectId();
145
+
146
+ await h.db.collection("organizations").insertMany([
147
+ // modules chosen on Invite Client; owner role = the permissions Seven365 chose
148
+ { _id: granted, name: "Granted Co", email: "g@e2e.example.com", type: "org", nature: "security_agency",
149
+ status: "active", createdAt: now, modules: ["visitor-mgmt", "members", "inventory"], invitedRole: ownerRole },
150
+ { _id: notGranted, name: "No Inventory Co", email: "n@e2e.example.com", type: "org", nature: "security_agency",
151
+ status: "active", createdAt: now, modules: ["visitor-mgmt", "members"] },
152
+ ]);
153
+ await h.db.collection("sites").insertOne({
154
+ _id: site, name: "Granted Site", orgId: granted, status: "active", createdAt: now,
155
+ metadata: { modules: ["visitor-mgmt", "members", "inventory"] },
156
+ });
157
+ await h.db.collection("roles").insertMany([
158
+ { _id: staffRole, name: "Super Admin", type: "admin", default: true, permissions: [], status: "active" },
159
+ // the owner grant as it stood before 2026-09-28: the old four inventory keys
160
+ { _id: ownerRole, name: "owner", org: granted, type: "security_agency", default: true,
161
+ permissions: [...OTHER, ...OLD], status: "active" },
162
+ ]);
163
+
164
+ const users = await h.db.collection("users").insertMany(
165
+ [STAFF, OWNER].map((email) => ({ email, name: email.split("@")[0], status: "active", createdAt: now })),
166
+ );
167
+ const hashed = await h.hashPassword(PASSWORD);
168
+ await h.db.collection("users").updateMany({ email: { $in: [STAFF, OWNER] } }, { $set: { password: hashed } });
169
+
170
+ await h.db.collection("members").insertMany([
171
+ { user: users.insertedIds[0], type: "admin", role: staffRole, status: "active" },
172
+ { user: users.insertedIds[1], org: granted, type: "security_agency", role: ownerRole, status: "active" },
173
+ ]);
174
+ return { granted, notGranted, site };
175
+ }
@@ -127,7 +127,7 @@ describe("a shared client template can be restored and nothing else", { concurre
127
127
  record("2. THE OUTAGE: a console save of the 34 strings is still refused", async () => {
128
128
  const res = await patchPermissions(id.template, CONSOLE_SAVE, staffSid);
129
129
 
130
- assert.equal(res.status, 401, JSON.stringify(res.body));
130
+ assert.equal(res.status, 403, JSON.stringify(res.body));
131
131
  assert.match(res.body?.message ?? "", REFUSAL);
132
132
  assert.deepEqual(
133
133
  await permissionsOf(id.template),
@@ -139,7 +139,7 @@ describe("a shared client template can be restored and nothing else", { concurre
139
139
  record("3. NARROWING: a strict subset of what it holds is refused", async () => {
140
140
  const res = await patchPermissions(id.template, CONSOLE_SAVE.slice(0, 10), staffSid);
141
141
 
142
- assert.equal(res.status, 401, JSON.stringify(res.body));
142
+ assert.equal(res.status, 403, JSON.stringify(res.body));
143
143
  assert.match(res.body?.message ?? "", REFUSAL);
144
144
  assert.deepEqual(await permissionsOf(id.template), CONSOLE_SAVE);
145
145
  });
@@ -151,7 +151,7 @@ describe("a shared client template can be restored and nothing else", { concurre
151
151
  staffSid,
152
152
  );
153
153
 
154
- assert.equal(res.status, 401, JSON.stringify(res.body));
154
+ assert.equal(res.status, 403, JSON.stringify(res.body));
155
155
  assert.deepEqual(await permissionsOf(id.template), CONSOLE_SAVE);
156
156
  });
157
157
 
@@ -174,7 +174,7 @@ describe("a shared client template can be restored and nothing else", { concurre
174
174
  *
175
175
  * `PATCH /roles/permissions/id/:id` refuses an empty list in the repository
176
176
  * (`role.repo.ts:481` — "Permissions cannot be empty."), and has since long
177
- * before any of this. It is a 400, not the 401 the template guard raises,
177
+ * before any of this. It is a 400, not the 403 the template guard raises,
178
178
  * so the guard did let it through; the storage layer is what says no.
179
179
  *
180
180
  * Deliberately NOT changed. Loosening it would let an empty list — which
@@ -213,7 +213,7 @@ describe("a shared client template can be restored and nothing else", { concurre
213
213
  sid: staffSid,
214
214
  body: { name: "Renamed Template", permissions: CONSOLE_SAVE },
215
215
  });
216
- assert.equal(refused.status, 401, JSON.stringify(refused.body));
216
+ assert.equal(refused.status, 403, JSON.stringify(refused.body));
217
217
  assert.match(refused.body?.message ?? "", REFUSAL);
218
218
 
219
219
  const restored = await h.api("/roles/id/" + id.thirdTemplate.toString(), {
@@ -254,13 +254,13 @@ describe("a shared client template can be restored and nothing else", { concurre
254
254
  staffSid,
255
255
  );
256
256
 
257
- assert.equal(res.status, 401, JSON.stringify(res.body));
257
+ assert.equal(res.status, 403, JSON.stringify(res.body));
258
258
  assert.match(res.body?.message ?? "", /Seven365 console permissions/);
259
259
  assert.deepEqual(await permissionsOf(id.clientRole), ["members:view-members"]);
260
260
  });
261
261
 
262
262
  record("11. POSITIVE CONTROL: the run really did write, and really did refuse", async () => {
263
- // If every case above 401'd, cases 5-7 and 9 would be vacuous; if every
263
+ // If every case above 403'd, cases 5-7 and 9 would be vacuous; if every
264
264
  // case 200'd, cases 2-4 and 8 would be. Count both.
265
265
  const verdicts = result.map(([v]) => v);
266
266
  assert.equal(verdicts.includes("FAIL"), false, JSON.stringify(result));
@@ -19,6 +19,9 @@ const blockB = new ObjectId();
19
19
  const level03 = new ObjectId();
20
20
  const unit06 = new ObjectId();
21
21
  const now = new Date("2026-09-28T08:00:00Z");
22
+ const passTpl = new ObjectId();
23
+ const passKey = new ObjectId();
24
+ const plainKey = new ObjectId();
22
25
 
23
26
  test.before(async () => {
24
27
  mongo = await MongoMemoryServer.create();
@@ -32,6 +35,12 @@ test.before(async () => {
32
35
  await db.collection("building-levels").insertOne({ _id: level03, site: siteA, name: "03" });
33
36
  await db.collection("building-units").insertOne({ _id: unit06, site: siteA, name: "06", level: level03, building: blockA });
34
37
 
38
+ await db.collection("qr-code-templates").insertOne({ _id: passTpl, prefixPass: "VP-C" });
39
+ await db.collection("keys").insertMany([
40
+ { _id: passKey, name: "02", template: passTpl },
41
+ { _id: plainKey, name: "X02" },
42
+ ]);
43
+
35
44
  const v = (o) => ({ status: "registered", type: "contractor", checkIn: now, expectedCheckIn: now, createdAt: now, ...o });
36
45
  await db.collection("visitor.transactions").insertMany([
37
46
  v({ site: siteA, name: "Dion", contact: "+6591177963", plateNumber: "SLM1189Y", company: "Asia", nric: "S1234963A", block: blockA, level: level03, unit: unit06 }),
@@ -40,6 +49,8 @@ test.before(async () => {
40
49
  v({ site: siteA, name: "Dion Pending", status: "pending" }),
41
50
  // Another customer's site: same name, same plate, same block name.
42
51
  v({ site: siteB, name: "Dion", plateNumber: "SLM1189Y", block: blockB }),
52
+ v({ site: siteA, name: "Pass Holder", visitorPass: [{ keyId: passKey }] }),
53
+ v({ site: siteA, name: "Key Holder", passKeys: [{ keyId: plainKey }] }),
43
54
  ...Array.from({ length: 12 }, (_, i) => v({ site: siteA, name: `Bulk ${i}`, company: "Bulkco" })),
44
55
  ]);
45
56
 
@@ -105,3 +116,19 @@ test("paging is bounded and counts describe the search", async () => {
105
116
  assert.equal(p2.items.length, 2);
106
117
  assert.equal(p1.pages, 2);
107
118
  });
119
+
120
+ test("pass / key by template prefix + name, across the join, any case", async () => {
121
+ // "VP-C" + "02": whole, spanning the prefix/name boundary, prefix only, name only.
122
+ assert.deepEqual(names(await repo.getAll({ ...base, search: "vp-c02" })), ["Pass Holder"]);
123
+ assert.deepEqual(names(await repo.getAll({ ...base, search: "C02" })), ["Pass Holder"]);
124
+ assert.deepEqual(names(await repo.getAll({ ...base, search: "vp-c" })), ["Pass Holder"]);
125
+ assert.deepEqual(names(await repo.getAll({ ...base, search: "02" })), ["Key Holder", "Pass Holder"]);
126
+ assert.deepEqual(names(await repo.getAll({ ...base, search: "x02" })), ["Key Holder"]);
127
+ assert.deepEqual(names(await repo.getAll({ ...base, search: "C03" })), []);
128
+ });
129
+
130
+ test("block / level / unit names and the combined form", async () => {
131
+ assert.deepEqual(names(await repo.getAll({ ...base, search: "3/03" })), ["Dion"]);
132
+ assert.deepEqual(names(await repo.getAll({ ...base, search: "3/3" })), []);
133
+ assert.deepEqual(names(await repo.getAll({ ...base, search: "4/03/06" })), []);
134
+ });
@@ -283,3 +283,24 @@ test("roles stay inside the owner grant Seven365 chose", async () => {
283
283
  "a string already held is never refused",
284
284
  );
285
285
  });
286
+
287
+ test("an owner grant from before the inventory split still covers the keys it granted", async () => {
288
+ const { boundToOwnerGrant } = await import("./.build/utils/org-modules.util.mjs");
289
+ const full = ["inventory:view-inventory", "inventory:request-stock", "inventory:approve-requests", "inventory:manage-inventory"];
290
+ const presets = [
291
+ ["inventory:view-inventory", "inventory:request-stock", "inventory:record-usage"],
292
+ ["inventory:view-inventory", "inventory:approve-requests", "inventory:view-reports"],
293
+ ["inventory:view-inventory", "inventory:approve-requests", "inventory:issue-receive-stock"],
294
+ ["inventory:some-future-key"],
295
+ ];
296
+ for (const p of presets) assert.deepEqual(boundToOwnerGrant(full, p).refused, [], p.join());
297
+
298
+ // without full access, only what the old keys did
299
+ const staffOnly = ["inventory:view-inventory", "inventory:request-stock"];
300
+ assert.deepEqual(boundToOwnerGrant(staffOnly, presets[0]).refused, []);
301
+ assert.deepEqual(
302
+ boundToOwnerGrant(staffOnly, ["inventory:issue-receive-stock", "inventory:manage-inventory"]).refused,
303
+ ["inventory:issue-receive-stock", "inventory:manage-inventory"],
304
+ );
305
+ assert.deepEqual(boundToOwnerGrant(full, ["vehicle-mgmt:view-vehicles"]).refused, ["vehicle-mgmt:view-vehicles"], "another module is not covered");
306
+ });
@@ -243,6 +243,19 @@ test("ONLY what the write ADDS is tested - a role keeps what it already holds",
243
243
  );
244
244
  });
245
245
 
246
+ test("a new key of a module the role already holds is not a new module", () => {
247
+ // The 2026-09-28 inventory split: a role granted inventory before the
248
+ // organisation's module list existed can move to a preset.
249
+ const previous = ["visitor-mgmt:view-visitors", "inventory:view-inventory", "inventory:request-stock"];
250
+ const staffPreset = ["visitor-mgmt:view-visitors", "inventory:view-inventory", "inventory:request-stock", "inventory:record-usage"];
251
+ assert.deepEqual(permissionsOutsideModules(["visitor-mgmt"], staffPreset, previous), []);
252
+ // CONTROL: a role with no inventory key still cannot be given one.
253
+ assert.deepEqual(
254
+ permissionsOutsideModules(["visitor-mgmt"], ["visitor-mgmt:view-visitors", "inventory:record-usage"], ["visitor-mgmt:view-visitors"]),
255
+ ["inventory:record-usage"],
256
+ );
257
+ });
258
+
246
259
  test("both spellings of EVERYTHING are never refused", () => {
247
260
  // An empty list and `["*"]` both mean everything and cannot name a module.
248
261
  assert.deepEqual(permissionsOutsideModules(["visitor-mgmt"], []), []);
@@ -118,5 +118,8 @@ test("repository wiring: the search box goes through the util, after the site/da
118
118
  assert.ok(!/name: \{ \$regex: search/.test(repo), "raw name-only regex is gone");
119
119
  assert.match(repo, /const text = normaliseVisitorSearch\(search\);/);
120
120
  assert.match(repo, /\.\.\.visitorRowSearchOr\(text\),/);
121
- assert.match(repo, /\{ \$match: query \},\s*\.\.\.buildSearchStages\(search\),/);
121
+ assert.match(repo, /\{ \$match: query \},\s*\.\.\.\(await buildSearchStages\(search, searchSiteIds\)\),/);
122
+ // No per-row joins in the search: block/level/unit and keys are resolved to ids first.
123
+ const stages = repo.slice(repo.indexOf("async function buildSearchStages"), repo.indexOf("async function createTextIndex"));
124
+ assert.ok(!stages.includes("$lookup"), "search stages must not $lookup per visitor row");
122
125
  });