@7365admin1/core 3.10.0 → 3.12.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.js CHANGED
@@ -21126,14 +21126,20 @@ var schemaUpdateVisTrans = import_joi37.default.object({
21126
21126
  import_joi37.default.object({
21127
21127
  keyId: import_joi37.default.string().hex().length(24).required(),
21128
21128
  status: import_joi37.default.string().optional().allow(null, ""),
21129
- remarks: import_joi37.default.string().optional().allow(null, "")
21129
+ remarks: import_joi37.default.string().optional().allow(null, ""),
21130
+ receivedDate: import_joi37.default.string().optional().allow(null, ""),
21131
+ lastUpdate: import_joi37.default.string().optional().allow(null, ""),
21132
+ add: import_joi37.default.boolean().optional().allow(null, "")
21130
21133
  })
21131
21134
  ).optional().allow(null),
21132
21135
  passKeys: import_joi37.default.array().items(
21133
21136
  import_joi37.default.object({
21134
21137
  keyId: import_joi37.default.string().hex().length(24).required(),
21135
21138
  status: import_joi37.default.string().optional().allow(null, ""),
21136
- remarks: import_joi37.default.string().optional().allow(null, "")
21139
+ remarks: import_joi37.default.string().optional().allow(null, ""),
21140
+ receivedDate: import_joi37.default.string().optional().allow(null, ""),
21141
+ lastUpdate: import_joi37.default.string().optional().allow(null, ""),
21142
+ add: import_joi37.default.boolean().optional().allow(null, "")
21137
21143
  })
21138
21144
  ).optional().allow(null),
21139
21145
  checkInRemarks: import_joi37.default.string().optional().allow("", null),
@@ -21156,7 +21162,8 @@ var schemaUpdateVisTrans = import_joi37.default.object({
21156
21162
  ).optional().allow(null),
21157
21163
  contact: import_joi37.default.string().optional().allow(null, "")
21158
21164
  })
21159
- ).optional().allow(null)
21165
+ ).optional().allow(null),
21166
+ updatedBy: import_joi37.default.string().hex().length(24).optional()
21160
21167
  });
21161
21168
  function MVisitorTransaction(value) {
21162
21169
  const { error } = schemaVisitorTransaction.validate(value, {
@@ -21314,6 +21321,15 @@ async function convertObjectIdUtil2(id, fieldName) {
21314
21321
  throw new Error(`Invalid ID conversion to ObjectId of ${fieldName}.`);
21315
21322
  }
21316
21323
  }
21324
+ function convertObjectIdUtilNonAsync(id, fieldName) {
21325
+ try {
21326
+ if (!id)
21327
+ throw new Error(`Invalid ID conversion to ObjectId of ${fieldName}.`);
21328
+ return new import_mongodb40.ObjectId(id);
21329
+ } catch (_) {
21330
+ throw new Error(`Invalid ID conversion to ObjectId of ${fieldName}.`);
21331
+ }
21332
+ }
21317
21333
 
21318
21334
  // src/repositories/visitor-transaction.repo.ts
21319
21335
  var import_moment_timezone = __toESM(require("moment-timezone"));
@@ -21507,6 +21523,7 @@ function useVisitorTransactionRepo() {
21507
21523
  preserveNullAndEmptyArrays: true
21508
21524
  }
21509
21525
  },
21526
+ // 1. Lookup visitorPass into a temporary field
21510
21527
  {
21511
21528
  $lookup: {
21512
21529
  from: "keys",
@@ -21518,48 +21535,66 @@ function useVisitorTransactionRepo() {
21518
21535
  from: "qr-code-templates",
21519
21536
  localField: "template",
21520
21537
  foreignField: "_id",
21521
- pipeline: [
21522
- {
21523
- $project: {
21524
- _id: 1,
21525
- prefixPass: 1,
21526
- name: 1,
21527
- remarks: 1
21528
- }
21529
- }
21530
- ],
21538
+ pipeline: [{ $project: { prefixPass: 1, name: 1 } }],
21531
21539
  as: "template"
21532
21540
  }
21533
21541
  },
21534
21542
  {
21535
21543
  $project: {
21536
- _id: 0,
21537
- keyId: "$_id",
21538
- status: 1,
21539
- description: 1,
21540
- remarks: {
21541
- $arrayElemAt: ["$template.remarks", 0]
21542
- },
21543
- templatePrefixPass: {
21544
- $arrayElemAt: ["$template.prefixPass", 0]
21545
- },
21544
+ _id: 1,
21545
+ // Kept only for matching purposes
21546
21546
  prefixAndName: {
21547
21547
  $concat: [
21548
- {
21549
- $ifNull: [
21550
- { $arrayElemAt: ["$template.prefixPass", 0] },
21551
- ""
21552
- ]
21553
- },
21548
+ { $ifNull: [{ $arrayElemAt: ["$template.prefixPass", 0] }, ""] },
21554
21549
  { $ifNull: ["$name", ""] }
21555
21550
  ]
21556
21551
  }
21557
21552
  }
21558
21553
  }
21559
21554
  ],
21560
- as: "visitorPass"
21555
+ as: "visitorPass_lookup"
21561
21556
  }
21562
21557
  },
21558
+ // 2. Map through original array, keep all fields, and inject prefixAndName
21559
+ {
21560
+ $addFields: {
21561
+ visitorPass: {
21562
+ $map: {
21563
+ input: "$visitorPass",
21564
+ as: "original",
21565
+ in: {
21566
+ // Explicitly retain all original fields from your document
21567
+ keyId: "$$original.keyId",
21568
+ receivedDate: "$$original.receivedDate",
21569
+ status: "$$original.status",
21570
+ lastUpdate: "$$original.lastUpdate",
21571
+ remarks: "$$original.remarks",
21572
+ // Find and extract ONLY the prefixAndName string from lookup
21573
+ prefixAndName: {
21574
+ $let: {
21575
+ vars: {
21576
+ matched: {
21577
+ $arrayElemAt: [
21578
+ {
21579
+ $filter: {
21580
+ input: "$visitorPass_lookup",
21581
+ as: "lookup",
21582
+ cond: { $eq: ["$$lookup._id", "$$original.keyId"] }
21583
+ }
21584
+ },
21585
+ 0
21586
+ ]
21587
+ }
21588
+ },
21589
+ in: "$$matched.prefixAndName"
21590
+ }
21591
+ }
21592
+ }
21593
+ }
21594
+ }
21595
+ }
21596
+ },
21597
+ // 3. Lookup passKeys into a temporary field
21563
21598
  {
21564
21599
  $lookup: {
21565
21600
  from: "keys",
@@ -21571,46 +21606,69 @@ function useVisitorTransactionRepo() {
21571
21606
  from: "qr-code-templates",
21572
21607
  localField: "template",
21573
21608
  foreignField: "_id",
21574
- pipeline: [
21575
- {
21576
- $project: {
21577
- _id: 1,
21578
- prefixPass: 1,
21579
- name: 1,
21580
- remarks: 1
21581
- }
21582
- }
21583
- ],
21609
+ pipeline: [{ $project: { prefixPass: 1, name: 1 } }],
21584
21610
  as: "template"
21585
21611
  }
21586
21612
  },
21587
21613
  {
21588
21614
  $project: {
21589
- _id: 0,
21590
- keyId: "$_id",
21591
- status: 1,
21592
- description: 1,
21593
- remarks: {
21594
- $arrayElemAt: ["$template.remarks", 0]
21595
- },
21596
- templatePrefixPass: {
21597
- $arrayElemAt: ["$template.prefixPass", 0]
21598
- },
21615
+ _id: 1,
21599
21616
  prefixAndName: {
21600
21617
  $concat: [
21601
- {
21602
- $ifNull: [
21603
- { $arrayElemAt: ["$template.prefixPass", 0] },
21604
- ""
21605
- ]
21606
- },
21618
+ { $ifNull: [{ $arrayElemAt: ["$template.prefixPass", 0] }, ""] },
21607
21619
  { $ifNull: ["$name", ""] }
21608
21620
  ]
21609
21621
  }
21610
21622
  }
21611
21623
  }
21612
21624
  ],
21613
- as: "passKeys"
21625
+ as: "passKeys_lookup"
21626
+ }
21627
+ },
21628
+ // 4. Map through original passKeys array, keep all fields, inject prefixAndName
21629
+ {
21630
+ $addFields: {
21631
+ passKeys: {
21632
+ $map: {
21633
+ input: "$passKeys",
21634
+ as: "original",
21635
+ in: {
21636
+ // Explicitly retain all original fields from your document
21637
+ keyId: "$$original.keyId",
21638
+ receivedDate: "$$original.receivedDate",
21639
+ status: "$$original.status",
21640
+ lastUpdate: "$$original.lastUpdate",
21641
+ remarks: "$$original.remarks",
21642
+ // Find and extract ONLY the prefixAndName string from lookup
21643
+ prefixAndName: {
21644
+ $let: {
21645
+ vars: {
21646
+ matched: {
21647
+ $arrayElemAt: [
21648
+ {
21649
+ $filter: {
21650
+ input: "$passKeys_lookup",
21651
+ as: "lookup",
21652
+ cond: { $eq: ["$$lookup._id", "$$original.keyId"] }
21653
+ }
21654
+ },
21655
+ 0
21656
+ ]
21657
+ }
21658
+ },
21659
+ in: "$$matched.prefixAndName"
21660
+ }
21661
+ }
21662
+ }
21663
+ }
21664
+ }
21665
+ }
21666
+ },
21667
+ // 5. Cleanup temporary arrays
21668
+ {
21669
+ $project: {
21670
+ visitorPass_lookup: 0,
21671
+ passKeys_lookup: 0
21614
21672
  }
21615
21673
  },
21616
21674
  {
@@ -21806,10 +21864,74 @@ function useVisitorTransactionRepo() {
21806
21864
  value.manualCheckout = true;
21807
21865
  }
21808
21866
  try {
21867
+ const updateFields = {};
21868
+ const pushFields = {};
21869
+ const arrayFilters = [];
21870
+ Object.keys(value).forEach((key) => {
21871
+ if (key !== "visitorPass" && key !== "passKeys") {
21872
+ const typedKey = key;
21873
+ updateFields[key] = value[typedKey];
21874
+ }
21875
+ });
21876
+ if (Array.isArray(value.visitorPass)) {
21877
+ value.visitorPass.forEach((item, index) => {
21878
+ if (item.add === true) {
21879
+ const { add: add2, ...cleanItem } = item;
21880
+ cleanItem.keyId = convertObjectIdUtilNonAsync(cleanItem.keyId, "Pass Id");
21881
+ if (!pushFields["visitorPass"]) {
21882
+ pushFields["visitorPass"] = { $each: [] };
21883
+ }
21884
+ pushFields["visitorPass"].$each.push(cleanItem);
21885
+ } else {
21886
+ const elementKey = `vElem${index}`;
21887
+ Object.keys(item).forEach((itemKey) => {
21888
+ if (itemKey !== "keyId") {
21889
+ const typedItemKey = itemKey;
21890
+ let itemValue = item[typedItemKey];
21891
+ updateFields[`visitorPass.$[${elementKey}].${itemKey}`] = itemValue;
21892
+ }
21893
+ });
21894
+ arrayFilters.push({ [`${elementKey}.keyId`]: new import_mongodb41.ObjectId(item.keyId) });
21895
+ }
21896
+ });
21897
+ }
21898
+ if (Array.isArray(value.passKeys)) {
21899
+ value.passKeys.forEach((item, index) => {
21900
+ if (item.add === true) {
21901
+ const { add: add2, ...cleanItem } = item;
21902
+ cleanItem.keyId = convertObjectIdUtilNonAsync(cleanItem.keyId, "Key Id");
21903
+ if (!pushFields["passKeys"]) {
21904
+ pushFields["passKeys"] = { $each: [] };
21905
+ }
21906
+ pushFields["passKeys"].$each.push(cleanItem);
21907
+ } else {
21908
+ const elementKey = `pElem${index}`;
21909
+ Object.keys(item).forEach((itemKey) => {
21910
+ if (itemKey !== "keyId") {
21911
+ const typedItemKey = itemKey;
21912
+ let itemValue = item[typedItemKey];
21913
+ updateFields[`passKeys.$[${elementKey}].${itemKey}`] = itemValue;
21914
+ }
21915
+ });
21916
+ arrayFilters.push({ [`${elementKey}.keyId`]: new import_mongodb41.ObjectId(item.keyId) });
21917
+ }
21918
+ });
21919
+ }
21920
+ const updatePayload = {};
21921
+ if (Object.keys(updateFields).length > 0) {
21922
+ updatePayload.$set = updateFields;
21923
+ }
21924
+ if (Object.keys(pushFields).length > 0) {
21925
+ updatePayload.$push = pushFields;
21926
+ }
21809
21927
  const result = await collection.updateOne(
21810
- { _id },
21811
- { $set: value },
21812
- { session }
21928
+ { _id: new import_mongodb41.ObjectId(_id) },
21929
+ updatePayload,
21930
+ {
21931
+ ...arrayFilters.length > 0 && { arrayFilters },
21932
+ // Only pass if not empty
21933
+ session
21934
+ }
21813
21935
  );
21814
21936
  return result;
21815
21937
  } catch (error) {
@@ -33502,10 +33624,12 @@ var KeyRepo = class {
33502
33624
  return Promise.reject("Server internal error.");
33503
33625
  }
33504
33626
  }
33505
- static async updateKeyById(keyId, key, site, session, isChild) {
33627
+ static async updateKeyById(keyId, key, site, session, isChild, visitorId) {
33506
33628
  keyId = await convertObjectIdUtil2(keyId, "keyId");
33507
33629
  if (site)
33508
33630
  site = await convertObjectIdUtil2(site, "Site");
33631
+ if (visitorId)
33632
+ visitorId = await convertObjectIdUtil2(visitorId, "visitor Id");
33509
33633
  if (key.updatedBy)
33510
33634
  key.updatedBy = await convertObjectIdUtil2(key.updatedBy, "Updated By");
33511
33635
  if (!key.status)
@@ -33522,7 +33646,6 @@ var KeyRepo = class {
33522
33646
  { session }
33523
33647
  );
33524
33648
  let setKeys = [];
33525
- console.log("updateKeyById result", result);
33526
33649
  if (result.modifiedCount > 0) {
33527
33650
  const updatedDocs = await this.collection().find(find).toArray();
33528
33651
  if (Array.isArray(updatedDocs) && updatedDocs.length > 0) {
@@ -33531,6 +33654,10 @@ var KeyRepo = class {
33531
33654
  keyItem["passOrKeyId"] = item?._id;
33532
33655
  keyItem["_id"] = new import_mongodb61.ObjectId();
33533
33656
  keyItem["previousStatus"] = item?.status;
33657
+ if (visitorId) {
33658
+ console.log("visitorId true key.repo");
33659
+ keyItem["visitorId"] = visitorId;
33660
+ }
33534
33661
  setKeys.push(keyItem);
33535
33662
  });
33536
33663
  }
@@ -33765,7 +33892,7 @@ var NotificationService = class {
33765
33892
  status: payload.status,
33766
33893
  module: "onlineForm",
33767
33894
  screen,
33768
- params: JSON.stringify(params)
33895
+ params
33769
33896
  },
33770
33897
  false,
33771
33898
  "iservice365-resident-mobile-app"
@@ -33826,6 +33953,14 @@ var NotificationService = class {
33826
33953
  }
33827
33954
  };
33828
33955
 
33956
+ // src/utils/valid-values.ts
33957
+ var VALID_STATUSES = /* @__PURE__ */ new Set([
33958
+ "In Use",
33959
+ "Available",
33960
+ "Damaged",
33961
+ "Lost"
33962
+ ]);
33963
+
33829
33964
  // src/services/visitor-transaction.service.ts
33830
33965
  function useVisitorTransactionService() {
33831
33966
  const MailerConfig = {
@@ -34014,48 +34149,26 @@ function useVisitorTransactionService() {
34014
34149
  const chunk = value.members.slice(i, i + chunkSize);
34015
34150
  for (const member of chunk) {
34016
34151
  await KeyRepo.checkPassKeyAvailability(member.visitorPass, member.passKeys);
34017
- if (Array.isArray(member.visitorPass)) {
34018
- for (const item of member.visitorPass) {
34019
- console.log("Type of keyId:", typeof item.keyId);
34020
- console.log("item visitorPass", item);
34021
- await KeyRepo.updateKeyById(
34022
- item.keyId,
34023
- {
34024
- status: "In Use" /* IN_USE */,
34025
- updatedBy: value.createdBy
34026
- },
34027
- value.site,
34028
- session
34029
- );
34030
- item.receivedDate = /* @__PURE__ */ new Date();
34031
- item.status = "Not Returned" /* NOT_RETURNED */;
34032
- item.lastUpdate = null;
34033
- item.remarks = "";
34034
- }
34035
- }
34036
- if (Array.isArray(member.passKeys)) {
34037
- for (const item of member.passKeys) {
34038
- await KeyRepo.updateKeyById(
34039
- item.keyId,
34040
- {
34041
- status: "In Use" /* IN_USE */,
34042
- updatedBy: value.createdBy
34043
- },
34044
- value.site,
34045
- session
34046
- );
34047
- item.receivedDate = /* @__PURE__ */ new Date();
34048
- item.status = "Not Returned" /* NOT_RETURNED */;
34049
- item.lastUpdate = null;
34050
- item.remarks = "";
34051
- }
34052
- }
34053
34152
  }
34054
34153
  await Promise.all(
34055
- chunk.map((member) => {
34154
+ chunk.map(async (member) => {
34056
34155
  const clonedMember = structuredClone(member);
34057
34156
  const { visitorPass, passKeys } = clonedMember;
34058
- return _add(
34157
+ const preparedVisitorPass = Array.isArray(visitorPass) ? visitorPass.map((item) => ({
34158
+ ...item,
34159
+ receivedDate: /* @__PURE__ */ new Date(),
34160
+ status: "Not Returned" /* NOT_RETURNED */,
34161
+ lastUpdate: null,
34162
+ remarks: ""
34163
+ })) : [];
34164
+ const preparedPassKeys = Array.isArray(passKeys) ? passKeys.map((item) => ({
34165
+ ...item,
34166
+ receivedDate: /* @__PURE__ */ new Date(),
34167
+ status: "Not Returned" /* NOT_RETURNED */,
34168
+ lastUpdate: null,
34169
+ remarks: ""
34170
+ })) : [];
34171
+ const visitorId = await _add(
34059
34172
  {
34060
34173
  ...clonedMember,
34061
34174
  block,
@@ -34069,13 +34182,38 @@ function useVisitorTransactionService() {
34069
34182
  remarks,
34070
34183
  contractorType,
34071
34184
  checkIn: start,
34072
- // expiredAt: end,
34073
- visitorPass: visitorPass ?? [],
34074
- passKeys: passKeys ?? [],
34185
+ visitorPass: preparedVisitorPass,
34186
+ passKeys: preparedPassKeys,
34075
34187
  status: "registered" /* REGISTERED */
34076
34188
  },
34077
34189
  session
34078
34190
  );
34191
+ console.log("visitorId service", visitorId);
34192
+ for (const item of preparedVisitorPass) {
34193
+ await KeyRepo.updateKeyById(
34194
+ item.keyId,
34195
+ {
34196
+ status: "In Use" /* IN_USE */,
34197
+ updatedBy: value.createdBy
34198
+ },
34199
+ value.site,
34200
+ session,
34201
+ void 0,
34202
+ visitorId
34203
+ );
34204
+ }
34205
+ for (const item of preparedPassKeys) {
34206
+ await KeyRepo.updateKeyById(
34207
+ item.keyId,
34208
+ {
34209
+ status: "In Use" /* IN_USE */,
34210
+ updatedBy: value.createdBy,
34211
+ visitorId
34212
+ },
34213
+ value.site,
34214
+ session
34215
+ );
34216
+ }
34079
34217
  })
34080
34218
  );
34081
34219
  }
@@ -34106,15 +34244,6 @@ function useVisitorTransactionService() {
34106
34244
  await KeyRepo.checkPassKeyAvailability(value.visitorPass, value.passKeys);
34107
34245
  if (Array.isArray(value.visitorPass)) {
34108
34246
  for (const item of value.visitorPass) {
34109
- await KeyRepo.updateKeyById(
34110
- item.keyId,
34111
- {
34112
- status: "In Use" /* IN_USE */,
34113
- updatedBy: value.createdBy
34114
- },
34115
- value.site,
34116
- session
34117
- );
34118
34247
  item.receivedDate = /* @__PURE__ */ new Date();
34119
34248
  item.status = "Not Returned" /* NOT_RETURNED */;
34120
34249
  item.lastUpdate = null;
@@ -34123,15 +34252,6 @@ function useVisitorTransactionService() {
34123
34252
  }
34124
34253
  if (Array.isArray(value.passKeys)) {
34125
34254
  for (const item of value.passKeys) {
34126
- await KeyRepo.updateKeyById(
34127
- item.keyId,
34128
- {
34129
- status: "In Use" /* IN_USE */,
34130
- updatedBy: value.createdBy
34131
- },
34132
- value.site,
34133
- session
34134
- );
34135
34255
  item.receivedDate = /* @__PURE__ */ new Date();
34136
34256
  item.status = "Not Returned" /* NOT_RETURNED */;
34137
34257
  item.lastUpdate = null;
@@ -34139,6 +34259,30 @@ function useVisitorTransactionService() {
34139
34259
  }
34140
34260
  }
34141
34261
  const result = await _add(value, session);
34262
+ if (Array.isArray(value.visitorPass)) {
34263
+ for (const item of value.visitorPass) {
34264
+ await KeyRepo.updateKeyById(
34265
+ item.keyId,
34266
+ { status: "In Use" /* IN_USE */, updatedBy: value.createdBy },
34267
+ value.site,
34268
+ session,
34269
+ void 0,
34270
+ result
34271
+ );
34272
+ }
34273
+ }
34274
+ if (Array.isArray(value.passKeys)) {
34275
+ for (const item of value.passKeys) {
34276
+ await KeyRepo.updateKeyById(
34277
+ item.keyId,
34278
+ { status: "In Use" /* IN_USE */, updatedBy: value.createdBy },
34279
+ value.site,
34280
+ session,
34281
+ void 0,
34282
+ result
34283
+ );
34284
+ }
34285
+ }
34142
34286
  await session?.commitTransaction();
34143
34287
  let openBarrier = null;
34144
34288
  const isOpenBarrier = allowedPersonTypes.includes(value?.type) || camera?.ANPRSwitches?.openBarrierPickUpDropOff == true;
@@ -34212,52 +34356,6 @@ function useVisitorTransactionService() {
34212
34356
  if (found === 1)
34213
34357
  throw new import_node_server_utils106.BadRequestError("This plate number is blocklisted");
34214
34358
  }
34215
- if (Array.isArray(value.visitorPass) && value.visitorPass.length > 0) {
34216
- const keptVisitorPass = [];
34217
- for (const vp of value.visitorPass) {
34218
- const updatePayload = {
34219
- ...vp.status && { status: vp.status },
34220
- ...vp.remarks && { remarks: vp.remarks }
34221
- };
34222
- const visitorPassId = typeof vp === "string" || vp instanceof import_mongodb62.ObjectId ? vp : vp.keyId;
34223
- await KeyRepo.updateKeyById(
34224
- visitorPassId,
34225
- updatePayload,
34226
- value.site
34227
- );
34228
- if (typeof vp !== "string" && !(vp instanceof import_mongodb62.ObjectId)) {
34229
- keptVisitorPass.push({
34230
- keyId: new import_mongodb62.ObjectId(visitorPassId)
34231
- });
34232
- }
34233
- }
34234
- value.visitorPass = keptVisitorPass;
34235
- }
34236
- if (value.passKeys && Array.isArray(value.passKeys) && value.passKeys.length > 0) {
34237
- const keptPassKeys = [];
34238
- for (const pk of value.passKeys) {
34239
- try {
34240
- const updatePayload = {
34241
- ...pk.status && { status: pk.status },
34242
- ...pk.remarks && { remarks: pk.remarks }
34243
- };
34244
- const passKeyId = typeof pk === "string" || pk instanceof import_mongodb62.ObjectId ? pk : pk.keyId;
34245
- await KeyRepo.updateKeyById(
34246
- passKeyId,
34247
- updatePayload,
34248
- value.site
34249
- );
34250
- if (typeof pk !== "string" && !(pk instanceof import_mongodb62.ObjectId)) {
34251
- keptPassKeys.push({
34252
- keyId: new import_mongodb62.ObjectId(passKeyId)
34253
- });
34254
- }
34255
- } catch (error) {
34256
- throw error;
34257
- }
34258
- }
34259
- value.passKeys = keptPassKeys;
34260
- }
34261
34359
  if (value.checkIn) {
34262
34360
  const parsed = new Date(value.checkIn);
34263
34361
  value.checkIn = isNaN(parsed.getTime()) ? null : parsed;
@@ -34274,6 +34372,103 @@ function useVisitorTransactionService() {
34274
34372
  const unit = await _getUnitById(value.unit);
34275
34373
  value.unitName = unit?.name;
34276
34374
  }
34375
+ if (value.updatedBy) {
34376
+ value.updatedBy = await convertObjectIdUtil2(value.updatedBy, "updatedBy Id");
34377
+ }
34378
+ if (Array.isArray(value.visitorPass)) {
34379
+ for (const item of value.visitorPass) {
34380
+ if (item.add) {
34381
+ await KeyRepo.updateKeyById(
34382
+ item.keyId,
34383
+ {
34384
+ status: "In Use" /* IN_USE */,
34385
+ updatedBy: value.updatedBy
34386
+ },
34387
+ value.site,
34388
+ session,
34389
+ void 0,
34390
+ id
34391
+ );
34392
+ item.receivedDate = /* @__PURE__ */ new Date();
34393
+ item.status = "Not Returned" /* NOT_RETURNED */;
34394
+ item.lastUpdate = null;
34395
+ item.remarks = "";
34396
+ } else {
34397
+ let status = "Invalid";
34398
+ if (item?.status == "Returned" /* RETURNED */) {
34399
+ status = "Available" /* AVAILABLE */;
34400
+ } else if (item?.status == "Removed" /* REMOVED */) {
34401
+ status = "Available" /* AVAILABLE */;
34402
+ } else if (item?.status == "Not Returned" /* NOT_RETURNED */) {
34403
+ status = "In Use" /* IN_USE */;
34404
+ } else if (item?.status && VALID_STATUSES.has(item.status)) {
34405
+ status = item.status;
34406
+ } else {
34407
+ throw new Error("Invalid Visitor Pass Status");
34408
+ }
34409
+ await KeyRepo.updateKeyById(
34410
+ item.keyId,
34411
+ {
34412
+ status,
34413
+ updatedBy: value.updatedBy
34414
+ },
34415
+ value.site,
34416
+ session,
34417
+ void 0,
34418
+ id
34419
+ );
34420
+ delete item.receivedDate;
34421
+ item.lastUpdate = /* @__PURE__ */ new Date();
34422
+ }
34423
+ }
34424
+ }
34425
+ if (Array.isArray(value.passKeys)) {
34426
+ for (const item of value.passKeys) {
34427
+ if (item.add) {
34428
+ await KeyRepo.updateKeyById(
34429
+ item.keyId,
34430
+ {
34431
+ status: "In Use" /* IN_USE */,
34432
+ updatedBy: value.updatedBy
34433
+ },
34434
+ value.site,
34435
+ session,
34436
+ void 0,
34437
+ id
34438
+ );
34439
+ item.receivedDate = /* @__PURE__ */ new Date();
34440
+ item.status = "Not Returned" /* NOT_RETURNED */;
34441
+ item.lastUpdate = null;
34442
+ item.remarks = "";
34443
+ } else {
34444
+ let status = "Invalid";
34445
+ if (item?.status == "Returned" /* RETURNED */) {
34446
+ status = "Available" /* AVAILABLE */;
34447
+ } else if (item?.status == "Removed" /* REMOVED */) {
34448
+ status = "Available" /* AVAILABLE */;
34449
+ } else if (item?.status == "Not Returned" /* NOT_RETURNED */) {
34450
+ status = "In Use" /* IN_USE */;
34451
+ } else if (item?.status && VALID_STATUSES.has(item.status)) {
34452
+ status = item.status;
34453
+ } else {
34454
+ throw new Error("Invalid Visitor Pass Status");
34455
+ }
34456
+ await KeyRepo.updateKeyById(
34457
+ item.keyId,
34458
+ {
34459
+ status,
34460
+ updatedBy: value.updatedBy
34461
+ },
34462
+ value.site,
34463
+ session,
34464
+ void 0,
34465
+ id
34466
+ );
34467
+ delete item.receivedDate;
34468
+ item.lastUpdate = /* @__PURE__ */ new Date();
34469
+ }
34470
+ }
34471
+ }
34277
34472
  await _updateVisitorTansactionById(id, value, session);
34278
34473
  const allowedPersonTypes = [
34279
34474
  "contractor" /* CONTRACTOR */,
@@ -60602,7 +60797,6 @@ function useHrmLabsAttendanceSrvc() {
60602
60797
  };
60603
60798
  } catch (error) {
60604
60799
  import_node_server_utils213.logger.error(error.message || error);
60605
- console.log("Error fetching attendance data:", error);
60606
60800
  return { success: false, message: error?.message || "Internal Server Error!", items: [], pages: 0, pageRange: "0-0 of 0", count: {} };
60607
60801
  }
60608
60802
  }
@@ -60716,7 +60910,6 @@ function useHrmLabsAttendanceSrvc() {
60716
60910
  return { totalCount };
60717
60911
  } catch (error) {
60718
60912
  import_node_server_utils213.logger.error(error.message || error);
60719
- console.log("Error fetching attendance data count:", error);
60720
60913
  return { success: false, message: error?.message || "Internal Server Error!", totalCount: null };
60721
60914
  }
60722
60915
  }
@@ -60888,7 +61081,6 @@ function useHrmLabsAttendanceSrvc() {
60888
61081
  };
60889
61082
  } catch (error) {
60890
61083
  import_node_server_utils213.logger.error(error.message || error);
60891
- console.log("Error fetching attendance data:", error);
60892
61084
  return { success: false, message: error?.message || "Internal Server Error!", items: [], count: {}, countPerJobTitle: {}, totalCount: null, countPerStatus: {} };
60893
61085
  }
60894
61086
  }
@@ -60995,7 +61187,6 @@ function useHrmLabsAttendanceSrvc() {
60995
61187
  };
60996
61188
  } catch (error) {
60997
61189
  import_node_server_utils213.logger.error(error.message || error);
60998
- console.log("Error fetching attendance data:", error);
60999
61190
  return { success: false, message: error?.message || "Internal Server Error!", chartCount: null };
61000
61191
  }
61001
61192
  }
@@ -66781,13 +66972,14 @@ var BidStatus = /* @__PURE__ */ ((BidStatus3) => {
66781
66972
  var schemaBidPreloved = import_joi145.default.object({
66782
66973
  type: import_joi145.default.string().valid(...Object.values(BidType)).required(),
66783
66974
  postId: import_joi145.default.string().hex().length(24).required(),
66975
+ receiverId: import_joi145.default.string().hex().length(24).required(),
66976
+ buyerId: import_joi145.default.string().hex().length(24).required(),
66784
66977
  price: import_joi145.default.when("type", {
66785
66978
  is: "bid" /* BID */,
66786
66979
  then: import_joi145.default.number().required(),
66787
66980
  otherwise: import_joi145.default.number().optional().allow(null)
66788
66981
  }),
66789
66982
  message: import_joi145.default.string().optional().allow("", null),
66790
- buyerId: import_joi145.default.string().hex().length(24).optional().allow("", null),
66791
66983
  status: import_joi145.default.string().valid(...Object.values(BidStatus)).optional().default("pending" /* PENDING */)
66792
66984
  });
66793
66985
  var schemaUpdateBidPreloved = import_joi145.default.object({
@@ -66856,29 +67048,92 @@ function useBidPrelovedRepo() {
66856
67048
  }
66857
67049
 
66858
67050
  // src/controllers/bid-preloved.controller.ts
66859
- var import_node_server_utils251 = require("@7365admin1/node-server-utils");
67051
+ var import_node_server_utils252 = require("@7365admin1/node-server-utils");
66860
67052
  var import_joi146 = __toESM(require("joi"));
67053
+
67054
+ // src/services/bid-preloved.service.ts
67055
+ var import_node_server_utils251 = require("@7365admin1/node-server-utils");
67056
+ function useBidPrelovedService() {
67057
+ const { add: _addBid } = useBidPrelovedRepo();
67058
+ const { add: _addChannel, getByParticipants: _getByParticipants } = useChannelPrelovedRepo();
67059
+ const { add: _addChat } = useChatPrelovedRepo();
67060
+ async function createBid(value) {
67061
+ const client = import_node_server_utils251.useAtlas.getClient();
67062
+ if (!client)
67063
+ throw new import_node_server_utils251.InternalServerError("Unable to connect to server.");
67064
+ const buyerId = value.buyerId;
67065
+ const receiverId = value.receiverId;
67066
+ const postId = value.postId;
67067
+ const messageText = value.message || "";
67068
+ const session = client.startSession();
67069
+ session.startTransaction();
67070
+ try {
67071
+ const bidId = await _addBid(value, session);
67072
+ const existingChannel = await _getByParticipants(
67073
+ buyerId,
67074
+ receiverId,
67075
+ postId
67076
+ );
67077
+ let channelId;
67078
+ if (existingChannel) {
67079
+ channelId = existingChannel._id.toString();
67080
+ } else {
67081
+ const newChannelId = await _addChannel(
67082
+ {
67083
+ senderId: buyerId,
67084
+ receiverId,
67085
+ postId
67086
+ },
67087
+ session
67088
+ );
67089
+ channelId = newChannelId.toString();
67090
+ }
67091
+ await _addChat(
67092
+ {
67093
+ channelId,
67094
+ senderId: buyerId,
67095
+ postId,
67096
+ bidId: bidId.toString(),
67097
+ message: {
67098
+ text: messageText,
67099
+ date: (/* @__PURE__ */ new Date()).toISOString(),
67100
+ senderId: buyerId
67101
+ }
67102
+ },
67103
+ session
67104
+ );
67105
+ await session.commitTransaction();
67106
+ return { bidId };
67107
+ } catch (error) {
67108
+ await session.abortTransaction();
67109
+ throw error;
67110
+ } finally {
67111
+ session.endSession();
67112
+ }
67113
+ }
67114
+ return { createBid };
67115
+ }
67116
+
67117
+ // src/controllers/bid-preloved.controller.ts
66861
67118
  function useBidPrelovedController() {
66862
- const {
66863
- add: _add,
66864
- getById: _getById,
66865
- updateStatus: _updateStatus
66866
- } = useBidPrelovedRepo();
67119
+ const { createBid: _createBid } = useBidPrelovedService();
67120
+ const { getById: _getById, updateStatus: _updateStatus } = useBidPrelovedRepo();
66867
67121
  async function add(req, res, next) {
66868
67122
  const { error, value } = schemaBidPreloved.validate(req.body, {
66869
67123
  abortEarly: false
66870
67124
  });
66871
67125
  if (error) {
66872
67126
  const messages = error.details.map((d) => d.message).join(", ");
66873
- import_node_server_utils251.logger.log({ level: "error", message: messages });
66874
- next(new import_node_server_utils251.BadRequestError(messages));
67127
+ import_node_server_utils252.logger.log({ level: "error", message: messages });
67128
+ next(new import_node_server_utils252.BadRequestError(messages));
66875
67129
  return;
66876
67130
  }
66877
67131
  try {
66878
- const data = await _add(value);
67132
+ const data = await _createBid(value);
66879
67133
  res.status(201).json(data);
66880
67134
  } catch (error2) {
66881
- import_node_server_utils251.logger.log({ level: "error", message: error2.message });
67135
+ console.log("error", error2);
67136
+ import_node_server_utils252.logger.log({ level: "error", message: error2.message });
66882
67137
  next(error2);
66883
67138
  }
66884
67139
  }
@@ -66890,23 +67145,23 @@ function useBidPrelovedController() {
66890
67145
  req.params
66891
67146
  );
66892
67147
  if (paramError) {
66893
- import_node_server_utils251.logger.log({ level: "error", message: paramError.message });
66894
- next(new import_node_server_utils251.BadRequestError(paramError.message));
67148
+ import_node_server_utils252.logger.log({ level: "error", message: paramError.message });
67149
+ next(new import_node_server_utils252.BadRequestError(paramError.message));
66895
67150
  return;
66896
67151
  }
66897
67152
  const { error: bodyError, value: body } = schemaUpdateBidPreloved.validate(
66898
67153
  req.body
66899
67154
  );
66900
67155
  if (bodyError) {
66901
- import_node_server_utils251.logger.log({ level: "error", message: bodyError.message });
66902
- next(new import_node_server_utils251.BadRequestError(bodyError.message));
67156
+ import_node_server_utils252.logger.log({ level: "error", message: bodyError.message });
67157
+ next(new import_node_server_utils252.BadRequestError(bodyError.message));
66903
67158
  return;
66904
67159
  }
66905
67160
  try {
66906
67161
  const data = await _updateStatus(params.id, body.status);
66907
67162
  res.status(200).json(data);
66908
67163
  } catch (error) {
66909
- import_node_server_utils251.logger.log({ level: "error", message: error.message });
67164
+ import_node_server_utils252.logger.log({ level: "error", message: error.message });
66910
67165
  next(error);
66911
67166
  }
66912
67167
  }
@@ -66916,15 +67171,15 @@ function useBidPrelovedController() {
66916
67171
  });
66917
67172
  const { error, value: params } = paramsSchema.validate(req.params);
66918
67173
  if (error) {
66919
- import_node_server_utils251.logger.log({ level: "error", message: error.message });
66920
- next(new import_node_server_utils251.BadRequestError(error.message));
67174
+ import_node_server_utils252.logger.log({ level: "error", message: error.message });
67175
+ next(new import_node_server_utils252.BadRequestError(error.message));
66921
67176
  return;
66922
67177
  }
66923
67178
  try {
66924
67179
  const data = await _getById(params.id);
66925
67180
  res.status(200).json(data);
66926
67181
  } catch (error2) {
66927
- import_node_server_utils251.logger.log({ level: "error", message: error2.message });
67182
+ import_node_server_utils252.logger.log({ level: "error", message: error2.message });
66928
67183
  next(error2);
66929
67184
  }
66930
67185
  }
@@ -67060,16 +67315,16 @@ var residentFormEntry = import_joi147.default.object({
67060
67315
  });
67061
67316
 
67062
67317
  // src/repositories/online-forms-v2.repository.ts
67063
- var import_node_server_utils252 = require("@7365admin1/node-server-utils");
67318
+ var import_node_server_utils253 = require("@7365admin1/node-server-utils");
67064
67319
  var import_mongodb152 = require("mongodb");
67065
67320
  var online_forms_namespace_collection = "online-forms";
67066
67321
  function useFormEntryRepo() {
67067
- const db = import_node_server_utils252.useAtlas.getDb();
67322
+ const db = import_node_server_utils253.useAtlas.getDb();
67068
67323
  if (!db) {
67069
- throw new import_node_server_utils252.InternalServerError("Unable to connect to server.");
67324
+ throw new import_node_server_utils253.InternalServerError("Unable to connect to server.");
67070
67325
  }
67071
67326
  const collection = db.collection(online_forms_namespace_collection);
67072
- const { delNamespace, getCache, setCache } = (0, import_node_server_utils252.useCache)(
67327
+ const { delNamespace, getCache, setCache } = (0, import_node_server_utils253.useCache)(
67073
67328
  online_forms_namespace_collection
67074
67329
  );
67075
67330
  const { getUserById } = useUserRepo();
@@ -67079,7 +67334,7 @@ function useFormEntryRepo() {
67079
67334
  name: "text"
67080
67335
  });
67081
67336
  } catch (error) {
67082
- throw new import_node_server_utils252.InternalServerError(
67337
+ throw new import_node_server_utils253.InternalServerError(
67083
67338
  "Failed to create text index on online form."
67084
67339
  );
67085
67340
  }
@@ -67088,21 +67343,11 @@ function useFormEntryRepo() {
67088
67343
  try {
67089
67344
  value = MFormEntry(value);
67090
67345
  const res = await collection.insertOne(value, { session });
67091
- delNamespace().then(() => {
67092
- import_node_server_utils252.logger.info(
67093
- `Cache cleared for namespace: ${online_forms_namespace_collection}`
67094
- );
67095
- }).catch((err) => {
67096
- import_node_server_utils252.logger.error(
67097
- `Failed to clear cache for namespace: ${online_forms_namespace_collection}`,
67098
- err
67099
- );
67100
- });
67101
67346
  return res.insertedId;
67102
67347
  } catch (error) {
67103
67348
  const isDuplicated = error.message.includes("duplicate");
67104
67349
  if (isDuplicated) {
67105
- throw new import_node_server_utils252.BadRequestError("Online Form already exists.");
67350
+ throw new import_node_server_utils253.BadRequestError("Online Form already exists.");
67106
67351
  }
67107
67352
  throw error;
67108
67353
  }
@@ -67120,18 +67365,10 @@ function useFormEntryRepo() {
67120
67365
  sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
67121
67366
  site = new import_mongodb152.ObjectId(site);
67122
67367
  org = new import_mongodb152.ObjectId(org);
67123
- const cacheOptions = {
67124
- page,
67125
- limit,
67126
- status,
67127
- sort: JSON.stringify(sort),
67128
- site,
67129
- ...search && { search }
67130
- };
67131
67368
  const query = {
67132
67369
  site,
67133
- status,
67134
- org
67370
+ status
67371
+ // org,
67135
67372
  };
67136
67373
  if (search && search !== "") {
67137
67374
  query.$or = [
@@ -67139,15 +67376,6 @@ function useFormEntryRepo() {
67139
67376
  { unitNumber: { $regex: search, $options: "i" } }
67140
67377
  ];
67141
67378
  }
67142
- const cacheKey = (0, import_node_server_utils252.makeCacheKey)(
67143
- online_forms_namespace_collection,
67144
- cacheOptions
67145
- );
67146
- const cachedData = await getCache(cacheKey);
67147
- if (cachedData) {
67148
- import_node_server_utils252.logger.info(`Cache hit for key: ${cacheKey}`);
67149
- return cachedData;
67150
- }
67151
67379
  try {
67152
67380
  const items = await collection.aggregate([
67153
67381
  { $match: query },
@@ -67172,40 +67400,24 @@ function useFormEntryRepo() {
67172
67400
  { $project: { user: 0 } }
67173
67401
  ]).toArray();
67174
67402
  const length = await collection.countDocuments(query);
67175
- const data = (0, import_node_server_utils252.paginate)(items, page, limit, length);
67176
- setCache(cacheKey, data, 15 * 60).then(() => {
67177
- import_node_server_utils252.logger.info(`Cache set for key: ${cacheKey}`);
67178
- }).catch((err) => {
67179
- import_node_server_utils252.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
67180
- });
67403
+ const data = (0, import_node_server_utils253.paginate)(items, page, limit, length);
67181
67404
  return data;
67182
67405
  } catch (error) {
67183
67406
  throw error;
67184
67407
  }
67185
67408
  }
67186
67409
  async function getFormEntryById(_id) {
67187
- const cacheKey = (0, import_node_server_utils252.makeCacheKey)(online_forms_namespace_collection, { _id });
67188
- const cachedData = await getCache(cacheKey);
67189
- if (cachedData) {
67190
- import_node_server_utils252.logger.info(`Cache hit for key: ${cacheKey}`);
67191
- return cachedData;
67192
- }
67193
67410
  try {
67194
67411
  _id = new import_mongodb152.ObjectId(_id);
67195
67412
  } catch (error) {
67196
- throw new import_node_server_utils252.BadRequestError("Invalid online form ID format.");
67413
+ throw new import_node_server_utils253.BadRequestError("Invalid online form ID format.");
67197
67414
  }
67198
67415
  const query = { _id, status: { $ne: "deleted" } };
67199
67416
  try {
67200
67417
  const [data] = await collection.aggregate([{ $match: query }]).toArray();
67201
67418
  if (!data) {
67202
- throw new import_node_server_utils252.NotFoundError("Document not found.");
67419
+ throw new import_node_server_utils253.NotFoundError("Document not found.");
67203
67420
  }
67204
- setCache(cacheKey, data, 15 * 60).then(() => {
67205
- import_node_server_utils252.logger.info(`Cache set for key: ${cacheKey}`);
67206
- }).catch((err) => {
67207
- import_node_server_utils252.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
67208
- });
67209
67421
  return data;
67210
67422
  } catch (error) {
67211
67423
  throw error;
@@ -67215,7 +67427,7 @@ function useFormEntryRepo() {
67215
67427
  try {
67216
67428
  _id = new import_mongodb152.ObjectId(_id);
67217
67429
  } catch (error) {
67218
- throw new import_node_server_utils252.BadRequestError("Invalid online form ID format.");
67430
+ throw new import_node_server_utils253.BadRequestError("Invalid online form ID format.");
67219
67431
  }
67220
67432
  try {
67221
67433
  const updateValue = {
@@ -67224,37 +67436,21 @@ function useFormEntryRepo() {
67224
67436
  };
67225
67437
  const res = await collection.updateOne({ _id }, { $set: updateValue });
67226
67438
  if (res.modifiedCount === 0) {
67227
- throw new import_node_server_utils252.InternalServerError("Unable to update online form.");
67439
+ throw new import_node_server_utils253.InternalServerError("Unable to update online form.");
67228
67440
  }
67229
67441
  const onlineFormRequest = await collection.findOne({ _id });
67230
67442
  if (!onlineFormRequest) {
67231
- throw new import_node_server_utils252.NotFoundError("Online form not found.");
67443
+ throw new import_node_server_utils253.NotFoundError("Online form not found.");
67232
67444
  }
67233
67445
  const user = await getUserById(onlineFormRequest.userId.toString());
67234
67446
  if (!user || !user._id) {
67235
- throw new import_node_server_utils252.NotFoundError("User not found.");
67447
+ throw new import_node_server_utils253.NotFoundError("User not found.");
67236
67448
  }
67237
67449
  const userId = user._id.toString();
67238
67450
  await NotificationService.onlineFormRequestStatusUpdated({
67239
67451
  to: userId,
67240
67452
  onlineFormId: onlineFormRequest._id,
67241
- // typeOfForm: onlineFormRequest.typeOfForm,
67242
- // unitNumber: onlineFormRequest.unitNumber,
67243
67453
  status: onlineFormRequest.status
67244
- // createdAt: onlineFormRequest.createdAt ?? "",
67245
- // fields: onlineFormRequest.fields ?? {},
67246
- // remarks: onlineFormRequest.remarks ?? "",
67247
- // managementValuesJson: onlineFormRequest.managementValues ?? {},
67248
- });
67249
- delNamespace().then(() => {
67250
- import_node_server_utils252.logger.info(
67251
- `Cache cleared for namespace: ${online_forms_namespace_collection}`
67252
- );
67253
- }).catch((err) => {
67254
- import_node_server_utils252.logger.error(
67255
- `Failed to clear cache for namespace: ${online_forms_namespace_collection}`,
67256
- err
67257
- );
67258
67454
  });
67259
67455
  return res.modifiedCount;
67260
67456
  } catch (error) {
@@ -67265,7 +67461,7 @@ function useFormEntryRepo() {
67265
67461
  try {
67266
67462
  _id = new import_mongodb152.ObjectId(_id);
67267
67463
  } catch (error) {
67268
- throw new import_node_server_utils252.BadRequestError("Invalid online form ID format.");
67464
+ throw new import_node_server_utils253.BadRequestError("Invalid online form ID format.");
67269
67465
  }
67270
67466
  try {
67271
67467
  const updateValue = {
@@ -67279,18 +67475,8 @@ function useFormEntryRepo() {
67279
67475
  { session }
67280
67476
  );
67281
67477
  if (res.modifiedCount === 0) {
67282
- throw new import_node_server_utils252.InternalServerError("Unable to delete online form.");
67478
+ throw new import_node_server_utils253.InternalServerError("Unable to delete online form.");
67283
67479
  }
67284
- delNamespace().then(() => {
67285
- import_node_server_utils252.logger.info(
67286
- `Cache cleared for namespace: ${online_forms_namespace_collection}`
67287
- );
67288
- }).catch((err) => {
67289
- import_node_server_utils252.logger.error(
67290
- `Failed to clear cache for namespace: ${online_forms_namespace_collection}`,
67291
- err
67292
- );
67293
- });
67294
67480
  return res.modifiedCount;
67295
67481
  } catch (error) {
67296
67482
  throw new Error(error.message);
@@ -67302,21 +67488,11 @@ function useFormEntryRepo() {
67302
67488
  value.org = new import_mongodb152.ObjectId(value.org);
67303
67489
  value.userId = new import_mongodb152.ObjectId(value.userId);
67304
67490
  const res = await collection.insertOne(value, { session });
67305
- delNamespace().then(() => {
67306
- import_node_server_utils252.logger.info(
67307
- `Cache cleared for namespace: ${online_forms_namespace_collection}`
67308
- );
67309
- }).catch((err) => {
67310
- import_node_server_utils252.logger.error(
67311
- `Failed to clear cache for namespace: ${online_forms_namespace_collection}`,
67312
- err
67313
- );
67314
- });
67315
67491
  return res.insertedId;
67316
67492
  } catch (error) {
67317
67493
  const isDuplicated = error.message.includes("duplicate");
67318
67494
  if (isDuplicated) {
67319
- throw new import_node_server_utils252.BadRequestError("Online Form already exists.");
67495
+ throw new import_node_server_utils253.BadRequestError("Online Form already exists.");
67320
67496
  }
67321
67497
  throw error;
67322
67498
  }
@@ -67324,22 +67500,61 @@ function useFormEntryRepo() {
67324
67500
  async function residentForm({
67325
67501
  userId,
67326
67502
  site,
67327
- org
67503
+ org,
67504
+ search = "",
67505
+ page = 1,
67506
+ limit = 10,
67507
+ sort = {}
67328
67508
  }) {
67509
+ page = page > 0 ? page - 1 : 0;
67510
+ sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
67511
+ const user = new import_mongodb152.ObjectId(userId);
67512
+ const siteId = new import_mongodb152.ObjectId(site);
67513
+ const orgId = new import_mongodb152.ObjectId(org);
67514
+ const cacheOptions = {
67515
+ page,
67516
+ limit,
67517
+ sort: JSON.stringify(sort),
67518
+ userId: user,
67519
+ site: siteId,
67520
+ org: orgId,
67521
+ ...search && { search }
67522
+ };
67523
+ const query = {
67524
+ userId: user,
67525
+ site: siteId,
67526
+ org: orgId
67527
+ };
67528
+ if (search && search !== "") {
67529
+ query.$or = [
67530
+ { typeOfForm: { $regex: search, $options: "i" } },
67531
+ { unitNumber: { $regex: search, $options: "i" } }
67532
+ ];
67533
+ }
67534
+ const cacheKey = (0, import_node_server_utils253.makeCacheKey)(
67535
+ online_forms_namespace_collection,
67536
+ cacheOptions
67537
+ );
67538
+ const cachedData = await getCache(cacheKey);
67539
+ if (cachedData) {
67540
+ import_node_server_utils253.logger.info(`Cache hit for key: ${cacheKey}`);
67541
+ return cachedData;
67542
+ }
67329
67543
  try {
67330
- const user = new import_mongodb152.ObjectId(userId);
67331
- const siteId = new import_mongodb152.ObjectId(site);
67332
- const orgId = new import_mongodb152.ObjectId(org);
67333
- const res = await collection.aggregate([
67334
- {
67335
- $match: {
67336
- userId: user,
67337
- site: siteId,
67338
- org: orgId
67339
- }
67340
- }
67544
+ const items = await collection.aggregate([
67545
+ { $match: query },
67546
+ { $sort: sort },
67547
+ { $skip: page * limit },
67548
+ { $limit: limit }
67341
67549
  ]).toArray();
67342
- return res;
67550
+ const length = await collection.countDocuments(query);
67551
+ const data = (0, import_node_server_utils253.paginate)(items, page, limit, length);
67552
+ setCache(cacheKey, data, 15 * 60).then(() => {
67553
+ import_node_server_utils253.logger.info(`Cache set for key: ${cacheKey}`);
67554
+ }).catch((err) => {
67555
+ import_node_server_utils253.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
67556
+ });
67557
+ return data;
67343
67558
  } catch (error) {
67344
67559
  throw error;
67345
67560
  }
@@ -67357,7 +67572,7 @@ function useFormEntryRepo() {
67357
67572
  }
67358
67573
 
67359
67574
  // src/controllers/online-forms-v2.controller.ts
67360
- var import_node_server_utils253 = require("@7365admin1/node-server-utils");
67575
+ var import_node_server_utils254 = require("@7365admin1/node-server-utils");
67361
67576
  var import_joi148 = __toESM(require("joi"));
67362
67577
  var import_exceljs3 = __toESM(require("exceljs"));
67363
67578
  var import_fs7 = __toESM(require("fs"));
@@ -67384,14 +67599,14 @@ function useFormEntryController() {
67384
67599
  async function uploadFormEntrys(req, res, next) {
67385
67600
  try {
67386
67601
  if (!req.file) {
67387
- next(new import_node_server_utils253.BadRequestError("Excel file is required."));
67602
+ next(new import_node_server_utils254.BadRequestError("Excel file is required."));
67388
67603
  return;
67389
67604
  }
67390
67605
  const workbook = new import_exceljs3.default.Workbook();
67391
67606
  await workbook.xlsx.readFile(req.file.path);
67392
67607
  const worksheet = workbook.worksheets[0];
67393
67608
  if (!worksheet) {
67394
- next(new import_node_server_utils253.BadRequestError("No worksheet found in uploaded Excel file."));
67609
+ next(new import_node_server_utils254.BadRequestError("No worksheet found in uploaded Excel file."));
67395
67610
  return;
67396
67611
  }
67397
67612
  const headerRow = worksheet.getRow(1);
@@ -67419,8 +67634,8 @@ function useFormEntryController() {
67419
67634
  });
67420
67635
  if (error) {
67421
67636
  const messages = error.details.map((d) => d.message).join(", ");
67422
- import_node_server_utils253.logger.log({ level: "error", message: messages });
67423
- next(new import_node_server_utils253.BadRequestError(messages));
67637
+ import_node_server_utils254.logger.log({ level: "error", message: messages });
67638
+ next(new import_node_server_utils254.BadRequestError(messages));
67424
67639
  return;
67425
67640
  }
67426
67641
  const result = await _add(value);
@@ -67428,7 +67643,7 @@ function useFormEntryController() {
67428
67643
  import_fs7.default.unlink(req.file.path, () => {
67429
67644
  });
67430
67645
  } catch (error) {
67431
- import_node_server_utils253.logger.log({ level: "error", message: error.message });
67646
+ import_node_server_utils254.logger.log({ level: "error", message: error.message });
67432
67647
  next(error);
67433
67648
  }
67434
67649
  }
@@ -67445,8 +67660,8 @@ function useFormEntryController() {
67445
67660
  const { error, value } = schema2.validate(req.query);
67446
67661
  if (error) {
67447
67662
  const messages = error.details.map((d) => d.message).join(", ");
67448
- import_node_server_utils253.logger.log({ level: "error", message: messages });
67449
- next(new import_node_server_utils253.BadRequestError(messages));
67663
+ import_node_server_utils254.logger.log({ level: "error", message: messages });
67664
+ next(new import_node_server_utils254.BadRequestError(messages));
67450
67665
  return;
67451
67666
  }
67452
67667
  const { search, page, limit, status, org, site } = value;
@@ -67454,7 +67669,7 @@ function useFormEntryController() {
67454
67669
  res.json(data);
67455
67670
  return;
67456
67671
  } catch (error) {
67457
- import_node_server_utils253.logger.log({ level: "error", message: error.message });
67672
+ import_node_server_utils254.logger.log({ level: "error", message: error.message });
67458
67673
  next(error);
67459
67674
  return;
67460
67675
  }
@@ -67467,8 +67682,8 @@ function useFormEntryController() {
67467
67682
  const { error, value } = schema2.validate({ _id: req.params.id });
67468
67683
  if (error) {
67469
67684
  const messages = error.details.map((d) => d.message).join(", ");
67470
- import_node_server_utils253.logger.log({ level: "error", message: messages });
67471
- next(new import_node_server_utils253.BadRequestError(messages));
67685
+ import_node_server_utils254.logger.log({ level: "error", message: messages });
67686
+ next(new import_node_server_utils254.BadRequestError(messages));
67472
67687
  return;
67473
67688
  }
67474
67689
  const { _id } = value;
@@ -67476,7 +67691,7 @@ function useFormEntryController() {
67476
67691
  res.json(data);
67477
67692
  return;
67478
67693
  } catch (error) {
67479
- import_node_server_utils253.logger.log({ level: "error", message: error.message });
67694
+ import_node_server_utils254.logger.log({ level: "error", message: error.message });
67480
67695
  next(error);
67481
67696
  return;
67482
67697
  }
@@ -67489,8 +67704,8 @@ function useFormEntryController() {
67489
67704
  });
67490
67705
  if (error) {
67491
67706
  const messages = error.details.map((d) => d.message).join(", ");
67492
- import_node_server_utils253.logger.log({ level: "error", message: messages });
67493
- next(new import_node_server_utils253.BadRequestError(messages));
67707
+ import_node_server_utils254.logger.log({ level: "error", message: messages });
67708
+ next(new import_node_server_utils254.BadRequestError(messages));
67494
67709
  return;
67495
67710
  }
67496
67711
  const { _id, ...rest } = value;
@@ -67498,7 +67713,7 @@ function useFormEntryController() {
67498
67713
  res.json({ message: "Successfully updated online form." });
67499
67714
  return;
67500
67715
  } catch (error) {
67501
- import_node_server_utils253.logger.log({ level: "error", message: error.message });
67716
+ import_node_server_utils254.logger.log({ level: "error", message: error.message });
67502
67717
  next(error);
67503
67718
  return;
67504
67719
  }
@@ -67509,15 +67724,15 @@ function useFormEntryController() {
67509
67724
  const _id = req.params.id;
67510
67725
  const { error } = validation.validate(_id);
67511
67726
  if (error) {
67512
- import_node_server_utils253.logger.log({ level: "error", message: error.message });
67513
- next(new import_node_server_utils253.BadRequestError(error.message));
67727
+ import_node_server_utils254.logger.log({ level: "error", message: error.message });
67728
+ next(new import_node_server_utils254.BadRequestError(error.message));
67514
67729
  return;
67515
67730
  }
67516
67731
  await _deleteOnlineFormById(_id);
67517
67732
  res.json({ message: "Successfully deleted online form." });
67518
67733
  return;
67519
67734
  } catch (error) {
67520
- import_node_server_utils253.logger.log({ level: "error", message: error.message });
67735
+ import_node_server_utils254.logger.log({ level: "error", message: error.message });
67521
67736
  next(error);
67522
67737
  return;
67523
67738
  }
@@ -67534,8 +67749,8 @@ function useFormEntryController() {
67534
67749
  });
67535
67750
  if (error) {
67536
67751
  const messages = error.details.map((d) => d.message).join(", ");
67537
- import_node_server_utils253.logger.log({ level: "error", message: messages });
67538
- next(new import_node_server_utils253.BadRequestError(messages));
67752
+ import_node_server_utils254.logger.log({ level: "error", message: messages });
67753
+ next(new import_node_server_utils254.BadRequestError(messages));
67539
67754
  return;
67540
67755
  }
67541
67756
  try {
@@ -67543,7 +67758,7 @@ function useFormEntryController() {
67543
67758
  res.status(201).json({ message: data });
67544
67759
  return;
67545
67760
  } catch (error2) {
67546
- import_node_server_utils253.logger.log({ level: "error", message: error2.message });
67761
+ import_node_server_utils254.logger.log({ level: "error", message: error2.message });
67547
67762
  next(error2);
67548
67763
  return;
67549
67764
  }
@@ -67555,8 +67770,8 @@ function useFormEntryController() {
67555
67770
  });
67556
67771
  if (error) {
67557
67772
  const messages = error.details.map((d) => d.message).join(", ");
67558
- import_node_server_utils253.logger.log({ level: "error", message: messages });
67559
- next(new import_node_server_utils253.BadRequestError(messages));
67773
+ import_node_server_utils254.logger.log({ level: "error", message: messages });
67774
+ next(new import_node_server_utils254.BadRequestError(messages));
67560
67775
  return;
67561
67776
  }
67562
67777
  try {
@@ -67564,30 +67779,35 @@ function useFormEntryController() {
67564
67779
  res.status(201).json({ message: data });
67565
67780
  return;
67566
67781
  } catch (error2) {
67567
- import_node_server_utils253.logger.log({ level: "error", message: error2.message });
67782
+ import_node_server_utils254.logger.log({ level: "error", message: error2.message });
67568
67783
  next(error2);
67569
67784
  return;
67570
67785
  }
67571
67786
  }
67572
67787
  async function residentForm(req, res, next) {
67573
67788
  try {
67574
- const { site, userId, org } = req.query;
67575
67789
  const residentFormPayload = import_joi148.default.object({
67576
67790
  org: import_joi148.default.string().hex().required(),
67577
67791
  site: import_joi148.default.string().hex().required(),
67578
- userId: import_joi148.default.string().hex().required()
67792
+ userId: import_joi148.default.string().hex().required(),
67793
+ search: import_joi148.default.string().optional().allow("", null),
67794
+ page: import_joi148.default.number().integer().min(1).allow("", null).default(1),
67795
+ limit: import_joi148.default.number().integer().min(1).max(100).allow("", null).default(10)
67796
+ });
67797
+ const { error, value } = residentFormPayload.validate(req.query, {
67798
+ abortEarly: true
67579
67799
  });
67580
- const { error } = residentFormPayload.validate({ site, userId, org }, { abortEarly: true });
67581
67800
  if (error) {
67582
67801
  const messages = error.details.map((d) => d.message).join(", ");
67583
- import_node_server_utils253.logger.log({ level: "error", message: messages });
67584
- next(new import_node_server_utils253.BadRequestError(messages));
67802
+ import_node_server_utils254.logger.log({ level: "error", message: messages });
67803
+ next(new import_node_server_utils254.BadRequestError(messages));
67585
67804
  return;
67586
67805
  }
67587
- const result = await _residentForm({ userId, site, org });
67806
+ const { site, userId, org, search, page, limit } = value;
67807
+ const result = await _residentForm({ userId, site, org, search, page, limit });
67588
67808
  res.json(result);
67589
67809
  } catch (error) {
67590
- import_node_server_utils253.logger.log({ level: "error", message: error.message });
67810
+ import_node_server_utils254.logger.log({ level: "error", message: error.message });
67591
67811
  next(error);
67592
67812
  return;
67593
67813
  }
@@ -67605,11 +67825,11 @@ function useFormEntryController() {
67605
67825
  }
67606
67826
 
67607
67827
  // src/services/building-level.service.ts
67608
- var import_node_server_utils254 = require("@7365admin1/node-server-utils");
67828
+ var import_node_server_utils255 = require("@7365admin1/node-server-utils");
67609
67829
  function useBuildingLevelService() {
67610
67830
  const { add: _add, updateLevelById: _updateLevelById } = useBuildingLevelRepo();
67611
67831
  async function add(value) {
67612
- const session = import_node_server_utils254.useAtlas.getClient()?.startSession();
67832
+ const session = import_node_server_utils255.useAtlas.getClient()?.startSession();
67613
67833
  try {
67614
67834
  session?.startTransaction();
67615
67835
  await _add(value, session);
@@ -67623,7 +67843,7 @@ function useBuildingLevelService() {
67623
67843
  }
67624
67844
  }
67625
67845
  async function updateLevelById(_id, value) {
67626
- const session = import_node_server_utils254.useAtlas.getClient()?.startSession();
67846
+ const session = import_node_server_utils255.useAtlas.getClient()?.startSession();
67627
67847
  try {
67628
67848
  session?.startTransaction();
67629
67849
  await _updateLevelById(_id, value, session);
@@ -67643,7 +67863,7 @@ function useBuildingLevelService() {
67643
67863
  }
67644
67864
 
67645
67865
  // src/controllers/building-level.controller.ts
67646
- var import_node_server_utils255 = require("@7365admin1/node-server-utils");
67866
+ var import_node_server_utils256 = require("@7365admin1/node-server-utils");
67647
67867
  var import_joi149 = __toESM(require("joi"));
67648
67868
  function useBuildingLevelController() {
67649
67869
  const { add: _add, updateLevelById: _updateLevelById } = useBuildingLevelService();
@@ -67661,8 +67881,8 @@ function useBuildingLevelController() {
67661
67881
  });
67662
67882
  if (error) {
67663
67883
  const messages = error.details.map((d) => d.message).join(", ");
67664
- import_node_server_utils255.logger.log({ level: "error", message: messages });
67665
- next(new import_node_server_utils255.BadRequestError(messages));
67884
+ import_node_server_utils256.logger.log({ level: "error", message: messages });
67885
+ next(new import_node_server_utils256.BadRequestError(messages));
67666
67886
  return;
67667
67887
  }
67668
67888
  const result = await _add(value);
@@ -67685,8 +67905,8 @@ function useBuildingLevelController() {
67685
67905
  });
67686
67906
  if (error) {
67687
67907
  const messages = error.details.map((d) => d.message);
67688
- import_node_server_utils255.logger.log({ level: "error", message: messages.join(", ") });
67689
- next(new import_node_server_utils255.BadRequestError(messages.join(", ")));
67908
+ import_node_server_utils256.logger.log({ level: "error", message: messages.join(", ") });
67909
+ next(new import_node_server_utils256.BadRequestError(messages.join(", ")));
67690
67910
  return;
67691
67911
  }
67692
67912
  const { page, limit, status, site, search } = value;
@@ -67711,8 +67931,8 @@ function useBuildingLevelController() {
67711
67931
  const { error, value } = schema2.validate({ id: req.params.id });
67712
67932
  if (error) {
67713
67933
  const messages = error.details.map((d) => d.message);
67714
- import_node_server_utils255.logger.log({ level: "error", message: messages.join(", ") });
67715
- next(new import_node_server_utils255.BadRequestError(messages.join(", ")));
67934
+ import_node_server_utils256.logger.log({ level: "error", message: messages.join(", ") });
67935
+ next(new import_node_server_utils256.BadRequestError(messages.join(", ")));
67716
67936
  return;
67717
67937
  }
67718
67938
  const { id } = value;
@@ -67731,8 +67951,8 @@ function useBuildingLevelController() {
67731
67951
  });
67732
67952
  if (error) {
67733
67953
  const messages = error.details.map((d) => d.message);
67734
- import_node_server_utils255.logger.log({ level: "error", message: messages.join(", ") });
67735
- next(new import_node_server_utils255.BadRequestError(messages.join(", ")));
67954
+ import_node_server_utils256.logger.log({ level: "error", message: messages.join(", ") });
67955
+ next(new import_node_server_utils256.BadRequestError(messages.join(", ")));
67736
67956
  return;
67737
67957
  }
67738
67958
  const { _id, ...rest } = value;
@@ -67750,8 +67970,8 @@ function useBuildingLevelController() {
67750
67970
  const { error, value } = schema2.validate({ id: req.params.id });
67751
67971
  if (error) {
67752
67972
  const messages = error.details.map((d) => d.message);
67753
- import_node_server_utils255.logger.log({ level: "error", message: messages.join(", ") });
67754
- next(new import_node_server_utils255.BadRequestError(messages.join(", ")));
67973
+ import_node_server_utils256.logger.log({ level: "error", message: messages.join(", ") });
67974
+ next(new import_node_server_utils256.BadRequestError(messages.join(", ")));
67755
67975
  return;
67756
67976
  }
67757
67977
  const { id } = value;
@@ -67775,8 +67995,8 @@ function useBuildingLevelController() {
67775
67995
  const { error, value } = schema2.validate(req.body);
67776
67996
  if (error) {
67777
67997
  const messages = error.details.map((d) => d.message);
67778
- import_node_server_utils255.logger.log({ level: "error", message: messages.join(", ") });
67779
- next(new import_node_server_utils255.BadRequestError(messages.join(", ")));
67998
+ import_node_server_utils256.logger.log({ level: "error", message: messages.join(", ") });
67999
+ next(new import_node_server_utils256.BadRequestError(messages.join(", ")));
67780
68000
  return;
67781
68001
  }
67782
68002
  const updates = value.map((item) => ({
@@ -67801,8 +68021,8 @@ function useBuildingLevelController() {
67801
68021
  });
67802
68022
  if (error) {
67803
68023
  const messages = error.details.map((d) => d.message).join(", ");
67804
- import_node_server_utils255.logger.log({ level: "error", message: messages });
67805
- next(new import_node_server_utils255.BadRequestError(messages));
68024
+ import_node_server_utils256.logger.log({ level: "error", message: messages });
68025
+ next(new import_node_server_utils256.BadRequestError(messages));
67806
68026
  return;
67807
68027
  }
67808
68028
  const { site, block } = value;
@@ -67825,7 +68045,7 @@ function useBuildingLevelController() {
67825
68045
  }
67826
68046
 
67827
68047
  // src/models/hid-amico.model.ts
67828
- var import_node_server_utils256 = require("@7365admin1/node-server-utils");
68048
+ var import_node_server_utils257 = require("@7365admin1/node-server-utils");
67829
68049
  var import_mongodb153 = require("mongodb");
67830
68050
  var import_joi150 = __toESM(require("joi"));
67831
68051
  function canReadObjectId(value) {
@@ -67865,7 +68085,7 @@ function toObjectId23(value, label = "ID") {
67865
68085
  return toObjectId23(text, label);
67866
68086
  }
67867
68087
  }
67868
- throw new import_node_server_utils256.BadRequestError(`Invalid ${label} format`);
68088
+ throw new import_node_server_utils257.BadRequestError(`Invalid ${label} format`);
67869
68089
  }
67870
68090
  var objectIdSchema2 = import_joi150.default.custom((value, helpers) => canReadObjectId(value) ? value : helpers.error("any.invalid"), "ObjectId").messages({
67871
68091
  "any.invalid": "{{#label}} must be a valid ObjectId"
@@ -68037,8 +68257,8 @@ var schemaHidAmicoNotificationParams = import_joi150.default.object({
68037
68257
  function MHidAmicoReader(value) {
68038
68258
  const { error } = schemaHidAmicoReader.validate(value);
68039
68259
  if (error) {
68040
- import_node_server_utils256.logger.info(`HID Amico reader: ${error.message}`);
68041
- throw new import_node_server_utils256.BadRequestError(error.message);
68260
+ import_node_server_utils257.logger.info(`HID Amico reader: ${error.message}`);
68261
+ throw new import_node_server_utils257.BadRequestError(error.message);
68042
68262
  }
68043
68263
  return {
68044
68264
  _id: value._id ? toObjectId23(value._id, "reader ID") : new import_mongodb153.ObjectId(),
@@ -68064,8 +68284,8 @@ function MHidAmicoReader(value) {
68064
68284
  function MHidAmicoEvent(value) {
68065
68285
  const { error } = schemaHidAmicoEvent.validate(value);
68066
68286
  if (error) {
68067
- import_node_server_utils256.logger.info(`HID Amico event: ${error.message}`);
68068
- throw new import_node_server_utils256.BadRequestError(error.message);
68287
+ import_node_server_utils257.logger.info(`HID Amico event: ${error.message}`);
68288
+ throw new import_node_server_utils257.BadRequestError(error.message);
68069
68289
  }
68070
68290
  return {
68071
68291
  _id: value._id ? toObjectId23(value._id, "event ID") : new import_mongodb153.ObjectId(),
@@ -68083,8 +68303,8 @@ function optionalObjectId(value) {
68083
68303
  function MHidAmicoIdentity(value) {
68084
68304
  const { error } = schemaHidAmicoIdentity.validate(value);
68085
68305
  if (error) {
68086
- import_node_server_utils256.logger.info(`HID Amico identity: ${error.message}`);
68087
- throw new import_node_server_utils256.BadRequestError(error.message);
68306
+ import_node_server_utils257.logger.info(`HID Amico identity: ${error.message}`);
68307
+ throw new import_node_server_utils257.BadRequestError(error.message);
68088
68308
  }
68089
68309
  return {
68090
68310
  _id: value._id ? toObjectId23(value._id, "identity ID") : new import_mongodb153.ObjectId(),
@@ -68107,13 +68327,13 @@ function MHidAmicoIdentity(value) {
68107
68327
  }
68108
68328
 
68109
68329
  // src/repositories/hid-amico.repo.ts
68110
- var import_node_server_utils257 = require("@7365admin1/node-server-utils");
68330
+ var import_node_server_utils258 = require("@7365admin1/node-server-utils");
68111
68331
  var import_mongodb154 = require("mongodb");
68112
68332
  function useHidAmicoRepo() {
68113
68333
  function db() {
68114
- const instance = import_node_server_utils257.useAtlas.getDb();
68334
+ const instance = import_node_server_utils258.useAtlas.getDb();
68115
68335
  if (!instance) {
68116
- throw new import_node_server_utils257.InternalServerError("Unable to connect to server.");
68336
+ throw new import_node_server_utils258.InternalServerError("Unable to connect to server.");
68117
68337
  }
68118
68338
  return instance;
68119
68339
  }
@@ -68130,7 +68350,7 @@ function useHidAmicoRepo() {
68130
68350
  try {
68131
68351
  return new import_mongodb154.ObjectId(id);
68132
68352
  } catch {
68133
- throw new import_node_server_utils257.BadRequestError(`Invalid ${label} format`);
68353
+ throw new import_node_server_utils258.BadRequestError(`Invalid ${label} format`);
68134
68354
  }
68135
68355
  }
68136
68356
  function hideSecret(reader) {
@@ -68181,7 +68401,7 @@ function useHidAmicoRepo() {
68181
68401
  ]);
68182
68402
  return "HID Amico indexes created.";
68183
68403
  } catch (error) {
68184
- import_node_server_utils257.logger.error(error.message);
68404
+ import_node_server_utils258.logger.error(error.message);
68185
68405
  throw new Error("Failed to create HID Amico indexes.");
68186
68406
  }
68187
68407
  }
@@ -68192,7 +68412,7 @@ function useHidAmicoRepo() {
68192
68412
  return hideSecret(doc);
68193
68413
  } catch (error) {
68194
68414
  if (error.message?.includes("duplicate")) {
68195
- throw new import_node_server_utils257.BadRequestError("HID Amico reader already exists for this site and URL.");
68415
+ throw new import_node_server_utils258.BadRequestError("HID Amico reader already exists for this site and URL.");
68196
68416
  }
68197
68417
  throw error;
68198
68418
  }
@@ -68206,7 +68426,7 @@ function useHidAmicoRepo() {
68206
68426
  }
68207
68427
  const items = await readers().find(query).sort({ createdAt: -1 }).skip(page * limit).limit(limit).toArray();
68208
68428
  const total = await readers().countDocuments(query);
68209
- return (0, import_node_server_utils257.paginate)(items.map(hideSecret), page, limit, total);
68429
+ return (0, import_node_server_utils258.paginate)(items.map(hideSecret), page, limit, total);
68210
68430
  }
68211
68431
  async function getById(id, options = {}) {
68212
68432
  const _id = toId(id, "reader ID");
@@ -68214,14 +68434,14 @@ function useHidAmicoRepo() {
68214
68434
  { _id, status: { $ne: "deleted" } }
68215
68435
  );
68216
68436
  if (!reader) {
68217
- throw new import_node_server_utils257.BadRequestError("HID Amico reader not found.");
68437
+ throw new import_node_server_utils258.BadRequestError("HID Amico reader not found.");
68218
68438
  }
68219
68439
  return options.includePassword ? reader : hideSecret(reader);
68220
68440
  }
68221
68441
  async function updateById(id, value, session) {
68222
68442
  const { error } = schemaUpdateHidAmicoReader.validate(value);
68223
68443
  if (error) {
68224
- throw new import_node_server_utils257.BadRequestError(error.message);
68444
+ throw new import_node_server_utils258.BadRequestError(error.message);
68225
68445
  }
68226
68446
  const _id = toId(id, "reader ID");
68227
68447
  const payload = {
@@ -68240,7 +68460,7 @@ function useHidAmicoRepo() {
68240
68460
  { returnDocument: "after", session }
68241
68461
  );
68242
68462
  if (!updated) {
68243
- throw new import_node_server_utils257.BadRequestError("HID Amico reader not found.");
68463
+ throw new import_node_server_utils258.BadRequestError("HID Amico reader not found.");
68244
68464
  }
68245
68465
  return hideSecret(updated);
68246
68466
  }
@@ -68269,7 +68489,7 @@ function useHidAmicoRepo() {
68269
68489
  }
68270
68490
  const items = await events().find(query).sort({ createdAt: -1 }).skip(page * limit).limit(limit).toArray();
68271
68491
  const total = await events().countDocuments(query);
68272
- return (0, import_node_server_utils257.paginate)(items, page, limit, total);
68492
+ return (0, import_node_server_utils258.paginate)(items, page, limit, total);
68273
68493
  }
68274
68494
  async function addIdentity(value, session) {
68275
68495
  try {
@@ -68281,16 +68501,16 @@ function useHidAmicoRepo() {
68281
68501
  cardNo: identity.cardNo
68282
68502
  });
68283
68503
  if (existing) {
68284
- throw new import_node_server_utils257.BadRequestError("HID Amico identity already exists for this reader.");
68504
+ throw new import_node_server_utils258.BadRequestError("HID Amico identity already exists for this reader.");
68285
68505
  }
68286
68506
  await identities().insertOne(identity, { session });
68287
68507
  return identity;
68288
68508
  } catch (error) {
68289
- if (error instanceof import_node_server_utils257.BadRequestError) {
68509
+ if (error instanceof import_node_server_utils258.BadRequestError) {
68290
68510
  throw error;
68291
68511
  }
68292
68512
  if (error.message?.includes("duplicate")) {
68293
- throw new import_node_server_utils257.BadRequestError("HID Amico identity already exists for this reader.");
68513
+ throw new import_node_server_utils258.BadRequestError("HID Amico identity already exists for this reader.");
68294
68514
  }
68295
68515
  throw error;
68296
68516
  }
@@ -68315,12 +68535,12 @@ function useHidAmicoRepo() {
68315
68535
  }
68316
68536
  const items = await identities().find(query).sort({ createdAt: -1 }).skip(page * limit).limit(limit).toArray();
68317
68537
  const total = await identities().countDocuments(query);
68318
- return (0, import_node_server_utils257.paginate)(items, page, limit, total);
68538
+ return (0, import_node_server_utils258.paginate)(items, page, limit, total);
68319
68539
  }
68320
68540
  async function updateIdentity(id, value, session) {
68321
68541
  const { error } = schemaUpdateHidAmicoIdentity.validate(value);
68322
68542
  if (error) {
68323
- throw new import_node_server_utils257.BadRequestError(error.message);
68543
+ throw new import_node_server_utils258.BadRequestError(error.message);
68324
68544
  }
68325
68545
  const payload = {
68326
68546
  ...value,
@@ -68348,7 +68568,7 @@ function useHidAmicoRepo() {
68348
68568
  { returnDocument: "after", session }
68349
68569
  );
68350
68570
  if (!updated) {
68351
- throw new import_node_server_utils257.BadRequestError("HID Amico identity not found.");
68571
+ throw new import_node_server_utils258.BadRequestError("HID Amico identity not found.");
68352
68572
  }
68353
68573
  return updated;
68354
68574
  }
@@ -68405,7 +68625,7 @@ function useHidAmicoRepo() {
68405
68625
  // src/services/hid-amico.service.ts
68406
68626
  var import_crypto3 = __toESM(require("crypto"));
68407
68627
  var import_axios4 = __toESM(require("axios"));
68408
- var import_node_server_utils258 = require("@7365admin1/node-server-utils");
68628
+ var import_node_server_utils259 = require("@7365admin1/node-server-utils");
68409
68629
  var PASSWORD_PREFIX = "v1";
68410
68630
  function getSecretKey() {
68411
68631
  const secret = process.env.HID_AMICO_SECRET || process.env.ACCESS_TOKEN_SECRET || "iservice365-hid-amico";
@@ -68449,7 +68669,7 @@ function toHidRequestError(error, path5) {
68449
68669
  status ? `status ${status}` : "",
68450
68670
  payload ? `response ${payload}` : error.message
68451
68671
  ].filter(Boolean).join(" - ");
68452
- return new import_node_server_utils258.BadRequestError(detail);
68672
+ return new import_node_server_utils259.BadRequestError(detail);
68453
68673
  }
68454
68674
  return error;
68455
68675
  }
@@ -68491,7 +68711,7 @@ var HidAmicoClient = class {
68491
68711
  password: decryptSecret(this.reader.password)
68492
68712
  });
68493
68713
  if (!res.data?.session) {
68494
- throw new import_node_server_utils258.BadRequestError("HID Amico login failed: missing session.");
68714
+ throw new import_node_server_utils259.BadRequestError("HID Amico login failed: missing session.");
68495
68715
  }
68496
68716
  this.session = res.data.session;
68497
68717
  return this.session;
@@ -68515,7 +68735,7 @@ var HidAmicoClient = class {
68515
68735
  const payload = JSON.parse(buffer.toString("utf8"));
68516
68736
  const image = payload.image || payload.photo || payload.data?.image || payload.data?.photo;
68517
68737
  if (!image) {
68518
- throw new import_node_server_utils258.BadRequestError("HID Amico user image response did not include an image.");
68738
+ throw new import_node_server_utils259.BadRequestError("HID Amico user image response did not include an image.");
68519
68739
  }
68520
68740
  return {
68521
68741
  contentType: payload.contentType || payload.mimeType || "image/jpeg",
@@ -68640,7 +68860,7 @@ function useHidAmicoService() {
68640
68860
  async function getActiveReader(id) {
68641
68861
  const reader = await repo.getById(id, { includePassword: true });
68642
68862
  if (reader.enabled === false || reader.status === "inactive" || reader.status === "deleted") {
68643
- throw new import_node_server_utils258.BadRequestError("HID Amico reader is not active.");
68863
+ throw new import_node_server_utils259.BadRequestError("HID Amico reader is not active.");
68644
68864
  }
68645
68865
  return reader;
68646
68866
  }
@@ -68895,13 +69115,13 @@ function useHidAmicoService() {
68895
69115
  }
68896
69116
 
68897
69117
  // src/controllers/hid-amico.controller.ts
68898
- var import_node_server_utils259 = require("@7365admin1/node-server-utils");
69118
+ var import_node_server_utils260 = require("@7365admin1/node-server-utils");
68899
69119
  function useHidAmicoController() {
68900
69120
  const service = useHidAmicoService();
68901
69121
  async function listReaders(req, res, next) {
68902
69122
  const { error, value } = schemaHidAmicoReaderListQuery.validate(req.query);
68903
69123
  if (error) {
68904
- next(new import_node_server_utils259.BadRequestError(error.message));
69124
+ next(new import_node_server_utils260.BadRequestError(error.message));
68905
69125
  return;
68906
69126
  }
68907
69127
  try {
@@ -68913,7 +69133,7 @@ function useHidAmicoController() {
68913
69133
  async function createReader(req, res, next) {
68914
69134
  const { error, value } = schemaHidAmicoReader.validate(req.body);
68915
69135
  if (error) {
68916
- next(new import_node_server_utils259.BadRequestError(error.message));
69136
+ next(new import_node_server_utils260.BadRequestError(error.message));
68917
69137
  return;
68918
69138
  }
68919
69139
  try {
@@ -68926,7 +69146,7 @@ function useHidAmicoController() {
68926
69146
  const params = schemaHidAmicoReaderIdParams.validate(req.params);
68927
69147
  const body = schemaUpdateHidAmicoReader.validate(req.body);
68928
69148
  if (params.error || body.error) {
68929
- next(new import_node_server_utils259.BadRequestError(params.error?.message || body.error?.message));
69149
+ next(new import_node_server_utils260.BadRequestError(params.error?.message || body.error?.message));
68930
69150
  return;
68931
69151
  }
68932
69152
  try {
@@ -68938,7 +69158,7 @@ function useHidAmicoController() {
68938
69158
  async function deleteReader(req, res, next) {
68939
69159
  const { error, value } = schemaHidAmicoReaderIdParams.validate(req.params);
68940
69160
  if (error) {
68941
- next(new import_node_server_utils259.BadRequestError(error.message));
69161
+ next(new import_node_server_utils260.BadRequestError(error.message));
68942
69162
  return;
68943
69163
  }
68944
69164
  try {
@@ -68950,7 +69170,7 @@ function useHidAmicoController() {
68950
69170
  async function testReader(req, res, next) {
68951
69171
  const { error, value } = schemaHidAmicoReaderIdParams.validate(req.params);
68952
69172
  if (error) {
68953
- next(new import_node_server_utils259.BadRequestError(error.message));
69173
+ next(new import_node_server_utils260.BadRequestError(error.message));
68954
69174
  return;
68955
69175
  }
68956
69176
  try {
@@ -68963,7 +69183,7 @@ function useHidAmicoController() {
68963
69183
  const params = schemaHidAmicoReaderIdParams.validate(req.params);
68964
69184
  const body = schemaHidAmicoSync.validate(req.body ?? {});
68965
69185
  if (params.error || body.error) {
68966
- next(new import_node_server_utils259.BadRequestError(params.error?.message || body.error?.message));
69186
+ next(new import_node_server_utils260.BadRequestError(params.error?.message || body.error?.message));
68967
69187
  return;
68968
69188
  }
68969
69189
  try {
@@ -68976,7 +69196,7 @@ function useHidAmicoController() {
68976
69196
  const params = schemaHidAmicoReaderIdParams.validate(req.params);
68977
69197
  const query = schemaHidAmicoLogQuery.validate(req.query);
68978
69198
  if (params.error || query.error) {
68979
- next(new import_node_server_utils259.BadRequestError(params.error?.message || query.error?.message));
69199
+ next(new import_node_server_utils260.BadRequestError(params.error?.message || query.error?.message));
68980
69200
  return;
68981
69201
  }
68982
69202
  try {
@@ -68989,7 +69209,7 @@ function useHidAmicoController() {
68989
69209
  const params = schemaHidAmicoReaderIdParams.validate(req.params);
68990
69210
  const query = schemaHidAmicoIdentityQuery.validate(req.query);
68991
69211
  if (params.error || query.error) {
68992
- next(new import_node_server_utils259.BadRequestError(params.error?.message || query.error?.message));
69212
+ next(new import_node_server_utils260.BadRequestError(params.error?.message || query.error?.message));
68993
69213
  return;
68994
69214
  }
68995
69215
  try {
@@ -69001,13 +69221,13 @@ function useHidAmicoController() {
69001
69221
  async function createIdentity(req, res, next) {
69002
69222
  const params = schemaHidAmicoReaderIdParams.validate(req.params);
69003
69223
  if (params.error) {
69004
- next(new import_node_server_utils259.BadRequestError(params.error.message));
69224
+ next(new import_node_server_utils260.BadRequestError(params.error.message));
69005
69225
  return;
69006
69226
  }
69007
69227
  try {
69008
69228
  const body = schemaCreateHidAmicoIdentity.validate(req.body);
69009
69229
  if (body.error) {
69010
- next(new import_node_server_utils259.BadRequestError(body.error.message));
69230
+ next(new import_node_server_utils260.BadRequestError(body.error.message));
69011
69231
  return;
69012
69232
  }
69013
69233
  res.status(201).json({ data: await service.createIdentity(params.value.readerId, body.value) });
@@ -69019,7 +69239,7 @@ function useHidAmicoController() {
69019
69239
  const params = schemaHidAmicoIdentityIdParams.validate(req.params);
69020
69240
  const body = schemaUpdateHidAmicoIdentity.validate(req.body ?? {});
69021
69241
  if (params.error || body.error) {
69022
- next(new import_node_server_utils259.BadRequestError(params.error?.message || body.error?.message));
69242
+ next(new import_node_server_utils260.BadRequestError(params.error?.message || body.error?.message));
69023
69243
  return;
69024
69244
  }
69025
69245
  try {
@@ -69031,7 +69251,7 @@ function useHidAmicoController() {
69031
69251
  async function deleteIdentity(req, res, next) {
69032
69252
  const { error, value } = schemaHidAmicoIdentityIdParams.validate(req.params);
69033
69253
  if (error) {
69034
- next(new import_node_server_utils259.BadRequestError(error.message));
69254
+ next(new import_node_server_utils260.BadRequestError(error.message));
69035
69255
  return;
69036
69256
  }
69037
69257
  try {
@@ -69043,7 +69263,7 @@ function useHidAmicoController() {
69043
69263
  async function receiveNotification(req, res, next) {
69044
69264
  const validation = schemaHidAmicoNotificationParams.validate(req.params);
69045
69265
  if (validation.error) {
69046
- next(new import_node_server_utils259.BadRequestError(validation.error.message));
69266
+ next(new import_node_server_utils260.BadRequestError(validation.error.message));
69047
69267
  return;
69048
69268
  }
69049
69269
  try {
@@ -69060,7 +69280,7 @@ function useHidAmicoController() {
69060
69280
  async function getDoorState(req, res, next) {
69061
69281
  const { error, value } = schemaHidAmicoReaderIdParams.validate(req.params);
69062
69282
  if (error) {
69063
- next(new import_node_server_utils259.BadRequestError(error.message));
69283
+ next(new import_node_server_utils260.BadRequestError(error.message));
69064
69284
  return;
69065
69285
  }
69066
69286
  try {
@@ -69072,7 +69292,7 @@ function useHidAmicoController() {
69072
69292
  async function getUserImage(req, res, next) {
69073
69293
  const { error, value } = schemaHidAmicoUserImageParams.validate(req.params);
69074
69294
  if (error) {
69075
- next(new import_node_server_utils259.BadRequestError(error.message));
69295
+ next(new import_node_server_utils260.BadRequestError(error.message));
69076
69296
  return;
69077
69297
  }
69078
69298
  try {
@@ -69086,7 +69306,7 @@ function useHidAmicoController() {
69086
69306
  const params = schemaHidAmicoReaderIdParams.validate(req.params);
69087
69307
  const body = schemaHidAmicoExecuteActions.validate(req.body ?? {});
69088
69308
  if (params.error || body.error) {
69089
- next(new import_node_server_utils259.BadRequestError(params.error?.message || body.error?.message));
69309
+ next(new import_node_server_utils260.BadRequestError(params.error?.message || body.error?.message));
69090
69310
  return;
69091
69311
  }
69092
69312
  try {
@@ -69099,7 +69319,7 @@ function useHidAmicoController() {
69099
69319
  const params = schemaHidAmicoReaderIdParams.validate(req.params);
69100
69320
  const body = schemaHidAmicoConfiguration.validate(req.body ?? {});
69101
69321
  if (params.error || body.error) {
69102
- next(new import_node_server_utils259.BadRequestError(params.error?.message || body.error?.message));
69322
+ next(new import_node_server_utils260.BadRequestError(params.error?.message || body.error?.message));
69103
69323
  return;
69104
69324
  }
69105
69325
  try {
@@ -69112,7 +69332,7 @@ function useHidAmicoController() {
69112
69332
  const params = schemaHidAmicoReaderIdParams.validate(req.params);
69113
69333
  const body = schemaHidAmicoSetConfiguration.validate(req.body ?? {});
69114
69334
  if (params.error || body.error) {
69115
- next(new import_node_server_utils259.BadRequestError(params.error?.message || body.error?.message));
69335
+ next(new import_node_server_utils260.BadRequestError(params.error?.message || body.error?.message));
69116
69336
  return;
69117
69337
  }
69118
69338
  try {
@@ -69125,20 +69345,20 @@ function useHidAmicoController() {
69125
69345
  const params = schemaHidAmicoReaderIdParams.validate(req.params);
69126
69346
  const body = schemaHidAmicoObjectOperation.validate(req.body ?? {});
69127
69347
  if (params.error || body.error) {
69128
- next(new import_node_server_utils259.BadRequestError(params.error?.message || body.error?.message));
69348
+ next(new import_node_server_utils260.BadRequestError(params.error?.message || body.error?.message));
69129
69349
  return;
69130
69350
  }
69131
69351
  const { operation, ...payload } = body.value;
69132
69352
  if (operation === "create" && !Array.isArray(payload.values)) {
69133
- next(new import_node_server_utils259.BadRequestError("values array is required for HID object create operations."));
69353
+ next(new import_node_server_utils260.BadRequestError("values array is required for HID object create operations."));
69134
69354
  return;
69135
69355
  }
69136
69356
  if (operation === "modify" && (!payload.values || Array.isArray(payload.values) || !payload.where)) {
69137
- next(new import_node_server_utils259.BadRequestError("values object and where are required for HID object modify operations."));
69357
+ next(new import_node_server_utils260.BadRequestError("values object and where are required for HID object modify operations."));
69138
69358
  return;
69139
69359
  }
69140
69360
  if (operation === "destroy" && !payload.where) {
69141
- next(new import_node_server_utils259.BadRequestError("where is required for HID object destroy operations."));
69361
+ next(new import_node_server_utils260.BadRequestError("where is required for HID object destroy operations."));
69142
69362
  return;
69143
69363
  }
69144
69364
  try {