@7365admin1/core 3.129.3-staging.431 → 3.129.3-staging.432

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.
@@ -0,0 +1,5 @@
1
+ ---
2
+ "@7365admin1/core": minor
3
+ ---
4
+
5
+ `POST /members/direct` enrolling somebody else now needs the Site access editor rule (organisation owner, cross-client Seven365 staff, or an organisation-wide member with members:assign-member-role; never a site-confined member), only at a site the organisation serves, never an owner role the caller does not hold, and never the owner's own access. Enrolling yourself is unchanged. `GET /members?type=organization&people=1` lists every person of the organisation: organisation-wide rows as before plus one row per site-limited person with `limitedTo` (site and role); without `people` the answer is unchanged.
package/dist/index.d.ts CHANGED
@@ -455,7 +455,16 @@ declare function useMemberRepo(): {
455
455
  getByUserIdType: (user: string | ObjectId, type: string) => Promise<TMember>;
456
456
  getByRoleId: (role: string | ObjectId) => Promise<any>;
457
457
  getAssignedByRoleId: (role: string | ObjectId) => Promise<TMember[]>;
458
- getAll: ({ search, page, limit, user, org, siteId, siteIds, type, status, roleState, unit, }: {
458
+ getAll: ({ search, page, limit, user, org, siteId, siteIds, type, status, roleState, unit, people, }: {
459
+ /**
460
+ * The Organization app's Members list (owner report 2026-09-29, Aigle /
461
+ * Kanesan): EVERY person of the organisation, once - the organisation-wide
462
+ * rows as before, plus people whose organisation access was removed and
463
+ * who are now limited to sites (their per-site rows, grouped into one row
464
+ * carrying `limitedTo`). Only with `type=organization`, an `org`, no
465
+ * `siteId` and no `roleState`; residents and Seven365 staff never.
466
+ */
467
+ people?: boolean | undefined;
459
468
  search?: string | undefined;
460
469
  page?: number | undefined;
461
470
  limit?: number | undefined;
package/dist/index.js CHANGED
@@ -4878,6 +4878,63 @@ function useMemberRepo() {
4878
4878
  throw error;
4879
4879
  }
4880
4880
  }
4881
+ async function peoplePage(query2, page2, limit, rowStages) {
4882
+ const noSite = { $in: ["", null] };
4883
+ const orgWide = await collection.distinct("user", {
4884
+ org: query2.org,
4885
+ type: query2.type,
4886
+ siteId: noSite,
4887
+ status: { $ne: "deleted" }
4888
+ });
4889
+ const { type: orgType, siteId: pinned, ...rest } = query2;
4890
+ const limitedRow = {
4891
+ siteId: pinned ?? { $nin: ["", null] },
4892
+ user: { $nin: orgWide }
4893
+ };
4894
+ const head = [
4895
+ // `$text` (search) may only sit in the first stage.
4896
+ { $match: { ...rest, type: { $nin: ["resident", "admin"] } } },
4897
+ { $match: pinned ? limitedRow : { $or: [{ type: orgType }, limitedRow] } },
4898
+ { $sort: { _id: -1 } },
4899
+ {
4900
+ $group: {
4901
+ _id: { $ifNull: ["$user", "$_id"] },
4902
+ doc: { $first: "$$ROOT" },
4903
+ orgWide: {
4904
+ $max: { $and: [{ $eq: ["$type", orgType] }, { $in: [{ $ifNull: ["$siteId", ""] }, ["", null]] }] }
4905
+ },
4906
+ limitedTo: { $push: { siteId: "$siteId", siteName: "$siteName", role: "$role" } }
4907
+ }
4908
+ },
4909
+ {
4910
+ $replaceRoot: {
4911
+ newRoot: { $mergeObjects: ["$doc", { limitedTo: { $cond: ["$orgWide", [], "$limitedTo"] } }] }
4912
+ }
4913
+ }
4914
+ ];
4915
+ const items = await collection.aggregate([...head, { $sort: { _id: -1 } }, { $skip: page2 * limit }, { $limit: limit }, ...rowStages]).toArray();
4916
+ const [counted] = await collection.aggregate([...head, { $count: "n" }]).toArray();
4917
+ const ids2 = (key) => [...new Set(items.flatMap((i) => (i.limitedTo ?? []).map((l) => l[key]?.toString?.() ?? "")))].filter((id) => import_mongodb15.ObjectId.isValid(id)).map((id) => new import_mongodb15.ObjectId(id));
4918
+ const [sites, roles] = await Promise.all([
4919
+ db2.collection("sites").find({ _id: { $in: ids2("siteId") } }, { projection: { name: 1 } }).toArray(),
4920
+ db2.collection("roles").find({ _id: { $in: ids2("role") } }, { projection: { name: 1 } }).toArray()
4921
+ ]);
4922
+ const nameOf = (list) => new Map(list.map((d) => [d._id.toString(), d.name ?? ""]));
4923
+ const siteName = nameOf(sites);
4924
+ const roleName = nameOf(roles);
4925
+ for (const item of items) {
4926
+ const bySite = /* @__PURE__ */ new Map();
4927
+ for (const l of item.limitedTo ?? []) {
4928
+ const site = l.siteId?.toString?.() ?? "";
4929
+ const role = roleName.get(l.role?.toString?.() ?? "") ?? "";
4930
+ const entry = bySite.get(site) ?? { siteId: site, siteName: siteName.get(site) || l.siteName || "", roleName: "" };
4931
+ entry.roleName = [entry.roleName, role].filter(Boolean).join(", ");
4932
+ bySite.set(site, entry);
4933
+ }
4934
+ item.limitedTo = [...bySite.values()].sort((a, b) => a.siteName.localeCompare(b.siteName));
4935
+ }
4936
+ return { items, length: counted?.n ?? 0 };
4937
+ }
4881
4938
  async function getAll({
4882
4939
  search = "",
4883
4940
  page: page2 = 1,
@@ -4889,7 +4946,8 @@ function useMemberRepo() {
4889
4946
  type = "",
4890
4947
  status = "",
4891
4948
  roleState = "",
4892
- unit = ""
4949
+ unit = "",
4950
+ people = false
4893
4951
  }) {
4894
4952
  limit = limit > 0 ? limit : 10;
4895
4953
  search = search || "";
@@ -4952,6 +5010,9 @@ function useMemberRepo() {
4952
5010
  query2.$or = await otherSiteRoleClauses2(query2.org);
4953
5011
  cacheOptions.roleState = roleState;
4954
5012
  }
5013
+ const peopleMode = people && type === "organization" && !!query2.org && !siteId && !roleState;
5014
+ if (peopleMode)
5015
+ cacheOptions.people = 1;
4955
5016
  const cacheKey = (0, import_node_server_utils16.makeCacheKey)(namespace_collection3, cacheOptions);
4956
5017
  const cachedData = await getCache(cacheKey);
4957
5018
  if (cachedData) {
@@ -4959,15 +5020,7 @@ function useMemberRepo() {
4959
5020
  return cachedData;
4960
5021
  }
4961
5022
  try {
4962
- const items = await collection.aggregate([
4963
- { $match: query2 },
4964
- { $sort: { _id: -1 } },
4965
- // `paginate()` only LABELS the range ("11-20 of 42"); it does not
4966
- // slice. Without this $skip every page returned page 1's rows under
4967
- // a later page's heading. `page` is already zero-based above, and
4968
- // this is the same $skip/$limit pair the two getOrgs* reads use.
4969
- { $skip: page2 * limit },
4970
- { $limit: limit },
5023
+ const rowStages = [
4971
5024
  {
4972
5025
  $lookup: {
4973
5026
  from: "roles",
@@ -5043,11 +5096,29 @@ function useMemberRepo() {
5043
5096
  siteName: 1,
5044
5097
  type: 1,
5045
5098
  unit: 1,
5046
- unitName: 1
5099
+ unitName: 1,
5100
+ limitedTo: 1
5047
5101
  }
5048
5102
  }
5049
- ]).toArray();
5050
- const length = await collection.countDocuments(query2);
5103
+ ];
5104
+ let items;
5105
+ let length;
5106
+ if (peopleMode) {
5107
+ ({ items, length } = await peoplePage(query2, page2, limit, rowStages));
5108
+ } else {
5109
+ items = await collection.aggregate([
5110
+ { $match: query2 },
5111
+ { $sort: { _id: -1 } },
5112
+ // `paginate()` only LABELS the range ("11-20 of 42"); it does not
5113
+ // slice. Without this $skip every page returned page 1's rows under
5114
+ // a later page's heading. `page` is already zero-based above, and
5115
+ // this is the same $skip/$limit pair the two getOrgs* reads use.
5116
+ { $skip: page2 * limit },
5117
+ { $limit: limit },
5118
+ ...rowStages
5119
+ ]).toArray();
5120
+ length = await collection.countDocuments(query2);
5121
+ }
5051
5122
  const data = { ...(0, import_node_server_utils16.paginate)(items, page2, limit, length), total: length };
5052
5123
  setCache(cacheKey, data, 15 * 60).then(() => {
5053
5124
  import_node_server_utils16.logger.info(`Cache set for key: ${cacheKey}`);
@@ -24265,6 +24336,29 @@ async function requireNotOwnerUnlessOwner(req, orgId, targetUser) {
24265
24336
  return;
24266
24337
  throw new import_node_server_utils59.UnauthorizedError("Only the organisation's owner can change the owner's access.");
24267
24338
  }
24339
+ async function requireMemberGrantor(req, input) {
24340
+ const { orgId, targetUser, roleId } = input;
24341
+ const siteId = input.siteId?.toString() ?? "";
24342
+ const id = await requireSiteAccessEditor(req, orgId);
24343
+ await requireNotOwnerUnlessOwner(req, orgId, targetUser);
24344
+ if (staffMayBypass(await resolveInviteActor(id)))
24345
+ return;
24346
+ if (siteId)
24347
+ await requireSiteServedByOrg(siteId, orgId);
24348
+ if (await hasOrgOwnership(id, orgId))
24349
+ return;
24350
+ const db2 = import_node_server_utils59.useAtlas.getDb();
24351
+ if (!db2 || !import_mongodb55.ObjectId.isValid(roleId))
24352
+ return;
24353
+ const role = await db2.collection("roles").findOne({ _id: new import_mongodb55.ObjectId(roleId) }, { projection: { permissions: 1 } });
24354
+ if (isOwnerRole(role)) {
24355
+ const rows = await db2.collection("members").find({ user: new import_mongodb55.ObjectId(id), org: new import_mongodb55.ObjectId(orgId), status: "active" }, { projection: { role: 1 } }).toArray();
24356
+ const held = rows.map((m) => m.role?.toString?.() ?? "").filter((r) => import_mongodb55.ObjectId.isValid(r)).map((r) => new import_mongodb55.ObjectId(r));
24357
+ const holdsOwner = held.length ? await db2.collection("roles").countDocuments({ _id: { $in: held }, permissions: "*", status: "active" }) : 0;
24358
+ if (!holdsOwner)
24359
+ throw new import_node_server_utils59.UnauthorizedError("Only the organisation's owner can give an owner role.");
24360
+ }
24361
+ }
24268
24362
  async function requireSiteReachInOrg(req, siteId, orgId) {
24269
24363
  if (await hasOrgOwnership(callerId(req), orgId)) {
24270
24364
  await requireSiteServedByOrg(siteId, orgId);
@@ -30808,7 +30902,9 @@ function useMemberController() {
30808
30902
  status: import_joi30.default.string().required(),
30809
30903
  roleState: import_joi30.default.string().valid("other-site").optional().allow("", null),
30810
30904
  // A building unit id, or "none" (members on no unit yet).
30811
- unit: import_joi30.default.alternatives().try(import_joi30.default.string().hex().length(24), import_joi30.default.string().valid(NO_UNIT)).optional().allow("", null)
30905
+ unit: import_joi30.default.alternatives().try(import_joi30.default.string().hex().length(24), import_joi30.default.string().valid(NO_UNIT)).optional().allow("", null),
30906
+ // Organization app Members: every person, site-limited ones included.
30907
+ people: import_joi30.default.boolean().truthy("1").falsy("0").optional().allow("", null)
30812
30908
  });
30813
30909
  const query2 = { ...req.query };
30814
30910
  const { error } = validation.validate(query2);
@@ -30864,7 +30960,8 @@ function useMemberController() {
30864
30960
  type,
30865
30961
  status,
30866
30962
  roleState: req.query.roleState ?? "",
30867
- unit: req.query.unit ?? ""
30963
+ unit: req.query.unit ?? "",
30964
+ people: ["1", "true"].includes(String(req.query.people ?? ""))
30868
30965
  });
30869
30966
  res.json(items);
30870
30967
  return;
@@ -31005,7 +31102,7 @@ function useMemberController() {
31005
31102
  await requireOrgReach(req, orgId);
31006
31103
  await requireSelfEnrolRole(req, orgId, roleId);
31007
31104
  } else {
31008
- await requireOrgAccess(req, orgId);
31105
+ await requireMemberGrantor(req, { orgId, targetUser: userId, roleId, siteId });
31009
31106
  }
31010
31107
  const data = await _createMemberDirect({ userId, orgId, roleId, app, siteId, siteName, onboardingRequired, callerId: callerId(req) });
31011
31108
  res.status(201).json(data);