@7365admin1/core 3.12.0 → 3.13.0

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/dist/index.mjs CHANGED
@@ -10150,6 +10150,56 @@ function useVerificationRepo() {
10150
10150
  async function findOne(query) {
10151
10151
  return await collection.findOne(query);
10152
10152
  }
10153
+ async function completePendingInvites({
10154
+ email,
10155
+ orgId,
10156
+ siteId,
10157
+ app,
10158
+ session
10159
+ }) {
10160
+ const orgCandidates = [orgId];
10161
+ try {
10162
+ orgCandidates.push(new ObjectId13(orgId));
10163
+ } catch {
10164
+ }
10165
+ const query = {
10166
+ email: { $regex: `^${email}$`, $options: "i" },
10167
+ status: "pending",
10168
+ type: {
10169
+ $in: ["user-invite" /* USER_INVITE */, "member-invite" /* MEMBER_INVITE */]
10170
+ },
10171
+ "metadata.org": { $in: orgCandidates }
10172
+ };
10173
+ if (siteId) {
10174
+ const siteCandidates = [siteId];
10175
+ try {
10176
+ siteCandidates.push(new ObjectId13(siteId));
10177
+ } catch {
10178
+ }
10179
+ query["metadata.siteId"] = { $in: siteCandidates };
10180
+ }
10181
+ if (app) {
10182
+ query["metadata.app"] = app;
10183
+ }
10184
+ try {
10185
+ const result = await collection.updateMany(
10186
+ query,
10187
+ { $set: { status: "complete", updatedAt: (/* @__PURE__ */ new Date()).toISOString() } },
10188
+ { session }
10189
+ );
10190
+ delNamespace().then(() => {
10191
+ logger9.info(`Cache cleared for namespace: ${namespace_collection}`);
10192
+ }).catch((err) => {
10193
+ logger9.error(
10194
+ `Failed to clear cache for namespace: ${namespace_collection}`,
10195
+ err
10196
+ );
10197
+ });
10198
+ return result;
10199
+ } catch (error) {
10200
+ throw new InternalServerError8("Failed to complete pending invites.");
10201
+ }
10202
+ }
10153
10203
  return {
10154
10204
  createIndex,
10155
10205
  createTextIndex,
@@ -10159,7 +10209,8 @@ function useVerificationRepo() {
10159
10209
  getByIdByType,
10160
10210
  updateStatusById,
10161
10211
  getByStatus,
10162
- findOne
10212
+ findOne,
10213
+ completePendingInvites
10163
10214
  };
10164
10215
  }
10165
10216
 
@@ -11769,6 +11820,61 @@ function useVerificationRepoV2() {
11769
11820
  throw new InternalServerError11("Failed to update verification code.");
11770
11821
  }
11771
11822
  }
11823
+ async function completePendingInvites({
11824
+ email,
11825
+ orgId,
11826
+ siteId,
11827
+ app,
11828
+ session
11829
+ }) {
11830
+ const orgCandidates = [orgId];
11831
+ try {
11832
+ orgCandidates.push(new ObjectId18(orgId));
11833
+ } catch {
11834
+ }
11835
+ const query = {
11836
+ email: { $regex: `^${email}$`, $options: "i" },
11837
+ status: "pending" /* PENDING */,
11838
+ type: {
11839
+ $in: ["user-invite" /* USER_INVITE */, "member-invite" /* MEMBER_INVITE */]
11840
+ },
11841
+ "metadata.org": { $in: orgCandidates }
11842
+ };
11843
+ if (siteId) {
11844
+ const siteCandidates = [siteId];
11845
+ try {
11846
+ siteCandidates.push(new ObjectId18(siteId));
11847
+ } catch {
11848
+ }
11849
+ query["metadata.siteId"] = { $in: siteCandidates };
11850
+ }
11851
+ if (app) {
11852
+ query["metadata.app"] = app;
11853
+ }
11854
+ try {
11855
+ const result = await collection.updateMany(
11856
+ query,
11857
+ {
11858
+ $set: {
11859
+ status: "complete" /* COMPLETE */,
11860
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
11861
+ }
11862
+ },
11863
+ { session }
11864
+ );
11865
+ delNamespace().then(() => {
11866
+ logger13.info(`Cache cleared for namespace: ${namespace_collection}`);
11867
+ }).catch((err) => {
11868
+ logger13.error(
11869
+ `Failed to clear cache for namespace: ${namespace_collection}`,
11870
+ err
11871
+ );
11872
+ });
11873
+ return result;
11874
+ } catch (error) {
11875
+ throw new InternalServerError11("Failed to complete pending invites.");
11876
+ }
11877
+ }
11772
11878
  return {
11773
11879
  createIndex,
11774
11880
  createTextIndex,
@@ -11780,7 +11886,8 @@ function useVerificationRepoV2() {
11780
11886
  updateStatusById,
11781
11887
  countPendingOrgInvites,
11782
11888
  getPendingVerificationByEmail,
11783
- updateVerificationCodeById
11889
+ updateVerificationCodeById,
11890
+ completePendingInvites
11784
11891
  };
11785
11892
  }
11786
11893
 
@@ -13893,7 +14000,7 @@ function useMemberService() {
13893
14000
  getAllByUserId: _getAllByUserId,
13894
14001
  completeOnboardingById: _completeOnboardingById
13895
14002
  } = useMemberRepo();
13896
- const { getById: _getVerificationById, updateStatusById } = useVerificationRepo();
14003
+ const { getById: _getVerificationById, updateStatusById, completePendingInvites } = useVerificationRepo();
13897
14004
  const { getUserByEmail, updateDefaultOrgByEmail, getUserById } = useUserRepo();
13898
14005
  const { getById: getOrgById } = useOrgRepo();
13899
14006
  const { getSiteById } = useSiteRepo();
@@ -13997,6 +14104,13 @@ function useMemberService() {
13997
14104
  session
13998
14105
  );
13999
14106
  }
14107
+ await completePendingInvites({
14108
+ email: user.email,
14109
+ orgId,
14110
+ siteId,
14111
+ app,
14112
+ session
14113
+ });
14000
14114
  await session?.commitTransaction();
14001
14115
  return { member };
14002
14116
  } catch (error) {
@@ -21059,6 +21173,7 @@ async function convertObjectIdUtil2(id, fieldName) {
21059
21173
  throw new Error(`Invalid ID conversion to ObjectId of ${fieldName}.`);
21060
21174
  return new ObjectId40(id);
21061
21175
  } catch (_) {
21176
+ console.log(`Invalid ID conversion to ObjectId of ${fieldName}. : `, id);
21062
21177
  throw new Error(`Invalid ID conversion to ObjectId of ${fieldName}.`);
21063
21178
  }
21064
21179
  }
@@ -33473,12 +33588,28 @@ var KeyRepo = class {
33473
33588
  return Promise.reject("Server internal error.");
33474
33589
  }
33475
33590
  }
33476
- static async updateKeyById(keyId, key, site, session, isChild, visitorId) {
33591
+ static async updateKeyById(keyId, key, site, session, isChild, visitorId, isDifferentStatus, isPassKeyVisitorAdd) {
33477
33592
  keyId = await convertObjectIdUtil2(keyId, "keyId");
33478
33593
  if (site)
33479
33594
  site = await convertObjectIdUtil2(site, "Site");
33480
- if (visitorId)
33481
- visitorId = await convertObjectIdUtil2(visitorId, "visitor Id");
33595
+ if (visitorId) {
33596
+ visitorId = await convertObjectIdUtil2(visitorId, "Visitor Id");
33597
+ if (!isPassKeyVisitorAdd) {
33598
+ const findOptions = { sort: { _id: -1 } };
33599
+ const latestHistory = await this.collectionDynamic("keys-histories").findOne(
33600
+ { passOrKeyId: keyId },
33601
+ findOptions
33602
+ );
33603
+ console.log("visitorId", visitorId.toString());
33604
+ console.log("latestHistory.visitorId", latestHistory?.visitorId.toString());
33605
+ if (latestHistory?.visitorId && latestHistory.visitorId?.toString() !== visitorId.toString()) {
33606
+ console.log("Not updating keyId", keyId);
33607
+ return;
33608
+ } else {
33609
+ console.log("updating keyId", keyId);
33610
+ }
33611
+ }
33612
+ }
33482
33613
  if (key.updatedBy)
33483
33614
  key.updatedBy = await convertObjectIdUtil2(key.updatedBy, "Updated By");
33484
33615
  if (!key.status)
@@ -33486,9 +33617,14 @@ var KeyRepo = class {
33486
33617
  try {
33487
33618
  key.updatedAt = /* @__PURE__ */ new Date();
33488
33619
  const query = { _id: keyId, ...site && { site } };
33489
- let find = query;
33490
- if (isChild)
33620
+ let find = { ...query };
33621
+ if (isChild) {
33491
33622
  find = { parentId: keyId, site };
33623
+ } else {
33624
+ if (isDifferentStatus && typeof key?.status == "string") {
33625
+ find.status = { $ne: key.status };
33626
+ }
33627
+ }
33492
33628
  const result = await this.collection().updateMany(
33493
33629
  find,
33494
33630
  { $set: key },
@@ -33514,7 +33650,11 @@ var KeyRepo = class {
33514
33650
  session
33515
33651
  });
33516
33652
  } else {
33517
- throw new Error("Failed Updating Keys");
33653
+ if (visitorId) {
33654
+ console.log(`visitorId: ${visitorId} keyId: ${keyId} Not Updated`);
33655
+ } else {
33656
+ throw new Error("Failed Updating Keys");
33657
+ }
33518
33658
  }
33519
33659
  return result;
33520
33660
  } catch (error) {
@@ -33879,40 +34019,6 @@ function useVisitorTransactionService() {
33879
34019
  }
33880
34020
  return normalized;
33881
34021
  }
33882
- async function syncTransactionKeys({
33883
- current,
33884
- incoming,
33885
- site,
33886
- session
33887
- }) {
33888
- const currentIds = current.map((item) => item.keyId.toString());
33889
- const incomingIds = incoming.map((item) => item.keyId.toString());
33890
- const currentSet = new Set(currentIds);
33891
- const incomingSet = new Set(incomingIds);
33892
- const removedIds = currentIds.filter((id) => !incomingSet.has(id));
33893
- const addedIds = incomingIds.filter((id) => !currentSet.has(id));
33894
- for (const keyId of removedIds) {
33895
- const existingKey = await KeyRepo.getById(keyId);
33896
- if (!existingKey)
33897
- continue;
33898
- if (existingKey.status === "In Use" /* IN_USE */) {
33899
- await KeyRepo.updateKeyById(
33900
- keyId,
33901
- { status: "Available" /* AVAILABLE */ },
33902
- site,
33903
- session
33904
- );
33905
- }
33906
- }
33907
- for (const keyId of addedIds) {
33908
- await KeyRepo.updateKeyById(
33909
- keyId,
33910
- { status: "In Use" /* IN_USE */ },
33911
- site,
33912
- session
33913
- );
33914
- }
33915
- }
33916
34022
  async function add(value) {
33917
34023
  const session = useAtlas50.getClient()?.startSession();
33918
34024
  const allowedPersonTypes = [
@@ -34048,7 +34154,9 @@ function useVisitorTransactionService() {
34048
34154
  value.site,
34049
34155
  session,
34050
34156
  void 0,
34051
- visitorId
34157
+ visitorId,
34158
+ void 0,
34159
+ true
34052
34160
  );
34053
34161
  }
34054
34162
  for (const item of preparedPassKeys) {
@@ -34060,7 +34168,11 @@ function useVisitorTransactionService() {
34060
34168
  visitorId
34061
34169
  },
34062
34170
  value.site,
34063
- session
34171
+ session,
34172
+ void 0,
34173
+ visitorId,
34174
+ void 0,
34175
+ true
34064
34176
  );
34065
34177
  }
34066
34178
  })
@@ -34116,7 +34228,9 @@ function useVisitorTransactionService() {
34116
34228
  value.site,
34117
34229
  session,
34118
34230
  void 0,
34119
- result
34231
+ result,
34232
+ void 0,
34233
+ true
34120
34234
  );
34121
34235
  }
34122
34236
  }
@@ -34128,7 +34242,9 @@ function useVisitorTransactionService() {
34128
34242
  value.site,
34129
34243
  session,
34130
34244
  void 0,
34131
- result
34245
+ result,
34246
+ void 0,
34247
+ true
34132
34248
  );
34133
34249
  }
34134
34250
  }
@@ -34264,7 +34380,8 @@ function useVisitorTransactionService() {
34264
34380
  value.site,
34265
34381
  session,
34266
34382
  void 0,
34267
- id
34383
+ id,
34384
+ true
34268
34385
  );
34269
34386
  delete item.receivedDate;
34270
34387
  item.lastUpdate = /* @__PURE__ */ new Date();
@@ -34311,7 +34428,8 @@ function useVisitorTransactionService() {
34311
34428
  value.site,
34312
34429
  session,
34313
34430
  void 0,
34314
- id
34431
+ id,
34432
+ true
34315
34433
  );
34316
34434
  delete item.receivedDate;
34317
34435
  item.lastUpdate = /* @__PURE__ */ new Date();
@@ -34482,63 +34600,12 @@ function useVisitorTransactionService() {
34482
34600
  session?.endSession();
34483
34601
  }
34484
34602
  }
34485
- async function changeVisitorTransactionKeysById(id, visitorPass, passKeys) {
34486
- const session = useAtlas50.getClient()?.startSession();
34487
- if (!session) {
34488
- throw new Error(
34489
- "Unable to start session for visitor transaction service."
34490
- );
34491
- }
34492
- try {
34493
- session.startTransaction();
34494
- const visitorTransaction = await _getVisitorTransactionById(id);
34495
- if (!visitorTransaction) {
34496
- throw new Error("Visitor transaction not found.");
34497
- }
34498
- const updatePayload = {};
34499
- if (visitorPass !== void 0) {
34500
- const currentVisitorPass = normalizeKeyRefs(
34501
- visitorTransaction.visitorPass
34502
- );
34503
- const incomingVisitorPass = normalizeKeyRefs(visitorPass);
34504
- await syncTransactionKeys({
34505
- current: currentVisitorPass,
34506
- incoming: incomingVisitorPass,
34507
- site: visitorTransaction.site,
34508
- session
34509
- });
34510
- updatePayload.visitorPass = incomingVisitorPass;
34511
- }
34512
- if (passKeys !== void 0) {
34513
- const currentPassKeys = normalizeKeyRefs(visitorTransaction.passKeys);
34514
- const incomingPassKeys = normalizeKeyRefs(passKeys);
34515
- await syncTransactionKeys({
34516
- current: currentPassKeys,
34517
- incoming: incomingPassKeys,
34518
- site: visitorTransaction.site,
34519
- session
34520
- });
34521
- updatePayload.passKeys = incomingPassKeys;
34522
- }
34523
- if (Object.keys(updatePayload).length > 0) {
34524
- await _updateVisitorTansactionById(id, updatePayload, session);
34525
- }
34526
- await session.commitTransaction();
34527
- return "Successfully changed visitor transaction keys.";
34528
- } catch (error) {
34529
- await session.abortTransaction();
34530
- logger80.error("Error in visitor transaction change keys by id:", error);
34531
- throw error;
34532
- } finally {
34533
- session.endSession();
34534
- }
34535
- }
34536
34603
  return {
34537
34604
  add,
34538
34605
  updateVisitorTransactionById,
34539
34606
  processTransactionDahuaStatus,
34540
- inviteVisitor,
34541
- changeVisitorTransactionKeysById
34607
+ inviteVisitor
34608
+ // changeVisitorTransactionKeysById,
34542
34609
  };
34543
34610
  }
34544
34611
 
@@ -34549,8 +34616,8 @@ function useVisitorTransactionController() {
34549
34616
  const {
34550
34617
  add: _add,
34551
34618
  updateVisitorTransactionById: _updateVisitorTransactionById,
34552
- inviteVisitor: _inviteVisitor,
34553
- changeVisitorTransactionKeysById: _changeVisitorTransactionKeysById
34619
+ inviteVisitor: _inviteVisitor
34620
+ // changeVisitorTransactionKeysById: _changeVisitorTransactionKeysById,
34554
34621
  } = useVisitorTransactionService();
34555
34622
  const {
34556
34623
  getAll: _getAll,
@@ -34707,7 +34774,12 @@ function useVisitorTransactionController() {
34707
34774
  return;
34708
34775
  } catch (error2) {
34709
34776
  logger81.log({ level: "error", message: error2.message });
34710
- next(error2);
34777
+ console.log("error", error2);
34778
+ if (error2?.message) {
34779
+ next(new BadRequestError101(error2?.message));
34780
+ } else {
34781
+ next(error2);
34782
+ }
34711
34783
  return;
34712
34784
  }
34713
34785
  }
@@ -34800,61 +34872,14 @@ function useVisitorTransactionController() {
34800
34872
  }
34801
34873
  }
34802
34874
  }
34803
- async function changeVisitorTransactionKeysById(req, res, next) {
34804
- const idValidation = Joi57.string().hex().length(24).required();
34805
- const bodyValidation = Joi57.object({
34806
- visitorPass: Joi57.array().items(
34807
- Joi57.object({
34808
- keyId: Joi57.string().hex().length(24).required()
34809
- })
34810
- ).optional(),
34811
- passKeys: Joi57.array().items(
34812
- Joi57.object({
34813
- keyId: Joi57.string().hex().length(24).required()
34814
- })
34815
- ).optional()
34816
- }).or("visitorPass", "passKeys");
34817
- const _id = req.params.id;
34818
- const { error: idError } = idValidation.validate(_id);
34819
- if (idError) {
34820
- logger81.log({ level: "error", message: idError.message });
34821
- next(new BadRequestError101(idError.message));
34822
- return;
34823
- }
34824
- const { error, value } = bodyValidation.validate(req.body, {
34825
- abortEarly: false
34826
- });
34827
- if (error) {
34828
- const message = error.details.map((d) => d.message).join(", ");
34829
- logger81.log({ level: "error", message });
34830
- next(new BadRequestError101(message));
34831
- return;
34832
- }
34833
- try {
34834
- const { visitorPass, passKeys } = value;
34835
- const result = await _changeVisitorTransactionKeysById(
34836
- _id,
34837
- visitorPass,
34838
- passKeys
34839
- );
34840
- res.status(200).json({
34841
- message: result || "Successfully changed visitor transaction keys."
34842
- });
34843
- return;
34844
- } catch (error2) {
34845
- logger81.log({ level: "error", message: error2.message });
34846
- next(error2);
34847
- return;
34848
- }
34849
- }
34850
34875
  return {
34851
34876
  add,
34852
34877
  getAll,
34853
34878
  updateVisitorTansactionById,
34854
34879
  deleteVisitorTransaction,
34855
34880
  inviteVisitor,
34856
- getVisitorTransactionById,
34857
- changeVisitorTransactionKeysById
34881
+ getVisitorTransactionById
34882
+ // changeVisitorTransactionKeysById,
34858
34883
  };
34859
34884
  }
34860
34885
 
@@ -70055,6 +70080,7 @@ export {
70055
70080
  useManpowerSitesSrvc,
70056
70081
  useMemberController,
70057
70082
  useMemberRepo,
70083
+ useMemberService,
70058
70084
  useNewDashboardController,
70059
70085
  useNewDashboardRepo,
70060
70086
  useNfcPatrolLogController,