@7365admin1/core 3.98.0 → 3.99.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@7365admin1/core",
3
3
  "license": "MIT",
4
- "version": "3.98.0",
4
+ "version": "3.99.0",
5
5
  "author": "7365admin1",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -1109,3 +1109,50 @@ test("several memberships at the site: the one the app is working in decides (DV
1109
1109
  null,
1110
1110
  );
1111
1111
  });
1112
+
1113
+ /**
1114
+ * Owner report 2026-09-24: a security company accepted a client's invitation
1115
+ * and the site never appeared on the company owner's Sites list, because the
1116
+ * owner's membership row happened to name one site. A company-wide role
1117
+ * (owner / org admin, `companyWideRole`) reaches the sites its company SERVES;
1118
+ * a guard pinned to a site still reaches only that site (policy 2026-08-10).
1119
+ */
1120
+ test("a company-wide role pinned to a site still reaches the sites its company serves", () => {
1121
+ const SITE_B = "6923d6664150ca6a69b9f333";
1122
+ const engagedOrgs = new Set([AGENCY_ORG]);
1123
+ const owner = [{ org: AGENCY_ORG, siteId: SITE_B, orgLevelRole: true, companyWideRole: true }];
1124
+ const guard = [{ org: AGENCY_ORG, siteId: SITE_B, orgLevelRole: true }];
1125
+
1126
+ assert.equal(
1127
+ isCameraEntitled({ cameraSite: SITE, cameraOrg: OWNER_ORG, memberships: owner, engagedOrgs }),
1128
+ true,
1129
+ );
1130
+ assert.equal(
1131
+ isCameraEntitled({ cameraSite: SITE, cameraOrg: OWNER_ORG, memberships: guard, engagedOrgs }),
1132
+ false,
1133
+ "an org-level guard role pinned to B must not reach A",
1134
+ );
1135
+ assert.equal(
1136
+ isCameraEntitled({ cameraSite: SITE, cameraOrg: OWNER_ORG, memberships: owner, engagedOrgs: new Set([OTHER_ORG]) }),
1137
+ false,
1138
+ "company-wide never reaches a site its company does not serve",
1139
+ );
1140
+ });
1141
+
1142
+ test("entitledSiteScope lists served sites for a company-wide role, not for a pinned guard", async () => {
1143
+ const { entitledSiteScope } = await import("./.build/utils/camera-view.util.mjs");
1144
+ const SITE_B = "6923d6664150ca6a69b9f333";
1145
+ const engagedSitesByOrg = { [AGENCY_ORG]: [SITE] };
1146
+ const owner = entitledSiteScope({
1147
+ memberships: [{ org: AGENCY_ORG, siteId: SITE_B, orgLevelRole: true, companyWideRole: true }],
1148
+ sitesByOrg: {},
1149
+ engagedSitesByOrg,
1150
+ });
1151
+ assert.deepEqual(owner.sort(), [SITE, SITE_B].sort());
1152
+ const guard = entitledSiteScope({
1153
+ memberships: [{ org: AGENCY_ORG, siteId: SITE_B, orgLevelRole: true }],
1154
+ sitesByOrg: {},
1155
+ engagedSitesByOrg,
1156
+ });
1157
+ assert.deepEqual(guard, [SITE_B]);
1158
+ });
@@ -279,7 +279,7 @@ describe("service-provider invitation approval, end to end", { concurrency: 1 },
279
279
  sid: providerSid,
280
280
  });
281
281
  assert.equal(seen.status, 200, JSON.stringify(seen.body));
282
- assert.equal(seen.body.statusLabel, "Waiting for the provider");
282
+ assert.equal(seen.body.statusLabel, "Waiting for you");
283
283
  assert.equal(seen.body.orgName, "Northwind Property Management");
284
284
 
285
285
  const res = await h.api(`/customer-sites/invite/${invitations.a}`, {
@@ -707,7 +707,9 @@ describe("service-provider invitation approval, end to end", { concurrency: 1 },
707
707
  assert.ok(item, "the open invitation is listed");
708
708
  assert.equal(item.orgName, "Northwind Property Management");
709
709
  assert.equal(item.siteName, "Harbour View");
710
- assert.equal(item.providerOrgId, id.providerOrg.toString());
710
+ // Which company gains it is decided at accept time (the company on
711
+ // screen), so an open invitation names none.
712
+ assert.equal(item.statusLabel, "Waiting for you");
711
713
  assert.ok(
712
714
  mine.body.items.every((i) => i.status === "pending"),
713
715
  "only invitations still waiting for an answer",
@@ -862,6 +864,165 @@ describe("service-provider invitation approval, end to end", { concurrency: 1 },
862
864
  assert.equal(await h.db.collection("customer.sites").countDocuments({ site: id.siteK }), 0);
863
865
  });
864
866
 
867
+
868
+ // -------------------------------------------------------------------------
869
+ // Owner report 2026-09-24: "Accepted", but the site was not on the company's
870
+ // Sites list. One person ran two companies under one email address; the
871
+ // engagement went to whichever company the email lookup found first, and the
872
+ // company owner's membership named a site, so served sites were hidden.
873
+ // -------------------------------------------------------------------------
874
+
875
+ it("28. accepting FOR the company on screen puts the site on THAT company's Sites list", async () => {
876
+ const inviteL = await sendAndApprove(id.siteL, "Lakeside Gardens");
877
+
878
+ const res = await h.api(`/service-provider-invitations/${inviteL}/accept`, {
879
+ method: "PUT",
880
+ sid: providerSid,
881
+ body: { org: id.twinOrg.toString() },
882
+ });
883
+ assert.equal(res.status, 200, JSON.stringify(res.body));
884
+
885
+ // The twin company, not the first company registered under the email.
886
+ assert.equal(
887
+ await h.db.collection("customer.sites").countDocuments({ org: id.twinOrg, site: id.siteL, status: "active" }),
888
+ 1,
889
+ );
890
+ assert.equal(
891
+ await h.db.collection("customer.sites").countDocuments({ org: id.providerOrg, site: id.siteL }),
892
+ 0,
893
+ );
894
+ const after = await row(PROVIDER_EMAIL, id.siteL);
895
+ assert.equal(after.status, "complete");
896
+ assert.equal(after.metadata.providerOrg, id.twinOrg.toString());
897
+ assert.equal(after.metadata.providerOrgName, "Sentry Patrol Pte Ltd");
898
+
899
+ // The twin's owner membership NAMES a site, with a company-wide role: the
900
+ // served site must still be on their Sites list and site switcher.
901
+ const list = await h.api(
902
+ `/customer-sites?org=${id.twinOrg.toString()}&status=active&page=1&limit=20`,
903
+ { sid: providerSid },
904
+ );
905
+ assert.equal(list.status, 200, JSON.stringify(list.body));
906
+ const served = list.body.items.find((i) => String(i.site) === id.siteL.toString());
907
+ assert.ok(served, "the accepted site is listed for the company that accepted it");
908
+ assert.equal(String(served.siteOrg), id.pmOrg.toString(), "marked as a client's site");
909
+
910
+ // Accepted tab, seen from the twin: on this company's list, in words.
911
+ const tab = await h.api(
912
+ `/service-provider-invitations/received?status=accepted&org=${id.twinOrg.toString()}`,
913
+ { sid: providerSid },
914
+ );
915
+ assert.equal(tab.status, 200, JSON.stringify(tab.body));
916
+ const card = tab.body.items.find((i) => i._id === inviteL);
917
+ assert.ok(card);
918
+ assert.equal(card.onThisOrg, true);
919
+ assert.equal(card.providerOrgName, "Sentry Patrol Pte Ltd");
920
+ assert.equal(card.statusLabel, "Accepted");
921
+ assert.ok(card.answeredAt, "when it was accepted");
922
+ assert.equal(card.invitedByName, "Pat Manager");
923
+
924
+ // Seen from the OTHER company: accepted, but not on this company's list.
925
+ const fromFirst = await h.api(
926
+ `/service-provider-invitations/received?status=accepted&org=${id.providerOrg.toString()}`,
927
+ { sid: providerSid },
928
+ );
929
+ assert.equal(fromFirst.body.items.find((i) => i._id === inviteL).onThisOrg, false);
930
+
931
+ // Twice, from the same company: harmless.
932
+ const twice = await h.api(`/service-provider-invitations/${inviteL}/accept`, {
933
+ method: "PUT",
934
+ sid: providerSid,
935
+ body: { org: id.twinOrg.toString() },
936
+ });
937
+ assert.equal(twice.status, 200, JSON.stringify(twice.body));
938
+ assert.equal(await h.db.collection("customer.sites").countDocuments({ site: id.siteL }), 1);
939
+ });
940
+
941
+ it("29. nobody can accept FOR a company they do not belong to", async () => {
942
+ const inviteM = await sendAndApprove(id.siteM, "Maple Court");
943
+
944
+ const foreign = await h.api(`/service-provider-invitations/${inviteM}/accept`, {
945
+ method: "PUT",
946
+ sid: providerSid,
947
+ body: { org: id.outsiderOrg.toString() },
948
+ });
949
+ assert.equal(foreign.status, 401, JSON.stringify(foreign.body));
950
+
951
+ const garbage = await h.api(`/service-provider-invitations/${inviteM}/accept`, {
952
+ method: "PUT",
953
+ sid: providerSid,
954
+ body: { org: "not-an-id" },
955
+ });
956
+ assert.equal(garbage.status, 400, JSON.stringify(garbage.body));
957
+
958
+ // A member of the named company, but not the invited account.
959
+ const outsider = await h.api(`/service-provider-invitations/${inviteM}/accept`, {
960
+ method: "PUT",
961
+ sid: outsiderSid,
962
+ body: { org: id.outsiderOrg.toString() },
963
+ });
964
+ assert.equal(outsider.status, 401, JSON.stringify(outsider.body));
965
+
966
+ const peek = await h.api(
967
+ `/service-provider-invitations/received?org=${id.outsiderOrg.toString()}`,
968
+ { sid: providerSid },
969
+ );
970
+ assert.equal(peek.status, 401, "cannot list as a company you are not in");
971
+
972
+ // A GUARD of a company (pinned to a site, vertical role) cannot make it the provider.
973
+ await h.db.collection("members").insertOne({
974
+ user: id.providerUser, name: "Sam Provider", role: id.guardRole, type: "security_agency",
975
+ org: id.guardOrg, siteId: new ObjectId(), status: "active", createdAt: new Date().toISOString(),
976
+ });
977
+ const asGuard = await h.api(`/service-provider-invitations/${inviteM}/accept`, {
978
+ method: "PUT",
979
+ sid: providerSid,
980
+ body: { org: id.guardOrg.toString() },
981
+ });
982
+ assert.equal(asGuard.status, 401, JSON.stringify(asGuard.body));
983
+
984
+ assert.equal(await h.db.collection("customer.sites").countDocuments({ site: id.siteM }), 0);
985
+ assert.equal((await row(PROVIDER_EMAIL, id.siteM)).status, "pending");
986
+ });
987
+
988
+ it("30. the four tabs: every answered invitation is in exactly one, held ones in none", async () => {
989
+ const get = (status) =>
990
+ h.api(
991
+ `/service-provider-invitations/received?status=${status}&org=${id.providerOrg.toString()}`,
992
+ { sid: providerSid },
993
+ );
994
+ const [pending, accepted, declined, expired] = await Promise.all(
995
+ ["pending", "accepted", "declined", "expired"].map(get),
996
+ );
997
+ for (const r of [pending, accepted, declined, expired]) assert.equal(r.status, 200, JSON.stringify(r.body));
998
+
999
+ const ids = (r) => r.body.items.map((i) => i._id);
1000
+ assert.ok(ids(pending).length >= 1 && pending.body.items.every((i) => i.statusLabel === "Waiting for you"));
1001
+ assert.ok(ids(accepted).includes(invitations.h), "accepted earlier (test 24)");
1002
+ assert.ok(declined.body.items.some((i) => i.siteName === "Juniper Hill"), "declined (test 26)");
1003
+ assert.ok(expired.body.items.some((i) => i.siteName === "Ivy Lodge"), "lapsed (test 25)");
1004
+
1005
+ const all = [pending, accepted, declined, expired].flatMap(ids);
1006
+ assert.equal(new Set(all).size, all.length, "no invitation is in two tabs");
1007
+ assert.deepEqual(pending.body.counts, {
1008
+ pending: ids(pending).length,
1009
+ accepted: ids(accepted).length,
1010
+ declined: ids(declined).length,
1011
+ expired: ids(expired).length,
1012
+ });
1013
+
1014
+ // A rejected one (test 12) was never told to the provider.
1015
+ const rejected = await row(PROVIDER_EMAIL, id.siteB);
1016
+ assert.equal(rejected.status, "rejected");
1017
+ assert.ok(!all.includes(rejected._id.toString()), "Seven365's rejection is not the provider's business");
1018
+
1019
+ const seen = await h.api(`/service-provider-invitations/${invitations.h}?org=${id.providerOrg.toString()}`, {
1020
+ sid: providerSid,
1021
+ });
1022
+ assert.equal(seen.body.tab, "accepted");
1023
+ assert.equal(seen.body.onThisOrg, true);
1024
+ });
1025
+
865
1026
  it("reports the exact wording the API returned", () => {
866
1027
  console.log("\n--- verbatim API messages ---");
867
1028
  for (const [k, v] of Object.entries(said)) console.log(`${k}: ${v}`);
@@ -899,6 +1060,13 @@ async function seed(h) {
899
1060
  const siteI = new ObjectId(); // lapsed while still pending
900
1061
  const siteJ = new ObjectId(); // declined, then an accept attempted
901
1062
  const siteK = new ObjectId(); // deleted by the sender, then an accept attempted
1063
+ const siteL = new ObjectId(); // accepted FOR the provider's second company
1064
+ const siteM = new ObjectId(); // accept attempted for a company not joined
1065
+ const twinOrg = new ObjectId(); // a second company under the provider's email
1066
+ const twinOwnSite = new ObjectId(); // the site the twin's owner row names
1067
+ const twinRole = new ObjectId();
1068
+ const guardOrg = new ObjectId(); // a company the provider is only a guard of
1069
+ const guardRole = new ObjectId();
902
1070
 
903
1071
  await h.db.collection("organizations").insertMany([
904
1072
  {
@@ -917,6 +1085,16 @@ async function seed(h) {
917
1085
  status: "active",
918
1086
  createdAt: now,
919
1087
  },
1088
+ {
1089
+ // Registered AFTER providerOrg on purpose: an email lookup finds
1090
+ // providerOrg first, so only binding to the chosen company reaches here.
1091
+ _id: twinOrg,
1092
+ name: "Sentry Patrol Pte Ltd",
1093
+ email: PROVIDER_EMAIL,
1094
+ type: "security_agency",
1095
+ status: "active",
1096
+ createdAt: now,
1097
+ },
920
1098
  {
921
1099
  _id: outsiderOrg,
922
1100
  name: "Vega Facilities",
@@ -940,6 +1118,8 @@ async function seed(h) {
940
1118
  [siteI, "Ivy Lodge"],
941
1119
  [siteJ, "Juniper Hill"],
942
1120
  [siteK, "Kestrel Point"],
1121
+ [siteL, "Lakeside Gardens"],
1122
+ [siteM, "Maple Court"],
943
1123
  ].map(([_id, name]) => ({
944
1124
  _id,
945
1125
  name,
@@ -995,6 +1175,10 @@ async function seed(h) {
995
1175
  { _id: pmRole, name: "Property Manager", type: "property_management_agency", permissions: ["*"], status: "active" },
996
1176
  { _id: providerRole, name: "Provider Admin", type: "security_agency", permissions: ["*"], status: "active" },
997
1177
  { _id: outsiderRole, name: "Outsider Admin", type: "property_management_agency", permissions: ["*"], status: "active" },
1178
+ // An org-console owner role (company-wide) that is NOT a wildcard.
1179
+ { _id: twinRole, name: "Org Owner", type: "organization", permissions: ["members.view"], status: "active" },
1180
+ // A vertical full-access template ("owner" with "*") held by a guard pinned to a site.
1181
+ { _id: guardRole, name: "owner", type: "security_agency", org: guardOrg.toString(), permissions: ["*"], status: "active" },
998
1182
  ]);
999
1183
 
1000
1184
  await h.db.collection("members").insertMany([
@@ -1002,6 +1186,8 @@ async function seed(h) {
1002
1186
  { user: pmUser, name: "Pat Manager", role: pmRole, type: "property_management_agency", org: pmOrg, status: "active", createdAt: now },
1003
1187
  { user: providerUser, name: "Sam Provider", role: providerRole, type: "security_agency", org: providerOrg, status: "active", createdAt: now },
1004
1188
  { user: outsiderUser, name: "Olive Outsider", role: outsiderRole, type: "property_management_agency", org: outsiderOrg, status: "active", createdAt: now },
1189
+ // The stray siteId that hid served sites from a company owner.
1190
+ { user: providerUser, name: "Sam Provider", role: twinRole, type: "security_agency", org: twinOrg, siteId: twinOwnSite, status: "active", createdAt: now },
1005
1191
  ]);
1006
1192
 
1007
1193
  return {
@@ -1023,5 +1209,10 @@ async function seed(h) {
1023
1209
  siteI,
1024
1210
  siteJ,
1025
1211
  siteK,
1212
+ siteL,
1213
+ siteM,
1214
+ twinOrg,
1215
+ guardOrg,
1216
+ guardRole,
1026
1217
  };
1027
1218
  }
@@ -200,7 +200,7 @@ test("accepting an invitation writes the engagement once and closes the invite",
200
200
  assert.match(body, /_countActiveByOrgAndSite\(/, "must check for a duplicate");
201
201
  assert.match(
202
202
  body,
203
- /_updateVerificationStatusById\(\s*invite,\s*"complete"/,
203
+ /_transitionInvite\(\{\s*id: invite,\s*from: \["pending"\],\s*to: "complete"/,
204
204
  "must not leave the row Pending after acceptance",
205
205
  );
206
206
  assert.match(
@@ -354,3 +354,40 @@ test("accepting is gated on the invited account and writes the engagement only w
354
354
  const controller = src("controllers/customer-site.controller.ts");
355
355
  assert.match(controller, /useServiceProviderInviteService\(\)\.accept\(/);
356
356
  });
357
+
358
+ /**
359
+ * "Client invitations" tabs (owner report 2026-09-24): every invitation the
360
+ * provider was told about lands in exactly one tab; the ones Seven365 held
361
+ * never show.
362
+ */
363
+ test("receivedInviteTab sorts invitations into Pending / Accepted / Declined / Expired", async () => {
364
+ const { receivedInviteTab, RECEIVED_INVITE_LABELS } = await import(
365
+ "./.build/utils/service-provider-invite.util.mjs"
366
+ );
367
+ const now = new Date("2026-09-24T08:00:00Z");
368
+ const future = "2026-09-27T08:00:00Z";
369
+ const past = "2026-09-20T08:00:00Z";
370
+ assert.equal(receivedInviteTab({ status: "pending", expireAt: future }, now), "pending");
371
+ assert.equal(receivedInviteTab({ status: "pending", expireAt: past }, now), "expired");
372
+ assert.equal(receivedInviteTab({ status: "complete", expireAt: past }, now), "accepted");
373
+ assert.equal(receivedInviteTab({ status: "declined" }, now), "declined");
374
+ assert.equal(receivedInviteTab({ status: "expired" }, now), "expired");
375
+ assert.equal(receivedInviteTab({ status: "cancelled" }, now), "expired");
376
+ assert.equal(receivedInviteTab({ status: "awaiting-approval" }, now), null);
377
+ assert.equal(receivedInviteTab({ status: "rejected" }, now), null);
378
+ assert.equal(RECEIVED_INVITE_LABELS.pending, "Waiting for you");
379
+ });
380
+
381
+ test("accept is bound to a company the caller belongs to, and records which company", () => {
382
+ const svc = src("services/service-provider-invite.service.ts");
383
+ const cs = src("services/customer-site.service.ts");
384
+ assert.match(svc, /function memberOrg\(actor: InviteActor, org\?: string\)/);
385
+ assert.match(svc, /m\.companyWideRole === true \|\| \(!m\.siteId && m\.orgLevelRole === true\)/, "only a company administrator may accept for it");
386
+ assert.match(svc, /const forOrg = memberOrg\(actor, org\);/);
387
+ assert.match(svc, /addViaInvite\(\s*id,\s*\{ id: actor\.id, name: actor\.name \},\s*forOrg \|\| undefined,/);
388
+ // The chosen company, not an email lookup, when one is named.
389
+ assert.match(cs, /const serviceProvider = forOrgId\s*\? await _getByOrgId\(forOrgId\)/);
390
+ // Status and link in one transaction, and the company kept on the invite.
391
+ assert.match(cs, /"metadata\.providerOrg": providerOrgId/);
392
+ assert.match(cs, /set: accepted,\s*session,/);
393
+ });