@7365admin1/core 3.125.0 → 3.126.0
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/CHANGELOG.md +12 -0
- package/dist/index.d.ts +101 -2
- package/dist/index.js +655 -58
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +710 -117
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/test/e2e/harness.mjs +2 -0
- package/test/e2e/service-provider-subcontract.e2e.test.mjs +389 -0
- package/test/e2e/visitor-search.e2e.test.mjs +107 -0
- package/test/invite-org-scope.test.mjs +11 -7
- package/test/service-provider-subcontract.test.mjs +125 -0
- package/test/visitor-search.test.mjs +122 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@7365admin1/core",
|
|
3
3
|
"license": "MIT",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.126.0",
|
|
5
5
|
"author": "7365admin1",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/index.mjs",
|
|
@@ -13,9 +13,9 @@
|
|
|
13
13
|
"build": "tsup src/index.ts --format cjs,esm --dts && shx cp -r src/public dist && shx cp -r src/utils/handlebars dist",
|
|
14
14
|
"release": "yarn run build && changeset publish",
|
|
15
15
|
"lint": "tsc",
|
|
16
|
-
"test": "tsup src/config.ts src/utils/building-map.util.ts src/utils/robot-cloud.util.ts src/utils/robot-adapter.util.ts src/utils/png-encoder.util.ts src/services/demo.adapter.ts src/services/ounitech.adapter.ts src/services/robot-cloud.service.ts src/services/robot-fleet.service.ts src/services/padbot.adapter.ts src/utils/padbot-sign.util.ts src/utils/padbot-mqtt.util.ts src/services/open-rmf.adapter.ts src/utils/robot-credentials.util.ts src/utils/rmf-building.util.ts src/utils/door-contract.util.ts src/utils/robot-fault.util.ts src/utils/robot-tracking.util.ts src/utils/robot-apps.util.ts src/utils/robot-permission.util.ts src/utils/robot-network.util.ts src/utils/lift-contract.util.ts src/models/robot-building-map.model.ts src/utils/anpr-revoke.util.ts src/utils/anpr-plate.util.ts src/utils/vehicle-request.util.ts src/utils/camera-view.util.ts src/utils/camera-bulk-import.util.ts src/utils/dashboard-metrics.util.ts src/utils/camera-capability.util.ts src/utils/camera-playback.util.ts src/services/camera-device-http.service.ts src/utils/camera-alert.util.ts src/utils/dahua-protocol.util.ts src/utils/site-name.util.ts src/utils/site-owner-edit.util.ts src/utils/service-provider-invite.util.ts src/utils/notification-category.util.ts src/utils/notification-kind.util.ts src/utils/notification-email.util.ts src/models/notification.model.ts src/utils/hid-card.util.ts src/utils/hid-json.util.ts src/utils/hid-access.util.ts src/models/customer-site.model.ts src/models/site-camera.model.ts src/models/site.model.ts src/models/member.model.ts src/models/subscription.model.ts src/utils/session-id.util.ts src/utils/occurrence-query.util.ts src/utils/occurrence-book-create.util.ts src/utils/update-result.util.ts src/utils/hid-amico-authz.util.ts src/utils/promo-code-currency.util.ts src/utils/console-audit.util.ts src/utils/org-suspension.util.ts src/utils/session-revoke.util.ts src/models/hid-amico.model.ts src/models/patrol-email.model.ts src/utils/patrol-email-pdf.util.ts src/utils/visitor-status.util.ts src/utils/self-service-email.util.ts src/utils/hid-secret.util.ts src/models/visitor-invite.model.ts src/models/guard-invite.model.ts src/utils/site-timezone.util.ts src/utils/bulletin-service-apps.util.ts src/models/bulletin-board.model.ts src/utils/vehicle-import.util.ts src/utils/expired-vehicle-sweep.util.ts src/models/vehicle.model.ts src/controllers/chat.controller.ts src/utils/file-owner.util.ts src/utils/console-permission.util.ts src/utils/role-scope.util.ts src/utils/org-default-roles.util.ts src/utils/org-modules.util.ts src/utils/site-modules.util.ts src/utils/resident-app-modules.util.ts src/utils/resident-default-role.util.ts src/utils/invite-role-scope.util.ts src/models/personal-emergency-chain.model.ts src/utils/self-signup-files.util.ts src/models/person.model.ts src/utils/module-gate.util.ts src/utils/member-lookup.util.ts src/utils/notification-access.util.ts src/utils/module-access.util.ts src/services/module-gate.service.ts src/controllers/site.controller.ts src/controllers/organization.controller.ts src/controllers/member.controller.ts src/utils/org-owner-repair.util.ts src/utils/socket-session.util.ts src/utils/people-create-gate.util.ts src/utils/service-app-type.util.ts src/utils/customer-site-list.util.ts src/utils/org-search.util.ts src/utils/invite-origin.util.ts src/utils/member-role-assign.util.ts src/utils/cross-site-role.util.ts --format esm --out-dir test/.build --no-dts --silent && node --test test/*.test.mjs",
|
|
16
|
+
"test": "tsup src/config.ts src/utils/building-map.util.ts src/utils/robot-cloud.util.ts src/utils/robot-adapter.util.ts src/utils/png-encoder.util.ts src/services/demo.adapter.ts src/services/ounitech.adapter.ts src/services/robot-cloud.service.ts src/services/robot-fleet.service.ts src/services/padbot.adapter.ts src/utils/padbot-sign.util.ts src/utils/padbot-mqtt.util.ts src/services/open-rmf.adapter.ts src/utils/robot-credentials.util.ts src/utils/rmf-building.util.ts src/utils/door-contract.util.ts src/utils/robot-fault.util.ts src/utils/robot-tracking.util.ts src/utils/robot-apps.util.ts src/utils/robot-permission.util.ts src/utils/robot-network.util.ts src/utils/lift-contract.util.ts src/models/robot-building-map.model.ts src/utils/anpr-revoke.util.ts src/utils/anpr-plate.util.ts src/utils/vehicle-request.util.ts src/utils/camera-view.util.ts src/utils/camera-bulk-import.util.ts src/utils/dashboard-metrics.util.ts src/utils/camera-capability.util.ts src/utils/camera-playback.util.ts src/services/camera-device-http.service.ts src/utils/camera-alert.util.ts src/utils/dahua-protocol.util.ts src/utils/site-name.util.ts src/utils/site-owner-edit.util.ts src/utils/service-provider-invite.util.ts src/utils/notification-category.util.ts src/utils/notification-kind.util.ts src/utils/notification-email.util.ts src/models/notification.model.ts src/utils/hid-card.util.ts src/utils/hid-json.util.ts src/utils/hid-access.util.ts src/models/customer-site.model.ts src/models/site-camera.model.ts src/models/site.model.ts src/models/member.model.ts src/models/subscription.model.ts src/utils/session-id.util.ts src/utils/occurrence-query.util.ts src/utils/occurrence-book-create.util.ts src/utils/update-result.util.ts src/utils/hid-amico-authz.util.ts src/utils/promo-code-currency.util.ts src/utils/console-audit.util.ts src/utils/org-suspension.util.ts src/utils/session-revoke.util.ts src/models/hid-amico.model.ts src/models/patrol-email.model.ts src/utils/patrol-email-pdf.util.ts src/utils/visitor-status.util.ts src/utils/self-service-email.util.ts src/utils/hid-secret.util.ts src/models/visitor-invite.model.ts src/models/guard-invite.model.ts src/utils/site-timezone.util.ts src/utils/bulletin-service-apps.util.ts src/models/bulletin-board.model.ts src/utils/vehicle-import.util.ts src/utils/expired-vehicle-sweep.util.ts src/models/vehicle.model.ts src/controllers/chat.controller.ts src/utils/file-owner.util.ts src/utils/console-permission.util.ts src/utils/role-scope.util.ts src/utils/org-default-roles.util.ts src/utils/org-modules.util.ts src/utils/site-modules.util.ts src/utils/resident-app-modules.util.ts src/utils/resident-default-role.util.ts src/utils/invite-role-scope.util.ts src/models/personal-emergency-chain.model.ts src/utils/self-signup-files.util.ts src/models/person.model.ts src/utils/module-gate.util.ts src/utils/member-lookup.util.ts src/utils/notification-access.util.ts src/utils/module-access.util.ts src/services/module-gate.service.ts src/controllers/site.controller.ts src/controllers/organization.controller.ts src/controllers/member.controller.ts src/utils/org-owner-repair.util.ts src/utils/socket-session.util.ts src/utils/people-create-gate.util.ts src/utils/service-app-type.util.ts src/utils/customer-site-list.util.ts src/utils/org-search.util.ts src/utils/invite-origin.util.ts src/utils/member-role-assign.util.ts src/utils/cross-site-role.util.ts src/utils/visitor-search.util.ts --format esm --out-dir test/.build --no-dts --silent && node --test test/*.test.mjs",
|
|
17
17
|
"test:camera": "yarn test",
|
|
18
|
-
"test:e2e": "yarn build && node --test --test-concurrency=1 --test-timeout=600000 \"test/e2e/*.test.mjs\""
|
|
18
|
+
"test:e2e": "yarn build && tsup src/repositories/visitor-transaction.repo.ts --format esm --out-dir test/.build-e2e/repositories --no-dts --silent && node --test --test-concurrency=1 --test-timeout=600000 \"test/e2e/*.test.mjs\""
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@changesets/cli": "^2.26.0",
|
package/test/e2e/harness.mjs
CHANGED
|
@@ -530,9 +530,11 @@ function buildApp(nsu, core) {
|
|
|
530
530
|
updateStatusById,
|
|
531
531
|
createServiceProvider,
|
|
532
532
|
getServiceProviders,
|
|
533
|
+
getSiteProviderMembers,
|
|
533
534
|
} = core.useServiceProviderController();
|
|
534
535
|
|
|
535
536
|
serviceProviders.post("/id/:id", requireAuth, createServiceProvider);
|
|
537
|
+
serviceProviders.get("/site-members", requireAuth, getSiteProviderMembers);
|
|
536
538
|
|
|
537
539
|
serviceProviders.get("/", requireAuth, getServiceProviders);
|
|
538
540
|
serviceProviders.get("/:id", requireAuth, getServiceProviderById);
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
// End-to-end proof: service providers inviting other service providers, and
|
|
2
|
+
// the property manager seeing every provider person with access to its site
|
|
3
|
+
// (owner requests 2026-09-28).
|
|
4
|
+
//
|
|
5
|
+
// 1. a provider engaged on a site (Aigle Security at One Pearl Bank) invites
|
|
6
|
+
// a subcontractor (Sparkle Cleaning) for THAT site;
|
|
7
|
+
// 2. it waits for the Seven365 super admin, who sees who sent it and whose
|
|
8
|
+
// site it is; staff are told, the subcontractor is told nothing;
|
|
9
|
+
// 3. approved -> the subcontractor accepts -> the site joins its list, owned
|
|
10
|
+
// by the property manager (never by the inviting provider);
|
|
11
|
+
// 4. the property manager sees Sparkle "via Aigle Security" and, under
|
|
12
|
+
// Members > Service Providers, every provider person pinned to the site;
|
|
13
|
+
// 5. refusals: a site the provider is not on, a guard (not owner/admin), an
|
|
14
|
+
// organisation the caller is not in, another organisation reading the
|
|
15
|
+
// people list; rejection and expiry behave as for a property manager.
|
|
16
|
+
//
|
|
17
|
+
// Real Express stack, real controllers/services/repositories, in-process
|
|
18
|
+
// MongoDB replica set; the mailer points at a loopback sink.
|
|
19
|
+
//
|
|
20
|
+
// Run with: yarn test:e2e
|
|
21
|
+
|
|
22
|
+
import { after, before, describe, it } from "node:test";
|
|
23
|
+
import assert from "node:assert/strict";
|
|
24
|
+
import { ObjectId } from "mongodb";
|
|
25
|
+
|
|
26
|
+
import { startHarness } from "./harness.mjs";
|
|
27
|
+
|
|
28
|
+
const PW = "Sub-Passw0rd!";
|
|
29
|
+
const PM_EMAIL = "pm@sub.example.com";
|
|
30
|
+
const AIGLE_EMAIL = "owner@aigle.example.com";
|
|
31
|
+
const GUARD_EMAIL = "guard@aigle.example.com";
|
|
32
|
+
const CLEAN_EMAIL = "owner@sparkle.example.com";
|
|
33
|
+
const CLEANER_EMAIL = "cleaner@sparkle.example.com";
|
|
34
|
+
const ADMIN_EMAIL = "admin@sub.example.com";
|
|
35
|
+
const OUTSIDER_EMAIL = "someone@rival.example.com";
|
|
36
|
+
const NEWCO_EMAIL = "hello@newco.example.com";
|
|
37
|
+
const LATE_EMAIL = "owner@late.example.com";
|
|
38
|
+
|
|
39
|
+
describe("service provider invites a service provider, end to end", { concurrency: 1 }, () => {
|
|
40
|
+
let h;
|
|
41
|
+
const id = {};
|
|
42
|
+
const sid = {};
|
|
43
|
+
const inv = {};
|
|
44
|
+
|
|
45
|
+
before(async () => {
|
|
46
|
+
h = await startHarness();
|
|
47
|
+
Object.assign(id, await seed(h));
|
|
48
|
+
for (const [k, e] of Object.entries({
|
|
49
|
+
pm: PM_EMAIL,
|
|
50
|
+
aigle: AIGLE_EMAIL,
|
|
51
|
+
guard: GUARD_EMAIL,
|
|
52
|
+
clean: CLEAN_EMAIL,
|
|
53
|
+
admin: ADMIN_EMAIL,
|
|
54
|
+
outsider: OUTSIDER_EMAIL,
|
|
55
|
+
late: LATE_EMAIL,
|
|
56
|
+
})) {
|
|
57
|
+
sid[k] = await h.login(e, PW);
|
|
58
|
+
}
|
|
59
|
+
}, { timeout: 300000 });
|
|
60
|
+
|
|
61
|
+
after(async () => {
|
|
62
|
+
if (h) await h.stop();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
function invite(who, { email, org, site, app = "cleaning_services" }) {
|
|
66
|
+
return h.api("/service-providers/invite", {
|
|
67
|
+
method: "POST",
|
|
68
|
+
sid: sid[who],
|
|
69
|
+
body: {
|
|
70
|
+
email,
|
|
71
|
+
orgId: org.toString(),
|
|
72
|
+
siteId: site.toString(),
|
|
73
|
+
siteName: "One Pearl Bank",
|
|
74
|
+
inviteType: "organization-invite",
|
|
75
|
+
app,
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const row = (email, site) =>
|
|
81
|
+
h.db.collection("verifications").findOne({ email, "metadata.siteId": site });
|
|
82
|
+
|
|
83
|
+
it("1. Aigle (engaged at One Pearl Bank) invites Sparkle - held for Seven365, Sparkle told nothing", async () => {
|
|
84
|
+
const mails = h.mails.length;
|
|
85
|
+
const res = await invite("aigle", { email: CLEAN_EMAIL, org: id.aigleOrg, site: id.siteA });
|
|
86
|
+
assert.equal(res.status, 200, JSON.stringify(res.body));
|
|
87
|
+
|
|
88
|
+
const written = await row(CLEAN_EMAIL, id.siteA);
|
|
89
|
+
inv.sparkle = written._id.toString();
|
|
90
|
+
assert.equal(written.status, "awaiting-approval");
|
|
91
|
+
assert.equal(written.metadata.org.toString(), id.aigleOrg.toString());
|
|
92
|
+
assert.equal(written.metadata.invitedByPropertyManagement, false);
|
|
93
|
+
assert.equal(h.mails.length, mails, "no email before approval");
|
|
94
|
+
assert.equal(
|
|
95
|
+
await h.db.collection("notifications").countDocuments({ userId: id.cleanUser }),
|
|
96
|
+
0,
|
|
97
|
+
"no in-app message to the subcontractor before approval",
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("2. Seven365 staff are told something is waiting", async () => {
|
|
102
|
+
// the send is best effort and not awaited by the request
|
|
103
|
+
let n = 0;
|
|
104
|
+
for (let i = 0; i < 20 && !n; i++) {
|
|
105
|
+
n = await h.db.collection("notifications").countDocuments({
|
|
106
|
+
userId: { $in: [id.adminUser, id.adminUser.toString()] },
|
|
107
|
+
"metadata.kind": "spApproval",
|
|
108
|
+
}).catch(() => 0);
|
|
109
|
+
if (!n) {
|
|
110
|
+
n = await h.db.collection("notifications").countDocuments({
|
|
111
|
+
userId: { $in: [id.adminUser, id.adminUser.toString()] },
|
|
112
|
+
title: /approve/i,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
if (!n) await new Promise((r) => setTimeout(r, 100));
|
|
116
|
+
}
|
|
117
|
+
assert.ok(n > 0, "the super admin has a notification about the waiting invitation");
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("3. a provider cannot invite for a site it does not work on", async () => {
|
|
121
|
+
const res = await invite("aigle", { email: NEWCO_EMAIL, org: id.aigleOrg, site: id.siteB });
|
|
122
|
+
assert.equal(res.status, 401, JSON.stringify(res.body));
|
|
123
|
+
assert.match(res.body.message, /sites your company works on/);
|
|
124
|
+
assert.equal(await row(NEWCO_EMAIL, id.siteB), null, "nothing written");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("4. a guard of the provider (not owner/admin) cannot invite", async () => {
|
|
128
|
+
const res = await invite("guard", { email: NEWCO_EMAIL, org: id.aigleOrg, site: id.siteA });
|
|
129
|
+
assert.equal(res.status, 401, JSON.stringify(res.body));
|
|
130
|
+
assert.match(res.body.message, /owner or an administrator/);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("5. a provider cannot invite in the property manager's name", async () => {
|
|
134
|
+
const res = await invite("aigle", { email: NEWCO_EMAIL, org: id.pmOrg, site: id.siteA });
|
|
135
|
+
assert.equal(res.status, 401, JSON.stringify(res.body));
|
|
136
|
+
assert.match(res.body.message, /your own organisation/);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("6. the approval queue says who sent it and whose site it is", async () => {
|
|
140
|
+
const res = await h.api("/service-provider-invitations/approvals?status=awaiting-approval", {
|
|
141
|
+
sid: sid.admin,
|
|
142
|
+
});
|
|
143
|
+
assert.equal(res.status, 200, JSON.stringify(res.body));
|
|
144
|
+
const item = res.body.items.find((i) => i._id.toString() === inv.sparkle);
|
|
145
|
+
assert.ok(item, "the invitation is in the queue");
|
|
146
|
+
assert.equal(item.inviterKind, "service-provider");
|
|
147
|
+
assert.equal(item.inviterOrgName, "Aigle Security");
|
|
148
|
+
assert.equal(item.siteOwnerName, "Northwind Property Management");
|
|
149
|
+
assert.equal(item.service, "cleaning_services");
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("7. approved -> Sparkle is told -> accepts -> the site is the property manager's, not Aigle's", async () => {
|
|
153
|
+
const mails = h.mails.length;
|
|
154
|
+
const ok = await h.api(`/service-provider-invitations/${inv.sparkle}/approve`, {
|
|
155
|
+
method: "PUT",
|
|
156
|
+
sid: sid.admin,
|
|
157
|
+
});
|
|
158
|
+
assert.equal(ok.status, 200, JSON.stringify(ok.body));
|
|
159
|
+
assert.ok(h.mails.length > mails, "the subcontractor is emailed on approval");
|
|
160
|
+
|
|
161
|
+
const acc = await h.api(`/service-provider-invitations/${inv.sparkle}/accept`, {
|
|
162
|
+
method: "PUT",
|
|
163
|
+
sid: sid.clean,
|
|
164
|
+
body: { org: id.cleanOrg.toString() },
|
|
165
|
+
});
|
|
166
|
+
assert.equal(acc.status, 200, JSON.stringify(acc.body));
|
|
167
|
+
|
|
168
|
+
const engagement = await h.db
|
|
169
|
+
.collection("customer.sites")
|
|
170
|
+
.findOne({ org: id.cleanOrg, site: id.siteA });
|
|
171
|
+
assert.ok(engagement, "the site is on Sparkle's list");
|
|
172
|
+
assert.equal(engagement.status, "active");
|
|
173
|
+
assert.equal(engagement.siteOrg.toString(), id.pmOrg.toString(), "owned by the client");
|
|
174
|
+
assert.equal(engagement.siteOrgName, "Northwind Property Management");
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("8. the property manager sees Sparkle on its Service Providers list, via Aigle", async () => {
|
|
178
|
+
const res = await h.api(
|
|
179
|
+
`/service-providers?siteId=${id.siteA}&status=active&include=engagements&limit=50`,
|
|
180
|
+
{ sid: sid.pm },
|
|
181
|
+
);
|
|
182
|
+
assert.equal(res.status, 200, JSON.stringify(res.body));
|
|
183
|
+
const names = res.body.items.map((i) => i.name).sort();
|
|
184
|
+
assert.deepEqual(names, ["Aigle Security", "Pestbusters", "Sparkle Cleaning"]);
|
|
185
|
+
const sparkle = res.body.items.find((i) => i.name === "Sparkle Cleaning");
|
|
186
|
+
assert.equal(sparkle.invitedVia, "Aigle Security");
|
|
187
|
+
const aigle = res.body.items.find((i) => i.name === "Aigle Security");
|
|
188
|
+
assert.equal(aigle.invitedVia, undefined, "the client's own provider has no via");
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("9. Aigle's own Service Providers page lists only its subcontractor", async () => {
|
|
192
|
+
const res = await h.api(
|
|
193
|
+
`/service-providers?siteId=${id.siteA}&status=active&include=engagements&limit=50`,
|
|
194
|
+
{ sid: sid.aigle },
|
|
195
|
+
);
|
|
196
|
+
assert.equal(res.status, 200, JSON.stringify(res.body));
|
|
197
|
+
assert.deepEqual(
|
|
198
|
+
res.body.items.map((i) => i.name),
|
|
199
|
+
["Sparkle Cleaning"],
|
|
200
|
+
"never the client's other contractors",
|
|
201
|
+
);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("10. Members > Service Providers: every provider person pinned to the site", async () => {
|
|
205
|
+
const res = await h.api(`/service-providers/site-members?siteId=${id.siteA}`, {
|
|
206
|
+
sid: sid.pm,
|
|
207
|
+
});
|
|
208
|
+
assert.equal(res.status, 200, JSON.stringify(res.body));
|
|
209
|
+
const people = res.body.items
|
|
210
|
+
.map((p) => `${p.name}|${p.company}|${p.type}|${p.role}|${p.invitedVia}`)
|
|
211
|
+
.sort();
|
|
212
|
+
assert.deepEqual(people, [
|
|
213
|
+
"Casey Cleaner|Sparkle Cleaning|cleaning_services|Cleaner|Aigle Security",
|
|
214
|
+
"Gus Guard|Aigle Security|security_agency|Guard|",
|
|
215
|
+
]);
|
|
216
|
+
const emails = res.body.items.map((p) => p.email).sort();
|
|
217
|
+
assert.deepEqual(emails, [CLEANER_EMAIL, GUARD_EMAIL]);
|
|
218
|
+
const counts = Object.fromEntries(res.body.companies.map((c) => [c.name, c.people]));
|
|
219
|
+
assert.deepEqual(counts, {
|
|
220
|
+
"Aigle Security": 1,
|
|
221
|
+
Pestbusters: 0,
|
|
222
|
+
"Sparkle Cleaning": 1,
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("10b. filters and the Excel export use the same rule", async () => {
|
|
227
|
+
const res = await h.api(`/service-providers/site-members?siteId=${id.siteA}`, { sid: sid.pm });
|
|
228
|
+
assert.deepEqual(
|
|
229
|
+
res.body.items.map((p) => p.typeLabel).sort(),
|
|
230
|
+
["Cleaning", "Security"],
|
|
231
|
+
);
|
|
232
|
+
const xlsx = await h.api(
|
|
233
|
+
`/service-providers/site-members?siteId=${id.siteA}&format=xlsx&type=security_agency`,
|
|
234
|
+
{ sid: sid.pm },
|
|
235
|
+
);
|
|
236
|
+
assert.equal(xlsx.status, 200);
|
|
237
|
+
assert.equal(String(xlsx.body).slice(0, 2), "PK", "an .xlsx (zip) file");
|
|
238
|
+
const bad = await h.api(`/service-providers/site-members?siteId=${id.siteA}&format=pdf`, {
|
|
239
|
+
sid: sid.pm,
|
|
240
|
+
});
|
|
241
|
+
assert.equal(bad.status, 400);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("11. a provider cannot read the site's provider people list; an outsider cannot either", async () => {
|
|
245
|
+
const asAigle = await h.api(`/service-providers/site-members?siteId=${id.siteA}`, {
|
|
246
|
+
sid: sid.aigle,
|
|
247
|
+
});
|
|
248
|
+
assert.equal(asAigle.status, 401, JSON.stringify(asAigle.body));
|
|
249
|
+
|
|
250
|
+
const asOutsider = await h.api(`/service-providers/site-members?siteId=${id.siteA}`, {
|
|
251
|
+
sid: sid.outsider,
|
|
252
|
+
});
|
|
253
|
+
assert.ok([401, 403, 404].includes(asOutsider.status), JSON.stringify(asOutsider.body));
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it("12. the property manager's invitation list never shows Aigle's invitations (isolation)", async () => {
|
|
257
|
+
const res = await h.api(
|
|
258
|
+
`/service-provider-invitations?org=${id.pmOrg}&siteId=${id.siteA}&status=awaiting-approval,pending,complete,rejected`,
|
|
259
|
+
{ sid: sid.pm },
|
|
260
|
+
);
|
|
261
|
+
assert.equal(res.status, 200, JSON.stringify(res.body));
|
|
262
|
+
assert.equal(
|
|
263
|
+
res.body.items.some((i) => i._id.toString() === inv.sparkle),
|
|
264
|
+
false,
|
|
265
|
+
);
|
|
266
|
+
const theirs = await h.api(
|
|
267
|
+
`/service-provider-invitations?org=${id.aigleOrg}&siteId=${id.siteA}&status=complete`,
|
|
268
|
+
{ sid: sid.aigle },
|
|
269
|
+
);
|
|
270
|
+
assert.equal(theirs.status, 200, JSON.stringify(theirs.body));
|
|
271
|
+
assert.equal(theirs.body.items.length, 1);
|
|
272
|
+
assert.equal(theirs.body.items[0].statusLabel, "Accepted");
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("13. rejected: Aigle sees the reason, the new company is told nothing", async () => {
|
|
276
|
+
const mails = h.mails.length;
|
|
277
|
+
const res = await invite("aigle", { email: NEWCO_EMAIL, org: id.aigleOrg, site: id.siteA });
|
|
278
|
+
assert.equal(res.status, 200, JSON.stringify(res.body));
|
|
279
|
+
const r = await row(NEWCO_EMAIL, id.siteA);
|
|
280
|
+
const rej = await h.api(`/service-provider-invitations/${r._id}/reject`, {
|
|
281
|
+
method: "PUT",
|
|
282
|
+
sid: sid.admin,
|
|
283
|
+
body: { reason: "Not contracted by the client." },
|
|
284
|
+
});
|
|
285
|
+
assert.equal(rej.status, 200, JSON.stringify(rej.body));
|
|
286
|
+
assert.equal(h.mails.length, mails, "the invitee never hears of a rejected invitation");
|
|
287
|
+
const list = await h.api(
|
|
288
|
+
`/service-provider-invitations?org=${id.aigleOrg}&siteId=${id.siteA}&status=rejected`,
|
|
289
|
+
{ sid: sid.aigle },
|
|
290
|
+
);
|
|
291
|
+
assert.equal(list.body.items[0].rejectionReason, "Not contracted by the client.");
|
|
292
|
+
assert.equal(list.body.items[0].statusLabel, "Not approved");
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it("14. expired: an approved invitation left too long cannot be accepted", async () => {
|
|
296
|
+
const res = await invite("aigle", { email: LATE_EMAIL, org: id.aigleOrg, site: id.siteA });
|
|
297
|
+
assert.equal(res.status, 200, JSON.stringify(res.body));
|
|
298
|
+
const r = await row(LATE_EMAIL, id.siteA);
|
|
299
|
+
await h.api(`/service-provider-invitations/${r._id}/approve`, { method: "PUT", sid: sid.admin });
|
|
300
|
+
// test fixture only: move the clock on this in-memory row
|
|
301
|
+
await h.db.collection("verifications").updateOne(
|
|
302
|
+
{ _id: r._id },
|
|
303
|
+
{ $set: { expireAt: new Date(Date.now() - 60_000).toISOString() } },
|
|
304
|
+
);
|
|
305
|
+
const acc = await h.api(`/service-provider-invitations/${r._id}/accept`, {
|
|
306
|
+
method: "PUT",
|
|
307
|
+
sid: sid.late,
|
|
308
|
+
body: { org: id.lateOrg.toString() },
|
|
309
|
+
});
|
|
310
|
+
assert.equal(acc.status, 400, JSON.stringify(acc.body));
|
|
311
|
+
assert.match(acc.body.message, /expired/i);
|
|
312
|
+
const list = await h.api(
|
|
313
|
+
`/service-provider-invitations?org=${id.aigleOrg}&siteId=${id.siteA}&status=pending`,
|
|
314
|
+
{ sid: sid.aigle },
|
|
315
|
+
);
|
|
316
|
+
const item = list.body.items.find((i) => i._id.toString() === r._id.toString());
|
|
317
|
+
assert.equal(item.viewStatus, "expired");
|
|
318
|
+
assert.equal(item.statusLabel, "Expired");
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
async function seed(h) {
|
|
323
|
+
const now = new Date().toISOString();
|
|
324
|
+
const o = () => new ObjectId();
|
|
325
|
+
const pmOrg = o(), aigleOrg = o(), cleanOrg = o(), pestOrg = o(), rivalOrg = o(), lateOrg = o();
|
|
326
|
+
const pmUser = o(), aigleUser = o(), guardUser = o(), cleanUser = o(), cleanerUser = o();
|
|
327
|
+
const adminUser = o(), outsiderUser = o(), lateUser = o();
|
|
328
|
+
const adminRole = o(), pmRole = o(), orgOwnerRole = o(), guardRole = o(), cleanerRole = o(), rivalRole = o();
|
|
329
|
+
const siteA = o(); // One Pearl Bank - Aigle and Pestbusters engaged
|
|
330
|
+
const siteB = o(); // Seventh Condominium - Aigle NOT engaged
|
|
331
|
+
|
|
332
|
+
await h.db.collection("organizations").insertMany([
|
|
333
|
+
{ _id: pmOrg, name: "Northwind Property Management", email: "pm-org@sub.example.com", nature: "property_management_agency", status: "active", createdAt: now },
|
|
334
|
+
{ _id: aigleOrg, name: "Aigle Security", email: AIGLE_EMAIL, nature: "security_agency", status: "active", createdAt: now },
|
|
335
|
+
{ _id: cleanOrg, name: "Sparkle Cleaning", email: CLEAN_EMAIL, nature: "cleaning_services", status: "active", createdAt: now },
|
|
336
|
+
{ _id: pestOrg, name: "Pestbusters", email: "pest@sub.example.com", nature: "pest_control_services", status: "active", createdAt: now },
|
|
337
|
+
{ _id: rivalOrg, name: "Rival Estates", email: OUTSIDER_EMAIL, nature: "property_management_agency", status: "active", createdAt: now },
|
|
338
|
+
{ _id: lateOrg, name: "Late Landscapes", email: LATE_EMAIL, nature: "landscaping_services", status: "active", createdAt: now },
|
|
339
|
+
]);
|
|
340
|
+
|
|
341
|
+
await h.db.collection("sites").insertMany([
|
|
342
|
+
{ _id: siteA, name: "One Pearl Bank", org: pmOrg.toString(), orgId: pmOrg, status: "active", createdAt: now },
|
|
343
|
+
{ _id: siteB, name: "Seventh Condominium", org: pmOrg.toString(), orgId: pmOrg, status: "active", createdAt: now },
|
|
344
|
+
]);
|
|
345
|
+
|
|
346
|
+
await h.db.collection("customer.sites").insertMany([
|
|
347
|
+
{ name: "One Pearl Bank", site: siteA, siteOrg: pmOrg, siteOrgName: "Northwind Property Management", org: aigleOrg, status: "active", createdAt: now },
|
|
348
|
+
{ name: "One Pearl Bank", site: siteA, siteOrg: pmOrg, siteOrgName: "Northwind Property Management", org: pestOrg, status: "active", createdAt: now },
|
|
349
|
+
]);
|
|
350
|
+
|
|
351
|
+
const user = async (_id, email, name, defaultOrg) => ({
|
|
352
|
+
_id, email, name, password: await h.hashPassword(PW), status: "active",
|
|
353
|
+
...(defaultOrg ? { defaultOrg: defaultOrg.toString() } : {}), createdAt: now,
|
|
354
|
+
});
|
|
355
|
+
await h.db.collection("users").insertMany([
|
|
356
|
+
await user(pmUser, PM_EMAIL, "Pat Manager", pmOrg),
|
|
357
|
+
await user(aigleUser, AIGLE_EMAIL, "Ada Aigle", aigleOrg),
|
|
358
|
+
await user(guardUser, GUARD_EMAIL, "Gus Guard", aigleOrg),
|
|
359
|
+
await user(cleanUser, CLEAN_EMAIL, "Cleo Sparkle", cleanOrg),
|
|
360
|
+
await user(cleanerUser, CLEANER_EMAIL, "Casey Cleaner", cleanOrg),
|
|
361
|
+
await user(adminUser, ADMIN_EMAIL, "Sasha Seven"),
|
|
362
|
+
await user(outsiderUser, OUTSIDER_EMAIL, "Olive Outsider", rivalOrg),
|
|
363
|
+
await user(lateUser, LATE_EMAIL, "Lee Late", lateOrg),
|
|
364
|
+
]);
|
|
365
|
+
|
|
366
|
+
await h.db.collection("roles").insertMany([
|
|
367
|
+
{ _id: adminRole, name: "Super Admin", type: "admin", default: true, permissions: ["*"], status: "active" },
|
|
368
|
+
{ _id: pmRole, name: "Property Manager", type: "property_management_agency", permissions: ["*"], status: "active" },
|
|
369
|
+
// company-wide: an organisation-console role
|
|
370
|
+
{ _id: orgOwnerRole, name: "Owner", type: "organization", permissions: ["*"], status: "active" },
|
|
371
|
+
// a guard's vertical role pinned to the site
|
|
372
|
+
{ _id: guardRole, name: "Guard", type: "security_agency", org: aigleOrg.toString(), site: siteA, permissions: ["*"], status: "active" },
|
|
373
|
+
{ _id: cleanerRole, name: "Cleaner", type: "cleaning_services", org: cleanOrg.toString(), site: siteA, permissions: ["feedbacks.view"], status: "active" },
|
|
374
|
+
{ _id: rivalRole, name: "Rival Admin", type: "property_management_agency", permissions: ["*"], status: "active" },
|
|
375
|
+
]);
|
|
376
|
+
|
|
377
|
+
await h.db.collection("members").insertMany([
|
|
378
|
+
{ user: adminUser, name: "Sasha Seven", role: adminRole, type: "admin", org: "", status: "active", createdAt: now },
|
|
379
|
+
{ user: pmUser, name: "Pat Manager", role: pmRole, type: "property_management_agency", org: pmOrg, status: "active", createdAt: now },
|
|
380
|
+
{ user: aigleUser, name: "Ada Aigle", role: orgOwnerRole, type: "security_agency", org: aigleOrg, status: "active", createdAt: now },
|
|
381
|
+
{ user: guardUser, name: "Gus Guard", role: guardRole, type: "security_agency", org: aigleOrg, siteId: siteA, status: "active", createdAt: now },
|
|
382
|
+
{ user: cleanUser, name: "Cleo Sparkle", role: orgOwnerRole, type: "cleaning_services", org: cleanOrg, status: "active", createdAt: now },
|
|
383
|
+
{ user: cleanerUser, name: "Casey Cleaner", role: cleanerRole, type: "cleaning_services", org: cleanOrg, siteId: siteA, status: "active", createdAt: now },
|
|
384
|
+
{ user: outsiderUser, name: "Olive Outsider", role: rivalRole, type: "property_management_agency", org: rivalOrg, status: "active", createdAt: now },
|
|
385
|
+
{ user: lateUser, name: "Lee Late", role: orgOwnerRole, type: "landscaping_services", org: lateOrg, status: "active", createdAt: now },
|
|
386
|
+
]);
|
|
387
|
+
|
|
388
|
+
return { pmOrg, aigleOrg, cleanOrg, pestOrg, lateOrg, adminUser, cleanUser, siteA, siteB };
|
|
389
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// Visitor Management search against a real MongoDB - an in-process, throwaway one
|
|
2
|
+
// (mongodb-memory-server). Runs the built repository's `getAll`, the function
|
|
3
|
+
// GET /api/visitor-transactions calls, so the row-field `$or`, the block/level/unit lookups,
|
|
4
|
+
// the site scope, the date/status filters and paging are exercised together.
|
|
5
|
+
|
|
6
|
+
import test from "node:test";
|
|
7
|
+
import assert from "node:assert/strict";
|
|
8
|
+
import { MongoMemoryServer } from "mongodb-memory-server";
|
|
9
|
+
import { ObjectId } from "mongodb";
|
|
10
|
+
import { useAtlas } from "@7365admin1/node-server-utils";
|
|
11
|
+
|
|
12
|
+
let mongo;
|
|
13
|
+
let repo;
|
|
14
|
+
|
|
15
|
+
const siteA = new ObjectId();
|
|
16
|
+
const siteB = new ObjectId();
|
|
17
|
+
const blockA = new ObjectId();
|
|
18
|
+
const blockB = new ObjectId();
|
|
19
|
+
const level03 = new ObjectId();
|
|
20
|
+
const unit06 = new ObjectId();
|
|
21
|
+
const now = new Date("2026-09-28T08:00:00Z");
|
|
22
|
+
|
|
23
|
+
test.before(async () => {
|
|
24
|
+
mongo = await MongoMemoryServer.create();
|
|
25
|
+
await useAtlas.initialize({ uri: mongo.getUri(), db: "visitor-search-e2e" });
|
|
26
|
+
const db = useAtlas.getDb();
|
|
27
|
+
|
|
28
|
+
await db.collection("buildings").insertMany([
|
|
29
|
+
{ _id: blockA, site: siteA, name: "Lobby 3", block: 3 },
|
|
30
|
+
{ _id: blockB, site: siteB, name: "Lobby 3", block: 3 },
|
|
31
|
+
]);
|
|
32
|
+
await db.collection("building-levels").insertOne({ _id: level03, site: siteA, name: "03" });
|
|
33
|
+
await db.collection("building-units").insertOne({ _id: unit06, site: siteA, name: "06", level: level03, building: blockA });
|
|
34
|
+
|
|
35
|
+
const v = (o) => ({ status: "registered", type: "contractor", checkIn: now, expectedCheckIn: now, createdAt: now, ...o });
|
|
36
|
+
await db.collection("visitor.transactions").insertMany([
|
|
37
|
+
v({ site: siteA, name: "Dion", contact: "+6591177963", plateNumber: "SLM1189Y", company: "Asia", nric: "S1234963A", block: blockA, level: level03, unit: unit06 }),
|
|
38
|
+
v({ site: siteA, name: "Oh", contact: "+6586836992", plateNumber: "GBC 1706B", company: "Samsung" }),
|
|
39
|
+
v({ site: siteA, name: "Old Dion", plateNumber: "SLM1189Y", checkIn: new Date("2025-01-01"), expectedCheckIn: new Date("2025-01-01") }),
|
|
40
|
+
v({ site: siteA, name: "Dion Pending", status: "pending" }),
|
|
41
|
+
// Another customer's site: same name, same plate, same block name.
|
|
42
|
+
v({ site: siteB, name: "Dion", plateNumber: "SLM1189Y", block: blockB }),
|
|
43
|
+
...Array.from({ length: 12 }, (_, i) => v({ site: siteA, name: `Bulk ${i}`, company: "Bulkco" })),
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
const mod = await import("../.build-e2e/repositories/visitor-transaction.repo.mjs");
|
|
47
|
+
repo = mod.useVisitorTransactionRepo();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test.after(async () => {
|
|
51
|
+
await useAtlas.close?.();
|
|
52
|
+
await mongo?.stop();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const base = {
|
|
56
|
+
site: String(siteA),
|
|
57
|
+
status: ["registered"],
|
|
58
|
+
dateFrom: "2026-09-01T00:00:00Z",
|
|
59
|
+
dateTo: "2026-09-30T23:59:59Z",
|
|
60
|
+
};
|
|
61
|
+
const names = (r) => r.items.map((i) => i.name).sort();
|
|
62
|
+
|
|
63
|
+
test("name, plate (any spacing/case), phone digits, company", async () => {
|
|
64
|
+
assert.deepEqual(names(await repo.getAll({ ...base, search: "dion" })), ["Dion"]);
|
|
65
|
+
assert.deepEqual(names(await repo.getAll({ ...base, search: "slm 1189y" })), ["Dion"]);
|
|
66
|
+
assert.deepEqual(names(await repo.getAll({ ...base, search: "gbc1706b" })), ["Oh"]);
|
|
67
|
+
assert.deepEqual(names(await repo.getAll({ ...base, search: "+65 9117 7963" })), ["Dion"]);
|
|
68
|
+
assert.deepEqual(names(await repo.getAll({ ...base, search: "SAMSUNG" })), ["Oh"]);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("NRIC, block name and block/level/unit", async () => {
|
|
72
|
+
assert.deepEqual(names(await repo.getAll({ ...base, search: "963a" })), ["Dion"]);
|
|
73
|
+
assert.deepEqual(names(await repo.getAll({ ...base, search: "Lobby 3" })), ["Dion"]);
|
|
74
|
+
assert.deepEqual(names(await repo.getAll({ ...base, search: "3/03/06" })), ["Dion"]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("site scope holds: another site's matching rows never appear", async () => {
|
|
78
|
+
const r = await repo.getAll({ ...base, search: "SLM1189Y" });
|
|
79
|
+
assert.ok(r.items.every((i) => String(i.site) === String(siteA)));
|
|
80
|
+
// A caller scoped to no site sees nothing, search or not.
|
|
81
|
+
const none = await repo.getAll({ ...base, site: "", siteScope: [], search: "dion" });
|
|
82
|
+
assert.equal(none.items.length, 0);
|
|
83
|
+
// Scoped to site B only -> only B's Dion.
|
|
84
|
+
const b = await repo.getAll({ ...base, site: "", siteScope: [String(siteB)], search: "slm1189y" });
|
|
85
|
+
assert.deepEqual(b.items.map((i) => String(i.site)), [String(siteB)]);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("combines with the date and status filters", async () => {
|
|
89
|
+
// "Old Dion" is outside the date range, "Dion Pending" has another status.
|
|
90
|
+
assert.deepEqual(names(await repo.getAll({ ...base, search: "dion" })), ["Dion"]);
|
|
91
|
+
assert.deepEqual(names(await repo.getAll({ ...base, status: ["pending"], search: "dion" })), ["Dion Pending"]);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("regex characters are literal, not a 500", async () => {
|
|
95
|
+
for (const q of ["(", "+", "[", "\\", ".*"]) {
|
|
96
|
+
const r = await repo.getAll({ ...base, search: q });
|
|
97
|
+
assert.equal(r.items.length, 0, q);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("paging is bounded and counts describe the search", async () => {
|
|
102
|
+
const p1 = await repo.getAll({ ...base, search: "bulkco", limit: 10, page: 1 });
|
|
103
|
+
const p2 = await repo.getAll({ ...base, search: "bulkco", limit: 10, page: 2 });
|
|
104
|
+
assert.equal(p1.items.length, 10);
|
|
105
|
+
assert.equal(p2.items.length, 2);
|
|
106
|
+
assert.equal(p1.pages, 2);
|
|
107
|
+
});
|
|
@@ -106,12 +106,16 @@ test("the two service-provider invites still resolve their caller themselves", (
|
|
|
106
106
|
for (const source of [verificationService, verificationServiceV2]) {
|
|
107
107
|
const body = fn(source, "createServiceProviderInvite");
|
|
108
108
|
assert.match(body, /resolveInviteActor\(invitedBy\)/);
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
// session, ahead of the caller's own orgs. The staff clause is now the
|
|
113
|
-
// cross-client grant (`staffMayBypass`), not identity alone.
|
|
114
|
-
/staffMayBypass\(actor\)[\s\S]{0,80}actor\.orgIds\.includes\(orgId\)/,
|
|
115
|
-
);
|
|
109
|
+
// The org check moved into one helper both routes call (owner request
|
|
110
|
+
// 2026-09-28 added the subcontract rule beside it).
|
|
111
|
+
assert.match(body, /assertServiceProviderInviteScope\(\s*actor,\s*orgId,\s*siteId,?\s*\)/);
|
|
116
112
|
}
|
|
113
|
+
const helper = fn(
|
|
114
|
+
read("services/service-provider-invite.service.ts"),
|
|
115
|
+
"assertServiceProviderInviteScope",
|
|
116
|
+
);
|
|
117
|
+
// A staff clause resolved from the session (the cross-client grant), and
|
|
118
|
+
// the caller's own orgs.
|
|
119
|
+
assert.match(helper, /staffMayBypass\(actor\)/);
|
|
120
|
+
assert.match(helper, /actor\.orgIds\.includes\(orgId\)/);
|
|
117
121
|
});
|