@7365admin1/core 3.93.0 → 3.94.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.mjs CHANGED
@@ -17507,13 +17507,44 @@ function vehicleViewOf(value) {
17507
17507
  return "residents" /* RESIDENTS */;
17508
17508
  }
17509
17509
  var VEHICLE_HISTORY_LIMIT = 20;
17510
+ function vehicleHistoryValue(field, value) {
17511
+ if (value === void 0 || value === null)
17512
+ return null;
17513
+ if (value instanceof Date) {
17514
+ return Number.isNaN(value.getTime()) ? null : value.toISOString();
17515
+ }
17516
+ if (value instanceof ObjectId46)
17517
+ return value.toHexString();
17518
+ if (typeof value === "number" || typeof value === "boolean")
17519
+ return value;
17520
+ const text2 = String(value).trim();
17521
+ if (!text2)
17522
+ return null;
17523
+ if (field === "nric")
17524
+ return text2.length > 4 ? `*****${text2.slice(-4)}` : "****";
17525
+ return text2.length > 300 ? `${text2.slice(0, 300)}...` : text2;
17526
+ }
17510
17527
  function vehicleHistoryEntry(action, user, fields) {
17511
17528
  const id = String(user ?? "");
17529
+ const changes = [];
17530
+ const seen2 = /* @__PURE__ */ new Set();
17531
+ for (const item of fields) {
17532
+ if (!item || typeof item === "string" || !item.field || seen2.has(item.field))
17533
+ continue;
17534
+ seen2.add(item.field);
17535
+ changes.push({
17536
+ field: item.field,
17537
+ from: vehicleHistoryValue(item.field, item.from),
17538
+ to: vehicleHistoryValue(item.field, item.to)
17539
+ });
17540
+ }
17541
+ const names = fields.map((item) => typeof item === "string" ? item : item?.field).filter((name) => !!name);
17512
17542
  return {
17513
17543
  action,
17514
17544
  updatedBy: ObjectId46.isValid(id) ? new ObjectId46(id) : null,
17515
17545
  updatedAt: /* @__PURE__ */ new Date(),
17516
- fields: Array.from(new Set(fields.filter(Boolean)))
17546
+ fields: Array.from(new Set(names)),
17547
+ ...changes.length ? { changes } : {}
17517
17548
  };
17518
17549
  }
17519
17550
  var VehicleOrder = /* @__PURE__ */ ((VehicleOrder2) => {
@@ -18289,11 +18320,51 @@ function useVehicleRepo() {
18289
18320
  for (const user of users)
18290
18321
  names.set(String(user._id), String(user.name ?? ""));
18291
18322
  }
18323
+ const PLACE_COLLECTIONS = {
18324
+ block: "buildings",
18325
+ level: "building-levels",
18326
+ unit: "building-units"
18327
+ };
18328
+ const placeIds = { block: /* @__PURE__ */ new Set(), level: /* @__PURE__ */ new Set(), unit: /* @__PURE__ */ new Set() };
18329
+ for (const entry of history) {
18330
+ for (const change of entry?.changes ?? []) {
18331
+ const bucket = placeIds[change?.field];
18332
+ if (!bucket)
18333
+ continue;
18334
+ for (const v of [change.from, change.to]) {
18335
+ if (typeof v === "string" && ObjectId47.isValid(v))
18336
+ bucket.add(v);
18337
+ }
18338
+ }
18339
+ }
18340
+ const placeNames = /* @__PURE__ */ new Map();
18341
+ if (db2) {
18342
+ for (const [field, set] of Object.entries(placeIds)) {
18343
+ if (!set.size)
18344
+ continue;
18345
+ const docs = await db2.collection(PLACE_COLLECTIONS[field]).find(
18346
+ { _id: { $in: Array.from(set).map((id) => new ObjectId47(id)) } },
18347
+ { projection: { name: 1 } }
18348
+ ).toArray();
18349
+ for (const doc of docs)
18350
+ placeNames.set(`${field}:${String(doc._id)}`, String(doc.name ?? ""));
18351
+ }
18352
+ }
18353
+ const placeName = (field, v) => typeof v === "string" ? placeNames.get(`${field}:${v}`) : void 0;
18292
18354
  return {
18293
18355
  ...result,
18294
18356
  history: history.map((entry) => ({
18295
18357
  ...entry,
18296
- updatedByName: entry?.updatedBy ? names.get(String(entry.updatedBy)) ?? "" : ""
18358
+ updatedByName: entry?.updatedBy ? names.get(String(entry.updatedBy)) ?? "" : "",
18359
+ ...Array.isArray(entry?.changes) && {
18360
+ changes: entry.changes.map(
18361
+ (change) => PLACE_COLLECTIONS[change.field] ? {
18362
+ ...change,
18363
+ fromName: placeName(change.field, change.from),
18364
+ toName: placeName(change.field, change.to)
18365
+ } : change
18366
+ )
18367
+ }
18297
18368
  }))
18298
18369
  };
18299
18370
  } catch (error) {
@@ -34163,7 +34234,11 @@ function useVehicleService() {
34163
34234
  } = useVehicleRepo();
34164
34235
  function changedFields(record, next) {
34165
34236
  const asText = (v) => v instanceof Date ? String(v.getTime()) : String(v ?? "");
34166
- return Object.entries(next).filter(([key, value]) => asText(value) !== asText(record?.[key])).map(([key]) => key);
34237
+ return Object.entries(next).filter(([key, value]) => asText(value) !== asText(record?.[key])).map(([key, value]) => ({
34238
+ field: key,
34239
+ from: record?.[key] ?? null,
34240
+ to: value ?? null
34241
+ }));
34167
34242
  }
34168
34243
  function anprCameraSummary(cameras, list) {
34169
34244
  return cameras.map((camera) => ({
@@ -34912,6 +34987,7 @@ function useVehicleService() {
34912
34987
  vehicleHistoryEntry(
34913
34988
  "created",
34914
34989
  user,
34990
+ // Created: each value it was saved with, from nothing.
34915
34991
  Object.entries({
34916
34992
  plateNumber: vehicleValue.plateNumber,
34917
34993
  name: vehicleValue.name,
@@ -34927,7 +35003,7 @@ function useVehicleService() {
34927
35003
  start: vehicleValue.start,
34928
35004
  end: vehicleValue.end,
34929
35005
  remarks: vehicleValue.remarks
34930
- }).filter(([, v]) => v !== void 0 && v !== null && String(v) !== "").map(([k]) => k)
35006
+ }).filter(([, v]) => v !== void 0 && v !== null && String(v) !== "").map(([k, v]) => ({ field: k, from: null, to: v }))
34931
35007
  )
34932
35008
  ];
34933
35009
  await _add(vehicleValue, session);
@@ -35011,7 +35087,9 @@ function useVehicleService() {
35011
35087
  session
35012
35088
  );
35013
35089
  await session.commitTransaction();
35014
- await recordHistory(_id, "deleted", user, ["status", "deletedAt"]);
35090
+ await recordHistory(_id, "deleted", user, [
35091
+ { field: "status", from: vehicle?.status ?? null, to: "deleted" /* DELETED */ }
35092
+ ]);
35015
35093
  return `Vehicle plate number deleted from ${type} record successfully.`;
35016
35094
  } catch (error) {
35017
35095
  logger68.error("Error in vehicle service delete:", error);
@@ -35113,7 +35191,7 @@ function useVehicleService() {
35113
35191
  };
35114
35192
  await _updateVehicleById(id, formattedValue, session);
35115
35193
  await session.commitTransaction();
35116
- await recordHistory(id, "approved", user, ["status", "start", "end"]);
35194
+ await recordHistory(id, "approved", user, changedFields(vehicle, formattedValue));
35117
35195
  return "Vehicle plate number approved and added successfully.";
35118
35196
  } catch (error) {
35119
35197
  logger68.error("Error in vehicle service approving and adding:", error);
@@ -35269,7 +35347,7 @@ function useVehicleService() {
35269
35347
  };
35270
35348
  await _updateVehicleById(id, formattedValue, session);
35271
35349
  await session.commitTransaction();
35272
- await recordHistory(id, "restored", user, ["status", "start", "end", "deletedAt"]);
35350
+ await recordHistory(id, "restored", user, changedFields(vehicle, formattedValue));
35273
35351
  return "Vehicle reactivated successfully.";
35274
35352
  } catch (error) {
35275
35353
  logger68.error("Error in vehicle service reactivation:", error);
@@ -98312,6 +98390,7 @@ export {
98312
98390
  userSchema,
98313
98391
  vehicleCategoryFor,
98314
98392
  vehicleHistoryEntry,
98393
+ vehicleHistoryValue,
98315
98394
  vehicleListViewQuery,
98316
98395
  vehicleSchema,
98317
98396
  vehicleViewOf,