@7365admin1/core 3.20.0 → 3.21.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
@@ -23883,7 +23883,14 @@ function useDahuaService() {
23883
23883
  }
23884
23884
  loggerDahua.info(`[${camera?.siteName}-${camera?.direction}] ANPR Listener stopped.`);
23885
23885
  }
23886
+ function extractRecNo(responseText) {
23887
+ if (!responseText)
23888
+ return null;
23889
+ const match = responseText.match(/recno=(\d+)/i);
23890
+ return match ? match[1] : null;
23891
+ }
23886
23892
  async function addPlateNumber(value) {
23893
+ let recno = null;
23887
23894
  const validation = Joi40.object({
23888
23895
  host: Joi40.string().required(),
23889
23896
  username: Joi40.string().required(),
@@ -23895,25 +23902,113 @@ function useDahuaService() {
23895
23902
  owner: Joi40.string().optional().allow("", null),
23896
23903
  isOpenGate: Joi40.boolean().optional().allow(null)
23897
23904
  });
23898
- const { error } = validation.validate(value);
23899
- if (error) {
23900
- throw new BadRequestError70(`Validation error: ${error.message}`);
23905
+ const { error: validationError } = validation.validate(value);
23906
+ if (validationError) {
23907
+ throw new BadRequestError70(`Validation error: ${validationError.message}`);
23901
23908
  }
23902
- value.owner = String(value.owner ?? "").substring(0, 15) || "unknown";
23903
- const _openGate = String(value.isOpenGate);
23904
- const isOpenGateString = _openGate && _openGate !== "undefined" ? _openGate : "true";
23905
- const endpoint = `/cgi-bin/recordUpdater.cgi?action=insert&name=${value.mode}&PlateNumber=${value.plateNumber}&BeginTime=${value.start}&CancelTime=${value.end}&+OpenGate=${isOpenGateString}&MasterOfCar=${value.owner}`;
23906
23909
  try {
23907
- const response = await useDahuaDigest({
23908
- host: value.host,
23909
- username: value.username,
23910
- password: value.password,
23911
- endpoint
23912
- });
23913
- return response;
23914
- } catch (error2) {
23915
- loggerDahua.error(`[${value.host}] Error adding plate number:`, error2);
23916
- throw new BadRequestError70(`Failed to add plate number: ${error2.message}`);
23910
+ value.owner = String(value.owner ?? "").substring(0, 15) || "unknown";
23911
+ const formatDahuaDate2 = (dateStr, fallbackYearsAhead = 0) => {
23912
+ const date = dateStr ? new Date(dateStr) : /* @__PURE__ */ new Date();
23913
+ if (!dateStr) {
23914
+ date.setMinutes(date.getMinutes() - 10);
23915
+ }
23916
+ if (fallbackYearsAhead > 0 && !dateStr) {
23917
+ date.setFullYear(date.getFullYear() + fallbackYearsAhead);
23918
+ }
23919
+ const pad = (num) => String(num).padStart(2, "0");
23920
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
23921
+ };
23922
+ const formattedStart = formatDahuaDate2(value.start);
23923
+ const formattedEnd = formatDahuaDate2(value.end, 10);
23924
+ const beginTime = encodeURIComponent(formattedStart);
23925
+ const cancelTime = encodeURIComponent(formattedEnd);
23926
+ const plateNumber = encodeURIComponent(value.plateNumber);
23927
+ const ownerName = encodeURIComponent(value.owner);
23928
+ const endpoint = `/cgi-bin/recordUpdater.cgi?action=insert&name=${value.mode}&PlateNumber=${plateNumber}&BeginTime=${beginTime}&CancelTime=${cancelTime}&MasterOfCar=${ownerName}`;
23929
+ try {
23930
+ const insertResponse = await useDahuaDigestWithRetry({
23931
+ host: value.host,
23932
+ username: value.username,
23933
+ password: value.password,
23934
+ endpoint,
23935
+ retries: 10,
23936
+ retryDelayMs: 0
23937
+ });
23938
+ const insertText = getDahuaResponseText(insertResponse);
23939
+ const insertedId = insertText.match(/\d+/)?.[0] || "unknown";
23940
+ return {
23941
+ ...insertResponse,
23942
+ statusCode: 200,
23943
+ data: Buffer.from(`recno=${insertedId}`)
23944
+ };
23945
+ } catch (insertError) {
23946
+ const errorMessage = insertError?.message || String(insertError);
23947
+ if (/recno=/i.test(errorMessage)) {
23948
+ loggerDahua.info(`[${value.host}] Insert confirmed successfully via raw RecNo response text.`);
23949
+ const insertedId = errorMessage.match(/\d+/)?.[0] || "unknown";
23950
+ return {
23951
+ statusCode: 200,
23952
+ data: Buffer.from(`recno=${insertedId}`)
23953
+ };
23954
+ }
23955
+ if (errorMessage.includes("Bad Request") || errorMessage.includes("Dahua response: Error")) {
23956
+ loggerDahua.info(`[${value.host}] Insert failed (duplicate). Checking if plate ${value.plateNumber} already exists...`);
23957
+ const findEndpoint = `/cgi-bin/recordFinder.cgi?action=find&name=${value.mode}&condition.PlateNumber=${plateNumber}`;
23958
+ try {
23959
+ const findResponse = await useDahuaDigestWithRetry({
23960
+ host: value.host,
23961
+ username: value.username,
23962
+ password: value.password,
23963
+ endpoint: findEndpoint,
23964
+ retries: 5
23965
+ });
23966
+ const textData = getDahuaResponseText(findResponse);
23967
+ recno = extractRecNo(textData);
23968
+ } catch (findError) {
23969
+ const findErrorMessage = findError?.message || String(findError);
23970
+ recno = extractRecNo(findErrorMessage);
23971
+ if (!recno) {
23972
+ loggerDahua.error(`[${value.host}] Failed handling existing duplicate plate flow`, findError);
23973
+ }
23974
+ }
23975
+ if (recno) {
23976
+ loggerDahua.info(`[${value.host}] Found existing record ID: ${recno}. Updating registration instead...`);
23977
+ const updateEndpoint = `/cgi-bin/recordUpdater.cgi?action=update&name=${value.mode}&recno=${recno}&PlateNumber=${plateNumber}&BeginTime=${beginTime}&CancelTime=${cancelTime}&MasterOfCar=${ownerName}`;
23978
+ try {
23979
+ const updateResponse = await useDahuaDigestWithRetry({
23980
+ host: value.host,
23981
+ username: value.username,
23982
+ password: value.password,
23983
+ endpoint: updateEndpoint,
23984
+ retries: 5
23985
+ });
23986
+ return {
23987
+ ...updateResponse,
23988
+ statusCode: 200,
23989
+ data: Buffer.from(`recno=${recno}`)
23990
+ };
23991
+ } catch (updateError) {
23992
+ const updateErrorMessage = updateError?.message || String(updateError);
23993
+ if (/recno=/i.test(updateErrorMessage) || /ok/i.test(updateErrorMessage)) {
23994
+ loggerDahua.info(`[${value.host}] Update confirmed successfully despite wrapper omission.`);
23995
+ if (!recno) {
23996
+ recno = extractRecNo(updateErrorMessage);
23997
+ }
23998
+ return {
23999
+ statusCode: 200,
24000
+ data: Buffer.from(`recno=${recno}`)
24001
+ };
24002
+ }
24003
+ throw updateError;
24004
+ }
24005
+ }
24006
+ }
24007
+ throw insertError;
24008
+ }
24009
+ } catch (finalError) {
24010
+ loggerDahua.error(`[${value.host}] Error adding plate number:`, finalError);
24011
+ throw new BadRequestError70(`Failed to add plate number: ${finalError.message || finalError}`);
23917
24012
  }
23918
24013
  }
23919
24014
  async function updatePlateNumber(value) {
@@ -26889,7 +26984,7 @@ function useVehicleService() {
26889
26984
  const siteCameraReq = await _getAllSiteCameras({
26890
26985
  site: siteId,
26891
26986
  type: "anpr",
26892
- direction: ["both", "entry"],
26987
+ direction: ["both", "entry", "residents"],
26893
26988
  page,
26894
26989
  limit
26895
26990
  });
@@ -26913,11 +27008,14 @@ function useVehicleService() {
26913
27008
  owner
26914
27009
  };
26915
27010
  const dahuaResponse = await _addPlateNumber(dahuaPayload);
26916
- if (dahuaResponse?.statusCode !== 200) {
27011
+ if (dahuaResponse?.statusCode != 200) {
27012
+ console.log("dahuaResponse", dahuaResponse);
27013
+ console.log("approveVehicleById dahuaResponse dahuaResponse?.statusCode != 200", dahuaResponse?.statusCode);
26917
27014
  throw new BadRequestError75("Failed to add plate number to ANPR");
26918
27015
  }
26919
27016
  const responseData = dahuaResponse?.data.toString("utf-8");
26920
27017
  value.recNo = responseData.split("=")[1]?.trim();
27018
+ console.log("approveVehicleById recNo", value.recNo);
26921
27019
  }
26922
27020
  value.status = "active" /* ACTIVE */;
26923
27021
  if (vehicle.peopleId && value.recNo) {
@@ -57903,6 +58001,26 @@ function useNewDashboardRepo() {
57903
58001
  const siteIdObj = toObjectId16(siteId);
57904
58002
  const startOfToday = moment2.tz("Asia/Singapore").startOf("day").toDate();
57905
58003
  const endOfToday = moment2.tz("Asia/Singapore").endOf("day").toDate();
58004
+ const localTodayStr = moment2.tz("Asia/Singapore").format("YYYY-MM-DD");
58005
+ const facilityTodayStart = moment2.utc(`${localTodayStr}T00:00:00.000Z`).toDate();
58006
+ const facilityTodayEnd = moment2.utc(`${localTodayStr}T23:59:59.999Z`).toDate();
58007
+ const localYesterdayStr = moment2.tz("Asia/Singapore").subtract(1, "day").format("YYYY-MM-DD");
58008
+ const facilityYesterdayStart = moment2.utc(`${localYesterdayStr}T00:00:00.000Z`).toDate();
58009
+ const facilityYesterdayEnd = moment2.utc(`${localYesterdayStr}T23:59:59.999Z`).toDate();
58010
+ let facilityPeriodRange = { $gte: facilityTodayStart, $lte: facilityTodayEnd };
58011
+ if (period === "thisWeek" /* THIS_WEEK */) {
58012
+ const startStr = moment2.tz("Asia/Singapore").subtract(7, "days").format("YYYY-MM-DD");
58013
+ facilityPeriodRange = {
58014
+ $gte: moment2.utc(`${startStr}T00:00:00.000Z`).toDate(),
58015
+ $lte: facilityTodayEnd
58016
+ };
58017
+ } else if (period === "thisMonth" /* THIS_MONTH */) {
58018
+ const startStr = moment2.tz("Asia/Singapore").subtract(30, "days").format("YYYY-MM-DD");
58019
+ facilityPeriodRange = {
58020
+ $gte: moment2.utc(`${startStr}T00:00:00.000Z`).toDate(),
58021
+ $lte: facilityTodayEnd
58022
+ };
58023
+ }
57906
58024
  const upcomingEvents = await db.collection(events_namespace_collection).find({
57907
58025
  site: { $in: [siteIdObj, siteId] },
57908
58026
  status: { $nin: ["deleted", "Deleted"] },
@@ -58114,11 +58232,11 @@ function useNewDashboardRepo() {
58114
58232
  site: { $in: [siteIdObj, siteId] },
58115
58233
  ...facilityMatchObj,
58116
58234
  $or: [
58117
- { createdAt: periodRange },
58235
+ { date: facilityPeriodRange },
58118
58236
  {
58119
- createdAt: {
58120
- $gte: periodRange.$gte.toISOString(),
58121
- $lte: periodRange.$lte.toISOString()
58237
+ date: {
58238
+ $gte: facilityPeriodRange.$gte.toISOString(),
58239
+ $lte: facilityPeriodRange.$lte.toISOString()
58122
58240
  }
58123
58241
  }
58124
58242
  ],
@@ -58157,11 +58275,11 @@ function useNewDashboardRepo() {
58157
58275
  site: { $in: [siteIdObj, siteId] },
58158
58276
  ...facilityMatchObj,
58159
58277
  $or: [
58160
- { createdAt: { $gte: yesterday, $lte: yesterdayEnd } },
58278
+ { date: { $gte: facilityYesterdayStart, $lte: facilityYesterdayEnd } },
58161
58279
  {
58162
- createdAt: {
58163
- $gte: yesterday.toISOString(),
58164
- $lte: yesterdayEnd.toISOString()
58280
+ date: {
58281
+ $gte: facilityYesterdayStart.toISOString(),
58282
+ $lte: facilityYesterdayEnd.toISOString()
58165
58283
  }
58166
58284
  }
58167
58285
  ],
@@ -58176,11 +58294,11 @@ function useNewDashboardRepo() {
58176
58294
  site: { $in: [siteIdObj, siteId] },
58177
58295
  ...facilityMatchObj,
58178
58296
  $or: [
58179
- { createdAt: { $gte: today, $lte: todayEnd } },
58297
+ { date: { $gte: facilityTodayStart, $lte: facilityTodayEnd } },
58180
58298
  {
58181
- createdAt: {
58182
- $gte: today.toISOString(),
58183
- $lte: todayEnd.toISOString()
58299
+ date: {
58300
+ $gte: facilityTodayStart.toISOString(),
58301
+ $lte: facilityTodayEnd.toISOString()
58184
58302
  }
58185
58303
  }
58186
58304
  ],