@7365admin1/core 3.129.0 → 3.129.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @iservice365/core
2
2
 
3
+ ## 3.129.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 6323620: 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.
8
+
3
9
  ## 3.129.0
4
10
 
5
11
  ### Minor Changes
package/dist/index.js CHANGED
@@ -16065,7 +16065,8 @@ function useVisitorTransactionRepo() {
16065
16065
  throw new import_node_server_utils48.InternalServerError("Unable to connect to server.");
16066
16066
  }
16067
16067
  const collection = db2.collection(visitors_namespace_collection);
16068
- function buildSearchStages(search) {
16068
+ const searchDb = db2;
16069
+ async function buildSearchStages(search, siteIds) {
16069
16070
  const text2 = normaliseVisitorSearch(search);
16070
16071
  if (!text2)
16071
16072
  return [];
@@ -16079,98 +16080,78 @@ function useVisitorTransactionRepo() {
16079
16080
  $regex: `(^|\\s)${escape(value)}$`,
16080
16081
  $options: "i"
16081
16082
  });
16082
- const nameLookup = (from, localField, as) => ({
16083
- $lookup: {
16084
- from,
16085
- localField,
16086
- foreignField: "_id",
16087
- pipeline: [{ $project: { _id: 0, name: 1 } }],
16088
- as
16089
- }
16090
- });
16091
- const keyNameLookup = (localField, as) => ({
16092
- $lookup: {
16093
- from: "keys",
16094
- localField,
16095
- foreignField: "_id",
16096
- pipeline: [
16097
- {
16098
- $lookup: {
16099
- from: "qr-code-templates",
16100
- localField: "template",
16101
- foreignField: "_id",
16102
- pipeline: [{ $project: { prefixPass: 1 } }],
16103
- as: "template"
16104
- }
16105
- },
16106
- {
16107
- $project: {
16108
- _id: 0,
16109
- prefixAndName: {
16110
- $concat: [
16111
- { $ifNull: [{ $arrayElemAt: ["$template.prefixPass", 0] }, ""] },
16112
- { $ifNull: ["$name", ""] }
16113
- ]
16114
- }
16115
- }
16116
- }
16117
- ],
16118
- as
16119
- }
16120
- });
16121
- const locationLookups = [
16122
- nameLookup("buildings", "block", "_searchBlock"),
16123
- nameLookup("building-levels", "level", "_searchLevel"),
16124
- nameLookup("building-units", "unit", "_searchUnit")
16125
- ];
16126
- const cleanup = {
16127
- $project: {
16128
- _searchBlock: 0,
16129
- _searchLevel: 0,
16130
- _searchUnit: 0,
16131
- _searchPass: 0,
16132
- _searchKeys: 0
16133
- }
16134
- };
16135
- const unitMatch = (value, match = like) => ({
16136
- $or: [{ "_searchUnit.name": match(value) }, { unitName: match(value) }]
16137
- });
16083
+ const onSites = siteIds ? { site: { $in: siteIds } } : {};
16084
+ const levelOnSites = siteIds ? { $or: [{ site: { $in: siteIds } }, { site: null }, { site: "" }] } : {};
16085
+ const idsOf = (from, filter) => searchDb.collection(from).distinct("_id", filter);
16086
+ const inIds = (field, ids2) => ids2.length ? [{ [field]: { $in: ids2 } }] : [];
16138
16087
  const parts = text2.split("/").map((part) => part.trim());
16139
16088
  if (parts.length >= 2 && parts.length <= 3 && parts.every(Boolean)) {
16140
16089
  const [block, level2, unit] = parts;
16090
+ const [blockIds2, levelIds2, unitIds2] = await Promise.all([
16091
+ idsOf("buildings", { ...onSites, name: blockName(block) }),
16092
+ idsOf("building-levels", { ...levelOnSites, name: exact(level2) }),
16093
+ unit ? idsOf("building-units", { ...onSites, name: exact(unit) }) : []
16094
+ ]);
16141
16095
  return [
16142
- ...locationLookups,
16143
16096
  {
16144
16097
  $match: {
16145
- "_searchBlock.name": blockName(block),
16146
- "_searchLevel.name": exact(level2),
16147
- ...unit && unitMatch(unit, exact)
16098
+ block: { $in: blockIds2 },
16099
+ level: { $in: levelIds2 },
16100
+ ...unit && {
16101
+ $or: [...inIds("unit", unitIds2), { unitName: exact(unit) }]
16102
+ }
16148
16103
  }
16149
- },
16150
- cleanup
16104
+ }
16151
16105
  ];
16152
16106
  }
16153
16107
  const compact = text2.replace(/\s+/g, "");
16108
+ const [blockIds, levelIds, unitIds, keyIds] = await Promise.all([
16109
+ idsOf("buildings", { ...onSites, name: like(text2) }),
16110
+ idsOf("building-levels", { ...levelOnSites, name: like(text2) }),
16111
+ idsOf("building-units", { ...onSites, name: like(text2) }),
16112
+ compact ? matchingKeyIds(compact) : []
16113
+ ]);
16154
16114
  return [
16155
- ...locationLookups,
16156
- keyNameLookup("visitorPass.keyId", "_searchPass"),
16157
- keyNameLookup("passKeys.keyId", "_searchKeys"),
16158
16115
  {
16159
16116
  $match: {
16160
16117
  $or: [
16161
16118
  // name, company, purpose, NRIC, types, members; plates ignoring
16162
16119
  // spaces/hyphens/case; phones by digits - visitor-search.util.
16163
16120
  ...visitorRowSearchOr(text2),
16164
- { "_searchBlock.name": like(text2) },
16165
- { "_searchLevel.name": like(text2) },
16166
- unitMatch(text2),
16167
- { "_searchPass.prefixAndName": like(compact) },
16168
- { "_searchKeys.prefixAndName": like(compact) }
16121
+ ...inIds("block", blockIds),
16122
+ ...inIds("level", levelIds),
16123
+ ...inIds("unit", unitIds),
16124
+ { unitName: like(text2) },
16125
+ ...inIds("visitorPass.keyId", keyIds),
16126
+ ...inIds("passKeys.keyId", keyIds)
16169
16127
  ]
16170
16128
  }
16171
- },
16172
- cleanup
16129
+ }
16130
+ ];
16131
+ }
16132
+ async function matchingKeyIds(compact) {
16133
+ const t = compact.toLowerCase();
16134
+ const templates = await searchDb.collection("qr-code-templates").find({ prefixPass: { $nin: [null, ""] } }, { projection: { prefixPass: 1 } }).toArray();
16135
+ const or = [
16136
+ { name: { $regex: escapeRegex2(compact), $options: "i" } }
16173
16137
  ];
16138
+ for (const tpl of templates) {
16139
+ const prefix = String(tpl.prefixPass).toLowerCase();
16140
+ const template = { $in: [tpl._id, String(tpl._id)] };
16141
+ if (prefix.includes(t)) {
16142
+ or.push({ template });
16143
+ continue;
16144
+ }
16145
+ for (let i = 1; i < t.length; i++) {
16146
+ if (prefix.endsWith(t.slice(0, i))) {
16147
+ or.push({
16148
+ template,
16149
+ name: { $regex: `^${escapeRegex2(compact.slice(i))}`, $options: "i" }
16150
+ });
16151
+ }
16152
+ }
16153
+ }
16154
+ return await searchDb.collection("keys").distinct("_id", { $or: or });
16174
16155
  }
16175
16156
  async function createTextIndex() {
16176
16157
  try {
@@ -16274,10 +16255,11 @@ function useVisitorTransactionRepo() {
16274
16255
  ...passOrKey?.type == "key" && { passKeys: { $elemMatch: { keyId: new import_mongodb46.ObjectId(passOrKey.keyId) } } }
16275
16256
  };
16276
16257
  const finalSort = Object.keys(sort).length > 0 ? sort : { latestDate: -1 };
16258
+ const searchSiteIds = query2.site instanceof import_mongodb46.ObjectId ? [query2.site] : query2.site?.$in ?? null;
16277
16259
  try {
16278
16260
  const basePipeline = [
16279
16261
  { $match: query2 },
16280
- ...buildSearchStages(search),
16262
+ ...await buildSearchStages(search, searchSiteIds),
16281
16263
  {
16282
16264
  $addFields: {
16283
16265
  latestDate: { $max: ["$createdAt", "$updatedAt"] }