@7365admin1/core 3.125.1-staging.420 → 3.125.1-staging.421

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": patch
3
+ ---
4
+
5
+ Create/rename role: a name already used in the organisation now says where (e.g. "already exists in this organisation at Winsland House II") and suggests a site-specific name, instead of the bare "Role already exists.". Role names stay unique per organisation and application.
package/dist/index.js CHANGED
@@ -25613,6 +25613,43 @@ function useRoleRepo() {
25613
25613
  }
25614
25614
  const { delNamespace, setCache, getCache } = (0, import_node_server_utils61.useCache)(namespace_collection3);
25615
25615
  const { delNamespace: _delDashboardNameSpace } = (0, import_node_server_utils61.useCache)("dashboard");
25616
+ async function nameTakenMessage(value) {
25617
+ const name = String(value?.name ?? "");
25618
+ const fallback = `A role named "${name}" already exists in this organisation. Please choose a different name.`;
25619
+ try {
25620
+ const clash = await collection.findOne(
25621
+ {
25622
+ name,
25623
+ type: value.type,
25624
+ org: value.org,
25625
+ deletedAt: value.deletedAt ?? ""
25626
+ },
25627
+ { projection: { site: 1 } }
25628
+ );
25629
+ if (!clash)
25630
+ return fallback;
25631
+ if (!clash.site) {
25632
+ return `A role named "${name}" already exists for the whole organisation. Role names must be different across the organisation and all its sites - please choose a different name.`;
25633
+ }
25634
+ const siteName = async (id) => {
25635
+ if (!id)
25636
+ return "";
25637
+ const doc = await db2?.collection("sites").findOne(
25638
+ { _id: new import_mongodb57.ObjectId(String(id)) },
25639
+ { projection: { name: 1 } }
25640
+ ).catch(() => null);
25641
+ return doc?.name ?? "";
25642
+ };
25643
+ if (value.site && String(value.site) === String(clash.site)) {
25644
+ return `A role named "${name}" already exists at this site. Please choose a different name.`;
25645
+ }
25646
+ const [there, here] = await Promise.all([siteName(clash.site), siteName(value.site)]);
25647
+ const suggestion = here ? `, for example "${name} - ${here}"` : "";
25648
+ return `A role named "${name}" already exists in this organisation${there ? ` at ${there}` : " at another site"}. Role names must be different across all your sites - please choose a different name${suggestion}.`;
25649
+ } catch {
25650
+ return fallback;
25651
+ }
25652
+ }
25616
25653
  async function addRole(value, session) {
25617
25654
  value = new MRole({ ...value, type: serviceAppType(value.type) });
25618
25655
  try {
@@ -25630,7 +25667,7 @@ function useRoleRepo() {
25630
25667
  import_node_server_utils61.logger.log({ level: "error", message: `${error}` });
25631
25668
  const isDuplicated = error.message.includes("duplicate");
25632
25669
  if (isDuplicated) {
25633
- throw new import_node_server_utils61.BadRequestError("Role already exists.");
25670
+ throw new import_node_server_utils61.BadRequestError(await nameTakenMessage(value));
25634
25671
  }
25635
25672
  throw new import_node_server_utils61.InternalServerError("Failed to create role.");
25636
25673
  }
@@ -25859,7 +25896,10 @@ function useRoleRepo() {
25859
25896
  } catch (error) {
25860
25897
  const isDuplicated = error.message.includes("duplicate");
25861
25898
  if (isDuplicated) {
25862
- throw new import_node_server_utils61.BadRequestError("Role name already exists.");
25899
+ const current = await collection.findOne({ _id }, { projection: { type: 1, org: 1, site: 1, deletedAt: 1 } }).catch(() => null);
25900
+ throw new import_node_server_utils61.BadRequestError(
25901
+ await nameTakenMessage({ ...current ?? {}, name: value.name })
25902
+ );
25863
25903
  }
25864
25904
  throw new import_node_server_utils61.InternalServerError("Failed to update role.");
25865
25905
  }