@7365admin1/core 3.125.1-staging.422 → 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": 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.js CHANGED
@@ -16195,7 +16195,8 @@ function useVisitorTransactionRepo() {
16195
16195
  throw new import_node_server_utils48.InternalServerError("Unable to connect to server.");
16196
16196
  }
16197
16197
  const collection = db2.collection(visitors_namespace_collection);
16198
- function buildSearchStages(search) {
16198
+ const searchDb = db2;
16199
+ async function buildSearchStages(search, siteIds) {
16199
16200
  const text2 = normaliseVisitorSearch(search);
16200
16201
  if (!text2)
16201
16202
  return [];
@@ -16209,98 +16210,78 @@ function useVisitorTransactionRepo() {
16209
16210
  $regex: `(^|\\s)${escape(value)}$`,
16210
16211
  $options: "i"
16211
16212
  });
16212
- const nameLookup = (from, localField, as) => ({
16213
- $lookup: {
16214
- from,
16215
- localField,
16216
- foreignField: "_id",
16217
- pipeline: [{ $project: { _id: 0, name: 1 } }],
16218
- as
16219
- }
16220
- });
16221
- const keyNameLookup = (localField, as) => ({
16222
- $lookup: {
16223
- from: "keys",
16224
- localField,
16225
- foreignField: "_id",
16226
- pipeline: [
16227
- {
16228
- $lookup: {
16229
- from: "qr-code-templates",
16230
- localField: "template",
16231
- foreignField: "_id",
16232
- pipeline: [{ $project: { prefixPass: 1 } }],
16233
- as: "template"
16234
- }
16235
- },
16236
- {
16237
- $project: {
16238
- _id: 0,
16239
- prefixAndName: {
16240
- $concat: [
16241
- { $ifNull: [{ $arrayElemAt: ["$template.prefixPass", 0] }, ""] },
16242
- { $ifNull: ["$name", ""] }
16243
- ]
16244
- }
16245
- }
16246
- }
16247
- ],
16248
- as
16249
- }
16250
- });
16251
- const locationLookups = [
16252
- nameLookup("buildings", "block", "_searchBlock"),
16253
- nameLookup("building-levels", "level", "_searchLevel"),
16254
- nameLookup("building-units", "unit", "_searchUnit")
16255
- ];
16256
- const cleanup = {
16257
- $project: {
16258
- _searchBlock: 0,
16259
- _searchLevel: 0,
16260
- _searchUnit: 0,
16261
- _searchPass: 0,
16262
- _searchKeys: 0
16263
- }
16264
- };
16265
- const unitMatch = (value, match = like) => ({
16266
- $or: [{ "_searchUnit.name": match(value) }, { unitName: match(value) }]
16267
- });
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 } }] : [];
16268
16217
  const parts = text2.split("/").map((part) => part.trim());
16269
16218
  if (parts.length >= 2 && parts.length <= 3 && parts.every(Boolean)) {
16270
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
+ ]);
16271
16225
  return [
16272
- ...locationLookups,
16273
16226
  {
16274
16227
  $match: {
16275
- "_searchBlock.name": blockName(block),
16276
- "_searchLevel.name": exact(level2),
16277
- ...unit && unitMatch(unit, exact)
16228
+ block: { $in: blockIds2 },
16229
+ level: { $in: levelIds2 },
16230
+ ...unit && {
16231
+ $or: [...inIds("unit", unitIds2), { unitName: exact(unit) }]
16232
+ }
16278
16233
  }
16279
- },
16280
- cleanup
16234
+ }
16281
16235
  ];
16282
16236
  }
16283
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
+ ]);
16284
16244
  return [
16285
- ...locationLookups,
16286
- keyNameLookup("visitorPass.keyId", "_searchPass"),
16287
- keyNameLookup("passKeys.keyId", "_searchKeys"),
16288
16245
  {
16289
16246
  $match: {
16290
16247
  $or: [
16291
16248
  // name, company, purpose, NRIC, types, members; plates ignoring
16292
16249
  // spaces/hyphens/case; phones by digits - visitor-search.util.
16293
16250
  ...visitorRowSearchOr(text2),
16294
- { "_searchBlock.name": like(text2) },
16295
- { "_searchLevel.name": like(text2) },
16296
- unitMatch(text2),
16297
- { "_searchPass.prefixAndName": like(compact) },
16298
- { "_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)
16299
16257
  ]
16300
16258
  }
16301
- },
16302
- 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" } }
16303
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 });
16304
16285
  }
16305
16286
  async function createTextIndex() {
16306
16287
  try {
@@ -16404,10 +16385,11 @@ function useVisitorTransactionRepo() {
16404
16385
  ...passOrKey?.type == "key" && { passKeys: { $elemMatch: { keyId: new import_mongodb46.ObjectId(passOrKey.keyId) } } }
16405
16386
  };
16406
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;
16407
16389
  try {
16408
16390
  const basePipeline = [
16409
16391
  { $match: query2 },
16410
- ...buildSearchStages(search),
16392
+ ...await buildSearchStages(search, searchSiteIds),
16411
16393
  {
16412
16394
  $addFields: {
16413
16395
  latestDate: { $max: ["$createdAt", "$updatedAt"] }