@7365admin1/core 3.129.3-staging.437 → 3.129.3-staging.438
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/.changeset/org-roles-per-site.md +5 -0
- package/dist/index.d.ts +11 -2
- package/dist/index.js +49 -16
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +49 -16
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/test/e2e/member-site-access.e2e.test.mjs +91 -2
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
---
|
|
2
|
+
"@7365admin1/core": minor
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Organisation roles used per site: Members -> Site access also offers the organisation's service roles (no site); every row it writes is stamped `createdBy`, which `siteInvitedPins` reads as "placed at this site", so such a row reaches that site only. Assign Role stamps the same when it gives an organisation role to a site-pinned membership. `GET /roles?scope=organisation` lists only roles with no site (and never self-heals).
|
package/dist/index.d.ts
CHANGED
|
@@ -329,7 +329,7 @@ declare function useRoleRepo(): {
|
|
|
329
329
|
getRoleByUserId: (value: string | ObjectId) => Promise<TRole | null>;
|
|
330
330
|
getRoleById: (_id: string | ObjectId) => Promise<TRole | null>;
|
|
331
331
|
getRoleByName: (name: string, type?: string) => Promise<TRole | null>;
|
|
332
|
-
getRoles: ({ search, page, limit, sort, type, org, site, }: {
|
|
332
|
+
getRoles: ({ search, page, limit, sort, type, org, site, scope, }: {
|
|
333
333
|
search?: string | undefined;
|
|
334
334
|
page?: number | undefined;
|
|
335
335
|
limit?: number | undefined;
|
|
@@ -337,6 +337,8 @@ declare function useRoleRepo(): {
|
|
|
337
337
|
type?: string | undefined;
|
|
338
338
|
org: string | ObjectId;
|
|
339
339
|
site?: string | ObjectId | undefined;
|
|
340
|
+
/** "organisation": roles with no site only (ignored when `site` is given) */
|
|
341
|
+
scope?: string | undefined;
|
|
340
342
|
}) => Promise<{}>;
|
|
341
343
|
getActiveByTypeOrg: (type: string, org: string | ObjectId) => Promise<TRole[]>;
|
|
342
344
|
updateRole: (_id: string | ObjectId, value: TMiniRole, session?: ClientSession) => Promise<ObjectId>;
|
|
@@ -516,7 +518,7 @@ declare function useMemberRepo(): {
|
|
|
516
518
|
updateMemberStatus: (_id: string | ObjectId, value: TMemberUpdateStatus) => Promise<number>;
|
|
517
519
|
countByOrg: (org: string | ObjectId) => Promise<number | TMember>;
|
|
518
520
|
countUserMembershipById: (user: string | ObjectId) => Promise<number | TMember>;
|
|
519
|
-
updateRoleById: (_id: string | ObjectId, role: string | ObjectId) => Promise<number>;
|
|
521
|
+
updateRoleById: (_id: string | ObjectId, role: string | ObjectId, createdBy?: string | ObjectId) => Promise<number>;
|
|
520
522
|
reassignRoles: (reassignments: Array<{
|
|
521
523
|
memberId: string;
|
|
522
524
|
roleId: string;
|
|
@@ -3285,6 +3287,13 @@ declare function pinKey(org: unknown, site: unknown): string;
|
|
|
3285
3287
|
* row to test (the usual case). Staging, 2026-09-28: 4 of 168 pinned
|
|
3286
3288
|
* org-level-role memberships match, so the stray-`siteId` rule is untouched
|
|
3287
3289
|
* for the rest.
|
|
3290
|
+
*
|
|
3291
|
+
* Also confined (owner request 2026-09-30, organisation roles used per site): a
|
|
3292
|
+
* pinned row somebody ELSE wrote - `createdBy` set and not the member. Only
|
|
3293
|
+
* Members -> Site access writes that (`member-site-access.service`); the one
|
|
3294
|
+
* other writer, the org owner's own row at onboarding, has `createdBy` = the
|
|
3295
|
+
* owner and no site. So "the owner placed this person at this site" narrows
|
|
3296
|
+
* exactly like "this person was invited to this site".
|
|
3288
3297
|
*/
|
|
3289
3298
|
declare function siteInvitedPins(db: Db, userId: string, memberships: Array<Record<string, any>>): Promise<Set<string>>;
|
|
3290
3299
|
/**
|
package/dist/index.js
CHANGED
|
@@ -5493,7 +5493,7 @@ function useMemberRepo() {
|
|
|
5493
5493
|
throw error;
|
|
5494
5494
|
}
|
|
5495
5495
|
}
|
|
5496
|
-
async function updateRoleById(_id, role) {
|
|
5496
|
+
async function updateRoleById(_id, role, createdBy) {
|
|
5497
5497
|
try {
|
|
5498
5498
|
_id = new import_mongodb15.ObjectId(_id);
|
|
5499
5499
|
} catch (error) {
|
|
@@ -5509,6 +5509,9 @@ function useMemberRepo() {
|
|
|
5509
5509
|
role,
|
|
5510
5510
|
updatedAt: /* @__PURE__ */ new Date()
|
|
5511
5511
|
};
|
|
5512
|
+
if (createdBy && import_mongodb15.ObjectId.isValid(String(createdBy))) {
|
|
5513
|
+
updateValue.createdBy = new import_mongodb15.ObjectId(String(createdBy));
|
|
5514
|
+
}
|
|
5512
5515
|
const res = await collection.updateOne({ _id }, { $set: updateValue });
|
|
5513
5516
|
if (res.modifiedCount === 0) {
|
|
5514
5517
|
throw new import_node_server_utils16.InternalServerError("Unable to update feedback status.");
|
|
@@ -10064,11 +10067,27 @@ async function siteInvitedPins(db2, userId, memberships) {
|
|
|
10064
10067
|
const keys = /* @__PURE__ */ new Set();
|
|
10065
10068
|
if (!pinned.length || !import_mongodb27.ObjectId.isValid(userId))
|
|
10066
10069
|
return keys;
|
|
10070
|
+
const both3 = (ids2) => [...new Set(ids2.map(String))].flatMap((v) => [v, new import_mongodb27.ObjectId(v)]);
|
|
10071
|
+
const wanted = new Set(pinned.map((m) => pinKey(m.org, m.siteId)));
|
|
10072
|
+
const placed = await db2.collection("members").find(
|
|
10073
|
+
{
|
|
10074
|
+
user: new import_mongodb27.ObjectId(userId),
|
|
10075
|
+
status: { $ne: "deleted" },
|
|
10076
|
+
org: { $in: both3(pinned.map((m) => m.org)) },
|
|
10077
|
+
siteId: { $in: both3(pinned.map((m) => m.siteId)) },
|
|
10078
|
+
createdBy: { $type: "objectId", $ne: new import_mongodb27.ObjectId(userId) }
|
|
10079
|
+
},
|
|
10080
|
+
{ projection: { org: 1, siteId: 1 } }
|
|
10081
|
+
).toArray();
|
|
10082
|
+
for (const row of placed) {
|
|
10083
|
+
const key = pinKey(row.org, row.siteId);
|
|
10084
|
+
if (wanted.has(key))
|
|
10085
|
+
keys.add(key);
|
|
10086
|
+
}
|
|
10067
10087
|
const user = await db2.collection("users").findOne({ _id: new import_mongodb27.ObjectId(userId) }, { projection: { email: 1 } });
|
|
10068
10088
|
const email = String(user?.email ?? "").trim();
|
|
10069
10089
|
if (!email)
|
|
10070
10090
|
return keys;
|
|
10071
|
-
const both3 = (ids2) => [...new Set(ids2.map(String))].flatMap((v) => [v, new import_mongodb27.ObjectId(v)]);
|
|
10072
10091
|
const invites = await db2.collection("verifications").find(
|
|
10073
10092
|
{
|
|
10074
10093
|
email,
|
|
@@ -10084,7 +10103,6 @@ async function siteInvitedPins(db2, userId, memberships) {
|
|
|
10084
10103
|
collation: { locale: "en", strength: 2 }
|
|
10085
10104
|
}
|
|
10086
10105
|
).toArray();
|
|
10087
|
-
const wanted = new Set(pinned.map((m) => pinKey(m.org, m.siteId)));
|
|
10088
10106
|
for (const invite of invites) {
|
|
10089
10107
|
const key = pinKey(invite.metadata?.org, invite.metadata?.siteId);
|
|
10090
10108
|
if (wanted.has(key))
|
|
@@ -26056,7 +26074,8 @@ function useRoleRepo() {
|
|
|
26056
26074
|
sort = {},
|
|
26057
26075
|
type = "",
|
|
26058
26076
|
org = "",
|
|
26059
|
-
site = ""
|
|
26077
|
+
site = "",
|
|
26078
|
+
scope = ""
|
|
26060
26079
|
}) {
|
|
26061
26080
|
page2 = page2 > 0 ? page2 - 1 : 0;
|
|
26062
26081
|
if (org && typeof org === "string" && org.length === 24) {
|
|
@@ -26078,6 +26097,9 @@ function useRoleRepo() {
|
|
|
26078
26097
|
if (site) {
|
|
26079
26098
|
query2.$or = [{ site }, { site: "" }, { site: null }];
|
|
26080
26099
|
cacheOptions.site = site.toString();
|
|
26100
|
+
} else if (scope === "organisation") {
|
|
26101
|
+
query2.site = { $in: ["", null] };
|
|
26102
|
+
cacheOptions.scope = scope;
|
|
26081
26103
|
}
|
|
26082
26104
|
sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
|
|
26083
26105
|
cacheOptions.sort = JSON.stringify(sort);
|
|
@@ -28714,7 +28736,9 @@ function useRoleController() {
|
|
|
28714
28736
|
limit: import_joi28.default.number().integer().min(1).max(100).allow("", null).default(10),
|
|
28715
28737
|
type: import_joi28.default.string().optional().allow("", null),
|
|
28716
28738
|
org: import_joi28.default.string().hex().optional().allow("", null),
|
|
28717
|
-
site: import_joi28.default.string().hex().optional().allow("", null)
|
|
28739
|
+
site: import_joi28.default.string().hex().optional().allow("", null),
|
|
28740
|
+
// "organisation": only the roles with no site (Organization app, service roles made once)
|
|
28741
|
+
scope: import_joi28.default.string().valid("organisation").optional().allow("", null)
|
|
28718
28742
|
});
|
|
28719
28743
|
const query2 = { ...req.query };
|
|
28720
28744
|
const { error } = validation.validate(query2);
|
|
@@ -28729,18 +28753,19 @@ function useRoleController() {
|
|
|
28729
28753
|
const type = req.query.type ?? "";
|
|
28730
28754
|
const org = req.query.org ?? "";
|
|
28731
28755
|
const site = req.query.site ?? "";
|
|
28756
|
+
const scope = req.query.scope ?? "";
|
|
28732
28757
|
try {
|
|
28733
28758
|
if (org) {
|
|
28734
28759
|
await requireOrgReach(req, org);
|
|
28735
28760
|
}
|
|
28736
|
-
let data = await _getRoles({ search, page: page2, limit, type, org, site });
|
|
28737
|
-
const listIsWholeOrg = org && !search && !site && page2 === 1;
|
|
28761
|
+
let data = await _getRoles({ search, page: page2, limit, type, org, site, scope });
|
|
28762
|
+
const listIsWholeOrg = org && !search && !site && !scope && page2 === 1;
|
|
28738
28763
|
const listed = data?.items;
|
|
28739
28764
|
if (listIsWholeOrg && listed?.length === 0) {
|
|
28740
28765
|
const seeded = await healDefaultRoles(org).catch(() => 0);
|
|
28741
28766
|
if (seeded > 0) {
|
|
28742
28767
|
try {
|
|
28743
|
-
data = await _getRoles({ search, page: page2, limit, type, org, site });
|
|
28768
|
+
data = await _getRoles({ search, page: page2, limit, type, org, site, scope });
|
|
28744
28769
|
} catch (error2) {
|
|
28745
28770
|
import_node_server_utils72.logger.log({ level: "error", message: error2.message });
|
|
28746
28771
|
}
|
|
@@ -29188,7 +29213,8 @@ function useMemberService() {
|
|
|
29188
29213
|
}
|
|
29189
29214
|
}
|
|
29190
29215
|
}
|
|
29191
|
-
|
|
29216
|
+
const placedBy = member.siteId && !newRole?.site && callerId5 && import_mongodb61.ObjectId.isValid(callerId5) && String(member.user ?? "") !== callerId5 ? callerId5 : void 0;
|
|
29217
|
+
await _updateRoleById(id, role, placedBy);
|
|
29192
29218
|
return { message: "Member role updated successfully." };
|
|
29193
29219
|
}
|
|
29194
29220
|
async function updateSiteById(id, siteId, siteName) {
|
|
@@ -29236,6 +29262,7 @@ function useMemberService() {
|
|
|
29236
29262
|
var import_node_server_utils74 = require("@7365admin1/node-server-utils");
|
|
29237
29263
|
var import_mongodb62 = require("mongodb");
|
|
29238
29264
|
var EXCLUDED_TYPES = ["resident", PLATFORM_STAFF_MEMBER_TYPE];
|
|
29265
|
+
var ORG_ROLE_EXCLUDED = [...EXCLUDED_TYPES, "organization"];
|
|
29239
29266
|
var both = (id) => [id, new import_mongodb62.ObjectId(id)];
|
|
29240
29267
|
function useMemberSiteAccessService() {
|
|
29241
29268
|
const { add, updateMemberStatus, updateRoleById } = useMemberRepo();
|
|
@@ -29275,10 +29302,13 @@ function useMemberSiteAccessService() {
|
|
|
29275
29302
|
const roles = await db2().collection("roles").find({
|
|
29276
29303
|
$or: [
|
|
29277
29304
|
{ _id: { $in: roleIds.map((r) => new import_mongodb62.ObjectId(r)) } },
|
|
29278
|
-
{ org: { $in: both(org) }, site: { $in: [...sites.keys()].flatMap(both) }, status: "active" }
|
|
29305
|
+
{ org: { $in: both(org) }, site: { $in: [...sites.keys()].flatMap(both) }, status: "active" },
|
|
29306
|
+
// organisation-level service roles (no site; never the org-console type)
|
|
29307
|
+
{ org: { $in: both(org) }, site: { $in: ["", null] }, status: "active", type: { $nin: ORG_ROLE_EXCLUDED } }
|
|
29279
29308
|
]
|
|
29280
29309
|
}).project({ name: 1, type: 1, site: 1, org: 1, status: 1 }).toArray();
|
|
29281
29310
|
const byId = new Map(roles.map((r) => [String(r._id), r]));
|
|
29311
|
+
const orgRoles = roles.filter((r) => !String(r.site ?? "") && String(r.org ?? "") === org && r.status === "active" && !ORG_ROLE_EXCLUDED.includes(r.type)).map((r) => ({ _id: String(r._id), name: r.name ?? "", type: r.type ?? "", scope: "organisation" })).sort((a, b) => a.name.localeCompare(b.name));
|
|
29282
29312
|
const view = (m) => ({
|
|
29283
29313
|
_id: String(m._id),
|
|
29284
29314
|
role: String(m.role ?? ""),
|
|
@@ -29292,7 +29322,7 @@ function useMemberSiteAccessService() {
|
|
|
29292
29322
|
...s,
|
|
29293
29323
|
membership: here[0] ?? null,
|
|
29294
29324
|
memberships: here,
|
|
29295
|
-
roles: roles.filter((r) => String(r.site ?? "") === s.site && String(r.org ?? "") === org && r.status === "active" && !EXCLUDED_TYPES.includes(r.type)).map((r) => ({ _id: String(r._id), name: r.name ?? "", type: r.type ?? "" }))
|
|
29325
|
+
roles: roles.filter((r) => String(r.site ?? "") === s.site && String(r.org ?? "") === org && r.status === "active" && !EXCLUDED_TYPES.includes(r.type)).map((r) => ({ _id: String(r._id), name: r.name ?? "", type: r.type ?? "", scope: "site" })).concat(orgRoles)
|
|
29296
29326
|
};
|
|
29297
29327
|
}).sort((a, b) => a.name.localeCompare(b.name));
|
|
29298
29328
|
const orgWide = members.filter((m) => !String(m.siteId ?? "")).map(view);
|
|
@@ -29318,7 +29348,7 @@ function useMemberSiteAccessService() {
|
|
|
29318
29348
|
throw new import_node_server_utils74.BadRequestError("A site is listed twice.");
|
|
29319
29349
|
seen2.add(choice.site);
|
|
29320
29350
|
if (!row.roles.some((r) => r._id === choice.role)) {
|
|
29321
|
-
throw new import_node_server_utils74.BadRequestError(`Choose a role created for ${row.name || "that site"}.`);
|
|
29351
|
+
throw new import_node_server_utils74.BadRequestError(`Choose a role created for ${row.name || "that site"}, or one of the organisation's service roles.`);
|
|
29322
29352
|
}
|
|
29323
29353
|
}
|
|
29324
29354
|
const userDoc = await db2().collection("users").findOne({ _id: new import_mongodb62.ObjectId(user) }, { projection: { email: 1, name: 1 } });
|
|
@@ -29332,10 +29362,11 @@ function useMemberSiteAccessService() {
|
|
|
29332
29362
|
const choice = sites.find((c) => c.site === row.site);
|
|
29333
29363
|
const role = choice ? row.roles.find((r) => r._id === choice.role) : null;
|
|
29334
29364
|
const keep = role ? row.memberships.find((m) => m.type === role.type) : void 0;
|
|
29335
|
-
if (role && keep && keep.role !== role._id) {
|
|
29365
|
+
if (role && keep && (keep.role !== role._id || role.scope === "organisation")) {
|
|
29336
29366
|
reroles.push(async () => {
|
|
29337
|
-
await updateRoleById(keep._id, role._id);
|
|
29338
|
-
|
|
29367
|
+
await updateRoleById(keep._id, role._id, actor);
|
|
29368
|
+
if (keep.role !== role._id)
|
|
29369
|
+
applied.changed.push(row.site);
|
|
29339
29370
|
});
|
|
29340
29371
|
} else if (role && !keep) {
|
|
29341
29372
|
adds.push(async () => {
|
|
@@ -29344,7 +29375,7 @@ function useMemberSiteAccessService() {
|
|
|
29344
29375
|
{ projection: { _id: 1 } }
|
|
29345
29376
|
);
|
|
29346
29377
|
if (old) {
|
|
29347
|
-
await updateRoleById(old._id, role._id);
|
|
29378
|
+
await updateRoleById(old._id, role._id, actor);
|
|
29348
29379
|
await updateMemberStatus(old._id, { status: suspended ? "suspended" : "active" });
|
|
29349
29380
|
} else {
|
|
29350
29381
|
await add({
|
|
@@ -29358,6 +29389,8 @@ function useMemberSiteAccessService() {
|
|
|
29358
29389
|
siteId: row.site,
|
|
29359
29390
|
siteName: row.name,
|
|
29360
29391
|
dateInvited: (/* @__PURE__ */ new Date()).toISOString(),
|
|
29392
|
+
// "placed at this site by the editor": confines an organisation role to it (siteInvitedPins)
|
|
29393
|
+
createdBy: actor,
|
|
29361
29394
|
...suspended ? { status: "suspended" } : {}
|
|
29362
29395
|
});
|
|
29363
29396
|
}
|