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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ ---
2
+ "@7365admin1/core": minor
3
+ ---
4
+
5
+ Console audit: `inventory.item-archived` / `inventory.item-restored` actions (target `inventory-item`) for the Seven365 staff archive of old inventory items.
@@ -0,0 +1,5 @@
1
+ ---
2
+ "@7365admin1/core": patch
3
+ ---
4
+
5
+ Visitor list search is fast on large sites: block/level/unit and pass/key names are resolved to ids first instead of joining five collections onto every visitor row (staging 120k-row site: 20 s -> 0.2 s with the default one-month range, 84 s -> 0.7 s all-time). Same matches as before.
package/dist/index.d.ts CHANGED
@@ -823,7 +823,9 @@ declare enum ConsoleAuditAction {
823
823
  * vs a service provider's own store) that was saved wrong. One row per store
824
824
  * changed, recorded by API-core's inventory repair endpoint.
825
825
  */
826
- INVENTORY_STORE_TYPE_REPAIRED = "inventory.store-type-repaired"
826
+ INVENTORY_STORE_TYPE_REPAIRED = "inventory.store-type-repaired",
827
+ INVENTORY_ITEM_ARCHIVED = "inventory.item-archived",
828
+ INVENTORY_ITEM_RESTORED = "inventory.item-restored"
827
829
  }
828
830
  /**
829
831
  * The three actions that live in `organization.controller.ts`.
@@ -849,6 +851,7 @@ declare enum ConsoleAuditTarget {
849
851
  ROBOT = "robot",
850
852
  ROBOT_MAP = "robot-map",
851
853
  INVENTORY_STORE = "inventory-store",
854
+ INVENTORY_ITEM = "inventory-item",
852
855
  MEMBER = "member"
853
856
  }
854
857
  /** A stored value is a primitive or it is not stored. */
package/dist/index.js CHANGED
@@ -11549,6 +11549,8 @@ var ConsoleAuditAction = /* @__PURE__ */ ((ConsoleAuditAction3) => {
11549
11549
  ConsoleAuditAction3["ROBOT_LIFT_REQUESTED"] = "robot.lift-requested";
11550
11550
  ConsoleAuditAction3["ROBOT_LIFT_RELEASED"] = "robot.lift-released";
11551
11551
  ConsoleAuditAction3["INVENTORY_STORE_TYPE_REPAIRED"] = "inventory.store-type-repaired";
11552
+ ConsoleAuditAction3["INVENTORY_ITEM_ARCHIVED"] = "inventory.item-archived";
11553
+ ConsoleAuditAction3["INVENTORY_ITEM_RESTORED"] = "inventory.item-restored";
11552
11554
  return ConsoleAuditAction3;
11553
11555
  })(ConsoleAuditAction || {});
11554
11556
  var CLIENT_ACTIONS = [
@@ -11588,7 +11590,9 @@ var CONSOLE_AUDIT_LABELS = {
11588
11590
  ["robot.map-removed" /* ROBOT_MAP_REMOVED */]: "Building map removed",
11589
11591
  ["robot.lift-requested" /* ROBOT_LIFT_REQUESTED */]: "Robot asked for a lift",
11590
11592
  ["robot.lift-released" /* ROBOT_LIFT_RELEASED */]: "Robot lift request released",
11591
- ["inventory.store-type-repaired" /* INVENTORY_STORE_TYPE_REPAIRED */]: "Inventory store type corrected"
11593
+ ["inventory.store-type-repaired" /* INVENTORY_STORE_TYPE_REPAIRED */]: "Inventory store type corrected",
11594
+ ["inventory.item-archived" /* INVENTORY_ITEM_ARCHIVED */]: "Old inventory item archived",
11595
+ ["inventory.item-restored" /* INVENTORY_ITEM_RESTORED */]: "Archived inventory item restored"
11592
11596
  };
11593
11597
  var ConsoleAuditTarget = /* @__PURE__ */ ((ConsoleAuditTarget2) => {
11594
11598
  ConsoleAuditTarget2["ORGANIZATION"] = "organization";
@@ -11601,6 +11605,7 @@ var ConsoleAuditTarget = /* @__PURE__ */ ((ConsoleAuditTarget2) => {
11601
11605
  ConsoleAuditTarget2["ROBOT"] = "robot";
11602
11606
  ConsoleAuditTarget2["ROBOT_MAP"] = "robot-map";
11603
11607
  ConsoleAuditTarget2["INVENTORY_STORE"] = "inventory-store";
11608
+ ConsoleAuditTarget2["INVENTORY_ITEM"] = "inventory-item";
11604
11609
  ConsoleAuditTarget2["MEMBER"] = "member";
11605
11610
  return ConsoleAuditTarget2;
11606
11611
  })(ConsoleAuditTarget || {});
@@ -11650,6 +11655,8 @@ var ROBOT_MAP_AUDIT_FIELDS = [
11650
11655
  var ROBOT_LIFT_AUDIT_FIELDS = ["name", "liftName", "fromFloor", "toFloor"];
11651
11656
  var AUDIT_FIELDS = {
11652
11657
  ["inventory.store-type-repaired" /* INVENTORY_STORE_TYPE_REPAIRED */]: ["ownerType", "name"],
11658
+ ["inventory.item-archived" /* INVENTORY_ITEM_ARCHIVED */]: ["status", "name", "store", "reason"],
11659
+ ["inventory.item-restored" /* INVENTORY_ITEM_RESTORED */]: ["status", "name", "store", "reason"],
11653
11660
  ["robot.map-saved" /* ROBOT_MAP_SAVED */]: ROBOT_MAP_AUDIT_FIELDS,
11654
11661
  ["robot.map-updated" /* ROBOT_MAP_UPDATED */]: ROBOT_MAP_AUDIT_FIELDS,
11655
11662
  ["robot.map-removed" /* ROBOT_MAP_REMOVED */]: ROBOT_MAP_AUDIT_FIELDS,
@@ -16188,7 +16195,8 @@ function useVisitorTransactionRepo() {
16188
16195
  throw new import_node_server_utils48.InternalServerError("Unable to connect to server.");
16189
16196
  }
16190
16197
  const collection = db2.collection(visitors_namespace_collection);
16191
- function buildSearchStages(search) {
16198
+ const searchDb = db2;
16199
+ async function buildSearchStages(search, siteIds) {
16192
16200
  const text2 = normaliseVisitorSearch(search);
16193
16201
  if (!text2)
16194
16202
  return [];
@@ -16202,98 +16210,78 @@ function useVisitorTransactionRepo() {
16202
16210
  $regex: `(^|\\s)${escape(value)}$`,
16203
16211
  $options: "i"
16204
16212
  });
16205
- const nameLookup = (from, localField, as) => ({
16206
- $lookup: {
16207
- from,
16208
- localField,
16209
- foreignField: "_id",
16210
- pipeline: [{ $project: { _id: 0, name: 1 } }],
16211
- as
16212
- }
16213
- });
16214
- const keyNameLookup = (localField, as) => ({
16215
- $lookup: {
16216
- from: "keys",
16217
- localField,
16218
- foreignField: "_id",
16219
- pipeline: [
16220
- {
16221
- $lookup: {
16222
- from: "qr-code-templates",
16223
- localField: "template",
16224
- foreignField: "_id",
16225
- pipeline: [{ $project: { prefixPass: 1 } }],
16226
- as: "template"
16227
- }
16228
- },
16229
- {
16230
- $project: {
16231
- _id: 0,
16232
- prefixAndName: {
16233
- $concat: [
16234
- { $ifNull: [{ $arrayElemAt: ["$template.prefixPass", 0] }, ""] },
16235
- { $ifNull: ["$name", ""] }
16236
- ]
16237
- }
16238
- }
16239
- }
16240
- ],
16241
- as
16242
- }
16243
- });
16244
- const locationLookups = [
16245
- nameLookup("buildings", "block", "_searchBlock"),
16246
- nameLookup("building-levels", "level", "_searchLevel"),
16247
- nameLookup("building-units", "unit", "_searchUnit")
16248
- ];
16249
- const cleanup = {
16250
- $project: {
16251
- _searchBlock: 0,
16252
- _searchLevel: 0,
16253
- _searchUnit: 0,
16254
- _searchPass: 0,
16255
- _searchKeys: 0
16256
- }
16257
- };
16258
- const unitMatch = (value, match = like) => ({
16259
- $or: [{ "_searchUnit.name": match(value) }, { unitName: match(value) }]
16260
- });
16213
+ const onSites = siteIds ? { site: { $in: siteIds } } : {};
16214
+ const levelOnSites = siteIds ? { $or: [{ site: { $in: siteIds } }, { site: null }, { site: "" }] } : {};
16215
+ const idsOf = (from, filter) => searchDb.collection(from).distinct("_id", filter);
16216
+ const inIds = (field, ids2) => ids2.length ? [{ [field]: { $in: ids2 } }] : [];
16261
16217
  const parts = text2.split("/").map((part) => part.trim());
16262
16218
  if (parts.length >= 2 && parts.length <= 3 && parts.every(Boolean)) {
16263
16219
  const [block, level2, unit] = parts;
16220
+ const [blockIds2, levelIds2, unitIds2] = await Promise.all([
16221
+ idsOf("buildings", { ...onSites, name: blockName(block) }),
16222
+ idsOf("building-levels", { ...levelOnSites, name: exact(level2) }),
16223
+ unit ? idsOf("building-units", { ...onSites, name: exact(unit) }) : []
16224
+ ]);
16264
16225
  return [
16265
- ...locationLookups,
16266
16226
  {
16267
16227
  $match: {
16268
- "_searchBlock.name": blockName(block),
16269
- "_searchLevel.name": exact(level2),
16270
- ...unit && unitMatch(unit, exact)
16228
+ block: { $in: blockIds2 },
16229
+ level: { $in: levelIds2 },
16230
+ ...unit && {
16231
+ $or: [...inIds("unit", unitIds2), { unitName: exact(unit) }]
16232
+ }
16271
16233
  }
16272
- },
16273
- cleanup
16234
+ }
16274
16235
  ];
16275
16236
  }
16276
16237
  const compact = text2.replace(/\s+/g, "");
16238
+ const [blockIds, levelIds, unitIds, keyIds] = await Promise.all([
16239
+ idsOf("buildings", { ...onSites, name: like(text2) }),
16240
+ idsOf("building-levels", { ...levelOnSites, name: like(text2) }),
16241
+ idsOf("building-units", { ...onSites, name: like(text2) }),
16242
+ compact ? matchingKeyIds(compact) : []
16243
+ ]);
16277
16244
  return [
16278
- ...locationLookups,
16279
- keyNameLookup("visitorPass.keyId", "_searchPass"),
16280
- keyNameLookup("passKeys.keyId", "_searchKeys"),
16281
16245
  {
16282
16246
  $match: {
16283
16247
  $or: [
16284
16248
  // name, company, purpose, NRIC, types, members; plates ignoring
16285
16249
  // spaces/hyphens/case; phones by digits - visitor-search.util.
16286
16250
  ...visitorRowSearchOr(text2),
16287
- { "_searchBlock.name": like(text2) },
16288
- { "_searchLevel.name": like(text2) },
16289
- unitMatch(text2),
16290
- { "_searchPass.prefixAndName": like(compact) },
16291
- { "_searchKeys.prefixAndName": like(compact) }
16251
+ ...inIds("block", blockIds),
16252
+ ...inIds("level", levelIds),
16253
+ ...inIds("unit", unitIds),
16254
+ { unitName: like(text2) },
16255
+ ...inIds("visitorPass.keyId", keyIds),
16256
+ ...inIds("passKeys.keyId", keyIds)
16292
16257
  ]
16293
16258
  }
16294
- },
16295
- cleanup
16259
+ }
16260
+ ];
16261
+ }
16262
+ async function matchingKeyIds(compact) {
16263
+ const t = compact.toLowerCase();
16264
+ const templates = await searchDb.collection("qr-code-templates").find({ prefixPass: { $nin: [null, ""] } }, { projection: { prefixPass: 1 } }).toArray();
16265
+ const or = [
16266
+ { name: { $regex: escapeRegex3(compact), $options: "i" } }
16296
16267
  ];
16268
+ for (const tpl of templates) {
16269
+ const prefix = String(tpl.prefixPass).toLowerCase();
16270
+ const template = { $in: [tpl._id, String(tpl._id)] };
16271
+ if (prefix.includes(t)) {
16272
+ or.push({ template });
16273
+ continue;
16274
+ }
16275
+ for (let i = 1; i < t.length; i++) {
16276
+ if (prefix.endsWith(t.slice(0, i))) {
16277
+ or.push({
16278
+ template,
16279
+ name: { $regex: `^${escapeRegex3(compact.slice(i))}`, $options: "i" }
16280
+ });
16281
+ }
16282
+ }
16283
+ }
16284
+ return await searchDb.collection("keys").distinct("_id", { $or: or });
16297
16285
  }
16298
16286
  async function createTextIndex() {
16299
16287
  try {
@@ -16397,10 +16385,11 @@ function useVisitorTransactionRepo() {
16397
16385
  ...passOrKey?.type == "key" && { passKeys: { $elemMatch: { keyId: new import_mongodb46.ObjectId(passOrKey.keyId) } } }
16398
16386
  };
16399
16387
  const finalSort = Object.keys(sort).length > 0 ? sort : { latestDate: -1 };
16388
+ const searchSiteIds = query2.site instanceof import_mongodb46.ObjectId ? [query2.site] : query2.site?.$in ?? null;
16400
16389
  try {
16401
16390
  const basePipeline = [
16402
16391
  { $match: query2 },
16403
- ...buildSearchStages(search),
16392
+ ...await buildSearchStages(search, searchSiteIds),
16404
16393
  {
16405
16394
  $addFields: {
16406
16395
  latestDate: { $max: ["$createdAt", "$updatedAt"] }