@7365admin1/core 2.34.0 → 2.35.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @iservice365/core
2
2
 
3
+ ## 2.35.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 9157cc3: Improve dob search entry / fix visitor transaction pick up
8
+
9
+ ## 2.35.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 2d50f94: get latest changes
14
+
3
15
  ## 2.34.0
4
16
 
5
17
  ### Minor Changes
package/dist/index.d.ts CHANGED
@@ -62,6 +62,7 @@ type TUser = {
62
62
  name: string;
63
63
  defaultOrg?: string | ObjectId;
64
64
  status?: string;
65
+ sid?: string;
65
66
  createdAt?: string;
66
67
  updatedAt?: string;
67
68
  deletedAt?: string;
@@ -122,6 +123,12 @@ declare function useUserRepo(): {
122
123
  }, session?: ClientSession) => Promise<string>;
123
124
  updateDefaultOrgByEmail: (email: string, value: string, session?: ClientSession) => Promise<string>;
124
125
  getUserByEmailStatus: (email: string) => Promise<TUser | null>;
126
+ updateUserSIDById: (id: string | ObjectId, sid: string, session?: ClientSession) => Promise<mongodb.UpdateResult<bson.Document>>;
127
+ resetPassword: ({ _id, password, sid, }: {
128
+ _id: string | ObjectId;
129
+ password: string;
130
+ sid: string;
131
+ }, session?: ClientSession) => Promise<mongodb.UpdateResult<bson.Document>>;
125
132
  };
126
133
 
127
134
  declare function useUserController(): {
@@ -2595,11 +2602,11 @@ declare function MVisitorTransaction(value: TVisitorTransaction): {
2595
2602
  numberOfPassengers: number | null;
2596
2603
  email: string | undefined;
2597
2604
  isOvernightParking: boolean;
2598
- invitedId: ObjectId;
2605
+ invitedId: string | ObjectId;
2599
2606
  overnightParking: {
2600
2607
  status: string;
2601
2608
  remarks: string;
2602
- updatedBy: ObjectId;
2609
+ updatedBy: string | ObjectId;
2603
2610
  } | null;
2604
2611
  };
2605
2612
 
@@ -6362,6 +6369,7 @@ declare function useAuthControllerV2(): {
6362
6369
  login: (req: Request, res: Response, next: NextFunction) => Promise<void>;
6363
6370
  logout: (req: Request, res: Response, next: NextFunction) => Promise<void>;
6364
6371
  resetPassword: (req: Request, res: Response, next: NextFunction) => Promise<void>;
6372
+ verifyPassword: (req: Request, res: Response, next: NextFunction) => Promise<void>;
6365
6373
  };
6366
6374
 
6367
6375
  declare function useAuthServiceV2(): {
@@ -6375,6 +6383,7 @@ declare function useAuthServiceV2(): {
6375
6383
  user: string;
6376
6384
  }>;
6377
6385
  logout: (sid: string) => Promise<string>;
6386
+ verifyPassword: (_id: string | ObjectId, password: string) => Promise<string>;
6378
6387
  };
6379
6388
 
6380
6389
  declare function useUserRepoV2(): {
package/dist/index.js CHANGED
@@ -1977,10 +1977,21 @@ function useOccurrenceEntryRepo() {
1977
1977
  } catch (error) {
1978
1978
  throw new import_node_server_utils8.BadRequestError("Invalid site ID format.");
1979
1979
  }
1980
- const baseQuery = {
1981
- site
1980
+ const escapedSearch = search ? search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") : "";
1981
+ const query = {
1982
+ site,
1983
+ ...search && {
1984
+ $or: [
1985
+ { occurrence: { $regex: escapedSearch, $options: "i" } },
1986
+ { bookEntryCount: { $regex: escapedSearch, $options: "i" } },
1987
+ { subjectName: { $regex: escapedSearch, $options: "i" } },
1988
+ { userName: { $regex: escapedSearch, $options: "i" } }
1989
+ ]
1990
+ },
1991
+ ...dailyOccurrenceBookId && {
1992
+ dailyOccurrenceBookId: typeof dailyOccurrenceBookId === "string" ? new import_mongodb7.ObjectId(dailyOccurrenceBookId) : dailyOccurrenceBookId
1993
+ }
1982
1994
  };
1983
- let query = { ...baseQuery };
1984
1995
  sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
1985
1996
  const cacheOptions = {
1986
1997
  site: site.toString(),
@@ -1989,20 +2000,9 @@ function useOccurrenceEntryRepo() {
1989
2000
  limit,
1990
2001
  ...dailyOccurrenceBookId && {
1991
2002
  dailyOccurrenceBookId
1992
- }
2003
+ },
2004
+ ...search && { search }
1993
2005
  };
1994
- if (search) {
1995
- query.$text = { $search: search };
1996
- cacheOptions.search = search;
1997
- }
1998
- if (dailyOccurrenceBookId) {
1999
- try {
2000
- dailyOccurrenceBookId = new import_mongodb7.ObjectId(dailyOccurrenceBookId);
2001
- query.dailyOccurrenceBookId = dailyOccurrenceBookId;
2002
- } catch (error) {
2003
- throw new import_node_server_utils8.BadRequestError("Invalid daily occurrence book ID format.");
2004
- }
2005
- }
2006
2006
  const cacheKey = (0, import_node_server_utils8.makeCacheKey)(namespace_collection, cacheOptions);
2007
2007
  const cachedData = await getCache(cacheKey);
2008
2008
  if (cachedData) {
@@ -2104,19 +2104,6 @@ function useOccurrenceEntryRepo() {
2104
2104
  { session }
2105
2105
  ).toArray();
2106
2106
  length = await collection.countDocuments(query, { session });
2107
- if ((!items || items.length === 0) && search) {
2108
- const regexQuery = {
2109
- ...baseQuery,
2110
- $or: [{ occurrence: { $regex: search, $options: "i" } }]
2111
- };
2112
- items = await collection.aggregate([
2113
- { $match: regexQuery },
2114
- { $sort: sort },
2115
- { $skip: page * limit },
2116
- { $limit: limit }
2117
- ]).toArray();
2118
- length = await collection.countDocuments(regexQuery, { session });
2119
- }
2120
2107
  const data = (0, import_node_server_utils8.paginate)(items, page, limit, length);
2121
2108
  setCache(cacheKey, data, 15 * 60).then(() => {
2122
2109
  import_node_server_utils8.logger.info(`Cache set for key: ${cacheKey}`);
@@ -2605,20 +2592,9 @@ function useUserRepo() {
2605
2592
  }
2606
2593
  async function getUserByEmail(email) {
2607
2594
  try {
2608
- const cacheKey = (0, import_node_server_utils10.makeCacheKey)(namespace_collection, { email });
2609
- const cachedData = await getCache(cacheKey);
2610
- if (cachedData) {
2611
- import_node_server_utils10.logger.info(`Cache hit for key: ${cacheKey}`);
2612
- return cachedData;
2613
- }
2614
2595
  const data = await collection.findOne({
2615
2596
  email: { $regex: `^${email}$`, $options: "i" }
2616
2597
  });
2617
- setCache(cacheKey, data, 15 * 60).then(() => {
2618
- import_node_server_utils10.logger.info(`Cache set for key: ${cacheKey}`);
2619
- }).catch((err) => {
2620
- import_node_server_utils10.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
2621
- });
2622
2598
  return data;
2623
2599
  } catch (error) {
2624
2600
  throw new import_node_server_utils10.InternalServerError("Failed to get user by email.");
@@ -2925,6 +2901,39 @@ function useUserRepo() {
2925
2901
  throw new import_node_server_utils10.InternalServerError("Failed to update user password.");
2926
2902
  }
2927
2903
  }
2904
+ async function resetPassword({
2905
+ _id,
2906
+ password,
2907
+ sid
2908
+ }, session) {
2909
+ try {
2910
+ _id = new import_mongodb9.ObjectId(_id);
2911
+ } catch (error) {
2912
+ throw new import_node_server_utils10.BadRequestError("Invalid user ID format.");
2913
+ }
2914
+ try {
2915
+ const result = await collection.updateOne(
2916
+ { _id },
2917
+ { $set: { password, updatedAt: (/* @__PURE__ */ new Date()).toISOString() } },
2918
+ { session }
2919
+ );
2920
+ const cacheKey = (0, import_node_server_utils10.makeCacheKey)(namespace_collection, { _id });
2921
+ delCache(cacheKey).then(() => {
2922
+ import_node_server_utils10.logger.info(`Cache deleted for key: ${cacheKey}`);
2923
+ }).catch((err) => {
2924
+ import_node_server_utils10.logger.error(`Failed to delete cache for key: ${cacheKey}`, err);
2925
+ });
2926
+ const authCacheKey = `sid:${sid}`;
2927
+ delCache(authCacheKey).then(() => {
2928
+ import_node_server_utils10.logger.info(`Cache deleted for key: ${authCacheKey}`);
2929
+ }).catch((err) => {
2930
+ import_node_server_utils10.logger.error(`Failed to delete cache for key: ${authCacheKey}`, err);
2931
+ });
2932
+ return result;
2933
+ } catch (error) {
2934
+ throw new import_node_server_utils10.InternalServerError("Failed to update user password.");
2935
+ }
2936
+ }
2928
2937
  async function updateBirthday({
2929
2938
  _id,
2930
2939
  month,
@@ -3017,6 +3026,19 @@ function useUserRepo() {
3017
3026
  throw new import_node_server_utils10.InternalServerError(`Failed to update user ${field}.`);
3018
3027
  }
3019
3028
  }
3029
+ async function updateUserSIDById(id, sid, session) {
3030
+ const _id = (0, import_node_server_utils10.toObjectId)(id);
3031
+ try {
3032
+ const result = await collection.updateOne(
3033
+ { _id },
3034
+ { $set: { sid, updatedAt: (/* @__PURE__ */ new Date()).toISOString() } },
3035
+ { session }
3036
+ );
3037
+ return result;
3038
+ } catch (error) {
3039
+ throw new import_node_server_utils10.InternalServerError("Failed to update user.");
3040
+ }
3041
+ }
3020
3042
  return {
3021
3043
  createIndex,
3022
3044
  createTextIndex,
@@ -3032,7 +3054,9 @@ function useUserRepo() {
3032
3054
  updateBirthday,
3033
3055
  updateUserFieldById,
3034
3056
  updateDefaultOrgByEmail,
3035
- getUserByEmailStatus
3057
+ getUserByEmailStatus,
3058
+ updateUserSIDById,
3059
+ resetPassword
3036
3060
  };
3037
3061
  }
3038
3062
 
@@ -3860,7 +3884,11 @@ function useMemberRepo() {
3860
3884
 
3861
3885
  // src/services/auth.service.ts
3862
3886
  function useAuthService() {
3863
- const { getUserByEmail, getUserById: _getUserById } = useUserRepo();
3887
+ const {
3888
+ getUserByEmail,
3889
+ getUserById: _getUserById,
3890
+ updateUserSIDById: _updateUserSIDById
3891
+ } = useUserRepo();
3864
3892
  const { getByToken, deleteByToken } = useSessionRepo();
3865
3893
  const expiresIn = "15m";
3866
3894
  const { setCache, delCache } = (0, import_node_server_utils13.useCache)("sessions");
@@ -3898,7 +3926,9 @@ function useAuthService() {
3898
3926
  }
3899
3927
  const sid = (0, import_uuid.v4)();
3900
3928
  const cacheKey = `sid:${sid}`;
3901
- setCache(cacheKey, user, 14400).then(() => {
3929
+ await _updateUserSIDById(user._id, sid);
3930
+ const updatedUser = await _getUserById(user._id);
3931
+ setCache(cacheKey, updatedUser, 14400).then(() => {
3902
3932
  console.log("Session ID cached successfully");
3903
3933
  }).catch((error) => {
3904
3934
  console.error("Error caching session ID:", error);
@@ -6063,7 +6093,8 @@ function useUserService() {
6063
6093
  getUserById,
6064
6094
  getUserByEmail,
6065
6095
  updatePassword,
6066
- updateUserFieldById: _updateUserFieldById
6096
+ updateUserFieldById: _updateUserFieldById,
6097
+ resetPassword: _resetPassword
6067
6098
  } = useUserRepo();
6068
6099
  const { getRoleByName, addRole } = useRoleRepo();
6069
6100
  const { add: addMember } = useMemberRepo();
@@ -6239,8 +6270,12 @@ function useUserService() {
6239
6270
  throw new import_node_server_utils24.InternalServerError("Invalid user ID.");
6240
6271
  }
6241
6272
  await updateStatusById(id, "complete", session);
6242
- await updatePassword(
6243
- { _id: user._id.toString(), password: hashedPassword },
6273
+ await _resetPassword(
6274
+ {
6275
+ _id: user._id.toString(),
6276
+ password: hashedPassword,
6277
+ sid: user.sid
6278
+ },
6244
6279
  session
6245
6280
  );
6246
6281
  await session?.commitTransaction();
@@ -8924,7 +8959,7 @@ var import_node_server_utils42 = require("@7365admin1/node-server-utils");
8924
8959
  var import_zod = require("zod");
8925
8960
  var import_mongodb26 = require("mongodb");
8926
8961
  var import_node_server_utils41 = require("@7365admin1/node-server-utils");
8927
- function toObjectId3(value) {
8962
+ function toObjectId4(value) {
8928
8963
  if (typeof value === "string") {
8929
8964
  if (!/^[a-fA-F0-9]{24}$/.test(value)) {
8930
8965
  throw new import_node_server_utils41.BadRequestError(`Invalid ObjectId format: ${value}`);
@@ -8976,7 +9011,7 @@ var TInvoice = import_zod.z.object({
8976
9011
  message: "Invalid ObjectId: Must be a 24-character hex string."
8977
9012
  }),
8978
9013
  import_zod.z.instanceof(import_mongodb26.ObjectId, { message: "Invalid ObjectId instance." })
8979
- ]).optional().transform((val) => val ? toObjectId3(val) : void 0),
9014
+ ]).optional().transform((val) => val ? toObjectId4(val) : void 0),
8980
9015
  invoiceNumber: import_zod.z.string({ required_error: "Invoice number is required." }),
8981
9016
  type: TInvoiceType.default("other"),
8982
9017
  amount: import_zod.z.number().min(0, { message: "Invoice amount must be at least 0." }),
@@ -13429,21 +13464,21 @@ function MVisitorTransaction(value) {
13429
13464
  try {
13430
13465
  value.org = new import_mongodb39.ObjectId(value.org);
13431
13466
  } catch (error2) {
13432
- throw new Error("Invalid ID.");
13467
+ throw new Error("Invalid org ID.");
13433
13468
  }
13434
13469
  }
13435
13470
  if (value.site && typeof value.site === "string") {
13436
13471
  try {
13437
13472
  value.site = new import_mongodb39.ObjectId(value.site);
13438
13473
  } catch (error2) {
13439
- throw new Error("Invalid ID.");
13474
+ throw new Error("Invalid site ID.");
13440
13475
  }
13441
13476
  }
13442
13477
  if (value.unit && typeof value.unit === "string") {
13443
13478
  try {
13444
13479
  value.unit = new import_mongodb39.ObjectId(value.unit);
13445
13480
  } catch (error2) {
13446
- throw new Error("Invalid ID.");
13481
+ throw new Error("Invalid unit ID.");
13447
13482
  }
13448
13483
  }
13449
13484
  if (value.manualCheckout && typeof value.manualCheckout === "string") {
@@ -13473,6 +13508,13 @@ function MVisitorTransaction(value) {
13473
13508
  return p;
13474
13509
  });
13475
13510
  }
13511
+ if (value.invitedId && typeof value.invitedId === "string") {
13512
+ try {
13513
+ value.invitedId = new import_mongodb39.ObjectId(value.invitedId);
13514
+ } catch (error2) {
13515
+ throw new Error("Invalid invited ID.");
13516
+ }
13517
+ }
13476
13518
  const newDate = (/* @__PURE__ */ new Date()).toISOString();
13477
13519
  return {
13478
13520
  _id: value._id,
@@ -13513,11 +13555,11 @@ function MVisitorTransaction(value) {
13513
13555
  numberOfPassengers: value.numberOfPassengers ?? null,
13514
13556
  email: value.email,
13515
13557
  isOvernightParking: value.isOvernightParking ?? false,
13516
- invitedId: new import_mongodb39.ObjectId(String(value.invitedId)) ?? null,
13558
+ invitedId: value.invitedId ?? "",
13517
13559
  overnightParking: value.isOvernightParking == true ? {
13518
13560
  status: "pending approval",
13519
13561
  remarks: "",
13520
- updatedBy: new import_mongodb39.ObjectId(String(value.invitedId)) ?? null
13562
+ updatedBy: value.invitedId ?? ""
13521
13563
  } : null
13522
13564
  };
13523
13565
  }
@@ -48309,9 +48351,25 @@ function useAuthServiceV2() {
48309
48351
  throw new import_node_server_utils222.InternalServerError("Error deleting token");
48310
48352
  }
48311
48353
  }
48354
+ async function verifyPassword(_id, password) {
48355
+ try {
48356
+ const user = await _getUserById(_id);
48357
+ const isPasswordMatch = await (0, import_node_server_utils222.comparePassword)(password, user.password);
48358
+ if (!isPasswordMatch) {
48359
+ throw new import_node_server_utils222.BadRequestError("Invalid credentials");
48360
+ }
48361
+ return "Verification successful.";
48362
+ } catch (error) {
48363
+ if (error instanceof import_node_server_utils222.BadRequestError) {
48364
+ throw error;
48365
+ }
48366
+ throw new import_node_server_utils222.InternalServerError("Error during password verification");
48367
+ }
48368
+ }
48312
48369
  return {
48313
48370
  login,
48314
- logout
48371
+ logout,
48372
+ verifyPassword
48315
48373
  };
48316
48374
  }
48317
48375
 
@@ -48519,7 +48577,11 @@ function useUserServiceV2() {
48519
48577
  // src/controllers/auth-v2.controller.ts
48520
48578
  function useAuthControllerV2() {
48521
48579
  const { signUp: _signUp } = useVerificationServiceV2();
48522
- const { login: _login, logout: _logout } = useAuthServiceV2();
48580
+ const {
48581
+ login: _login,
48582
+ logout: _logout,
48583
+ verifyPassword: _verifyPassword
48584
+ } = useAuthServiceV2();
48523
48585
  const { resetPassword: _resetPassword } = useUserServiceV2();
48524
48586
  async function signUp(req, res, next) {
48525
48587
  try {
@@ -48633,11 +48695,38 @@ function useAuthControllerV2() {
48633
48695
  return;
48634
48696
  }
48635
48697
  }
48698
+ async function verifyPassword(req, res, next) {
48699
+ const schema2 = import_joi127.default.object({
48700
+ _id: import_joi127.default.string().hex().required(),
48701
+ password: import_joi127.default.string().required().min(8)
48702
+ });
48703
+ const { error, value } = schema2.validate(
48704
+ { _id: req.params.id, ...req.body },
48705
+ { abortEarly: false }
48706
+ );
48707
+ if (error) {
48708
+ const messages = error.details.map((d) => d.message);
48709
+ import_node_server_utils224.logger.log({ level: "error", message: messages.join(", ") });
48710
+ next(new import_node_server_utils224.BadRequestError(messages.join(", ")));
48711
+ return;
48712
+ }
48713
+ const { _id, password } = value;
48714
+ try {
48715
+ const message = await _verifyPassword(_id, password);
48716
+ res.json({ message });
48717
+ return;
48718
+ } catch (error2) {
48719
+ import_node_server_utils224.logger.log({ level: "error", message: error2.message });
48720
+ next(error2);
48721
+ return;
48722
+ }
48723
+ }
48636
48724
  return {
48637
48725
  signUp,
48638
48726
  login,
48639
48727
  logout,
48640
- resetPassword
48728
+ resetPassword,
48729
+ verifyPassword
48641
48730
  };
48642
48731
  }
48643
48732