@7365admin1/core 3.125.1-staging.419 → 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": minor
3
+ ---
4
+
5
+ "Last active": every session issued (web sign-in, mobile sign-in, mobile session refresh) stamps `users.lastLoginAt` without awaiting it, so a failed write never blocks or slows a sign-in. `GET /api/members` and `GET /api/service-providers/site-members` return `lastLoginAt` per person (absent/null until their next sign-in; no backfill).
@@ -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.d.ts CHANGED
@@ -119,6 +119,8 @@ type TUser = {
119
119
  defaultOrg?: string | ObjectId;
120
120
  status?: string;
121
121
  sid?: string;
122
+ /** Last time a session was issued (sign-in or mobile session refresh). Shown as "Last active". */
123
+ lastLoginAt?: Date;
122
124
  block?: string | ObjectId | null;
123
125
  level?: string | ObjectId | null;
124
126
  type?: string;
@@ -214,6 +216,7 @@ declare function useUserRepo(): {
214
216
  updateDefaultOrgByEmail: (email: string, value: string, session?: ClientSession) => Promise<string>;
215
217
  getUserByEmailStatus: (email: string) => Promise<TUser | null>;
216
218
  updateUserSIDById: (id: string | ObjectId, sid: string, session?: ClientSession) => Promise<mongodb.UpdateResult<bson.Document>>;
219
+ recordLastLogin: (id: string | ObjectId) => Promise<void>;
217
220
  resetPassword: ({ _id, password, sid, }: {
218
221
  _id: string | ObjectId;
219
222
  password: string;
package/dist/index.js CHANGED
@@ -4157,6 +4157,9 @@ function useUserRepo() {
4157
4157
  throw new import_node_server_utils12.InternalServerError("Failed to update user.");
4158
4158
  }
4159
4159
  }
4160
+ async function recordLastLogin(id) {
4161
+ await collection.updateOne({ _id: (0, import_node_server_utils12.toObjectId)(id) }, { $set: { lastLoginAt: /* @__PURE__ */ new Date() } });
4162
+ }
4160
4163
  async function updateUserOrgById(id, org, session) {
4161
4164
  const _id = (0, import_node_server_utils12.toObjectId)(id);
4162
4165
  const orgId = (0, import_node_server_utils12.toObjectId)(org);
@@ -4318,6 +4321,7 @@ function useUserRepo() {
4318
4321
  updateDefaultOrgByEmail,
4319
4322
  getUserByEmailStatus,
4320
4323
  updateUserSIDById,
4324
+ recordLastLogin,
4321
4325
  resetPassword,
4322
4326
  updateUserOrgById,
4323
4327
  updateUserUnitById
@@ -4942,7 +4946,7 @@ function useMemberRepo() {
4942
4946
  localField: "user",
4943
4947
  foreignField: "_id",
4944
4948
  as: "users",
4945
- pipeline: [{ $project: { email: 1 } }]
4949
+ pipeline: [{ $project: { email: 1, lastLoginAt: 1 } }]
4946
4950
  }
4947
4951
  },
4948
4952
  { $unwind: { path: "$users", preserveNullAndEmptyArrays: true } },
@@ -4951,6 +4955,7 @@ function useMemberRepo() {
4951
4955
  name: 1,
4952
4956
  email: { $ifNull: ["$email", "$users.email"] },
4953
4957
  user: 1,
4958
+ lastLoginAt: "$users.lastLoginAt",
4954
4959
  roleName: "$roles.name",
4955
4960
  role: "$roles._id",
4956
4961
  // Whether roleName is a role this member can really use, so the
@@ -5836,7 +5841,8 @@ function useAuthService() {
5836
5841
  const {
5837
5842
  getUserByEmail,
5838
5843
  getUserById: _getUserById,
5839
- updateUserSIDById: _updateUserSIDById
5844
+ updateUserSIDById: _updateUserSIDById,
5845
+ recordLastLogin
5840
5846
  } = useUserRepo();
5841
5847
  const {
5842
5848
  createFamily: _createFamily,
@@ -5855,6 +5861,7 @@ function useAuthService() {
5855
5861
  const updatedUser = await _getUserById(userId);
5856
5862
  assertAccountLive(updatedUser?.status);
5857
5863
  await rememberSession(userId, sid);
5864
+ recordLastLogin(userId).catch((error) => console.error("Error recording last login:", error));
5858
5865
  setCache(cacheKey, updatedUser, SESSION_TTL).then(() => {
5859
5866
  console.log("Session ID cached successfully");
5860
5867
  }).catch((error) => {
@@ -13683,7 +13690,7 @@ function useServiceProviderRepo() {
13683
13690
  localField: "user",
13684
13691
  foreignField: "_id",
13685
13692
  as: "_user",
13686
- pipeline: [{ $project: { email: 1, name: 1 } }]
13693
+ pipeline: [{ $project: { email: 1, name: 1, lastLoginAt: 1 } }]
13687
13694
  }
13688
13695
  },
13689
13696
  {
@@ -13709,7 +13716,8 @@ function useServiceProviderRepo() {
13709
13716
  email: {
13710
13717
  $ifNull: [{ $arrayElemAt: ["$_user.email", 0] }, "$email"]
13711
13718
  },
13712
- roleName: { $ifNull: [{ $arrayElemAt: ["$_role.name", 0] }, ""] }
13719
+ roleName: { $ifNull: [{ $arrayElemAt: ["$_role.name", 0] }, ""] },
13720
+ lastLoginAt: { $arrayElemAt: ["$_user.lastLoginAt", 0] }
13713
13721
  }
13714
13722
  }
13715
13723
  ]).toArray();
@@ -25605,6 +25613,43 @@ function useRoleRepo() {
25605
25613
  }
25606
25614
  const { delNamespace, setCache, getCache } = (0, import_node_server_utils61.useCache)(namespace_collection3);
25607
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
+ }
25608
25653
  async function addRole(value, session) {
25609
25654
  value = new MRole({ ...value, type: serviceAppType(value.type) });
25610
25655
  try {
@@ -25622,7 +25667,7 @@ function useRoleRepo() {
25622
25667
  import_node_server_utils61.logger.log({ level: "error", message: `${error}` });
25623
25668
  const isDuplicated = error.message.includes("duplicate");
25624
25669
  if (isDuplicated) {
25625
- throw new import_node_server_utils61.BadRequestError("Role already exists.");
25670
+ throw new import_node_server_utils61.BadRequestError(await nameTakenMessage(value));
25626
25671
  }
25627
25672
  throw new import_node_server_utils61.InternalServerError("Failed to create role.");
25628
25673
  }
@@ -25851,7 +25896,10 @@ function useRoleRepo() {
25851
25896
  } catch (error) {
25852
25897
  const isDuplicated = error.message.includes("duplicate");
25853
25898
  if (isDuplicated) {
25854
- 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
+ );
25855
25903
  }
25856
25904
  throw new import_node_server_utils61.InternalServerError("Failed to update role.");
25857
25905
  }
@@ -36326,6 +36374,7 @@ function useServiceProviderService() {
36326
36374
  role: m.roleName ?? "",
36327
36375
  status: m.status ?? "active",
36328
36376
  addedAt: m.createdAt ?? null,
36377
+ lastLoginAt: m.lastLoginAt ?? null,
36329
36378
  invitedVia: company.invitedVia ?? ""
36330
36379
  };
36331
36380
  });
@@ -99885,6 +99934,7 @@ var SESSION_TTL2 = 14400;
99885
99934
  var REFRESH_TOKEN_TTL2 = 7 * 24 * 60 * 60;
99886
99935
  function useAuthServiceV2() {
99887
99936
  const { getUserByEmail, getUserById: _getUserById } = useUserRepoV2();
99937
+ const { recordLastLogin } = useUserRepo();
99888
99938
  const {
99889
99939
  createFamily: _createFamily,
99890
99940
  findActiveByToken: _findActiveByToken,
@@ -99901,6 +99951,8 @@ function useAuthServiceV2() {
99901
99951
  const cacheKey = `sid:${sid}`;
99902
99952
  const { password: _password, ...safeUser } = user;
99903
99953
  await rememberSession(user?._id, sid);
99954
+ if (user?._id)
99955
+ recordLastLogin(user._id).catch((error) => console.error("Error recording last login:", error));
99904
99956
  setCache(cacheKey, safeUser, SESSION_TTL2).then(() => {
99905
99957
  console.log("Session ID cached successfully");
99906
99958
  }).catch((error) => {