@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/dist/index.mjs CHANGED
@@ -15078,7 +15078,8 @@ function useVisitorTransactionRepo() {
15078
15078
  throw new InternalServerError18("Unable to connect to server.");
15079
15079
  }
15080
15080
  const collection = db2.collection(visitors_namespace_collection);
15081
- function buildSearchStages(search) {
15081
+ const searchDb = db2;
15082
+ async function buildSearchStages(search, siteIds) {
15082
15083
  const text2 = normaliseVisitorSearch(search);
15083
15084
  if (!text2)
15084
15085
  return [];
@@ -15092,98 +15093,78 @@ function useVisitorTransactionRepo() {
15092
15093
  $regex: `(^|\\s)${escape(value)}$`,
15093
15094
  $options: "i"
15094
15095
  });
15095
- const nameLookup = (from, localField, as) => ({
15096
- $lookup: {
15097
- from,
15098
- localField,
15099
- foreignField: "_id",
15100
- pipeline: [{ $project: { _id: 0, name: 1 } }],
15101
- as
15102
- }
15103
- });
15104
- const keyNameLookup = (localField, as) => ({
15105
- $lookup: {
15106
- from: "keys",
15107
- localField,
15108
- foreignField: "_id",
15109
- pipeline: [
15110
- {
15111
- $lookup: {
15112
- from: "qr-code-templates",
15113
- localField: "template",
15114
- foreignField: "_id",
15115
- pipeline: [{ $project: { prefixPass: 1 } }],
15116
- as: "template"
15117
- }
15118
- },
15119
- {
15120
- $project: {
15121
- _id: 0,
15122
- prefixAndName: {
15123
- $concat: [
15124
- { $ifNull: [{ $arrayElemAt: ["$template.prefixPass", 0] }, ""] },
15125
- { $ifNull: ["$name", ""] }
15126
- ]
15127
- }
15128
- }
15129
- }
15130
- ],
15131
- as
15132
- }
15133
- });
15134
- const locationLookups = [
15135
- nameLookup("buildings", "block", "_searchBlock"),
15136
- nameLookup("building-levels", "level", "_searchLevel"),
15137
- nameLookup("building-units", "unit", "_searchUnit")
15138
- ];
15139
- const cleanup = {
15140
- $project: {
15141
- _searchBlock: 0,
15142
- _searchLevel: 0,
15143
- _searchUnit: 0,
15144
- _searchPass: 0,
15145
- _searchKeys: 0
15146
- }
15147
- };
15148
- const unitMatch = (value, match = like) => ({
15149
- $or: [{ "_searchUnit.name": match(value) }, { unitName: match(value) }]
15150
- });
15096
+ const onSites = siteIds ? { site: { $in: siteIds } } : {};
15097
+ const levelOnSites = siteIds ? { $or: [{ site: { $in: siteIds } }, { site: null }, { site: "" }] } : {};
15098
+ const idsOf = (from, filter) => searchDb.collection(from).distinct("_id", filter);
15099
+ const inIds = (field, ids2) => ids2.length ? [{ [field]: { $in: ids2 } }] : [];
15151
15100
  const parts = text2.split("/").map((part) => part.trim());
15152
15101
  if (parts.length >= 2 && parts.length <= 3 && parts.every(Boolean)) {
15153
15102
  const [block, level2, unit] = parts;
15103
+ const [blockIds2, levelIds2, unitIds2] = await Promise.all([
15104
+ idsOf("buildings", { ...onSites, name: blockName(block) }),
15105
+ idsOf("building-levels", { ...levelOnSites, name: exact(level2) }),
15106
+ unit ? idsOf("building-units", { ...onSites, name: exact(unit) }) : []
15107
+ ]);
15154
15108
  return [
15155
- ...locationLookups,
15156
15109
  {
15157
15110
  $match: {
15158
- "_searchBlock.name": blockName(block),
15159
- "_searchLevel.name": exact(level2),
15160
- ...unit && unitMatch(unit, exact)
15111
+ block: { $in: blockIds2 },
15112
+ level: { $in: levelIds2 },
15113
+ ...unit && {
15114
+ $or: [...inIds("unit", unitIds2), { unitName: exact(unit) }]
15115
+ }
15161
15116
  }
15162
- },
15163
- cleanup
15117
+ }
15164
15118
  ];
15165
15119
  }
15166
15120
  const compact = text2.replace(/\s+/g, "");
15121
+ const [blockIds, levelIds, unitIds, keyIds] = await Promise.all([
15122
+ idsOf("buildings", { ...onSites, name: like(text2) }),
15123
+ idsOf("building-levels", { ...levelOnSites, name: like(text2) }),
15124
+ idsOf("building-units", { ...onSites, name: like(text2) }),
15125
+ compact ? matchingKeyIds(compact) : []
15126
+ ]);
15167
15127
  return [
15168
- ...locationLookups,
15169
- keyNameLookup("visitorPass.keyId", "_searchPass"),
15170
- keyNameLookup("passKeys.keyId", "_searchKeys"),
15171
15128
  {
15172
15129
  $match: {
15173
15130
  $or: [
15174
15131
  // name, company, purpose, NRIC, types, members; plates ignoring
15175
15132
  // spaces/hyphens/case; phones by digits - visitor-search.util.
15176
15133
  ...visitorRowSearchOr(text2),
15177
- { "_searchBlock.name": like(text2) },
15178
- { "_searchLevel.name": like(text2) },
15179
- unitMatch(text2),
15180
- { "_searchPass.prefixAndName": like(compact) },
15181
- { "_searchKeys.prefixAndName": like(compact) }
15134
+ ...inIds("block", blockIds),
15135
+ ...inIds("level", levelIds),
15136
+ ...inIds("unit", unitIds),
15137
+ { unitName: like(text2) },
15138
+ ...inIds("visitorPass.keyId", keyIds),
15139
+ ...inIds("passKeys.keyId", keyIds)
15182
15140
  ]
15183
15141
  }
15184
- },
15185
- cleanup
15142
+ }
15143
+ ];
15144
+ }
15145
+ async function matchingKeyIds(compact) {
15146
+ const t = compact.toLowerCase();
15147
+ const templates = await searchDb.collection("qr-code-templates").find({ prefixPass: { $nin: [null, ""] } }, { projection: { prefixPass: 1 } }).toArray();
15148
+ const or = [
15149
+ { name: { $regex: escapeRegex2(compact), $options: "i" } }
15186
15150
  ];
15151
+ for (const tpl of templates) {
15152
+ const prefix = String(tpl.prefixPass).toLowerCase();
15153
+ const template = { $in: [tpl._id, String(tpl._id)] };
15154
+ if (prefix.includes(t)) {
15155
+ or.push({ template });
15156
+ continue;
15157
+ }
15158
+ for (let i = 1; i < t.length; i++) {
15159
+ if (prefix.endsWith(t.slice(0, i))) {
15160
+ or.push({
15161
+ template,
15162
+ name: { $regex: `^${escapeRegex2(compact.slice(i))}`, $options: "i" }
15163
+ });
15164
+ }
15165
+ }
15166
+ }
15167
+ return await searchDb.collection("keys").distinct("_id", { $or: or });
15187
15168
  }
15188
15169
  async function createTextIndex() {
15189
15170
  try {
@@ -15287,10 +15268,11 @@ function useVisitorTransactionRepo() {
15287
15268
  ...passOrKey?.type == "key" && { passKeys: { $elemMatch: { keyId: new ObjectId46(passOrKey.keyId) } } }
15288
15269
  };
15289
15270
  const finalSort = Object.keys(sort).length > 0 ? sort : { latestDate: -1 };
15271
+ const searchSiteIds = query2.site instanceof ObjectId46 ? [query2.site] : query2.site?.$in ?? null;
15290
15272
  try {
15291
15273
  const basePipeline = [
15292
15274
  { $match: query2 },
15293
- ...buildSearchStages(search),
15275
+ ...await buildSearchStages(search, searchSiteIds),
15294
15276
  {
15295
15277
  $addFields: {
15296
15278
  latestDate: { $max: ["$createdAt", "$updatedAt"] }