@7365admin1/core 2.46.0 → 2.47.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/CHANGELOG.md +6 -0
- package/dist/index.d.ts +79 -48
- package/dist/index.js +1084 -833
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1079 -833
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -49,6 +49,10 @@ __export(src_exports, {
|
|
|
49
49
|
EmailSender: () => EmailSender,
|
|
50
50
|
EntryOrder: () => EntryOrder,
|
|
51
51
|
EntrySort: () => EntrySort,
|
|
52
|
+
EventOrder: () => EventOrder,
|
|
53
|
+
EventSort: () => EventSort,
|
|
54
|
+
EventStatus: () => EventStatus,
|
|
55
|
+
EventType: () => EventType,
|
|
52
56
|
FacilitySort: () => FacilitySort,
|
|
53
57
|
FacilityStatus: () => FacilityStatus,
|
|
54
58
|
GuestSort: () => GuestSort,
|
|
@@ -161,6 +165,7 @@ __export(src_exports, {
|
|
|
161
165
|
events_namespace_collection: () => events_namespace_collection,
|
|
162
166
|
facility_bookings_namespace_collection: () => facility_bookings_namespace_collection,
|
|
163
167
|
feedbackSchema: () => feedbackSchema,
|
|
168
|
+
feedbacks2_namespace_collection: () => feedbacks2_namespace_collection,
|
|
164
169
|
feedbacks_namespace_collection: () => feedbacks_namespace_collection,
|
|
165
170
|
formatDahuaDate: () => formatDahuaDate,
|
|
166
171
|
getPeriodRangeWithPrevious: () => getPeriodRangeWithPrevious,
|
|
@@ -716,15 +721,22 @@ function MFeedback(value) {
|
|
|
716
721
|
// src/repositories/feedback.repo.ts
|
|
717
722
|
var import_node_server_utils5 = require("@7365admin1/node-server-utils");
|
|
718
723
|
var feedbacks_namespace_collection = "feedbacks";
|
|
724
|
+
var feedbacks2_namespace_collection = "feedbacks2";
|
|
719
725
|
function useFeedbackRepo() {
|
|
720
726
|
const db = import_node_server_utils5.useAtlas.getDb();
|
|
721
727
|
if (!db) {
|
|
722
728
|
throw new import_node_server_utils5.InternalServerError("Unable to connect to server.");
|
|
723
729
|
}
|
|
724
730
|
const collection = db.collection(feedbacks_namespace_collection);
|
|
731
|
+
const feedbacks2Collection = db.collection(feedbacks2_namespace_collection);
|
|
725
732
|
const { delNamespace, getCache, setCache } = (0, import_node_server_utils5.useCache)(
|
|
726
733
|
feedbacks_namespace_collection
|
|
727
734
|
);
|
|
735
|
+
const {
|
|
736
|
+
delNamespace: delNamespaceFeedbacks2,
|
|
737
|
+
getCache: getCacheFeedbacks2,
|
|
738
|
+
setCache: setCacheFeedbacks2
|
|
739
|
+
} = (0, import_node_server_utils5.useCache)(feedbacks2_namespace_collection);
|
|
728
740
|
const { delNamespace: _delDashboardNameSpace } = (0, import_node_server_utils5.useCache)("dashboard");
|
|
729
741
|
async function createIndex() {
|
|
730
742
|
try {
|
|
@@ -1185,7 +1197,7 @@ function useFeedbackRepo() {
|
|
|
1185
1197
|
updatedAt: /* @__PURE__ */ new Date(),
|
|
1186
1198
|
deletedAt: /* @__PURE__ */ new Date()
|
|
1187
1199
|
};
|
|
1188
|
-
const res = await
|
|
1200
|
+
const res = await feedbacks2Collection.updateOne(
|
|
1189
1201
|
{ _id },
|
|
1190
1202
|
{ $set: updateValue },
|
|
1191
1203
|
{ session }
|
|
@@ -1193,13 +1205,13 @@ function useFeedbackRepo() {
|
|
|
1193
1205
|
if (res.modifiedCount === 0) {
|
|
1194
1206
|
throw new import_node_server_utils5.InternalServerError("Unable to delete feedback.");
|
|
1195
1207
|
}
|
|
1196
|
-
|
|
1208
|
+
delNamespaceFeedbacks2().then(() => {
|
|
1197
1209
|
import_node_server_utils5.logger.info(
|
|
1198
|
-
`Cache cleared for namespace: ${
|
|
1210
|
+
`Cache cleared for namespace: ${feedbacks2_namespace_collection}`
|
|
1199
1211
|
);
|
|
1200
1212
|
}).catch((err) => {
|
|
1201
1213
|
import_node_server_utils5.logger.error(
|
|
1202
|
-
`Failed to clear cache for namespace: ${
|
|
1214
|
+
`Failed to clear cache for namespace: ${feedbacks2_namespace_collection}`,
|
|
1203
1215
|
err
|
|
1204
1216
|
);
|
|
1205
1217
|
});
|
|
@@ -13050,7 +13062,7 @@ function useCustomerController() {
|
|
|
13050
13062
|
var import_node_server_utils77 = require("@7365admin1/node-server-utils");
|
|
13051
13063
|
|
|
13052
13064
|
// src/repositories/site-camera.repo.ts
|
|
13053
|
-
var
|
|
13065
|
+
var import_node_server_utils76 = require("@7365admin1/node-server-utils");
|
|
13054
13066
|
|
|
13055
13067
|
// src/models/site-camera.model.ts
|
|
13056
13068
|
var import_node_server_utils66 = require("@7365admin1/node-server-utils");
|
|
@@ -13115,406 +13127,26 @@ function MSiteCamera(value) {
|
|
|
13115
13127
|
}
|
|
13116
13128
|
|
|
13117
13129
|
// src/repositories/site-camera.repo.ts
|
|
13118
|
-
var
|
|
13119
|
-
function useSiteCameraRepo() {
|
|
13120
|
-
const db = import_node_server_utils67.useAtlas.getDb();
|
|
13121
|
-
if (!db) {
|
|
13122
|
-
throw new Error("Unable to connect to server.");
|
|
13123
|
-
}
|
|
13124
|
-
const namespace_collection = "site.cameras";
|
|
13125
|
-
const collection = db.collection(namespace_collection);
|
|
13126
|
-
const { delNamespace, getCache, setCache } = (0, import_node_server_utils67.useCache)(namespace_collection);
|
|
13127
|
-
async function createIndexes() {
|
|
13128
|
-
try {
|
|
13129
|
-
await collection.createIndexes([
|
|
13130
|
-
{
|
|
13131
|
-
key: { site: 1 }
|
|
13132
|
-
},
|
|
13133
|
-
{
|
|
13134
|
-
key: { type: 1 }
|
|
13135
|
-
},
|
|
13136
|
-
{
|
|
13137
|
-
key: { host: 1, type: 1, status: 1, site: 1 },
|
|
13138
|
-
unique: true,
|
|
13139
|
-
partialFilterExpression: { status: "active" }
|
|
13140
|
-
}
|
|
13141
|
-
]);
|
|
13142
|
-
return `Successfully created ${namespace_collection} indexes.`;
|
|
13143
|
-
} catch (error) {
|
|
13144
|
-
import_node_server_utils67.logger.log({
|
|
13145
|
-
level: "error",
|
|
13146
|
-
message: error.message
|
|
13147
|
-
});
|
|
13148
|
-
}
|
|
13149
|
-
}
|
|
13150
|
-
async function add(value, session) {
|
|
13151
|
-
try {
|
|
13152
|
-
value = MSiteCamera(value);
|
|
13153
|
-
const res = await collection.insertOne(value, { session });
|
|
13154
|
-
delCachedData();
|
|
13155
|
-
return res.insertedId;
|
|
13156
|
-
} catch (error) {
|
|
13157
|
-
import_node_server_utils67.logger.log({
|
|
13158
|
-
level: "error",
|
|
13159
|
-
message: error.message
|
|
13160
|
-
});
|
|
13161
|
-
const isDuplicated = error.message.includes("duplicate");
|
|
13162
|
-
if (isDuplicated) {
|
|
13163
|
-
throw new import_node_server_utils67.BadRequestError("ANPR already exist.");
|
|
13164
|
-
}
|
|
13165
|
-
if (error instanceof import_node_server_utils67.AppError) {
|
|
13166
|
-
throw error;
|
|
13167
|
-
} else {
|
|
13168
|
-
throw new Error("Failed to create ANPR.");
|
|
13169
|
-
}
|
|
13170
|
-
}
|
|
13171
|
-
}
|
|
13172
|
-
async function getAll(value) {
|
|
13173
|
-
value.page = value.page ? value.page - 1 : 0;
|
|
13174
|
-
value.limit = value.limit || 10;
|
|
13175
|
-
const cacheKeyOptions = {
|
|
13176
|
-
type: value.type,
|
|
13177
|
-
page: value.page,
|
|
13178
|
-
limit: value.limit,
|
|
13179
|
-
...value.direction && { direction: value.direction }
|
|
13180
|
-
};
|
|
13181
|
-
const query = {
|
|
13182
|
-
type: value.type,
|
|
13183
|
-
...value.direction && {
|
|
13184
|
-
direction: {
|
|
13185
|
-
$in: Array.isArray(value.direction) ? value.direction : [value.direction]
|
|
13186
|
-
}
|
|
13187
|
-
}
|
|
13188
|
-
};
|
|
13189
|
-
if (value.site) {
|
|
13190
|
-
cacheKeyOptions.site = value.site;
|
|
13191
|
-
try {
|
|
13192
|
-
value.site = new import_mongodb38.ObjectId(value.site);
|
|
13193
|
-
query.site = value.site;
|
|
13194
|
-
} catch (error) {
|
|
13195
|
-
throw new import_node_server_utils67.BadRequestError("Invalid site ID format");
|
|
13196
|
-
}
|
|
13197
|
-
}
|
|
13198
|
-
const cacheKey = (0, import_node_server_utils67.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
13199
|
-
const cachedData = await getCache(cacheKey);
|
|
13200
|
-
if (cachedData) {
|
|
13201
|
-
return cachedData;
|
|
13202
|
-
}
|
|
13203
|
-
try {
|
|
13204
|
-
const items = await collection.aggregate([
|
|
13205
|
-
{
|
|
13206
|
-
$match: query
|
|
13207
|
-
},
|
|
13208
|
-
{
|
|
13209
|
-
$skip: value.page * value.limit
|
|
13210
|
-
},
|
|
13211
|
-
{
|
|
13212
|
-
$limit: value.limit
|
|
13213
|
-
},
|
|
13214
|
-
{
|
|
13215
|
-
$project: {
|
|
13216
|
-
password: 0
|
|
13217
|
-
}
|
|
13218
|
-
}
|
|
13219
|
-
]).toArray();
|
|
13220
|
-
const length = await collection.countDocuments(query);
|
|
13221
|
-
const data = (0, import_node_server_utils67.paginate)(items, value.page, value.limit, length);
|
|
13222
|
-
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
13223
|
-
import_node_server_utils67.logger.info(`Cache set for key: ${cacheKey}`);
|
|
13224
|
-
}).catch((err) => {
|
|
13225
|
-
import_node_server_utils67.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
13226
|
-
});
|
|
13227
|
-
return data;
|
|
13228
|
-
} catch (error) {
|
|
13229
|
-
if (error instanceof import_node_server_utils67.BadRequestError) {
|
|
13230
|
-
throw error;
|
|
13231
|
-
}
|
|
13232
|
-
throw new import_node_server_utils67.BadRequestError("Failed to retrieve site cameras.");
|
|
13233
|
-
}
|
|
13234
|
-
}
|
|
13235
|
-
async function findSiteCameras({ site, type }) {
|
|
13236
|
-
const pipeline = [];
|
|
13237
|
-
const cacheObject = {};
|
|
13238
|
-
if (type) {
|
|
13239
|
-
cacheObject.type = type;
|
|
13240
|
-
pipeline.push({ $match: { "anprs.type": type } });
|
|
13241
|
-
}
|
|
13242
|
-
if (site) {
|
|
13243
|
-
const _site = new import_mongodb38.ObjectId(site);
|
|
13244
|
-
cacheObject.site = site;
|
|
13245
|
-
pipeline.push({ $match: { site: _site } });
|
|
13246
|
-
}
|
|
13247
|
-
pipeline.push({
|
|
13248
|
-
$project: {
|
|
13249
|
-
_id: 1,
|
|
13250
|
-
site: 1,
|
|
13251
|
-
cameras: 1
|
|
13252
|
-
}
|
|
13253
|
-
});
|
|
13254
|
-
try {
|
|
13255
|
-
const items = await collection.aggregate(pipeline).toArray();
|
|
13256
|
-
return items;
|
|
13257
|
-
} catch (error) {
|
|
13258
|
-
if (error instanceof import_node_server_utils67.BadRequestError) {
|
|
13259
|
-
throw error;
|
|
13260
|
-
}
|
|
13261
|
-
throw new import_node_server_utils67.BadRequestError("Failed to retrieve site cameras.");
|
|
13262
|
-
}
|
|
13263
|
-
}
|
|
13264
|
-
function delCachedData() {
|
|
13265
|
-
delNamespace().then(() => {
|
|
13266
|
-
import_node_server_utils67.logger.log({
|
|
13267
|
-
level: "info",
|
|
13268
|
-
message: `Cache namespace cleared for ${namespace_collection}`
|
|
13269
|
-
});
|
|
13270
|
-
}).catch((err) => {
|
|
13271
|
-
import_node_server_utils67.logger.log({
|
|
13272
|
-
level: "error",
|
|
13273
|
-
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
13274
|
-
});
|
|
13275
|
-
});
|
|
13276
|
-
}
|
|
13277
|
-
async function getBySite(site, options = {}) {
|
|
13278
|
-
const _site = (0, import_node_server_utils67.toObjectId)(site);
|
|
13279
|
-
const cacheKeyOptions = {
|
|
13280
|
-
site,
|
|
13281
|
-
tag: "get-by-site",
|
|
13282
|
-
...options.category && { category: options.category },
|
|
13283
|
-
...options.type && { type: options.type },
|
|
13284
|
-
...options.guardPost && { guardPost: options.guardPost }
|
|
13285
|
-
};
|
|
13286
|
-
const cacheKey = (0, import_node_server_utils67.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
13287
|
-
const cachedData = await getCache(cacheKey);
|
|
13288
|
-
if (cachedData) {
|
|
13289
|
-
return cachedData;
|
|
13290
|
-
}
|
|
13291
|
-
try {
|
|
13292
|
-
delete cacheKeyOptions.tag;
|
|
13293
|
-
cacheKeyOptions.site = _site;
|
|
13294
|
-
const item = await collection.findOne(cacheKeyOptions);
|
|
13295
|
-
setCache(cacheKey, item, 15 * 60).then(() => {
|
|
13296
|
-
import_node_server_utils67.logger.log({
|
|
13297
|
-
level: "info",
|
|
13298
|
-
message: `Cache set for getBySite: ${cacheKey}`
|
|
13299
|
-
});
|
|
13300
|
-
}).catch((err) => {
|
|
13301
|
-
import_node_server_utils67.logger.log({
|
|
13302
|
-
level: "error",
|
|
13303
|
-
message: `Failed to set cache for getBySite: ${err.message}`
|
|
13304
|
-
});
|
|
13305
|
-
});
|
|
13306
|
-
return item;
|
|
13307
|
-
} catch (error) {
|
|
13308
|
-
if (error instanceof import_node_server_utils67.BadRequestError) {
|
|
13309
|
-
throw error;
|
|
13310
|
-
}
|
|
13311
|
-
throw new import_node_server_utils67.BadRequestError("Failed to retrieve site cameras.");
|
|
13312
|
-
}
|
|
13313
|
-
}
|
|
13314
|
-
async function getBySiteGuardPost(site, guardPost) {
|
|
13315
|
-
try {
|
|
13316
|
-
site = new import_mongodb38.ObjectId(site);
|
|
13317
|
-
} catch (error) {
|
|
13318
|
-
throw new import_node_server_utils67.BadRequestError("Invalid site ID format");
|
|
13319
|
-
}
|
|
13320
|
-
const cacheKeyOptions = {
|
|
13321
|
-
site,
|
|
13322
|
-
guardPost,
|
|
13323
|
-
tag: "get-by-site"
|
|
13324
|
-
};
|
|
13325
|
-
const cacheKey = (0, import_node_server_utils67.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
13326
|
-
const cachedData = await getCache(cacheKey);
|
|
13327
|
-
if (cachedData) {
|
|
13328
|
-
return cachedData;
|
|
13329
|
-
}
|
|
13330
|
-
try {
|
|
13331
|
-
delete cacheKeyOptions.tag;
|
|
13332
|
-
const item = await collection.findOne({
|
|
13333
|
-
site,
|
|
13334
|
-
guardPost: { $gt: guardPost }
|
|
13335
|
-
});
|
|
13336
|
-
setCache(cacheKey, item, 15 * 60).then(() => {
|
|
13337
|
-
import_node_server_utils67.logger.log({
|
|
13338
|
-
level: "info",
|
|
13339
|
-
message: `Cache set for getBySite: ${cacheKey}`
|
|
13340
|
-
});
|
|
13341
|
-
}).catch((err) => {
|
|
13342
|
-
import_node_server_utils67.logger.log({
|
|
13343
|
-
level: "error",
|
|
13344
|
-
message: `Failed to set cache for getBySite: ${err.message}`
|
|
13345
|
-
});
|
|
13346
|
-
});
|
|
13347
|
-
return item;
|
|
13348
|
-
} catch (error) {
|
|
13349
|
-
if (error instanceof import_node_server_utils67.BadRequestError) {
|
|
13350
|
-
throw error;
|
|
13351
|
-
}
|
|
13352
|
-
throw new import_node_server_utils67.BadRequestError("Failed to retrieve site cameras.");
|
|
13353
|
-
}
|
|
13354
|
-
}
|
|
13355
|
-
async function getBySites(site, options = {}) {
|
|
13356
|
-
try {
|
|
13357
|
-
site = new import_mongodb38.ObjectId(site);
|
|
13358
|
-
} catch (error) {
|
|
13359
|
-
throw new import_node_server_utils67.BadRequestError("Invalid site ID format");
|
|
13360
|
-
}
|
|
13361
|
-
const cacheKeyOptions = { site };
|
|
13362
|
-
if (options.category) {
|
|
13363
|
-
cacheKeyOptions.category = options.category;
|
|
13364
|
-
}
|
|
13365
|
-
if (options.type) {
|
|
13366
|
-
cacheKeyOptions.type = options.type;
|
|
13367
|
-
}
|
|
13368
|
-
if (options.direction) {
|
|
13369
|
-
cacheKeyOptions.direction = options.direction;
|
|
13370
|
-
}
|
|
13371
|
-
const cacheKey = (0, import_node_server_utils67.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
13372
|
-
const cachedData = await getCache(cacheKey);
|
|
13373
|
-
if (cachedData) {
|
|
13374
|
-
return cachedData;
|
|
13375
|
-
}
|
|
13376
|
-
try {
|
|
13377
|
-
const items = await collection.find(cacheKeyOptions);
|
|
13378
|
-
setCache(cacheKey, items, 15 * 60).then(() => {
|
|
13379
|
-
import_node_server_utils67.logger.log({
|
|
13380
|
-
level: "info",
|
|
13381
|
-
message: `Cache set for getBySite: ${cacheKey}`
|
|
13382
|
-
});
|
|
13383
|
-
}).catch((err) => {
|
|
13384
|
-
import_node_server_utils67.logger.log({
|
|
13385
|
-
level: "error",
|
|
13386
|
-
message: `Failed to set cache for getBySite: ${err.message}`
|
|
13387
|
-
});
|
|
13388
|
-
});
|
|
13389
|
-
return items;
|
|
13390
|
-
} catch (error) {
|
|
13391
|
-
if (error instanceof import_node_server_utils67.BadRequestError) {
|
|
13392
|
-
throw error;
|
|
13393
|
-
}
|
|
13394
|
-
throw new import_node_server_utils67.BadRequestError("Failed to retrieve site cameras.");
|
|
13395
|
-
}
|
|
13396
|
-
}
|
|
13397
|
-
async function updateById(_id, value, session) {
|
|
13398
|
-
const { error } = schemaUpdateSiteCamera.validate(value);
|
|
13399
|
-
if (error) {
|
|
13400
|
-
import_node_server_utils67.logger.info(`Site Camera Management: ${error.message}`);
|
|
13401
|
-
throw new import_node_server_utils67.BadRequestError(error.message);
|
|
13402
|
-
}
|
|
13403
|
-
try {
|
|
13404
|
-
_id = new import_mongodb38.ObjectId(_id);
|
|
13405
|
-
} catch (error2) {
|
|
13406
|
-
throw new import_node_server_utils67.BadRequestError("Invalid camera ID format");
|
|
13407
|
-
}
|
|
13408
|
-
try {
|
|
13409
|
-
await collection.updateOne({ _id }, { $set: value }, { session });
|
|
13410
|
-
delCachedData();
|
|
13411
|
-
return "Successfully updated site camera.";
|
|
13412
|
-
} catch (error2) {
|
|
13413
|
-
import_node_server_utils67.logger.log({
|
|
13414
|
-
level: "error",
|
|
13415
|
-
message: error2.message
|
|
13416
|
-
});
|
|
13417
|
-
const isDuplicated = error2.message.includes("duplicate");
|
|
13418
|
-
if (isDuplicated) {
|
|
13419
|
-
throw new import_node_server_utils67.BadRequestError("ANPR already exist.");
|
|
13420
|
-
}
|
|
13421
|
-
if (error2 instanceof import_node_server_utils67.AppError) {
|
|
13422
|
-
throw error2;
|
|
13423
|
-
}
|
|
13424
|
-
throw new Error("Failed to update ANPR.");
|
|
13425
|
-
}
|
|
13426
|
-
}
|
|
13427
|
-
async function deleteById(_id, session) {
|
|
13428
|
-
try {
|
|
13429
|
-
_id = new import_mongodb38.ObjectId(_id);
|
|
13430
|
-
} catch (error) {
|
|
13431
|
-
throw new import_node_server_utils67.BadRequestError("Invalid camera ID format");
|
|
13432
|
-
}
|
|
13433
|
-
try {
|
|
13434
|
-
await collection.deleteOne({ _id }, { session });
|
|
13435
|
-
delCachedData();
|
|
13436
|
-
return "Successfully deleted site camera.";
|
|
13437
|
-
} catch (error) {
|
|
13438
|
-
import_node_server_utils67.logger.log({
|
|
13439
|
-
level: "error",
|
|
13440
|
-
message: error.message
|
|
13441
|
-
});
|
|
13442
|
-
if (error instanceof import_node_server_utils67.AppError) {
|
|
13443
|
-
throw error;
|
|
13444
|
-
}
|
|
13445
|
-
throw new Error("Failed to delete site camera.");
|
|
13446
|
-
}
|
|
13447
|
-
}
|
|
13448
|
-
async function getAllCameraWithPassword(value) {
|
|
13449
|
-
value.page = value.page ? value.page - 1 : 0;
|
|
13450
|
-
value.limit = value.limit || 10;
|
|
13451
|
-
const query = {
|
|
13452
|
-
type: value.type,
|
|
13453
|
-
...value.direction && {
|
|
13454
|
-
direction: {
|
|
13455
|
-
$in: Array.isArray(value.direction) ? value.direction : [value.direction]
|
|
13456
|
-
}
|
|
13457
|
-
}
|
|
13458
|
-
};
|
|
13459
|
-
if (value.site) {
|
|
13460
|
-
try {
|
|
13461
|
-
value.site = new import_mongodb38.ObjectId(value.site);
|
|
13462
|
-
query.site = value.site;
|
|
13463
|
-
} catch (error) {
|
|
13464
|
-
throw new import_node_server_utils67.BadRequestError("Invalid site ID format");
|
|
13465
|
-
}
|
|
13466
|
-
}
|
|
13467
|
-
try {
|
|
13468
|
-
const items = await collection.aggregate([
|
|
13469
|
-
{
|
|
13470
|
-
$match: query
|
|
13471
|
-
},
|
|
13472
|
-
{
|
|
13473
|
-
$skip: value.page * value.limit
|
|
13474
|
-
},
|
|
13475
|
-
{
|
|
13476
|
-
$limit: value.limit
|
|
13477
|
-
}
|
|
13478
|
-
]).toArray();
|
|
13479
|
-
const length = await collection.countDocuments(query);
|
|
13480
|
-
const data = (0, import_node_server_utils67.paginate)(items, value.page, value.limit, length);
|
|
13481
|
-
return data;
|
|
13482
|
-
} catch (error) {
|
|
13483
|
-
if (error instanceof import_node_server_utils67.BadRequestError) {
|
|
13484
|
-
throw error;
|
|
13485
|
-
}
|
|
13486
|
-
throw new import_node_server_utils67.BadRequestError("Failed to retrieve site cameras.");
|
|
13487
|
-
}
|
|
13488
|
-
}
|
|
13489
|
-
return {
|
|
13490
|
-
createIndexes,
|
|
13491
|
-
add,
|
|
13492
|
-
getAll,
|
|
13493
|
-
findSiteCameras,
|
|
13494
|
-
getBySite,
|
|
13495
|
-
getBySites,
|
|
13496
|
-
delCachedData,
|
|
13497
|
-
getBySiteGuardPost,
|
|
13498
|
-
updateById,
|
|
13499
|
-
deleteById,
|
|
13500
|
-
getAllCameraWithPassword
|
|
13501
|
-
};
|
|
13502
|
-
}
|
|
13130
|
+
var import_mongodb47 = require("mongodb");
|
|
13503
13131
|
|
|
13504
|
-
// src/services/
|
|
13505
|
-
var
|
|
13132
|
+
// src/services/dahua.service.ts
|
|
13133
|
+
var import_node_server_utils75 = require("@7365admin1/node-server-utils");
|
|
13134
|
+
var import_fs = require("fs");
|
|
13135
|
+
var import_joi40 = __toESM(require("joi"));
|
|
13136
|
+
var path = __toESM(require("path"));
|
|
13137
|
+
var import_urllib = require("urllib");
|
|
13506
13138
|
|
|
13507
13139
|
// src/repositories/visitor-transaction.repo.ts
|
|
13508
|
-
var
|
|
13140
|
+
var import_node_server_utils68 = require("@7365admin1/node-server-utils");
|
|
13509
13141
|
|
|
13510
13142
|
// src/models/visitor-transactions.model.ts
|
|
13511
13143
|
var import_joi36 = __toESM(require("joi"));
|
|
13512
|
-
var
|
|
13144
|
+
var import_mongodb39 = require("mongodb");
|
|
13513
13145
|
|
|
13514
13146
|
// src/models/person.model.ts
|
|
13515
|
-
var
|
|
13147
|
+
var import_node_server_utils67 = require("@7365admin1/node-server-utils");
|
|
13516
13148
|
var import_joi35 = __toESM(require("joi"));
|
|
13517
|
-
var
|
|
13149
|
+
var import_mongodb38 = require("mongodb");
|
|
13518
13150
|
var PersonTypes = /* @__PURE__ */ ((PersonTypes3) => {
|
|
13519
13151
|
PersonTypes3["WALK_IN"] = "walk-in";
|
|
13520
13152
|
PersonTypes3["DROP_OFF"] = "drop-off";
|
|
@@ -13607,25 +13239,25 @@ var schemaUpdatePerson = import_joi35.default.object({
|
|
|
13607
13239
|
function MPerson(value) {
|
|
13608
13240
|
const { error } = schemaPerson.validate(value);
|
|
13609
13241
|
if (error) {
|
|
13610
|
-
throw new
|
|
13242
|
+
throw new import_node_server_utils67.BadRequestError(error.details[0].message);
|
|
13611
13243
|
}
|
|
13612
13244
|
if (value._id && typeof value._id === "string") {
|
|
13613
13245
|
try {
|
|
13614
|
-
value._id = new
|
|
13246
|
+
value._id = new import_mongodb38.ObjectId(value._id);
|
|
13615
13247
|
} catch (error2) {
|
|
13616
13248
|
throw new Error("Invalid ID.");
|
|
13617
13249
|
}
|
|
13618
13250
|
}
|
|
13619
13251
|
if (value.org && typeof value.org === "string") {
|
|
13620
13252
|
try {
|
|
13621
|
-
value.org = new
|
|
13253
|
+
value.org = new import_mongodb38.ObjectId(value.org);
|
|
13622
13254
|
} catch (error2) {
|
|
13623
13255
|
throw new Error("Invalid org ID.");
|
|
13624
13256
|
}
|
|
13625
13257
|
}
|
|
13626
13258
|
if (value.site && typeof value.site === "string") {
|
|
13627
13259
|
try {
|
|
13628
|
-
value.site = new
|
|
13260
|
+
value.site = new import_mongodb38.ObjectId(value.site);
|
|
13629
13261
|
} catch (error2) {
|
|
13630
13262
|
throw new Error("Invalid site ID.");
|
|
13631
13263
|
}
|
|
@@ -13634,9 +13266,9 @@ function MPerson(value) {
|
|
|
13634
13266
|
value.files = value.files.map((file) => {
|
|
13635
13267
|
if (file.id && typeof file.id === "string") {
|
|
13636
13268
|
try {
|
|
13637
|
-
file.id = new
|
|
13269
|
+
file.id = new import_mongodb38.ObjectId(file.id);
|
|
13638
13270
|
} catch {
|
|
13639
|
-
throw new
|
|
13271
|
+
throw new import_node_server_utils67.BadRequestError("Invalid file id format");
|
|
13640
13272
|
}
|
|
13641
13273
|
}
|
|
13642
13274
|
return file;
|
|
@@ -13644,7 +13276,7 @@ function MPerson(value) {
|
|
|
13644
13276
|
}
|
|
13645
13277
|
if (value.user && typeof value.user === "string") {
|
|
13646
13278
|
try {
|
|
13647
|
-
value.user = new
|
|
13279
|
+
value.user = new import_mongodb38.ObjectId(value.user);
|
|
13648
13280
|
} catch (error2) {
|
|
13649
13281
|
throw new Error("Invalid user ID.");
|
|
13650
13282
|
}
|
|
@@ -13814,28 +13446,28 @@ function MVisitorTransaction(value) {
|
|
|
13814
13446
|
}
|
|
13815
13447
|
if (value._id && typeof value._id === "string") {
|
|
13816
13448
|
try {
|
|
13817
|
-
value._id = new
|
|
13449
|
+
value._id = new import_mongodb39.ObjectId(value._id);
|
|
13818
13450
|
} catch (error2) {
|
|
13819
13451
|
throw new Error("Invalid ID.");
|
|
13820
13452
|
}
|
|
13821
13453
|
}
|
|
13822
13454
|
if (value.org && typeof value.org === "string") {
|
|
13823
13455
|
try {
|
|
13824
|
-
value.org = new
|
|
13456
|
+
value.org = new import_mongodb39.ObjectId(value.org);
|
|
13825
13457
|
} catch (error2) {
|
|
13826
13458
|
throw new Error("Invalid org ID.");
|
|
13827
13459
|
}
|
|
13828
13460
|
}
|
|
13829
13461
|
if (value.site && typeof value.site === "string") {
|
|
13830
13462
|
try {
|
|
13831
|
-
value.site = new
|
|
13463
|
+
value.site = new import_mongodb39.ObjectId(value.site);
|
|
13832
13464
|
} catch (error2) {
|
|
13833
13465
|
throw new Error("Invalid site ID.");
|
|
13834
13466
|
}
|
|
13835
13467
|
}
|
|
13836
13468
|
if (value.unit && typeof value.unit === "string") {
|
|
13837
13469
|
try {
|
|
13838
|
-
value.unit = new
|
|
13470
|
+
value.unit = new import_mongodb39.ObjectId(value.unit);
|
|
13839
13471
|
} catch (error2) {
|
|
13840
13472
|
throw new Error("Invalid unit ID.");
|
|
13841
13473
|
}
|
|
@@ -13850,9 +13482,9 @@ function MVisitorTransaction(value) {
|
|
|
13850
13482
|
if (value.visitorPass && Array.isArray(value.visitorPass) && value.visitorPass.length > 0) {
|
|
13851
13483
|
value.visitorPass = value.visitorPass.map((v) => {
|
|
13852
13484
|
if (typeof v === "string")
|
|
13853
|
-
return { keyId: new
|
|
13485
|
+
return { keyId: new import_mongodb39.ObjectId(v) };
|
|
13854
13486
|
if (v?.keyId && typeof v.keyId === "string") {
|
|
13855
|
-
return { keyId: new
|
|
13487
|
+
return { keyId: new import_mongodb39.ObjectId(v.keyId) };
|
|
13856
13488
|
}
|
|
13857
13489
|
return v;
|
|
13858
13490
|
});
|
|
@@ -13860,16 +13492,16 @@ function MVisitorTransaction(value) {
|
|
|
13860
13492
|
if (value.passKeys && Array.isArray(value.passKeys) && value.passKeys.length > 0) {
|
|
13861
13493
|
value.passKeys = value.passKeys.map((p) => {
|
|
13862
13494
|
if (typeof p === "string")
|
|
13863
|
-
return { keyId: new
|
|
13495
|
+
return { keyId: new import_mongodb39.ObjectId(p) };
|
|
13864
13496
|
if (p?.keyId && typeof p.keyId === "string") {
|
|
13865
|
-
return { keyId: new
|
|
13497
|
+
return { keyId: new import_mongodb39.ObjectId(p.keyId) };
|
|
13866
13498
|
}
|
|
13867
13499
|
return p;
|
|
13868
13500
|
});
|
|
13869
13501
|
}
|
|
13870
13502
|
if (value.inviterId && typeof value.inviterId === "string") {
|
|
13871
13503
|
try {
|
|
13872
|
-
value.inviterId = new
|
|
13504
|
+
value.inviterId = new import_mongodb39.ObjectId(value.inviterId);
|
|
13873
13505
|
} catch (error2) {
|
|
13874
13506
|
throw new Error("Invalid inviter ID.");
|
|
13875
13507
|
}
|
|
@@ -13926,12 +13558,12 @@ function MVisitorTransaction(value) {
|
|
|
13926
13558
|
}
|
|
13927
13559
|
|
|
13928
13560
|
// src/repositories/visitor-transaction.repo.ts
|
|
13929
|
-
var
|
|
13561
|
+
var import_mongodb40 = require("mongodb");
|
|
13930
13562
|
var visitors_namespace_collection = "visitor.transactions";
|
|
13931
13563
|
function useVisitorTransactionRepo() {
|
|
13932
|
-
const db =
|
|
13564
|
+
const db = import_node_server_utils68.useAtlas.getDb();
|
|
13933
13565
|
if (!db) {
|
|
13934
|
-
throw new
|
|
13566
|
+
throw new import_node_server_utils68.InternalServerError("Unable to connect to server.");
|
|
13935
13567
|
}
|
|
13936
13568
|
const collection = db.collection(visitors_namespace_collection);
|
|
13937
13569
|
async function createTextIndex() {
|
|
@@ -13945,7 +13577,7 @@ function useVisitorTransactionRepo() {
|
|
|
13945
13577
|
contact: "text"
|
|
13946
13578
|
});
|
|
13947
13579
|
} catch (error) {
|
|
13948
|
-
throw new
|
|
13580
|
+
throw new import_node_server_utils68.InternalServerError(
|
|
13949
13581
|
"Failed to create text index on visitor transaction."
|
|
13950
13582
|
);
|
|
13951
13583
|
}
|
|
@@ -13959,7 +13591,7 @@ function useVisitorTransactionRepo() {
|
|
|
13959
13591
|
console.log("Error in add visitor transaction:", error);
|
|
13960
13592
|
const isDuplicated = error.message.includes("duplicate");
|
|
13961
13593
|
if (isDuplicated) {
|
|
13962
|
-
throw new
|
|
13594
|
+
throw new import_node_server_utils68.BadRequestError("Visitor already exists.");
|
|
13963
13595
|
}
|
|
13964
13596
|
throw error;
|
|
13965
13597
|
}
|
|
@@ -13995,8 +13627,8 @@ function useVisitorTransactionRepo() {
|
|
|
13995
13627
|
expectedCheckInFilter.$lte = new Date(dateTo);
|
|
13996
13628
|
}
|
|
13997
13629
|
const query = {
|
|
13998
|
-
...
|
|
13999
|
-
...
|
|
13630
|
+
...import_mongodb40.ObjectId.isValid(org) && { org: new import_mongodb40.ObjectId(org) },
|
|
13631
|
+
...import_mongodb40.ObjectId.isValid(site) && { site: new import_mongodb40.ObjectId(site) },
|
|
14000
13632
|
...Object.keys(checkInFilter).length > 0 && { checkIn: checkInFilter },
|
|
14001
13633
|
...Object.keys(expectedCheckInFilter).length > 0 && {
|
|
14002
13634
|
expectedCheckIn: expectedCheckInFilter
|
|
@@ -14181,14 +13813,14 @@ function useVisitorTransactionRepo() {
|
|
|
14181
13813
|
collection.aggregate([...basePipeline, { $count: "total" }]).toArray()
|
|
14182
13814
|
]);
|
|
14183
13815
|
const totalCount = countResult[0]?.total || 0;
|
|
14184
|
-
const data = (0,
|
|
13816
|
+
const data = (0, import_node_server_utils68.paginate)(items, page, limit, totalCount);
|
|
14185
13817
|
return data;
|
|
14186
13818
|
} catch (error) {
|
|
14187
13819
|
throw error;
|
|
14188
13820
|
}
|
|
14189
13821
|
}
|
|
14190
13822
|
async function getVisitorTransactionById(id) {
|
|
14191
|
-
const _id = (0,
|
|
13823
|
+
const _id = (0, import_node_server_utils68.toObjectId)(id);
|
|
14192
13824
|
try {
|
|
14193
13825
|
const basePipeline = [{ $match: { _id } }];
|
|
14194
13826
|
const [result] = await collection.aggregate([
|
|
@@ -14230,7 +13862,7 @@ function useVisitorTransactionRepo() {
|
|
|
14230
13862
|
}
|
|
14231
13863
|
}
|
|
14232
13864
|
async function getOpenByPlateNumber(plateNumber, site) {
|
|
14233
|
-
const _site = typeof site === "string" ? (0,
|
|
13865
|
+
const _site = typeof site === "string" ? (0, import_node_server_utils68.toObjectId)(site) : site;
|
|
14234
13866
|
const query = {
|
|
14235
13867
|
plateNumber,
|
|
14236
13868
|
site: _site,
|
|
@@ -14240,16 +13872,16 @@ function useVisitorTransactionRepo() {
|
|
|
14240
13872
|
const data = await collection.findOne(query);
|
|
14241
13873
|
return data;
|
|
14242
13874
|
} catch (error) {
|
|
14243
|
-
throw new
|
|
13875
|
+
throw new import_node_server_utils68.InternalServerError(
|
|
14244
13876
|
"Failed to fetch visitor transaction by plate number."
|
|
14245
13877
|
);
|
|
14246
13878
|
}
|
|
14247
13879
|
}
|
|
14248
13880
|
async function updateById(_id, value, session) {
|
|
14249
13881
|
try {
|
|
14250
|
-
_id = new
|
|
13882
|
+
_id = new import_mongodb40.ObjectId(_id);
|
|
14251
13883
|
} catch (error) {
|
|
14252
|
-
throw new
|
|
13884
|
+
throw new import_node_server_utils68.BadRequestError("Invalid visitor transaction ID format.");
|
|
14253
13885
|
}
|
|
14254
13886
|
value.updatedAt = /* @__PURE__ */ new Date();
|
|
14255
13887
|
if (value.checkOut) {
|
|
@@ -14268,9 +13900,9 @@ function useVisitorTransactionRepo() {
|
|
|
14268
13900
|
}
|
|
14269
13901
|
async function deleteVisitorTransaction(_id) {
|
|
14270
13902
|
try {
|
|
14271
|
-
_id = new
|
|
13903
|
+
_id = new import_mongodb40.ObjectId(_id);
|
|
14272
13904
|
} catch (error) {
|
|
14273
|
-
throw new
|
|
13905
|
+
throw new import_node_server_utils68.BadRequestError("Invalid customer ID format.");
|
|
14274
13906
|
}
|
|
14275
13907
|
try {
|
|
14276
13908
|
const updateValue = {
|
|
@@ -14280,7 +13912,7 @@ function useVisitorTransactionRepo() {
|
|
|
14280
13912
|
};
|
|
14281
13913
|
const res = await collection.updateOne({ _id }, { $set: updateValue });
|
|
14282
13914
|
if (res.modifiedCount === 0) {
|
|
14283
|
-
throw new
|
|
13915
|
+
throw new import_node_server_utils68.InternalServerError("Unable to delete visitor transaction.");
|
|
14284
13916
|
}
|
|
14285
13917
|
return res.modifiedCount;
|
|
14286
13918
|
} catch (error) {
|
|
@@ -14314,7 +13946,7 @@ function useVisitorTransactionRepo() {
|
|
|
14314
13946
|
}
|
|
14315
13947
|
}
|
|
14316
13948
|
async function getExpiredCheckedOutTransactionsBySite(siteId) {
|
|
14317
|
-
const site = (0,
|
|
13949
|
+
const site = (0, import_node_server_utils68.toObjectId)(siteId);
|
|
14318
13950
|
try {
|
|
14319
13951
|
const now = /* @__PURE__ */ new Date();
|
|
14320
13952
|
const expiredTransactions = await collection.find({
|
|
@@ -14335,7 +13967,7 @@ function useVisitorTransactionRepo() {
|
|
|
14335
13967
|
async function updateManyDahuaSyncStatus(ids, dahuaSyncStatus, session) {
|
|
14336
13968
|
try {
|
|
14337
13969
|
const objectIds = ids.map(
|
|
14338
|
-
(id) => typeof id === "string" ? new
|
|
13970
|
+
(id) => typeof id === "string" ? new import_mongodb40.ObjectId(id) : id
|
|
14339
13971
|
);
|
|
14340
13972
|
const res = await collection.updateMany(
|
|
14341
13973
|
{ _id: { $in: objectIds } },
|
|
@@ -14366,10 +13998,13 @@ function useVisitorTransactionRepo() {
|
|
|
14366
13998
|
};
|
|
14367
13999
|
}
|
|
14368
14000
|
|
|
14369
|
-
// src/
|
|
14001
|
+
// src/repositories/vehicle.repo.ts
|
|
14370
14002
|
var import_node_server_utils70 = require("@7365admin1/node-server-utils");
|
|
14003
|
+
|
|
14004
|
+
// src/models/vehicle.model.ts
|
|
14005
|
+
var import_node_server_utils69 = require("@7365admin1/node-server-utils");
|
|
14371
14006
|
var import_joi37 = __toESM(require("joi"));
|
|
14372
|
-
var
|
|
14007
|
+
var import_mongodb41 = require("mongodb");
|
|
14373
14008
|
var VehicleType = /* @__PURE__ */ ((VehicleType2) => {
|
|
14374
14009
|
VehicleType2["WHITELIST"] = "whitelist";
|
|
14375
14010
|
VehicleType2["BLOCKLIST"] = "blocklist";
|
|
@@ -14446,35 +14081,35 @@ var vehicleSchema = import_joi37.default.object({
|
|
|
14446
14081
|
function MVehicle(value) {
|
|
14447
14082
|
const { error } = vehicleSchema.validate(value);
|
|
14448
14083
|
if (error) {
|
|
14449
|
-
|
|
14450
|
-
throw new
|
|
14084
|
+
import_node_server_utils69.logger.log({ level: "error", message: error.message });
|
|
14085
|
+
throw new import_node_server_utils69.BadRequestError(error.message);
|
|
14451
14086
|
}
|
|
14452
14087
|
if (value.org) {
|
|
14453
14088
|
try {
|
|
14454
|
-
value.org = new
|
|
14089
|
+
value.org = new import_mongodb41.ObjectId(value.org);
|
|
14455
14090
|
} catch (error2) {
|
|
14456
|
-
throw new
|
|
14091
|
+
throw new import_node_server_utils69.BadRequestError("Invalid org ID format.");
|
|
14457
14092
|
}
|
|
14458
14093
|
}
|
|
14459
14094
|
if (value.site) {
|
|
14460
14095
|
try {
|
|
14461
|
-
value.site = new
|
|
14096
|
+
value.site = new import_mongodb41.ObjectId(value.site);
|
|
14462
14097
|
} catch (error2) {
|
|
14463
|
-
throw new
|
|
14098
|
+
throw new import_node_server_utils69.BadRequestError("Invalid site ID format.");
|
|
14464
14099
|
}
|
|
14465
14100
|
}
|
|
14466
14101
|
if (value.unit) {
|
|
14467
14102
|
try {
|
|
14468
|
-
value.unit = new
|
|
14103
|
+
value.unit = new import_mongodb41.ObjectId(value.unit);
|
|
14469
14104
|
} catch (error2) {
|
|
14470
|
-
throw new
|
|
14105
|
+
throw new import_node_server_utils69.BadRequestError("Invalid building unit ID format.");
|
|
14471
14106
|
}
|
|
14472
14107
|
}
|
|
14473
14108
|
if (value.peopleId) {
|
|
14474
14109
|
try {
|
|
14475
|
-
value.peopleId = new
|
|
14110
|
+
value.peopleId = new import_mongodb41.ObjectId(value.peopleId);
|
|
14476
14111
|
} catch (error2) {
|
|
14477
|
-
throw new
|
|
14112
|
+
throw new import_node_server_utils69.BadRequestError("Invalid building unit ID format.");
|
|
14478
14113
|
}
|
|
14479
14114
|
}
|
|
14480
14115
|
const createdAtDate = value.createdAt ? new Date(value.createdAt) : /* @__PURE__ */ new Date();
|
|
@@ -14522,20 +14157,20 @@ var schemaVehicleTransaction = import_joi37.default.object({
|
|
|
14522
14157
|
function MVehicleTransaction(value) {
|
|
14523
14158
|
const { error } = schemaVehicleTransaction.validate(value);
|
|
14524
14159
|
if (error) {
|
|
14525
|
-
|
|
14526
|
-
throw new
|
|
14160
|
+
import_node_server_utils69.logger.log({ level: "error", message: error.message });
|
|
14161
|
+
throw new import_node_server_utils69.BadRequestError(error.message);
|
|
14527
14162
|
}
|
|
14528
14163
|
if (value._id) {
|
|
14529
14164
|
try {
|
|
14530
|
-
value._id = new
|
|
14165
|
+
value._id = new import_mongodb41.ObjectId(value._id);
|
|
14531
14166
|
} catch (error2) {
|
|
14532
|
-
throw new
|
|
14167
|
+
throw new import_node_server_utils69.BadRequestError("Invalid ID.");
|
|
14533
14168
|
}
|
|
14534
14169
|
}
|
|
14535
14170
|
try {
|
|
14536
|
-
value.site = new
|
|
14171
|
+
value.site = new import_mongodb41.ObjectId(value.site);
|
|
14537
14172
|
} catch (error2) {
|
|
14538
|
-
throw new
|
|
14173
|
+
throw new import_node_server_utils69.BadRequestError("Invalid site ID.");
|
|
14539
14174
|
}
|
|
14540
14175
|
return {
|
|
14541
14176
|
_id: value._id,
|
|
@@ -14551,23 +14186,15 @@ function MVehicleTransaction(value) {
|
|
|
14551
14186
|
};
|
|
14552
14187
|
}
|
|
14553
14188
|
|
|
14554
|
-
// src/services/dahua.service.ts
|
|
14555
|
-
var import_node_server_utils76 = require("@7365admin1/node-server-utils");
|
|
14556
|
-
var import_fs = require("fs");
|
|
14557
|
-
var import_joi40 = __toESM(require("joi"));
|
|
14558
|
-
var path = __toESM(require("path"));
|
|
14559
|
-
var import_urllib = require("urllib");
|
|
14560
|
-
|
|
14561
14189
|
// src/repositories/vehicle.repo.ts
|
|
14562
|
-
var
|
|
14563
|
-
var import_mongodb43 = require("mongodb");
|
|
14190
|
+
var import_mongodb42 = require("mongodb");
|
|
14564
14191
|
var import_joi38 = __toESM(require("joi"));
|
|
14565
14192
|
var vehicles_namespace_collection = "vehicles";
|
|
14566
14193
|
function useVehicleRepo() {
|
|
14567
14194
|
vehicles_namespace_collection;
|
|
14568
|
-
const db =
|
|
14195
|
+
const db = import_node_server_utils70.useAtlas.getDb();
|
|
14569
14196
|
if (!db) {
|
|
14570
|
-
throw new
|
|
14197
|
+
throw new import_node_server_utils70.InternalServerError("Unable to connect to server.");
|
|
14571
14198
|
}
|
|
14572
14199
|
const collection = db.collection(vehicles_namespace_collection);
|
|
14573
14200
|
async function createIndex() {
|
|
@@ -14578,7 +14205,7 @@ function useVehicleRepo() {
|
|
|
14578
14205
|
{ key: { category: 1 } }
|
|
14579
14206
|
]);
|
|
14580
14207
|
} catch (error) {
|
|
14581
|
-
throw new
|
|
14208
|
+
throw new import_node_server_utils70.InternalServerError("Failed to create index on vehicle.");
|
|
14582
14209
|
}
|
|
14583
14210
|
}
|
|
14584
14211
|
async function createTextIndex() {
|
|
@@ -14592,7 +14219,7 @@ function useVehicleRepo() {
|
|
|
14592
14219
|
nric: "text"
|
|
14593
14220
|
});
|
|
14594
14221
|
} catch (error) {
|
|
14595
|
-
throw new
|
|
14222
|
+
throw new import_node_server_utils70.InternalServerError(
|
|
14596
14223
|
"Failed to create text index on visitor transaction."
|
|
14597
14224
|
);
|
|
14598
14225
|
}
|
|
@@ -14603,11 +14230,11 @@ function useVehicleRepo() {
|
|
|
14603
14230
|
const res = await collection.insertOne(value, { session });
|
|
14604
14231
|
return res.insertedId;
|
|
14605
14232
|
} catch (error) {
|
|
14606
|
-
|
|
14233
|
+
import_node_server_utils70.logger.log({
|
|
14607
14234
|
level: "error",
|
|
14608
14235
|
message: error.message
|
|
14609
14236
|
});
|
|
14610
|
-
if (error instanceof
|
|
14237
|
+
if (error instanceof import_node_server_utils70.AppError) {
|
|
14611
14238
|
throw error;
|
|
14612
14239
|
} else {
|
|
14613
14240
|
throw new Error("Failed to create vehicle.");
|
|
@@ -14788,7 +14415,7 @@ function useVehicleRepo() {
|
|
|
14788
14415
|
const regexCountResult = await collection.aggregate(buildGroupedCountPipeline(regexQuery)).toArray();
|
|
14789
14416
|
length = regexCountResult[0]?.total || 0;
|
|
14790
14417
|
}
|
|
14791
|
-
const data = (0,
|
|
14418
|
+
const data = (0, import_node_server_utils70.paginate)(items, page, limit, length);
|
|
14792
14419
|
return data;
|
|
14793
14420
|
} catch (error) {
|
|
14794
14421
|
throw error;
|
|
@@ -14796,9 +14423,9 @@ function useVehicleRepo() {
|
|
|
14796
14423
|
}
|
|
14797
14424
|
async function getSeasonPassTypes(site) {
|
|
14798
14425
|
try {
|
|
14799
|
-
site = new
|
|
14426
|
+
site = new import_mongodb42.ObjectId(site);
|
|
14800
14427
|
} catch (error) {
|
|
14801
|
-
throw new
|
|
14428
|
+
throw new import_node_server_utils70.BadRequestError("Invalid site ID format.");
|
|
14802
14429
|
}
|
|
14803
14430
|
try {
|
|
14804
14431
|
const categories = await collection.aggregate([
|
|
@@ -14821,17 +14448,17 @@ function useVehicleRepo() {
|
|
|
14821
14448
|
]).toArray();
|
|
14822
14449
|
return categories;
|
|
14823
14450
|
} catch (error) {
|
|
14824
|
-
if (error instanceof
|
|
14451
|
+
if (error instanceof import_node_server_utils70.BadRequestError) {
|
|
14825
14452
|
throw error;
|
|
14826
14453
|
}
|
|
14827
|
-
throw new
|
|
14454
|
+
throw new import_node_server_utils70.BadRequestError("Failed to retrieve season pass types.");
|
|
14828
14455
|
}
|
|
14829
14456
|
}
|
|
14830
14457
|
async function getVehicleById(_id) {
|
|
14831
14458
|
try {
|
|
14832
|
-
_id = new
|
|
14459
|
+
_id = new import_mongodb42.ObjectId(_id);
|
|
14833
14460
|
} catch (error) {
|
|
14834
|
-
throw new
|
|
14461
|
+
throw new import_node_server_utils70.BadRequestError("Invalid vehicle ID format.");
|
|
14835
14462
|
}
|
|
14836
14463
|
try {
|
|
14837
14464
|
const data = await collection.aggregate([
|
|
@@ -15000,7 +14627,7 @@ function useVehicleRepo() {
|
|
|
15000
14627
|
}
|
|
15001
14628
|
]).toArray();
|
|
15002
14629
|
if (!data || !data.length) {
|
|
15003
|
-
throw new
|
|
14630
|
+
throw new import_node_server_utils70.NotFoundError("Vehicle not found.");
|
|
15004
14631
|
}
|
|
15005
14632
|
const result = data[0];
|
|
15006
14633
|
return result;
|
|
@@ -15011,7 +14638,7 @@ function useVehicleRepo() {
|
|
|
15011
14638
|
async function getByPlaceNumber(value) {
|
|
15012
14639
|
const { error } = import_joi38.default.string().required().validate(value);
|
|
15013
14640
|
if (error) {
|
|
15014
|
-
throw new
|
|
14641
|
+
throw new import_node_server_utils70.BadRequestError(error.details[0].message);
|
|
15015
14642
|
}
|
|
15016
14643
|
try {
|
|
15017
14644
|
const data = await collection.findOne({
|
|
@@ -15019,7 +14646,7 @@ function useVehicleRepo() {
|
|
|
15019
14646
|
status: "active"
|
|
15020
14647
|
});
|
|
15021
14648
|
if (!data) {
|
|
15022
|
-
throw new
|
|
14649
|
+
throw new import_node_server_utils70.NotFoundError("Vehicle not found.");
|
|
15023
14650
|
}
|
|
15024
14651
|
return data;
|
|
15025
14652
|
} catch (error2) {
|
|
@@ -15028,9 +14655,9 @@ function useVehicleRepo() {
|
|
|
15028
14655
|
}
|
|
15029
14656
|
async function updateVehicleById(_id, value, session) {
|
|
15030
14657
|
try {
|
|
15031
|
-
_id = new
|
|
14658
|
+
_id = new import_mongodb42.ObjectId(_id);
|
|
15032
14659
|
} catch (error) {
|
|
15033
|
-
throw new
|
|
14660
|
+
throw new import_node_server_utils70.BadRequestError("Invalid vehicle ID format.");
|
|
15034
14661
|
}
|
|
15035
14662
|
try {
|
|
15036
14663
|
const updateValue = {
|
|
@@ -15043,7 +14670,7 @@ function useVehicleRepo() {
|
|
|
15043
14670
|
{ session }
|
|
15044
14671
|
);
|
|
15045
14672
|
if (res.modifiedCount === 0) {
|
|
15046
|
-
throw new
|
|
14673
|
+
throw new import_node_server_utils70.InternalServerError("Unable to update vehicle.");
|
|
15047
14674
|
}
|
|
15048
14675
|
return res.modifiedCount;
|
|
15049
14676
|
} catch (error) {
|
|
@@ -15052,9 +14679,9 @@ function useVehicleRepo() {
|
|
|
15052
14679
|
}
|
|
15053
14680
|
async function deleteVehicle(_id, session) {
|
|
15054
14681
|
try {
|
|
15055
|
-
_id = new
|
|
14682
|
+
_id = new import_mongodb42.ObjectId(_id);
|
|
15056
14683
|
} catch (error) {
|
|
15057
|
-
throw new
|
|
14684
|
+
throw new import_node_server_utils70.BadRequestError("Invalid vehicle ID format.");
|
|
15058
14685
|
}
|
|
15059
14686
|
try {
|
|
15060
14687
|
const updateValue = {
|
|
@@ -15067,7 +14694,7 @@ function useVehicleRepo() {
|
|
|
15067
14694
|
{ session }
|
|
15068
14695
|
);
|
|
15069
14696
|
if (res.modifiedCount === 0)
|
|
15070
|
-
throw new
|
|
14697
|
+
throw new import_node_server_utils70.InternalServerError("Unable to delete vehicle.");
|
|
15071
14698
|
return res.modifiedCount;
|
|
15072
14699
|
} catch (error) {
|
|
15073
14700
|
throw error;
|
|
@@ -15076,7 +14703,7 @@ function useVehicleRepo() {
|
|
|
15076
14703
|
async function getVehicleByPlateNumber(plateNumber) {
|
|
15077
14704
|
const { error } = import_joi38.default.string().required().validate(plateNumber);
|
|
15078
14705
|
if (error) {
|
|
15079
|
-
throw new
|
|
14706
|
+
throw new import_node_server_utils70.BadRequestError(error.details[0].message);
|
|
15080
14707
|
}
|
|
15081
14708
|
try {
|
|
15082
14709
|
const data = await collection.findOne(
|
|
@@ -15085,7 +14712,7 @@ function useVehicleRepo() {
|
|
|
15085
14712
|
);
|
|
15086
14713
|
return data;
|
|
15087
14714
|
} catch (error2) {
|
|
15088
|
-
throw new
|
|
14715
|
+
throw new import_node_server_utils70.InternalServerError(
|
|
15089
14716
|
"Failed to fetch individual by plate number."
|
|
15090
14717
|
);
|
|
15091
14718
|
}
|
|
@@ -15098,7 +14725,7 @@ function useVehicleRepo() {
|
|
|
15098
14725
|
}) {
|
|
15099
14726
|
page = page > 0 ? page - 1 : 0;
|
|
15100
14727
|
if (!nric) {
|
|
15101
|
-
throw new
|
|
14728
|
+
throw new import_node_server_utils70.BadRequestError("NRIC is required.");
|
|
15102
14729
|
}
|
|
15103
14730
|
const _nric = nric.trim();
|
|
15104
14731
|
const query = {
|
|
@@ -15125,7 +14752,7 @@ function useVehicleRepo() {
|
|
|
15125
14752
|
}
|
|
15126
14753
|
]).toArray();
|
|
15127
14754
|
const length = await collection.countDocuments(query);
|
|
15128
|
-
const data = (0,
|
|
14755
|
+
const data = (0, import_node_server_utils70.paginate)(items, page, limit, length);
|
|
15129
14756
|
return data;
|
|
15130
14757
|
} catch (error) {
|
|
15131
14758
|
throw error;
|
|
@@ -15157,7 +14784,7 @@ function useVehicleRepo() {
|
|
|
15157
14784
|
page = page > 0 ? page - 1 : 0;
|
|
15158
14785
|
const skip = page * limit;
|
|
15159
14786
|
try {
|
|
15160
|
-
const unit = (0,
|
|
14787
|
+
const unit = (0, import_node_server_utils70.toObjectId)(unitId);
|
|
15161
14788
|
const query = {
|
|
15162
14789
|
unit
|
|
15163
14790
|
};
|
|
@@ -15172,7 +14799,7 @@ function useVehicleRepo() {
|
|
|
15172
14799
|
limit
|
|
15173
14800
|
}).toArray();
|
|
15174
14801
|
const length = await collection.countDocuments(query);
|
|
15175
|
-
const data = (0,
|
|
14802
|
+
const data = (0, import_node_server_utils70.paginate)(items, page, limit, length);
|
|
15176
14803
|
return data;
|
|
15177
14804
|
} catch (error) {
|
|
15178
14805
|
throw error;
|
|
@@ -15208,7 +14835,7 @@ function useVehicleRepo() {
|
|
|
15208
14835
|
return {
|
|
15209
14836
|
updateOne: {
|
|
15210
14837
|
filter: {
|
|
15211
|
-
site: new
|
|
14838
|
+
site: new import_mongodb42.ObjectId(vehicle.site),
|
|
15212
14839
|
plateNumber,
|
|
15213
14840
|
$or: [
|
|
15214
14841
|
{ deletedAt: "" },
|
|
@@ -15219,9 +14846,9 @@ function useVehicleRepo() {
|
|
|
15219
14846
|
update: {
|
|
15220
14847
|
$set: {
|
|
15221
14848
|
...rest,
|
|
15222
|
-
site: new
|
|
15223
|
-
unit: vehicle.unit ? new
|
|
15224
|
-
org: vehicle.org ? new
|
|
14849
|
+
site: new import_mongodb42.ObjectId(vehicle.site),
|
|
14850
|
+
unit: vehicle.unit ? new import_mongodb42.ObjectId(vehicle.unit) : null,
|
|
14851
|
+
org: vehicle.org ? new import_mongodb42.ObjectId(vehicle.org) : null,
|
|
15225
14852
|
plateNumber,
|
|
15226
14853
|
updatedAt: now
|
|
15227
14854
|
},
|
|
@@ -15241,11 +14868,11 @@ function useVehicleRepo() {
|
|
|
15241
14868
|
upsertedIds: res.upsertedIds
|
|
15242
14869
|
};
|
|
15243
14870
|
} catch (error) {
|
|
15244
|
-
|
|
14871
|
+
import_node_server_utils70.logger.log({
|
|
15245
14872
|
level: "error",
|
|
15246
14873
|
message: error.message
|
|
15247
14874
|
});
|
|
15248
|
-
if (error instanceof
|
|
14875
|
+
if (error instanceof import_node_server_utils70.AppError) {
|
|
15249
14876
|
throw error;
|
|
15250
14877
|
}
|
|
15251
14878
|
throw new Error("Failed to bulk upsert vehicles.");
|
|
@@ -15271,19 +14898,19 @@ function useVehicleRepo() {
|
|
|
15271
14898
|
}
|
|
15272
14899
|
|
|
15273
14900
|
// src/services/vehicle.service.ts
|
|
15274
|
-
var
|
|
14901
|
+
var import_node_server_utils74 = require("@7365admin1/node-server-utils");
|
|
15275
14902
|
|
|
15276
14903
|
// src/repositories/person.repo.ts
|
|
15277
|
-
var
|
|
15278
|
-
var
|
|
14904
|
+
var import_node_server_utils71 = require("@7365admin1/node-server-utils");
|
|
14905
|
+
var import_mongodb43 = require("mongodb");
|
|
15279
14906
|
var site_people_namespace_collection = "site.people";
|
|
15280
14907
|
function usePersonRepo() {
|
|
15281
|
-
const db =
|
|
14908
|
+
const db = import_node_server_utils71.useAtlas.getDb();
|
|
15282
14909
|
if (!db) {
|
|
15283
|
-
throw new
|
|
14910
|
+
throw new import_node_server_utils71.InternalServerError("Unable to connect to server.");
|
|
15284
14911
|
}
|
|
15285
14912
|
const collection = db.collection(site_people_namespace_collection);
|
|
15286
|
-
const { delNamespace, getCache, setCache } = (0,
|
|
14913
|
+
const { delNamespace, getCache, setCache } = (0, import_node_server_utils71.useCache)(
|
|
15287
14914
|
site_people_namespace_collection
|
|
15288
14915
|
);
|
|
15289
14916
|
async function createIndexes() {
|
|
@@ -15293,7 +14920,7 @@ function usePersonRepo() {
|
|
|
15293
14920
|
{ key: { nric: 1 } }
|
|
15294
14921
|
]);
|
|
15295
14922
|
} catch (error) {
|
|
15296
|
-
throw new
|
|
14923
|
+
throw new import_node_server_utils71.InternalServerError("Failed to create index on site people.");
|
|
15297
14924
|
}
|
|
15298
14925
|
}
|
|
15299
14926
|
async function createTextIndex() {
|
|
@@ -15307,7 +14934,7 @@ function usePersonRepo() {
|
|
|
15307
14934
|
contact: "text"
|
|
15308
14935
|
});
|
|
15309
14936
|
} catch (error) {
|
|
15310
|
-
throw new
|
|
14937
|
+
throw new import_node_server_utils71.InternalServerError(
|
|
15311
14938
|
"Failed to create text index on site people."
|
|
15312
14939
|
);
|
|
15313
14940
|
}
|
|
@@ -15317,11 +14944,11 @@ function usePersonRepo() {
|
|
|
15317
14944
|
value = MPerson(value);
|
|
15318
14945
|
const res = await collection.insertOne(value, { session });
|
|
15319
14946
|
delNamespace().then(() => {
|
|
15320
|
-
|
|
14947
|
+
import_node_server_utils71.logger.info(
|
|
15321
14948
|
`Cache cleared for namespace: ${site_people_namespace_collection}`
|
|
15322
14949
|
);
|
|
15323
14950
|
}).catch((err) => {
|
|
15324
|
-
|
|
14951
|
+
import_node_server_utils71.logger.error(
|
|
15325
14952
|
`Failed to clear cache for namespace: ${site_people_namespace_collection}`,
|
|
15326
14953
|
err
|
|
15327
14954
|
);
|
|
@@ -15330,7 +14957,7 @@ function usePersonRepo() {
|
|
|
15330
14957
|
} catch (error) {
|
|
15331
14958
|
const isDuplicated = error.message.includes("duplicate");
|
|
15332
14959
|
if (isDuplicated) {
|
|
15333
|
-
throw new
|
|
14960
|
+
throw new import_node_server_utils71.BadRequestError("Person already exists.");
|
|
15334
14961
|
}
|
|
15335
14962
|
throw error;
|
|
15336
14963
|
}
|
|
@@ -15367,8 +14994,8 @@ function usePersonRepo() {
|
|
|
15367
14994
|
{ "plates.plateNumber": { $regex: search, $options: "i" } }
|
|
15368
14995
|
]
|
|
15369
14996
|
},
|
|
15370
|
-
...
|
|
15371
|
-
...
|
|
14997
|
+
...import_mongodb43.ObjectId.isValid(org) && { org: new import_mongodb43.ObjectId(org) },
|
|
14998
|
+
...import_mongodb43.ObjectId.isValid(site) && { site: new import_mongodb43.ObjectId(site) },
|
|
15372
14999
|
...PERSON_TYPES.includes(type) && { type }
|
|
15373
15000
|
};
|
|
15374
15001
|
sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
|
|
@@ -15384,13 +15011,13 @@ function usePersonRepo() {
|
|
|
15384
15011
|
...query.site && { site: query.site.toString() },
|
|
15385
15012
|
...PERSON_TYPES.includes(type) && { type }
|
|
15386
15013
|
};
|
|
15387
|
-
const cacheKey = (0,
|
|
15014
|
+
const cacheKey = (0, import_node_server_utils71.makeCacheKey)(
|
|
15388
15015
|
site_people_namespace_collection,
|
|
15389
15016
|
cacheOptions
|
|
15390
15017
|
);
|
|
15391
15018
|
const cachedData = await getCache(cacheKey);
|
|
15392
15019
|
if (cachedData) {
|
|
15393
|
-
|
|
15020
|
+
import_node_server_utils71.logger.info(`Cache hit for key: ${cacheKey}`);
|
|
15394
15021
|
return cachedData;
|
|
15395
15022
|
}
|
|
15396
15023
|
try {
|
|
@@ -15422,9 +15049,9 @@ function usePersonRepo() {
|
|
|
15422
15049
|
collection.aggregate([{ $match: query }, { $count: "total" }], { session }).toArray()
|
|
15423
15050
|
]);
|
|
15424
15051
|
const totalCount = countResult[0]?.total || 0;
|
|
15425
|
-
const data = (0,
|
|
15426
|
-
setCache(cacheKey, data, 15 * 60).then(() =>
|
|
15427
|
-
(err) =>
|
|
15052
|
+
const data = (0, import_node_server_utils71.paginate)(items, page, limit, totalCount);
|
|
15053
|
+
setCache(cacheKey, data, 15 * 60).then(() => import_node_server_utils71.logger.info(`Cache set for key: ${cacheKey}`)).catch(
|
|
15054
|
+
(err) => import_node_server_utils71.logger.error(`Failed to set cache for key: ${cacheKey}`, err)
|
|
15428
15055
|
);
|
|
15429
15056
|
return data;
|
|
15430
15057
|
} catch (error) {
|
|
@@ -15433,9 +15060,9 @@ function usePersonRepo() {
|
|
|
15433
15060
|
}
|
|
15434
15061
|
async function updateById(_id, value, session) {
|
|
15435
15062
|
try {
|
|
15436
|
-
_id = new
|
|
15063
|
+
_id = new import_mongodb43.ObjectId(_id);
|
|
15437
15064
|
} catch (error) {
|
|
15438
|
-
throw new
|
|
15065
|
+
throw new import_node_server_utils71.BadRequestError("Invalid person transaction ID format.");
|
|
15439
15066
|
}
|
|
15440
15067
|
value.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
15441
15068
|
try {
|
|
@@ -15445,11 +15072,11 @@ function usePersonRepo() {
|
|
|
15445
15072
|
{ session }
|
|
15446
15073
|
);
|
|
15447
15074
|
delNamespace().then(() => {
|
|
15448
|
-
|
|
15075
|
+
import_node_server_utils71.logger.info(
|
|
15449
15076
|
`Cache cleared for namespace: ${site_people_namespace_collection}`
|
|
15450
15077
|
);
|
|
15451
15078
|
}).catch((err) => {
|
|
15452
|
-
|
|
15079
|
+
import_node_server_utils71.logger.error(
|
|
15453
15080
|
`Failed to clear cache for namespace: ${site_people_namespace_collection}`,
|
|
15454
15081
|
err
|
|
15455
15082
|
);
|
|
@@ -15461,9 +15088,9 @@ function usePersonRepo() {
|
|
|
15461
15088
|
}
|
|
15462
15089
|
async function deleteById(_id) {
|
|
15463
15090
|
try {
|
|
15464
|
-
_id = new
|
|
15091
|
+
_id = new import_mongodb43.ObjectId(_id);
|
|
15465
15092
|
} catch (error) {
|
|
15466
|
-
throw new
|
|
15093
|
+
throw new import_node_server_utils71.BadRequestError("Invalid customer ID format.");
|
|
15467
15094
|
}
|
|
15468
15095
|
try {
|
|
15469
15096
|
const updateValue = {
|
|
@@ -15473,14 +15100,14 @@ function usePersonRepo() {
|
|
|
15473
15100
|
};
|
|
15474
15101
|
const res = await collection.updateOne({ _id }, { $set: updateValue });
|
|
15475
15102
|
if (res.modifiedCount === 0) {
|
|
15476
|
-
throw new
|
|
15103
|
+
throw new import_node_server_utils71.InternalServerError("Unable to delete visitor transaction.");
|
|
15477
15104
|
}
|
|
15478
15105
|
delNamespace().then(() => {
|
|
15479
|
-
|
|
15106
|
+
import_node_server_utils71.logger.info(
|
|
15480
15107
|
`Cache cleared for namespace: ${site_people_namespace_collection}`
|
|
15481
15108
|
);
|
|
15482
15109
|
}).catch((err) => {
|
|
15483
|
-
|
|
15110
|
+
import_node_server_utils71.logger.error(
|
|
15484
15111
|
`Failed to clear cache for namespace: ${site_people_namespace_collection}`,
|
|
15485
15112
|
err
|
|
15486
15113
|
);
|
|
@@ -15492,17 +15119,17 @@ function usePersonRepo() {
|
|
|
15492
15119
|
}
|
|
15493
15120
|
async function getById(_id) {
|
|
15494
15121
|
try {
|
|
15495
|
-
_id = new
|
|
15122
|
+
_id = new import_mongodb43.ObjectId(_id);
|
|
15496
15123
|
} catch (error) {
|
|
15497
|
-
throw new
|
|
15124
|
+
throw new import_node_server_utils71.BadRequestError("Invalid person ID.");
|
|
15498
15125
|
}
|
|
15499
|
-
const cacheKey = (0,
|
|
15126
|
+
const cacheKey = (0, import_node_server_utils71.makeCacheKey)(site_people_namespace_collection, {
|
|
15500
15127
|
_id: String(_id)
|
|
15501
15128
|
});
|
|
15502
15129
|
try {
|
|
15503
15130
|
const cached = await getCache(cacheKey);
|
|
15504
15131
|
if (cached) {
|
|
15505
|
-
|
|
15132
|
+
import_node_server_utils71.logger.log({
|
|
15506
15133
|
level: "info",
|
|
15507
15134
|
message: `Cache hit for getById person: ${cacheKey}`
|
|
15508
15135
|
});
|
|
@@ -15512,34 +15139,34 @@ function usePersonRepo() {
|
|
|
15512
15139
|
_id
|
|
15513
15140
|
});
|
|
15514
15141
|
setCache(cacheKey, result, 300).then(() => {
|
|
15515
|
-
|
|
15142
|
+
import_node_server_utils71.logger.log({
|
|
15516
15143
|
level: "info",
|
|
15517
15144
|
message: `Cache set for person by id: ${cacheKey}`
|
|
15518
15145
|
});
|
|
15519
15146
|
}).catch((err) => {
|
|
15520
|
-
|
|
15147
|
+
import_node_server_utils71.logger.log({
|
|
15521
15148
|
level: "error",
|
|
15522
15149
|
message: `Failed to set cache for person by id: ${err.message}`
|
|
15523
15150
|
});
|
|
15524
15151
|
});
|
|
15525
15152
|
return result;
|
|
15526
15153
|
} catch (error) {
|
|
15527
|
-
if (error instanceof
|
|
15154
|
+
if (error instanceof import_node_server_utils71.AppError) {
|
|
15528
15155
|
throw error;
|
|
15529
15156
|
} else {
|
|
15530
|
-
throw new
|
|
15157
|
+
throw new import_node_server_utils71.InternalServerError("Failed to get person by id.");
|
|
15531
15158
|
}
|
|
15532
15159
|
}
|
|
15533
15160
|
}
|
|
15534
15161
|
async function getPersonByPlateNumber(plateNumber) {
|
|
15535
|
-
const cacheKey = (0,
|
|
15162
|
+
const cacheKey = (0, import_node_server_utils71.makeCacheKey)(site_people_namespace_collection, {
|
|
15536
15163
|
plateNumber,
|
|
15537
15164
|
key: "get-person-plate-number"
|
|
15538
15165
|
});
|
|
15539
15166
|
try {
|
|
15540
15167
|
const cacheData = await getCache(cacheKey);
|
|
15541
15168
|
if (cacheData) {
|
|
15542
|
-
|
|
15169
|
+
import_node_server_utils71.logger.info(`Cache hit for key: ${cacheKey}`);
|
|
15543
15170
|
return cacheData;
|
|
15544
15171
|
}
|
|
15545
15172
|
const data = await collection.findOne(
|
|
@@ -15547,57 +15174,57 @@ function usePersonRepo() {
|
|
|
15547
15174
|
{ sort: { _id: -1 } }
|
|
15548
15175
|
);
|
|
15549
15176
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
15550
|
-
|
|
15177
|
+
import_node_server_utils71.logger.info(`Cache set for key: ${cacheKey}`);
|
|
15551
15178
|
}).catch((err) => {
|
|
15552
|
-
|
|
15179
|
+
import_node_server_utils71.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
15553
15180
|
});
|
|
15554
15181
|
return data;
|
|
15555
15182
|
} catch (error) {
|
|
15556
|
-
throw new
|
|
15183
|
+
throw new import_node_server_utils71.InternalServerError(
|
|
15557
15184
|
"Failed to fetch individual by plate number."
|
|
15558
15185
|
);
|
|
15559
15186
|
}
|
|
15560
15187
|
}
|
|
15561
15188
|
async function getByNRIC(value) {
|
|
15562
15189
|
try {
|
|
15563
|
-
const cacheKey = (0,
|
|
15190
|
+
const cacheKey = (0, import_node_server_utils71.makeCacheKey)(site_people_namespace_collection, {
|
|
15564
15191
|
nric: value
|
|
15565
15192
|
});
|
|
15566
15193
|
const cachedData = await getCache(cacheKey);
|
|
15567
15194
|
if (cachedData) {
|
|
15568
|
-
|
|
15195
|
+
import_node_server_utils71.logger.info(`Cache hit for key: ${cacheKey}`);
|
|
15569
15196
|
return cachedData;
|
|
15570
15197
|
}
|
|
15571
15198
|
const data = await collection.findOne({ nric: value });
|
|
15572
15199
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
15573
|
-
|
|
15200
|
+
import_node_server_utils71.logger.info(`Cache set for key: ${cacheKey}`);
|
|
15574
15201
|
}).catch((err) => {
|
|
15575
|
-
|
|
15202
|
+
import_node_server_utils71.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
15576
15203
|
});
|
|
15577
15204
|
return data;
|
|
15578
15205
|
} catch (error) {
|
|
15579
|
-
throw new
|
|
15206
|
+
throw new import_node_server_utils71.InternalServerError("Failed to retrieve person by NRIC.");
|
|
15580
15207
|
}
|
|
15581
15208
|
}
|
|
15582
15209
|
async function getPersonByPhoneNumber(value) {
|
|
15583
15210
|
try {
|
|
15584
|
-
const cacheKey = (0,
|
|
15211
|
+
const cacheKey = (0, import_node_server_utils71.makeCacheKey)(site_people_namespace_collection, {
|
|
15585
15212
|
contact: value
|
|
15586
15213
|
});
|
|
15587
15214
|
const cachedData = await getCache(cacheKey);
|
|
15588
15215
|
if (cachedData) {
|
|
15589
|
-
|
|
15216
|
+
import_node_server_utils71.logger.info(`Cache hit for key: ${cacheKey}`);
|
|
15590
15217
|
return cachedData;
|
|
15591
15218
|
}
|
|
15592
15219
|
const data = await collection.findOne({ contact: value });
|
|
15593
15220
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
15594
|
-
|
|
15221
|
+
import_node_server_utils71.logger.info(`Cache set for key: ${cacheKey}`);
|
|
15595
15222
|
}).catch((err) => {
|
|
15596
|
-
|
|
15223
|
+
import_node_server_utils71.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
15597
15224
|
});
|
|
15598
15225
|
return data;
|
|
15599
15226
|
} catch (error) {
|
|
15600
|
-
throw new
|
|
15227
|
+
throw new import_node_server_utils71.InternalServerError(
|
|
15601
15228
|
"Failed to retrieve person by Phone Number."
|
|
15602
15229
|
);
|
|
15603
15230
|
}
|
|
@@ -15607,7 +15234,7 @@ function usePersonRepo() {
|
|
|
15607
15234
|
type = [],
|
|
15608
15235
|
unit
|
|
15609
15236
|
}, session) {
|
|
15610
|
-
const cacheKey = (0,
|
|
15237
|
+
const cacheKey = (0, import_node_server_utils71.makeCacheKey)(site_people_namespace_collection, {
|
|
15611
15238
|
unit: JSON.stringify(unit),
|
|
15612
15239
|
status,
|
|
15613
15240
|
type,
|
|
@@ -15615,7 +15242,7 @@ function usePersonRepo() {
|
|
|
15615
15242
|
});
|
|
15616
15243
|
const cacheData = await getCache(cacheKey);
|
|
15617
15244
|
if (cacheData) {
|
|
15618
|
-
|
|
15245
|
+
import_node_server_utils71.logger.info(`Cache hit for key: ${cacheKey}`);
|
|
15619
15246
|
return cacheData;
|
|
15620
15247
|
}
|
|
15621
15248
|
try {
|
|
@@ -15628,25 +15255,25 @@ function usePersonRepo() {
|
|
|
15628
15255
|
};
|
|
15629
15256
|
const data = await collection.find(query).sort({ _id: -1 }).toArray();
|
|
15630
15257
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
15631
|
-
|
|
15258
|
+
import_node_server_utils71.logger.info(`Cache set for key: ${cacheKey}`);
|
|
15632
15259
|
}).catch((err) => {
|
|
15633
|
-
|
|
15260
|
+
import_node_server_utils71.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
15634
15261
|
});
|
|
15635
15262
|
return data;
|
|
15636
15263
|
} catch (error) {
|
|
15637
|
-
throw new
|
|
15264
|
+
throw new import_node_server_utils71.InternalServerError(
|
|
15638
15265
|
"Failed to fetch people type residents by unit."
|
|
15639
15266
|
);
|
|
15640
15267
|
}
|
|
15641
15268
|
}
|
|
15642
15269
|
async function getCompany(search) {
|
|
15643
15270
|
try {
|
|
15644
|
-
const cacheKey = (0,
|
|
15271
|
+
const cacheKey = (0, import_node_server_utils71.makeCacheKey)(site_people_namespace_collection, {
|
|
15645
15272
|
company: search
|
|
15646
15273
|
});
|
|
15647
15274
|
const cachedData = await getCache(cacheKey);
|
|
15648
15275
|
if (cachedData) {
|
|
15649
|
-
|
|
15276
|
+
import_node_server_utils71.logger.info(`Cache hit for key: ${cacheKey}`);
|
|
15650
15277
|
return cachedData;
|
|
15651
15278
|
}
|
|
15652
15279
|
const baseQuery = {
|
|
@@ -15690,24 +15317,24 @@ function usePersonRepo() {
|
|
|
15690
15317
|
]).toArray();
|
|
15691
15318
|
}
|
|
15692
15319
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
15693
|
-
|
|
15320
|
+
import_node_server_utils71.logger.info(`Cache set for key: ${cacheKey}`);
|
|
15694
15321
|
}).catch((err) => {
|
|
15695
|
-
|
|
15322
|
+
import_node_server_utils71.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
15696
15323
|
});
|
|
15697
15324
|
return data;
|
|
15698
15325
|
} catch (error) {
|
|
15699
|
-
throw new
|
|
15326
|
+
throw new import_node_server_utils71.InternalServerError("Failed to fetch visitor by company.");
|
|
15700
15327
|
}
|
|
15701
15328
|
}
|
|
15702
15329
|
async function getPeopleByPlateNumber(plateNumber) {
|
|
15703
|
-
const cacheKey = (0,
|
|
15330
|
+
const cacheKey = (0, import_node_server_utils71.makeCacheKey)(site_people_namespace_collection, {
|
|
15704
15331
|
plateNumber,
|
|
15705
15332
|
key: "get-people-plate-number"
|
|
15706
15333
|
});
|
|
15707
15334
|
try {
|
|
15708
15335
|
const cacheData = await getCache(cacheKey);
|
|
15709
15336
|
if (cacheData) {
|
|
15710
|
-
|
|
15337
|
+
import_node_server_utils71.logger.info(`Cache hit for key: ${cacheKey}`);
|
|
15711
15338
|
return cacheData;
|
|
15712
15339
|
}
|
|
15713
15340
|
const data = await collection.find(
|
|
@@ -15715,13 +15342,13 @@ function usePersonRepo() {
|
|
|
15715
15342
|
{ sort: { _id: -1 } }
|
|
15716
15343
|
).toArray();
|
|
15717
15344
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
15718
|
-
|
|
15345
|
+
import_node_server_utils71.logger.info(`Cache set for key: ${cacheKey}`);
|
|
15719
15346
|
}).catch((err) => {
|
|
15720
|
-
|
|
15347
|
+
import_node_server_utils71.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
15721
15348
|
});
|
|
15722
15349
|
return data;
|
|
15723
15350
|
} catch (error) {
|
|
15724
|
-
throw new
|
|
15351
|
+
throw new import_node_server_utils71.InternalServerError("Failed to fetch people by plate number.");
|
|
15725
15352
|
}
|
|
15726
15353
|
}
|
|
15727
15354
|
async function getPeopleByNRIC({
|
|
@@ -15736,7 +15363,7 @@ function usePersonRepo() {
|
|
|
15736
15363
|
sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
|
|
15737
15364
|
const query = {
|
|
15738
15365
|
...nric && { nric: normalizedNric },
|
|
15739
|
-
site: typeof site === "string" ? new
|
|
15366
|
+
site: typeof site === "string" ? new import_mongodb43.ObjectId(site) : site
|
|
15740
15367
|
};
|
|
15741
15368
|
const cacheOptions = {
|
|
15742
15369
|
site,
|
|
@@ -15746,32 +15373,32 @@ function usePersonRepo() {
|
|
|
15746
15373
|
sort: JSON.stringify(sort)
|
|
15747
15374
|
};
|
|
15748
15375
|
try {
|
|
15749
|
-
const cacheKey = (0,
|
|
15376
|
+
const cacheKey = (0, import_node_server_utils71.makeCacheKey)(
|
|
15750
15377
|
site_people_namespace_collection,
|
|
15751
15378
|
cacheOptions
|
|
15752
15379
|
);
|
|
15753
15380
|
const cachedData = await getCache(cacheKey);
|
|
15754
15381
|
if (cachedData) {
|
|
15755
|
-
|
|
15382
|
+
import_node_server_utils71.logger.info(`Cache hit for key: ${cacheKey}`);
|
|
15756
15383
|
return cachedData;
|
|
15757
15384
|
}
|
|
15758
15385
|
const items = await collection.find(query).sort(sort).skip(page * limit).limit(limit).toArray();
|
|
15759
15386
|
const length = await collection.countDocuments(query);
|
|
15760
|
-
const data = (0,
|
|
15387
|
+
const data = (0, import_node_server_utils71.paginate)(items, page, limit, length);
|
|
15761
15388
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
15762
|
-
|
|
15389
|
+
import_node_server_utils71.logger.info(`Cache set for key: ${cacheKey}`);
|
|
15763
15390
|
}).catch((err) => {
|
|
15764
|
-
|
|
15391
|
+
import_node_server_utils71.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
15765
15392
|
});
|
|
15766
15393
|
return data;
|
|
15767
15394
|
} catch (error) {
|
|
15768
|
-
throw new
|
|
15395
|
+
throw new import_node_server_utils71.InternalServerError("Failed to retrieve person by NRIC.");
|
|
15769
15396
|
}
|
|
15770
15397
|
}
|
|
15771
15398
|
async function pushVehicleById(id, plate, session) {
|
|
15772
15399
|
const { plateNumber, recNo } = plate;
|
|
15773
15400
|
const updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
15774
|
-
const _id = (0,
|
|
15401
|
+
const _id = (0, import_node_server_utils71.toObjectId)(id);
|
|
15775
15402
|
try {
|
|
15776
15403
|
const updateExisting = await collection.updateOne(
|
|
15777
15404
|
{
|
|
@@ -15804,7 +15431,7 @@ function usePersonRepo() {
|
|
|
15804
15431
|
);
|
|
15805
15432
|
}
|
|
15806
15433
|
} catch (error) {
|
|
15807
|
-
throw new
|
|
15434
|
+
throw new import_node_server_utils71.InternalServerError("Failed to push vehicle plates by nric.");
|
|
15808
15435
|
}
|
|
15809
15436
|
}
|
|
15810
15437
|
async function pullVehicleByRecNo(recNo, session) {
|
|
@@ -15825,18 +15452,18 @@ function usePersonRepo() {
|
|
|
15825
15452
|
{ session }
|
|
15826
15453
|
);
|
|
15827
15454
|
} catch (error) {
|
|
15828
|
-
throw new
|
|
15455
|
+
throw new import_node_server_utils71.InternalServerError("Failed to pull vehicle plates by nric.");
|
|
15829
15456
|
}
|
|
15830
15457
|
}
|
|
15831
15458
|
async function getByUserId(userId) {
|
|
15832
|
-
const user = (0,
|
|
15833
|
-
const cacheKey = (0,
|
|
15459
|
+
const user = (0, import_node_server_utils71.toObjectId)(userId);
|
|
15460
|
+
const cacheKey = (0, import_node_server_utils71.makeCacheKey)(site_people_namespace_collection, {
|
|
15834
15461
|
user: userId
|
|
15835
15462
|
});
|
|
15836
15463
|
try {
|
|
15837
15464
|
const cached = await getCache(cacheKey);
|
|
15838
15465
|
if (cached) {
|
|
15839
|
-
|
|
15466
|
+
import_node_server_utils71.logger.log({
|
|
15840
15467
|
level: "info",
|
|
15841
15468
|
message: `Cache hit for getByUserId person: ${cacheKey}`
|
|
15842
15469
|
});
|
|
@@ -15846,31 +15473,31 @@ function usePersonRepo() {
|
|
|
15846
15473
|
user
|
|
15847
15474
|
});
|
|
15848
15475
|
setCache(cacheKey, result, 15 * 60).then(() => {
|
|
15849
|
-
|
|
15476
|
+
import_node_server_utils71.logger.log({
|
|
15850
15477
|
level: "info",
|
|
15851
15478
|
message: `Cache set for person by user: ${cacheKey}`
|
|
15852
15479
|
});
|
|
15853
15480
|
}).catch((err) => {
|
|
15854
|
-
|
|
15481
|
+
import_node_server_utils71.logger.log({
|
|
15855
15482
|
level: "error",
|
|
15856
15483
|
message: `Failed to set cache for person by user: ${err.message}`
|
|
15857
15484
|
});
|
|
15858
15485
|
});
|
|
15859
15486
|
return result;
|
|
15860
15487
|
} catch (error) {
|
|
15861
|
-
if (error instanceof
|
|
15488
|
+
if (error instanceof import_node_server_utils71.AppError) {
|
|
15862
15489
|
throw error;
|
|
15863
15490
|
} else {
|
|
15864
|
-
throw new
|
|
15491
|
+
throw new import_node_server_utils71.InternalServerError("Failed to get person by user.");
|
|
15865
15492
|
}
|
|
15866
15493
|
}
|
|
15867
15494
|
}
|
|
15868
15495
|
async function reviewResidentPerson(_id, value, session) {
|
|
15869
15496
|
value.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
15870
15497
|
try {
|
|
15871
|
-
_id = new
|
|
15498
|
+
_id = new import_mongodb43.ObjectId(_id);
|
|
15872
15499
|
} catch (error) {
|
|
15873
|
-
throw new
|
|
15500
|
+
throw new import_node_server_utils71.BadRequestError("Invalid ID format.");
|
|
15874
15501
|
}
|
|
15875
15502
|
try {
|
|
15876
15503
|
const res = await collection.updateOne(
|
|
@@ -15879,14 +15506,14 @@ function usePersonRepo() {
|
|
|
15879
15506
|
{ session }
|
|
15880
15507
|
);
|
|
15881
15508
|
if (res.modifiedCount === 0) {
|
|
15882
|
-
throw new
|
|
15509
|
+
throw new import_node_server_utils71.InternalServerError(`Unable to ${value.status} person.`);
|
|
15883
15510
|
}
|
|
15884
15511
|
delNamespace().then(() => {
|
|
15885
|
-
|
|
15512
|
+
import_node_server_utils71.logger.info(
|
|
15886
15513
|
`Cache cleared for namespace: ${site_people_namespace_collection}`
|
|
15887
15514
|
);
|
|
15888
15515
|
}).catch((err) => {
|
|
15889
|
-
|
|
15516
|
+
import_node_server_utils71.logger.error(
|
|
15890
15517
|
`Failed to clear cache for namespace: ${site_people_namespace_collection}`,
|
|
15891
15518
|
err
|
|
15892
15519
|
);
|
|
@@ -15919,15 +15546,15 @@ function usePersonRepo() {
|
|
|
15919
15546
|
}
|
|
15920
15547
|
|
|
15921
15548
|
// src/services/vehicle.service.ts
|
|
15922
|
-
var
|
|
15549
|
+
var import_mongodb46 = require("mongodb");
|
|
15923
15550
|
|
|
15924
15551
|
// src/repositories/building-unit.repository.ts
|
|
15925
|
-
var
|
|
15552
|
+
var import_node_server_utils73 = require("@7365admin1/node-server-utils");
|
|
15926
15553
|
|
|
15927
15554
|
// src/models/building.model.ts
|
|
15928
|
-
var
|
|
15555
|
+
var import_node_server_utils72 = require("@7365admin1/node-server-utils");
|
|
15929
15556
|
var import_joi39 = __toESM(require("joi"));
|
|
15930
|
-
var
|
|
15557
|
+
var import_mongodb44 = require("mongodb");
|
|
15931
15558
|
var SortFields = /* @__PURE__ */ ((SortFields2) => {
|
|
15932
15559
|
SortFields2["ID"] = "_id";
|
|
15933
15560
|
SortFields2["CREATED_AT"] = "createdAt";
|
|
@@ -16016,20 +15643,20 @@ var schemaUpdateOptions = import_joi39.default.object({
|
|
|
16016
15643
|
function MBuilding(value) {
|
|
16017
15644
|
const { error } = schemaBuilding.validate(value);
|
|
16018
15645
|
if (error) {
|
|
16019
|
-
|
|
16020
|
-
throw new
|
|
15646
|
+
import_node_server_utils72.logger.info(`Building Model: ${error.message}`);
|
|
15647
|
+
throw new import_node_server_utils72.BadRequestError(error.message);
|
|
16021
15648
|
}
|
|
16022
15649
|
if (value._id && typeof value._id === "string") {
|
|
16023
15650
|
try {
|
|
16024
|
-
value._id = new
|
|
15651
|
+
value._id = new import_mongodb44.ObjectId(value._id);
|
|
16025
15652
|
} catch (error2) {
|
|
16026
|
-
throw new
|
|
15653
|
+
throw new import_node_server_utils72.BadRequestError("Invalid _id format");
|
|
16027
15654
|
}
|
|
16028
15655
|
}
|
|
16029
15656
|
try {
|
|
16030
|
-
value.site = new
|
|
15657
|
+
value.site = new import_mongodb44.ObjectId(value.site);
|
|
16031
15658
|
} catch (error2) {
|
|
16032
|
-
throw new
|
|
15659
|
+
throw new import_node_server_utils72.BadRequestError("Invalid site format");
|
|
16033
15660
|
}
|
|
16034
15661
|
return {
|
|
16035
15662
|
_id: value._id ?? void 0,
|
|
@@ -16048,40 +15675,40 @@ function MBuilding(value) {
|
|
|
16048
15675
|
function MBuildingUnit(value) {
|
|
16049
15676
|
const { error } = schemaBuildingUnit.validate(value);
|
|
16050
15677
|
if (error) {
|
|
16051
|
-
|
|
16052
|
-
throw new
|
|
15678
|
+
import_node_server_utils72.logger.info(`Building Unit Model: ${error.message}`);
|
|
15679
|
+
throw new import_node_server_utils72.BadRequestError(error.message);
|
|
16053
15680
|
}
|
|
16054
15681
|
if (value._id && typeof value._id === "string") {
|
|
16055
15682
|
try {
|
|
16056
|
-
value._id = new
|
|
15683
|
+
value._id = new import_mongodb44.ObjectId(value._id);
|
|
16057
15684
|
} catch (error2) {
|
|
16058
|
-
throw new
|
|
15685
|
+
throw new import_node_server_utils72.BadRequestError("Invalid ID");
|
|
16059
15686
|
}
|
|
16060
15687
|
}
|
|
16061
15688
|
try {
|
|
16062
|
-
value.site = new
|
|
15689
|
+
value.site = new import_mongodb44.ObjectId(value.site);
|
|
16063
15690
|
} catch (error2) {
|
|
16064
|
-
throw new
|
|
15691
|
+
throw new import_node_server_utils72.BadRequestError("Invalid site ID");
|
|
16065
15692
|
}
|
|
16066
15693
|
try {
|
|
16067
|
-
value.building = new
|
|
15694
|
+
value.building = new import_mongodb44.ObjectId(value.building);
|
|
16068
15695
|
} catch (error2) {
|
|
16069
|
-
throw new
|
|
15696
|
+
throw new import_node_server_utils72.BadRequestError("Invalid building ID");
|
|
16070
15697
|
}
|
|
16071
15698
|
if (value.owner && typeof value.owner === "string") {
|
|
16072
15699
|
try {
|
|
16073
|
-
value.owner = new
|
|
15700
|
+
value.owner = new import_mongodb44.ObjectId(value.owner);
|
|
16074
15701
|
} catch (error2) {
|
|
16075
|
-
throw new
|
|
15702
|
+
throw new import_node_server_utils72.BadRequestError("Invalid Owner ID");
|
|
16076
15703
|
}
|
|
16077
15704
|
}
|
|
16078
15705
|
if (value.billing && Array.isArray(value.billing)) {
|
|
16079
15706
|
value.billing = value.billing.map((billing) => {
|
|
16080
15707
|
if (billing._id && typeof billing._id === "string") {
|
|
16081
15708
|
try {
|
|
16082
|
-
billing._id = new
|
|
15709
|
+
billing._id = new import_mongodb44.ObjectId(billing._id);
|
|
16083
15710
|
} catch {
|
|
16084
|
-
throw new
|
|
15711
|
+
throw new import_node_server_utils72.BadRequestError("Invalid billing id format");
|
|
16085
15712
|
}
|
|
16086
15713
|
}
|
|
16087
15714
|
return billing;
|
|
@@ -16113,15 +15740,15 @@ function MBuildingUnit(value) {
|
|
|
16113
15740
|
}
|
|
16114
15741
|
|
|
16115
15742
|
// src/repositories/building-unit.repository.ts
|
|
16116
|
-
var
|
|
15743
|
+
var import_mongodb45 = require("mongodb");
|
|
16117
15744
|
function useBuildingUnitRepo() {
|
|
16118
|
-
const db =
|
|
15745
|
+
const db = import_node_server_utils73.useAtlas.getDb();
|
|
16119
15746
|
if (!db) {
|
|
16120
15747
|
throw new Error("Unable to connect to server.");
|
|
16121
15748
|
}
|
|
16122
15749
|
const namespace_collection = "building-units";
|
|
16123
15750
|
const collection = db.collection(namespace_collection);
|
|
16124
|
-
const { getCache, setCache, delNamespace, delCache } = (0,
|
|
15751
|
+
const { getCache, setCache, delNamespace, delCache } = (0, import_node_server_utils73.useCache)(namespace_collection);
|
|
16125
15752
|
async function createIndexes() {
|
|
16126
15753
|
try {
|
|
16127
15754
|
await collection.createIndexes([
|
|
@@ -16145,12 +15772,12 @@ function useBuildingUnitRepo() {
|
|
|
16145
15772
|
}
|
|
16146
15773
|
function delCachedData() {
|
|
16147
15774
|
delNamespace().then(() => {
|
|
16148
|
-
|
|
15775
|
+
import_node_server_utils73.logger.log({
|
|
16149
15776
|
level: "info",
|
|
16150
15777
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
16151
15778
|
});
|
|
16152
15779
|
}).catch((err) => {
|
|
16153
|
-
|
|
15780
|
+
import_node_server_utils73.logger.log({
|
|
16154
15781
|
level: "error",
|
|
16155
15782
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
16156
15783
|
});
|
|
@@ -16175,15 +15802,15 @@ function useBuildingUnitRepo() {
|
|
|
16175
15802
|
delCachedData();
|
|
16176
15803
|
return res.insertedId;
|
|
16177
15804
|
} catch (error) {
|
|
16178
|
-
|
|
15805
|
+
import_node_server_utils73.logger.log({
|
|
16179
15806
|
level: "error",
|
|
16180
15807
|
message: error.message
|
|
16181
15808
|
});
|
|
16182
15809
|
const isDuplicated = error.message.includes("duplicate");
|
|
16183
15810
|
if (isDuplicated) {
|
|
16184
|
-
throw new
|
|
15811
|
+
throw new import_node_server_utils73.BadRequestError("Building unit already exists.");
|
|
16185
15812
|
}
|
|
16186
|
-
if (error instanceof
|
|
15813
|
+
if (error instanceof import_node_server_utils73.AppError) {
|
|
16187
15814
|
throw error;
|
|
16188
15815
|
} else {
|
|
16189
15816
|
throw new Error("Failed to create building unit.");
|
|
@@ -16193,27 +15820,27 @@ function useBuildingUnitRepo() {
|
|
|
16193
15820
|
async function updateById(_id, value, session) {
|
|
16194
15821
|
const { error } = schemaUpdateOptions.validate(value);
|
|
16195
15822
|
if (error) {
|
|
16196
|
-
throw new
|
|
15823
|
+
throw new import_node_server_utils73.BadRequestError(error.message);
|
|
16197
15824
|
}
|
|
16198
15825
|
try {
|
|
16199
|
-
_id = new
|
|
15826
|
+
_id = new import_mongodb45.ObjectId(_id);
|
|
16200
15827
|
} catch (error2) {
|
|
16201
|
-
throw new
|
|
15828
|
+
throw new import_node_server_utils73.BadRequestError("Invalid ID.");
|
|
16202
15829
|
}
|
|
16203
15830
|
if (value.billing && Array.isArray(value.billing)) {
|
|
16204
15831
|
value.billing = value.billing.map((billing) => {
|
|
16205
15832
|
if (billing._id && typeof billing._id === "string") {
|
|
16206
15833
|
try {
|
|
16207
|
-
billing._id = new
|
|
15834
|
+
billing._id = new import_mongodb45.ObjectId(billing._id);
|
|
16208
15835
|
} catch {
|
|
16209
|
-
throw new
|
|
15836
|
+
throw new import_node_server_utils73.BadRequestError("Invalid billing id format");
|
|
16210
15837
|
}
|
|
16211
15838
|
}
|
|
16212
15839
|
return billing;
|
|
16213
15840
|
});
|
|
16214
15841
|
}
|
|
16215
15842
|
if (value.owner) {
|
|
16216
|
-
value.owner = new
|
|
15843
|
+
value.owner = new import_mongodb45.ObjectId(value.owner);
|
|
16217
15844
|
}
|
|
16218
15845
|
try {
|
|
16219
15846
|
const res = await collection.updateOne(
|
|
@@ -16224,15 +15851,15 @@ function useBuildingUnitRepo() {
|
|
|
16224
15851
|
delCachedData();
|
|
16225
15852
|
return res;
|
|
16226
15853
|
} catch (error2) {
|
|
16227
|
-
|
|
15854
|
+
import_node_server_utils73.logger.log({
|
|
16228
15855
|
level: "error",
|
|
16229
15856
|
message: error2.message
|
|
16230
15857
|
});
|
|
16231
15858
|
const isDuplicated = error2.message.includes("duplicate");
|
|
16232
15859
|
if (isDuplicated) {
|
|
16233
|
-
throw new
|
|
15860
|
+
throw new import_node_server_utils73.BadRequestError("Building unit already exists.");
|
|
16234
15861
|
}
|
|
16235
|
-
if (error2 instanceof
|
|
15862
|
+
if (error2 instanceof import_node_server_utils73.AppError) {
|
|
16236
15863
|
throw error2;
|
|
16237
15864
|
} else {
|
|
16238
15865
|
throw new Error("Failed to create building unit.");
|
|
@@ -16242,12 +15869,12 @@ function useBuildingUnitRepo() {
|
|
|
16242
15869
|
async function updateByBuildingId(building, value, session) {
|
|
16243
15870
|
const { error } = schemaUpdateOptions.validate(value);
|
|
16244
15871
|
if (error) {
|
|
16245
|
-
throw new
|
|
15872
|
+
throw new import_node_server_utils73.BadRequestError(error.message);
|
|
16246
15873
|
}
|
|
16247
15874
|
try {
|
|
16248
|
-
building = new
|
|
15875
|
+
building = new import_mongodb45.ObjectId(building);
|
|
16249
15876
|
} catch (error2) {
|
|
16250
|
-
throw new
|
|
15877
|
+
throw new import_node_server_utils73.BadRequestError("Invalid building ID.");
|
|
16251
15878
|
}
|
|
16252
15879
|
try {
|
|
16253
15880
|
const res = await collection.updateMany(
|
|
@@ -16258,15 +15885,15 @@ function useBuildingUnitRepo() {
|
|
|
16258
15885
|
delCachedData();
|
|
16259
15886
|
return res;
|
|
16260
15887
|
} catch (error2) {
|
|
16261
|
-
|
|
15888
|
+
import_node_server_utils73.logger.log({
|
|
16262
15889
|
level: "error",
|
|
16263
15890
|
message: error2.message
|
|
16264
15891
|
});
|
|
16265
15892
|
const isDuplicated = error2.message.includes("duplicate");
|
|
16266
15893
|
if (isDuplicated) {
|
|
16267
|
-
throw new
|
|
15894
|
+
throw new import_node_server_utils73.BadRequestError("Building unit already exists.");
|
|
16268
15895
|
}
|
|
16269
|
-
if (error2 instanceof
|
|
15896
|
+
if (error2 instanceof import_node_server_utils73.AppError) {
|
|
16270
15897
|
throw error2;
|
|
16271
15898
|
} else {
|
|
16272
15899
|
throw new Error("Failed to create building unit.");
|
|
@@ -16286,8 +15913,8 @@ function useBuildingUnitRepo() {
|
|
|
16286
15913
|
const query = {
|
|
16287
15914
|
status,
|
|
16288
15915
|
...search && { $text: { $search: search } },
|
|
16289
|
-
...site && { site: (0,
|
|
16290
|
-
...building && { building: (0,
|
|
15916
|
+
...site && { site: (0, import_node_server_utils73.toObjectId)(site) },
|
|
15917
|
+
...building && { building: (0, import_node_server_utils73.toObjectId)(building) }
|
|
16291
15918
|
};
|
|
16292
15919
|
sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
|
|
16293
15920
|
const cacheParams = {
|
|
@@ -16299,15 +15926,15 @@ function useBuildingUnitRepo() {
|
|
|
16299
15926
|
...building && { building },
|
|
16300
15927
|
...status && { status }
|
|
16301
15928
|
};
|
|
16302
|
-
const cacheKey = (0,
|
|
16303
|
-
|
|
15929
|
+
const cacheKey = (0, import_node_server_utils73.makeCacheKey)(namespace_collection, cacheParams);
|
|
15930
|
+
import_node_server_utils73.logger.log({
|
|
16304
15931
|
level: "info",
|
|
16305
15932
|
message: `Cache key for getAll building units: ${cacheKey}`
|
|
16306
15933
|
});
|
|
16307
15934
|
try {
|
|
16308
15935
|
const cached = await getCache(cacheKey);
|
|
16309
15936
|
if (cached) {
|
|
16310
|
-
|
|
15937
|
+
import_node_server_utils73.logger.log({
|
|
16311
15938
|
level: "info",
|
|
16312
15939
|
message: `Cache hit for getAll building units: ${cacheKey}`
|
|
16313
15940
|
});
|
|
@@ -16346,35 +15973,35 @@ function useBuildingUnitRepo() {
|
|
|
16346
15973
|
{ $limit: limit }
|
|
16347
15974
|
]).toArray();
|
|
16348
15975
|
const length = await collection.countDocuments(query);
|
|
16349
|
-
const data = (0,
|
|
15976
|
+
const data = (0, import_node_server_utils73.paginate)(items, page, limit, length);
|
|
16350
15977
|
setCache(cacheKey, data, 600).then(() => {
|
|
16351
|
-
|
|
15978
|
+
import_node_server_utils73.logger.log({
|
|
16352
15979
|
level: "info",
|
|
16353
15980
|
message: `Cache set for getAll building units: ${cacheKey}`
|
|
16354
15981
|
});
|
|
16355
15982
|
}).catch((err) => {
|
|
16356
|
-
|
|
15983
|
+
import_node_server_utils73.logger.log({
|
|
16357
15984
|
level: "error",
|
|
16358
15985
|
message: `Failed to set cache for getAll building units: ${err.message}`
|
|
16359
15986
|
});
|
|
16360
15987
|
});
|
|
16361
15988
|
return data;
|
|
16362
15989
|
} catch (error) {
|
|
16363
|
-
|
|
15990
|
+
import_node_server_utils73.logger.log({ level: "error", message: `${error}` });
|
|
16364
15991
|
throw error;
|
|
16365
15992
|
}
|
|
16366
15993
|
}
|
|
16367
15994
|
async function getById(_id, session) {
|
|
16368
15995
|
try {
|
|
16369
|
-
_id = new
|
|
15996
|
+
_id = new import_mongodb45.ObjectId(_id);
|
|
16370
15997
|
} catch (error) {
|
|
16371
|
-
throw new
|
|
15998
|
+
throw new import_node_server_utils73.BadRequestError("Invalid ID.");
|
|
16372
15999
|
}
|
|
16373
|
-
const cacheKey = (0,
|
|
16000
|
+
const cacheKey = (0, import_node_server_utils73.makeCacheKey)(namespace_collection, { _id: String(_id) });
|
|
16374
16001
|
try {
|
|
16375
16002
|
const cached = await getCache(cacheKey);
|
|
16376
16003
|
if (cached) {
|
|
16377
|
-
|
|
16004
|
+
import_node_server_utils73.logger.log({
|
|
16378
16005
|
level: "info",
|
|
16379
16006
|
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
16380
16007
|
});
|
|
@@ -16385,42 +16012,42 @@ function useBuildingUnitRepo() {
|
|
|
16385
16012
|
deletedAt: { $in: ["", null] }
|
|
16386
16013
|
});
|
|
16387
16014
|
if (!result) {
|
|
16388
|
-
throw new
|
|
16015
|
+
throw new import_node_server_utils73.BadRequestError("Building unit not found.");
|
|
16389
16016
|
}
|
|
16390
16017
|
setCache(cacheKey, result, 300).then(() => {
|
|
16391
|
-
|
|
16018
|
+
import_node_server_utils73.logger.log({
|
|
16392
16019
|
level: "info",
|
|
16393
16020
|
message: `Cache set for building unit by id: ${cacheKey}`
|
|
16394
16021
|
});
|
|
16395
16022
|
}).catch((err) => {
|
|
16396
|
-
|
|
16023
|
+
import_node_server_utils73.logger.log({
|
|
16397
16024
|
level: "error",
|
|
16398
16025
|
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
16399
16026
|
});
|
|
16400
16027
|
});
|
|
16401
16028
|
return result;
|
|
16402
16029
|
} catch (error) {
|
|
16403
|
-
if (error instanceof
|
|
16030
|
+
if (error instanceof import_node_server_utils73.AppError) {
|
|
16404
16031
|
throw error;
|
|
16405
16032
|
} else {
|
|
16406
|
-
throw new
|
|
16033
|
+
throw new import_node_server_utils73.InternalServerError("Failed to get building unit.");
|
|
16407
16034
|
}
|
|
16408
16035
|
}
|
|
16409
16036
|
}
|
|
16410
16037
|
async function getByBuildingLevel(building, level) {
|
|
16411
16038
|
try {
|
|
16412
|
-
building = new
|
|
16039
|
+
building = new import_mongodb45.ObjectId(building);
|
|
16413
16040
|
} catch (error) {
|
|
16414
|
-
throw new
|
|
16041
|
+
throw new import_node_server_utils73.BadRequestError("Invalid building ID.");
|
|
16415
16042
|
}
|
|
16416
|
-
const cacheKey = (0,
|
|
16043
|
+
const cacheKey = (0, import_node_server_utils73.makeCacheKey)(namespace_collection, {
|
|
16417
16044
|
building: String(building),
|
|
16418
16045
|
level
|
|
16419
16046
|
});
|
|
16420
16047
|
try {
|
|
16421
16048
|
const cached = await getCache(cacheKey);
|
|
16422
16049
|
if (cached) {
|
|
16423
|
-
|
|
16050
|
+
import_node_server_utils73.logger.log({
|
|
16424
16051
|
level: "info",
|
|
16425
16052
|
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
16426
16053
|
});
|
|
@@ -16432,30 +16059,30 @@ function useBuildingUnitRepo() {
|
|
|
16432
16059
|
status: "active"
|
|
16433
16060
|
});
|
|
16434
16061
|
setCache(cacheKey, result, 300).then(() => {
|
|
16435
|
-
|
|
16062
|
+
import_node_server_utils73.logger.log({
|
|
16436
16063
|
level: "info",
|
|
16437
16064
|
message: `Cache set for building unit by id: ${cacheKey}`
|
|
16438
16065
|
});
|
|
16439
16066
|
}).catch((err) => {
|
|
16440
|
-
|
|
16067
|
+
import_node_server_utils73.logger.log({
|
|
16441
16068
|
level: "error",
|
|
16442
16069
|
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
16443
16070
|
});
|
|
16444
16071
|
});
|
|
16445
16072
|
return result;
|
|
16446
16073
|
} catch (error) {
|
|
16447
|
-
if (error instanceof
|
|
16074
|
+
if (error instanceof import_node_server_utils73.AppError) {
|
|
16448
16075
|
throw error;
|
|
16449
16076
|
} else {
|
|
16450
|
-
throw new
|
|
16077
|
+
throw new import_node_server_utils73.InternalServerError("Failed to get building unit.");
|
|
16451
16078
|
}
|
|
16452
16079
|
}
|
|
16453
16080
|
}
|
|
16454
16081
|
async function updateLevelByBuildingLevel(building, level, newLevel, session) {
|
|
16455
16082
|
try {
|
|
16456
|
-
building = new
|
|
16083
|
+
building = new import_mongodb45.ObjectId(building);
|
|
16457
16084
|
} catch (error) {
|
|
16458
|
-
throw new
|
|
16085
|
+
throw new import_node_server_utils73.BadRequestError("Invalid building ID.");
|
|
16459
16086
|
}
|
|
16460
16087
|
try {
|
|
16461
16088
|
const result = await collection.updateMany(
|
|
@@ -16474,26 +16101,26 @@ function useBuildingUnitRepo() {
|
|
|
16474
16101
|
delCachedData();
|
|
16475
16102
|
return result;
|
|
16476
16103
|
} catch (error) {
|
|
16477
|
-
if (error instanceof
|
|
16104
|
+
if (error instanceof import_node_server_utils73.AppError) {
|
|
16478
16105
|
throw error;
|
|
16479
16106
|
} else {
|
|
16480
|
-
throw new
|
|
16107
|
+
throw new import_node_server_utils73.InternalServerError("Failed to update building unit level.");
|
|
16481
16108
|
}
|
|
16482
16109
|
}
|
|
16483
16110
|
}
|
|
16484
16111
|
async function getByBuilding(building) {
|
|
16485
16112
|
try {
|
|
16486
|
-
building = new
|
|
16113
|
+
building = new import_mongodb45.ObjectId(building);
|
|
16487
16114
|
} catch (error) {
|
|
16488
|
-
throw new
|
|
16115
|
+
throw new import_node_server_utils73.BadRequestError("Invalid building ID.");
|
|
16489
16116
|
}
|
|
16490
|
-
const cacheKey = (0,
|
|
16117
|
+
const cacheKey = (0, import_node_server_utils73.makeCacheKey)(namespace_collection, {
|
|
16491
16118
|
building: String(building)
|
|
16492
16119
|
});
|
|
16493
16120
|
try {
|
|
16494
16121
|
const cached = await getCache(cacheKey);
|
|
16495
16122
|
if (cached) {
|
|
16496
|
-
|
|
16123
|
+
import_node_server_utils73.logger.log({
|
|
16497
16124
|
level: "info",
|
|
16498
16125
|
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
16499
16126
|
});
|
|
@@ -16504,30 +16131,30 @@ function useBuildingUnitRepo() {
|
|
|
16504
16131
|
status: "active"
|
|
16505
16132
|
});
|
|
16506
16133
|
setCache(cacheKey, result, 300).then(() => {
|
|
16507
|
-
|
|
16134
|
+
import_node_server_utils73.logger.log({
|
|
16508
16135
|
level: "info",
|
|
16509
16136
|
message: `Cache set for building unit by id: ${cacheKey}`
|
|
16510
16137
|
});
|
|
16511
16138
|
}).catch((err) => {
|
|
16512
|
-
|
|
16139
|
+
import_node_server_utils73.logger.log({
|
|
16513
16140
|
level: "error",
|
|
16514
16141
|
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
16515
16142
|
});
|
|
16516
16143
|
});
|
|
16517
16144
|
return result;
|
|
16518
16145
|
} catch (error) {
|
|
16519
|
-
if (error instanceof
|
|
16146
|
+
if (error instanceof import_node_server_utils73.AppError) {
|
|
16520
16147
|
throw error;
|
|
16521
16148
|
} else {
|
|
16522
|
-
throw new
|
|
16149
|
+
throw new import_node_server_utils73.InternalServerError("Failed to get building unit.");
|
|
16523
16150
|
}
|
|
16524
16151
|
}
|
|
16525
16152
|
}
|
|
16526
16153
|
async function deleteById(_id, session) {
|
|
16527
16154
|
try {
|
|
16528
|
-
_id = new
|
|
16155
|
+
_id = new import_mongodb45.ObjectId(_id);
|
|
16529
16156
|
} catch (error) {
|
|
16530
|
-
throw new
|
|
16157
|
+
throw new import_node_server_utils73.BadRequestError("Invalid ID.");
|
|
16531
16158
|
}
|
|
16532
16159
|
try {
|
|
16533
16160
|
const res = await collection.updateOne(
|
|
@@ -16538,11 +16165,11 @@ function useBuildingUnitRepo() {
|
|
|
16538
16165
|
delCachedData();
|
|
16539
16166
|
return "Room/Facility deleted successfully.";
|
|
16540
16167
|
} catch (error) {
|
|
16541
|
-
|
|
16168
|
+
import_node_server_utils73.logger.log({
|
|
16542
16169
|
level: "error",
|
|
16543
16170
|
message: error.message
|
|
16544
16171
|
});
|
|
16545
|
-
if (error instanceof
|
|
16172
|
+
if (error instanceof import_node_server_utils73.AppError) {
|
|
16546
16173
|
throw error;
|
|
16547
16174
|
} else {
|
|
16548
16175
|
throw new Error("Failed to deleted room/facility.");
|
|
@@ -16551,9 +16178,9 @@ function useBuildingUnitRepo() {
|
|
|
16551
16178
|
}
|
|
16552
16179
|
async function getBuildingUnits(site, block, level) {
|
|
16553
16180
|
try {
|
|
16554
|
-
site = new
|
|
16181
|
+
site = new import_mongodb45.ObjectId(site);
|
|
16555
16182
|
} catch (error) {
|
|
16556
|
-
throw new
|
|
16183
|
+
throw new import_node_server_utils73.BadRequestError("Invalid site ID format.");
|
|
16557
16184
|
}
|
|
16558
16185
|
const query = { site, block, level };
|
|
16559
16186
|
const cacheOptions = {
|
|
@@ -16561,33 +16188,33 @@ function useBuildingUnitRepo() {
|
|
|
16561
16188
|
block,
|
|
16562
16189
|
level
|
|
16563
16190
|
};
|
|
16564
|
-
const cacheKey = (0,
|
|
16191
|
+
const cacheKey = (0, import_node_server_utils73.makeCacheKey)(namespace_collection, cacheOptions);
|
|
16565
16192
|
const cachedData = await getCache(cacheKey);
|
|
16566
16193
|
if (cachedData) {
|
|
16567
|
-
|
|
16194
|
+
import_node_server_utils73.logger.info(`Cache hit for key: ${cacheKey}`);
|
|
16568
16195
|
return cachedData;
|
|
16569
16196
|
}
|
|
16570
16197
|
try {
|
|
16571
16198
|
const data = await collection.aggregate([{ $match: query }, { $project: { name: 1 } }]).toArray();
|
|
16572
16199
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
16573
|
-
|
|
16200
|
+
import_node_server_utils73.logger.info(`Cache set for key: ${cacheKey}`);
|
|
16574
16201
|
}).catch((err) => {
|
|
16575
|
-
|
|
16202
|
+
import_node_server_utils73.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
16576
16203
|
});
|
|
16577
16204
|
return data;
|
|
16578
16205
|
} catch (error) {
|
|
16579
|
-
if (error instanceof
|
|
16206
|
+
if (error instanceof import_node_server_utils73.AppError) {
|
|
16580
16207
|
throw error;
|
|
16581
16208
|
} else {
|
|
16582
|
-
throw new
|
|
16209
|
+
throw new import_node_server_utils73.InternalServerError("Failed to get building units.");
|
|
16583
16210
|
}
|
|
16584
16211
|
}
|
|
16585
16212
|
}
|
|
16586
16213
|
async function getBuildingUnitsWithOwner(site, unitIds) {
|
|
16587
16214
|
try {
|
|
16588
|
-
site = new
|
|
16215
|
+
site = new import_mongodb45.ObjectId(site);
|
|
16589
16216
|
} catch (error) {
|
|
16590
|
-
throw new
|
|
16217
|
+
throw new import_node_server_utils73.BadRequestError("Invalid site ID format.");
|
|
16591
16218
|
}
|
|
16592
16219
|
const query = {
|
|
16593
16220
|
site,
|
|
@@ -16599,25 +16226,25 @@ function useBuildingUnitRepo() {
|
|
|
16599
16226
|
const cacheOptions = {
|
|
16600
16227
|
site: site.toString()
|
|
16601
16228
|
};
|
|
16602
|
-
const cacheKey = (0,
|
|
16229
|
+
const cacheKey = (0, import_node_server_utils73.makeCacheKey)(namespace_collection, cacheOptions);
|
|
16603
16230
|
const cachedData = await getCache(cacheKey);
|
|
16604
16231
|
if (cachedData) {
|
|
16605
|
-
|
|
16232
|
+
import_node_server_utils73.logger.info(`Cache hit for key: ${cacheKey}`);
|
|
16606
16233
|
return cachedData;
|
|
16607
16234
|
}
|
|
16608
16235
|
try {
|
|
16609
16236
|
const data = await collection.aggregate([{ $match: query }]).toArray();
|
|
16610
16237
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
16611
|
-
|
|
16238
|
+
import_node_server_utils73.logger.info(`Cache set for key: ${cacheKey}`);
|
|
16612
16239
|
}).catch((err) => {
|
|
16613
|
-
|
|
16240
|
+
import_node_server_utils73.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
16614
16241
|
});
|
|
16615
16242
|
return data;
|
|
16616
16243
|
} catch (error) {
|
|
16617
|
-
if (error instanceof
|
|
16244
|
+
if (error instanceof import_node_server_utils73.AppError) {
|
|
16618
16245
|
throw error;
|
|
16619
16246
|
} else {
|
|
16620
|
-
throw new
|
|
16247
|
+
throw new import_node_server_utils73.InternalServerError(
|
|
16621
16248
|
"Failed to get building units with owner."
|
|
16622
16249
|
);
|
|
16623
16250
|
}
|
|
@@ -16626,9 +16253,9 @@ function useBuildingUnitRepo() {
|
|
|
16626
16253
|
async function getUnitByBlockLevelUnitNumber(block, level, unitNumber, site, session) {
|
|
16627
16254
|
let _site;
|
|
16628
16255
|
try {
|
|
16629
|
-
_site = new
|
|
16256
|
+
_site = new import_mongodb45.ObjectId(site);
|
|
16630
16257
|
} catch (error) {
|
|
16631
|
-
throw new
|
|
16258
|
+
throw new import_node_server_utils73.BadRequestError("Invalid ID.");
|
|
16632
16259
|
}
|
|
16633
16260
|
const cacheOptions = {
|
|
16634
16261
|
block,
|
|
@@ -16647,11 +16274,11 @@ function useBuildingUnitRepo() {
|
|
|
16647
16274
|
{ deletedAt: "" }
|
|
16648
16275
|
]
|
|
16649
16276
|
};
|
|
16650
|
-
const cacheKey = (0,
|
|
16277
|
+
const cacheKey = (0, import_node_server_utils73.makeCacheKey)(namespace_collection, cacheOptions);
|
|
16651
16278
|
try {
|
|
16652
16279
|
const cached = await getCache(cacheKey);
|
|
16653
16280
|
if (cached) {
|
|
16654
|
-
|
|
16281
|
+
import_node_server_utils73.logger.log({
|
|
16655
16282
|
level: "info",
|
|
16656
16283
|
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
16657
16284
|
});
|
|
@@ -16661,25 +16288,25 @@ function useBuildingUnitRepo() {
|
|
|
16661
16288
|
session
|
|
16662
16289
|
});
|
|
16663
16290
|
if (!result) {
|
|
16664
|
-
throw new
|
|
16291
|
+
throw new import_node_server_utils73.BadRequestError("Building unit not found.");
|
|
16665
16292
|
}
|
|
16666
16293
|
setCache(cacheKey, result, 300).then(() => {
|
|
16667
|
-
|
|
16294
|
+
import_node_server_utils73.logger.log({
|
|
16668
16295
|
level: "info",
|
|
16669
16296
|
message: `Cache set for building unit by id: ${cacheKey}`
|
|
16670
16297
|
});
|
|
16671
16298
|
}).catch((err) => {
|
|
16672
|
-
|
|
16299
|
+
import_node_server_utils73.logger.log({
|
|
16673
16300
|
level: "error",
|
|
16674
16301
|
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
16675
16302
|
});
|
|
16676
16303
|
});
|
|
16677
16304
|
return result;
|
|
16678
16305
|
} catch (error) {
|
|
16679
|
-
if (error instanceof
|
|
16306
|
+
if (error instanceof import_node_server_utils73.AppError) {
|
|
16680
16307
|
throw error;
|
|
16681
16308
|
} else {
|
|
16682
|
-
throw new
|
|
16309
|
+
throw new import_node_server_utils73.InternalServerError("Failed to get building unit.");
|
|
16683
16310
|
}
|
|
16684
16311
|
}
|
|
16685
16312
|
}
|
|
@@ -16748,7 +16375,7 @@ function useVehicleService() {
|
|
|
16748
16375
|
async function add(value, session) {
|
|
16749
16376
|
const isExternalSession = !!session;
|
|
16750
16377
|
if (!session) {
|
|
16751
|
-
session = await
|
|
16378
|
+
session = await import_node_server_utils74.useAtlas.getClient()?.startSession();
|
|
16752
16379
|
if (!session) {
|
|
16753
16380
|
throw new Error("Unable to start session for vehicle service.");
|
|
16754
16381
|
}
|
|
@@ -16759,11 +16386,11 @@ function useVehicleService() {
|
|
|
16759
16386
|
_getById(value.org)
|
|
16760
16387
|
]);
|
|
16761
16388
|
if (!org)
|
|
16762
|
-
throw new
|
|
16389
|
+
throw new import_node_server_utils74.BadRequestError("Org not found");
|
|
16763
16390
|
if (existingPlateNumber)
|
|
16764
|
-
throw new
|
|
16391
|
+
throw new import_node_server_utils74.BadRequestError("Vehicle plate number already exists");
|
|
16765
16392
|
if (!Object.values(OrgNature).includes(org.nature)) {
|
|
16766
|
-
throw new
|
|
16393
|
+
throw new import_node_server_utils74.BadRequestError(
|
|
16767
16394
|
"This organization is not allowed to add vehicles."
|
|
16768
16395
|
);
|
|
16769
16396
|
}
|
|
@@ -16830,7 +16457,7 @@ function useVehicleService() {
|
|
|
16830
16457
|
page++;
|
|
16831
16458
|
} while (page <= pages);
|
|
16832
16459
|
if (!siteCameras.length) {
|
|
16833
|
-
throw new
|
|
16460
|
+
throw new import_node_server_utils74.BadRequestError("No site cameras found.");
|
|
16834
16461
|
}
|
|
16835
16462
|
}
|
|
16836
16463
|
for (const plateNumber of plateNumbers) {
|
|
@@ -16866,7 +16493,7 @@ function useVehicleService() {
|
|
|
16866
16493
|
};
|
|
16867
16494
|
const dahuaResponse = await _addPlateNumber(dahuaPayload);
|
|
16868
16495
|
if (dahuaResponse?.statusCode !== 200) {
|
|
16869
|
-
throw new
|
|
16496
|
+
throw new import_node_server_utils74.BadRequestError(
|
|
16870
16497
|
`Failed to add plate number to ANPR ${_type}`
|
|
16871
16498
|
);
|
|
16872
16499
|
}
|
|
@@ -16887,7 +16514,7 @@ function useVehicleService() {
|
|
|
16887
16514
|
};
|
|
16888
16515
|
const dahuaResponse = await _updatePlateNumber(dahuaPayload);
|
|
16889
16516
|
if (dahuaResponse?.statusCode !== 200) {
|
|
16890
|
-
throw new
|
|
16517
|
+
throw new import_node_server_utils74.BadRequestError(
|
|
16891
16518
|
`Failed to update plate number to ANPR ${_type}`
|
|
16892
16519
|
);
|
|
16893
16520
|
}
|
|
@@ -16913,7 +16540,7 @@ function useVehicleService() {
|
|
|
16913
16540
|
}
|
|
16914
16541
|
return message;
|
|
16915
16542
|
} catch (error) {
|
|
16916
|
-
|
|
16543
|
+
import_node_server_utils74.logger.error("Error in vehicle service add:", error);
|
|
16917
16544
|
if (!isExternalSession) {
|
|
16918
16545
|
await session.abortTransaction();
|
|
16919
16546
|
}
|
|
@@ -16925,7 +16552,7 @@ function useVehicleService() {
|
|
|
16925
16552
|
}
|
|
16926
16553
|
}
|
|
16927
16554
|
async function deleteVehicle(_id, recno, site, type, bypass = false) {
|
|
16928
|
-
const session =
|
|
16555
|
+
const session = import_node_server_utils74.useAtlas.getClient()?.startSession();
|
|
16929
16556
|
if (!session) {
|
|
16930
16557
|
throw new Error("Unable to start session for vehicle service.");
|
|
16931
16558
|
}
|
|
@@ -16948,7 +16575,7 @@ function useVehicleService() {
|
|
|
16948
16575
|
page++;
|
|
16949
16576
|
} while (page < pages);
|
|
16950
16577
|
if (!siteCameras.length) {
|
|
16951
|
-
throw new
|
|
16578
|
+
throw new import_node_server_utils74.BadRequestError("No site cameras found.");
|
|
16952
16579
|
}
|
|
16953
16580
|
for (const camera of siteCameras) {
|
|
16954
16581
|
const host = camera.host;
|
|
@@ -16963,7 +16590,7 @@ function useVehicleService() {
|
|
|
16963
16590
|
};
|
|
16964
16591
|
const dahuaResponse = await _removePlateNumber(dahuaPayload);
|
|
16965
16592
|
if (!bypass && dahuaResponse?.statusCode !== 200) {
|
|
16966
|
-
throw new
|
|
16593
|
+
throw new import_node_server_utils74.BadRequestError(
|
|
16967
16594
|
`Failed to remove plate number to ANPR from ${type}`
|
|
16968
16595
|
);
|
|
16969
16596
|
}
|
|
@@ -16975,7 +16602,7 @@ function useVehicleService() {
|
|
|
16975
16602
|
await session.commitTransaction();
|
|
16976
16603
|
return `Vehicle plate number deleted from ${type} record successfully.`;
|
|
16977
16604
|
} catch (error) {
|
|
16978
|
-
|
|
16605
|
+
import_node_server_utils74.logger.error("Error in vehicle service delete:", error);
|
|
16979
16606
|
await session.abortTransaction();
|
|
16980
16607
|
throw error;
|
|
16981
16608
|
} finally {
|
|
@@ -16983,16 +16610,16 @@ function useVehicleService() {
|
|
|
16983
16610
|
}
|
|
16984
16611
|
}
|
|
16985
16612
|
async function approveVehicleById(id, orgId, siteId) {
|
|
16986
|
-
const session =
|
|
16613
|
+
const session = import_node_server_utils74.useAtlas.getClient()?.startSession();
|
|
16987
16614
|
if (!session) {
|
|
16988
16615
|
throw new Error("Unable to start session for vehicle service.");
|
|
16989
16616
|
}
|
|
16990
16617
|
const org = await _getById(orgId);
|
|
16991
16618
|
if (!org)
|
|
16992
|
-
throw new
|
|
16619
|
+
throw new import_node_server_utils74.BadRequestError("Org not found");
|
|
16993
16620
|
const allowedNatures2 = "property_management_agency" /* PROPERTY_MANAGEMENT_AGENCY */;
|
|
16994
16621
|
if (!allowedNatures2.includes(org.nature)) {
|
|
16995
|
-
throw new
|
|
16622
|
+
throw new import_node_server_utils74.BadRequestError(
|
|
16996
16623
|
"Only property management can approve vehicles."
|
|
16997
16624
|
);
|
|
16998
16625
|
}
|
|
@@ -17000,7 +16627,7 @@ function useVehicleService() {
|
|
|
17000
16627
|
const plate = vehicle.plates.find((p) => p._id.toString() === id);
|
|
17001
16628
|
const _plateNumber = plate?.plateNumber;
|
|
17002
16629
|
if (!vehicle) {
|
|
17003
|
-
throw new
|
|
16630
|
+
throw new import_node_server_utils74.BadRequestError("Vehicle not found");
|
|
17004
16631
|
}
|
|
17005
16632
|
const owner = vehicle.name;
|
|
17006
16633
|
const hasStart = typeof vehicle.start === "string" ? vehicle.start.trim() !== "" : !!vehicle.start;
|
|
@@ -17042,7 +16669,7 @@ function useVehicleService() {
|
|
|
17042
16669
|
page++;
|
|
17043
16670
|
} while (page < pages);
|
|
17044
16671
|
if (!siteCameras.length) {
|
|
17045
|
-
throw new
|
|
16672
|
+
throw new import_node_server_utils74.BadRequestError("No site cameras found.");
|
|
17046
16673
|
}
|
|
17047
16674
|
for (const camera of siteCameras) {
|
|
17048
16675
|
const { host, username, password } = camera;
|
|
@@ -17058,7 +16685,7 @@ function useVehicleService() {
|
|
|
17058
16685
|
};
|
|
17059
16686
|
const dahuaResponse = await _addPlateNumber(dahuaPayload);
|
|
17060
16687
|
if (dahuaResponse?.statusCode !== 200) {
|
|
17061
|
-
throw new
|
|
16688
|
+
throw new import_node_server_utils74.BadRequestError("Failed to add plate number to ANPR");
|
|
17062
16689
|
}
|
|
17063
16690
|
const responseData = dahuaResponse?.data.toString("utf-8");
|
|
17064
16691
|
value.recNo = responseData.split("=")[1]?.trim();
|
|
@@ -17083,7 +16710,7 @@ function useVehicleService() {
|
|
|
17083
16710
|
await session.commitTransaction();
|
|
17084
16711
|
return "Vehicle plate number approved and added successfully.";
|
|
17085
16712
|
} catch (error) {
|
|
17086
|
-
|
|
16713
|
+
import_node_server_utils74.logger.error("Error in vehicle service approving and adding:", error);
|
|
17087
16714
|
await session.abortTransaction();
|
|
17088
16715
|
throw error;
|
|
17089
16716
|
} finally {
|
|
@@ -17091,7 +16718,7 @@ function useVehicleService() {
|
|
|
17091
16718
|
}
|
|
17092
16719
|
}
|
|
17093
16720
|
async function processDeletingExpiredVehicles() {
|
|
17094
|
-
const session =
|
|
16721
|
+
const session = import_node_server_utils74.useAtlas.getClient()?.startSession();
|
|
17095
16722
|
if (!session) {
|
|
17096
16723
|
throw new Error("Unable to start session for vehicle service.");
|
|
17097
16724
|
}
|
|
@@ -17118,7 +16745,7 @@ function useVehicleService() {
|
|
|
17118
16745
|
page++;
|
|
17119
16746
|
} while (page < pages);
|
|
17120
16747
|
if (!siteCameras.length) {
|
|
17121
|
-
throw new
|
|
16748
|
+
throw new import_node_server_utils74.BadRequestError("No site cameras found.");
|
|
17122
16749
|
}
|
|
17123
16750
|
for (const camera of siteCameras) {
|
|
17124
16751
|
const host = camera.host;
|
|
@@ -17138,7 +16765,7 @@ function useVehicleService() {
|
|
|
17138
16765
|
await session.commitTransaction();
|
|
17139
16766
|
return `Expired Vehicle plate numbers deleted successfully.`;
|
|
17140
16767
|
} catch (error) {
|
|
17141
|
-
|
|
16768
|
+
import_node_server_utils74.logger.error(
|
|
17142
16769
|
"Error in vehicle service process deleting expired vehicles:",
|
|
17143
16770
|
error
|
|
17144
16771
|
);
|
|
@@ -17149,16 +16776,16 @@ function useVehicleService() {
|
|
|
17149
16776
|
}
|
|
17150
16777
|
}
|
|
17151
16778
|
async function reactivateVehicleById(id, orgId, siteId) {
|
|
17152
|
-
const session =
|
|
16779
|
+
const session = import_node_server_utils74.useAtlas.getClient()?.startSession();
|
|
17153
16780
|
if (!session) {
|
|
17154
16781
|
throw new Error("Unable to start session for vehicle service.");
|
|
17155
16782
|
}
|
|
17156
16783
|
const org = await _getById(orgId);
|
|
17157
16784
|
if (!org)
|
|
17158
|
-
throw new
|
|
16785
|
+
throw new import_node_server_utils74.BadRequestError("Org not found");
|
|
17159
16786
|
const allowedNatures2 = "property_management_agency" /* PROPERTY_MANAGEMENT_AGENCY */;
|
|
17160
16787
|
if (!allowedNatures2.includes(org.nature)) {
|
|
17161
|
-
throw new
|
|
16788
|
+
throw new import_node_server_utils74.BadRequestError(
|
|
17162
16789
|
"Only property management can reactivate vehicles."
|
|
17163
16790
|
);
|
|
17164
16791
|
}
|
|
@@ -17166,7 +16793,7 @@ function useVehicleService() {
|
|
|
17166
16793
|
const plate = vehicle.plates.find((p) => p._id.toString() === id);
|
|
17167
16794
|
const _plateNumber = plate?.plateNumber;
|
|
17168
16795
|
if (!vehicle) {
|
|
17169
|
-
throw new
|
|
16796
|
+
throw new import_node_server_utils74.BadRequestError("Vehicle not found");
|
|
17170
16797
|
}
|
|
17171
16798
|
const owner = vehicle.name;
|
|
17172
16799
|
let startDate = /* @__PURE__ */ new Date();
|
|
@@ -17196,7 +16823,7 @@ function useVehicleService() {
|
|
|
17196
16823
|
page++;
|
|
17197
16824
|
} while (page < pages);
|
|
17198
16825
|
if (!siteCameras.length) {
|
|
17199
|
-
throw new
|
|
16826
|
+
throw new import_node_server_utils74.BadRequestError("No site cameras found.");
|
|
17200
16827
|
}
|
|
17201
16828
|
for (const camera of siteCameras) {
|
|
17202
16829
|
const { host, username, password } = camera;
|
|
@@ -17212,7 +16839,7 @@ function useVehicleService() {
|
|
|
17212
16839
|
};
|
|
17213
16840
|
const dahuaResponse = await _addPlateNumber(dahuaPayload);
|
|
17214
16841
|
if (dahuaResponse?.statusCode !== 200) {
|
|
17215
|
-
throw new
|
|
16842
|
+
throw new import_node_server_utils74.BadRequestError("Failed to add plate number to ANPR");
|
|
17216
16843
|
}
|
|
17217
16844
|
const responseData = dahuaResponse?.data.toString("utf-8");
|
|
17218
16845
|
value.recNo = responseData.split("=")[1]?.trim();
|
|
@@ -17238,7 +16865,7 @@ function useVehicleService() {
|
|
|
17238
16865
|
await session.commitTransaction();
|
|
17239
16866
|
return "Vehicle reactivated successfully.";
|
|
17240
16867
|
} catch (error) {
|
|
17241
|
-
|
|
16868
|
+
import_node_server_utils74.logger.error("Error in vehicle service reactivation:", error);
|
|
17242
16869
|
await session.abortTransaction();
|
|
17243
16870
|
throw error;
|
|
17244
16871
|
} finally {
|
|
@@ -17246,7 +16873,7 @@ function useVehicleService() {
|
|
|
17246
16873
|
}
|
|
17247
16874
|
}
|
|
17248
16875
|
async function updateVehicleById(_id, value) {
|
|
17249
|
-
const session =
|
|
16876
|
+
const session = import_node_server_utils74.useAtlas.getClient()?.startSession();
|
|
17250
16877
|
if (!session) {
|
|
17251
16878
|
throw new Error("Unable to start session for vehicle service.");
|
|
17252
16879
|
}
|
|
@@ -17281,7 +16908,7 @@ function useVehicleService() {
|
|
|
17281
16908
|
page++;
|
|
17282
16909
|
} while (page < pages);
|
|
17283
16910
|
if (!siteCameras.length) {
|
|
17284
|
-
throw new
|
|
16911
|
+
throw new import_node_server_utils74.BadRequestError("No site cameras found.");
|
|
17285
16912
|
}
|
|
17286
16913
|
for (const camera of siteCameras) {
|
|
17287
16914
|
const { host, username, password } = camera;
|
|
@@ -17298,7 +16925,7 @@ function useVehicleService() {
|
|
|
17298
16925
|
removePlateNumber
|
|
17299
16926
|
);
|
|
17300
16927
|
if (responseForDeletion?.statusCode !== 200) {
|
|
17301
|
-
throw new
|
|
16928
|
+
throw new import_node_server_utils74.BadRequestError(
|
|
17302
16929
|
"Failed to delete plate number to ANPR"
|
|
17303
16930
|
);
|
|
17304
16931
|
}
|
|
@@ -17315,7 +16942,7 @@ function useVehicleService() {
|
|
|
17315
16942
|
};
|
|
17316
16943
|
const dahuaResponse = await _addPlateNumber(dahuaPayload);
|
|
17317
16944
|
if (dahuaResponse?.statusCode !== 200) {
|
|
17318
|
-
throw new
|
|
16945
|
+
throw new import_node_server_utils74.BadRequestError(
|
|
17319
16946
|
"Failed to update plate number to ANPR"
|
|
17320
16947
|
);
|
|
17321
16948
|
}
|
|
@@ -17335,7 +16962,7 @@ function useVehicleService() {
|
|
|
17335
16962
|
};
|
|
17336
16963
|
const dahuaResponse = await _updatePlateNumber(dahuaPayload);
|
|
17337
16964
|
if (dahuaResponse?.statusCode !== 200) {
|
|
17338
|
-
throw new
|
|
16965
|
+
throw new import_node_server_utils74.BadRequestError(
|
|
17339
16966
|
"Failed to update plate number to ANPR"
|
|
17340
16967
|
);
|
|
17341
16968
|
}
|
|
@@ -17349,7 +16976,7 @@ function useVehicleService() {
|
|
|
17349
16976
|
...start && { start },
|
|
17350
16977
|
...end && { end },
|
|
17351
16978
|
...unit && {
|
|
17352
|
-
unit: typeof unit === "string" ? new
|
|
16979
|
+
unit: typeof unit === "string" ? new import_mongodb46.ObjectId(unit) : unit
|
|
17353
16980
|
},
|
|
17354
16981
|
...value.recNo && { recNo: value.recNo },
|
|
17355
16982
|
...type && { type: value.type }
|
|
@@ -17357,7 +16984,7 @@ function useVehicleService() {
|
|
|
17357
16984
|
await _updateVehicleById(_id, formattedValue, session);
|
|
17358
16985
|
await session.commitTransaction();
|
|
17359
16986
|
} catch (error) {
|
|
17360
|
-
|
|
16987
|
+
import_node_server_utils74.logger.error("Error in vehicle service update:", error);
|
|
17361
16988
|
await session.abortTransaction();
|
|
17362
16989
|
throw error;
|
|
17363
16990
|
} finally {
|
|
@@ -17379,7 +17006,7 @@ function useVehicleService() {
|
|
|
17379
17006
|
item.unit,
|
|
17380
17007
|
site
|
|
17381
17008
|
);
|
|
17382
|
-
const unitId = unit && unit._id &&
|
|
17009
|
+
const unitId = unit && unit._id && import_mongodb46.ObjectId.isValid(unit._id) ? unit._id.toString() : unit?._id;
|
|
17383
17010
|
return {
|
|
17384
17011
|
...item,
|
|
17385
17012
|
org,
|
|
@@ -17492,6 +17119,7 @@ var loggerDahua = winston.createLogger({
|
|
|
17492
17119
|
});
|
|
17493
17120
|
|
|
17494
17121
|
// src/services/dahua.service.ts
|
|
17122
|
+
var cameraRegistry = /* @__PURE__ */ new Map();
|
|
17495
17123
|
function useDahuaDigest({
|
|
17496
17124
|
host = "",
|
|
17497
17125
|
username = "",
|
|
@@ -17840,7 +17468,33 @@ function useDahuaService() {
|
|
|
17840
17468
|
throw error;
|
|
17841
17469
|
}
|
|
17842
17470
|
}
|
|
17843
|
-
async function
|
|
17471
|
+
async function listenToCamera(camera) {
|
|
17472
|
+
if (!camera?._id) {
|
|
17473
|
+
loggerDahua.error(`Camera _id is required to listen to camera.`);
|
|
17474
|
+
throw new import_node_server_utils75.BadRequestError("Camera _id is required to listen to camera.");
|
|
17475
|
+
}
|
|
17476
|
+
const cameraId = camera._id.toString();
|
|
17477
|
+
if (cameraRegistry.has(cameraId)) {
|
|
17478
|
+
loggerDahua.info(`[${camera.host}] Stopping existing connection for update.`);
|
|
17479
|
+
cameraRegistry.get(cameraId)?.abort();
|
|
17480
|
+
cameraRegistry.delete(cameraId);
|
|
17481
|
+
}
|
|
17482
|
+
const controller = new AbortController();
|
|
17483
|
+
cameraRegistry.set(cameraId, controller);
|
|
17484
|
+
getTrafficJunction(
|
|
17485
|
+
camera.host,
|
|
17486
|
+
camera.username,
|
|
17487
|
+
camera.password,
|
|
17488
|
+
camera.site.toString(),
|
|
17489
|
+
`guard-post-${camera.guardPost}`,
|
|
17490
|
+
camera.direction,
|
|
17491
|
+
cameraId,
|
|
17492
|
+
controller.signal
|
|
17493
|
+
);
|
|
17494
|
+
}
|
|
17495
|
+
async function getTrafficJunction(host = "", username = "", password = "", site = "", gate = "", designation = "", cameraId, signal) {
|
|
17496
|
+
if (signal.aborted)
|
|
17497
|
+
return;
|
|
17844
17498
|
try {
|
|
17845
17499
|
const response = await useDahuaDigest({
|
|
17846
17500
|
host,
|
|
@@ -17864,32 +17518,58 @@ function useDahuaService() {
|
|
|
17864
17518
|
password
|
|
17865
17519
|
);
|
|
17866
17520
|
bufferQueue.setStream(response.res);
|
|
17521
|
+
const onAbort = () => {
|
|
17522
|
+
loggerDahua.info(`[${site}]-[${host}] Abort triggered. Cleaning up...`);
|
|
17523
|
+
if (response.res && !response.res.destroyed) {
|
|
17524
|
+
response.res.once("error", (err) => {
|
|
17525
|
+
if (err?.code === "UND_ERR_ABORTED") {
|
|
17526
|
+
loggerDahua.debug(`[${site}]-[${host}] Stream aborted successfully.`);
|
|
17527
|
+
} else {
|
|
17528
|
+
loggerDahua.error(`[${site}]-[${host}] Stream error during abort:`, err);
|
|
17529
|
+
}
|
|
17530
|
+
});
|
|
17531
|
+
response.res.destroy();
|
|
17532
|
+
}
|
|
17533
|
+
};
|
|
17534
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
17867
17535
|
if ([400, 401, 403, 500].includes(response.statusCode)) {
|
|
17868
17536
|
loggerDahua.error(
|
|
17869
|
-
`[${
|
|
17537
|
+
`[${host}] Connection error:`,
|
|
17870
17538
|
response.statusCode
|
|
17871
17539
|
);
|
|
17872
|
-
throw new
|
|
17540
|
+
throw new import_node_server_utils75.BadRequestError(
|
|
17873
17541
|
`Failed to connect to ANPR: ${response.statusCode}`
|
|
17874
17542
|
);
|
|
17875
17543
|
}
|
|
17876
17544
|
if ([200, 201, 202].includes(response.statusCode)) {
|
|
17877
|
-
loggerDahua.info(`[${
|
|
17878
|
-
console.log(`[${
|
|
17545
|
+
loggerDahua.info(`[${host}] Successfully connected to ANPR.`);
|
|
17546
|
+
console.log(`[${host}] Successfully connected to ANPR.`);
|
|
17879
17547
|
}
|
|
17880
17548
|
response.res.on("data", (chunk) => {
|
|
17549
|
+
if (signal.aborted) {
|
|
17550
|
+
response.res.destroy();
|
|
17551
|
+
return;
|
|
17552
|
+
}
|
|
17881
17553
|
bufferQueue.enqueue(chunk);
|
|
17882
17554
|
});
|
|
17883
17555
|
const handleDisconnect = async (reason, err) => {
|
|
17556
|
+
signal.removeEventListener("abort", onAbort);
|
|
17557
|
+
if (bufferQueue.destroy)
|
|
17558
|
+
bufferQueue.destroy();
|
|
17559
|
+
if (response.res.destroy)
|
|
17560
|
+
response.res.destroy();
|
|
17561
|
+
if (signal.aborted) {
|
|
17562
|
+
loggerDahua.info(`[${site}]-[${host}] Loop terminated. Not reconnecting.`);
|
|
17563
|
+
return;
|
|
17564
|
+
}
|
|
17884
17565
|
loggerDahua.error(
|
|
17885
|
-
`[${site}][${
|
|
17566
|
+
`[${site}]-[${host}] ${reason}`,
|
|
17886
17567
|
err ? err.code || err : ""
|
|
17887
17568
|
);
|
|
17888
|
-
bufferQueue.destroy();
|
|
17889
|
-
if (response.res.destroy)
|
|
17890
|
-
response.res.destroy();
|
|
17891
17569
|
await new Promise((res) => setTimeout(res, 5e3));
|
|
17892
|
-
|
|
17570
|
+
if (signal.aborted)
|
|
17571
|
+
return;
|
|
17572
|
+
getTrafficJunction(host, username, password, site, gate, designation, cameraId, signal);
|
|
17893
17573
|
};
|
|
17894
17574
|
response.res.on(
|
|
17895
17575
|
"end",
|
|
@@ -17904,12 +17584,16 @@ function useDahuaService() {
|
|
|
17904
17584
|
(err) => handleDisconnect("Connection error:", err)
|
|
17905
17585
|
);
|
|
17906
17586
|
} catch (error) {
|
|
17587
|
+
if (signal.aborted)
|
|
17588
|
+
return;
|
|
17907
17589
|
loggerDahua.error(
|
|
17908
17590
|
`[${site}][${gate}] Initial connect error:`,
|
|
17909
17591
|
error.code || error
|
|
17910
17592
|
);
|
|
17911
17593
|
await new Promise((res) => setTimeout(res, 5e3));
|
|
17912
|
-
|
|
17594
|
+
if (signal.aborted)
|
|
17595
|
+
return;
|
|
17596
|
+
getTrafficJunction(host, username, password, site, gate, designation, cameraId, signal);
|
|
17913
17597
|
}
|
|
17914
17598
|
}
|
|
17915
17599
|
async function addPlateNumber(value) {
|
|
@@ -17926,7 +17610,7 @@ function useDahuaService() {
|
|
|
17926
17610
|
});
|
|
17927
17611
|
const { error } = validation.validate(value);
|
|
17928
17612
|
if (error) {
|
|
17929
|
-
throw new
|
|
17613
|
+
throw new import_node_server_utils75.BadRequestError(`Validation error: ${error.message}`);
|
|
17930
17614
|
}
|
|
17931
17615
|
value.owner = String(value.owner ?? "").substring(0, 15) || "unknown";
|
|
17932
17616
|
const _openGate = String(value.isOpenGate);
|
|
@@ -17942,7 +17626,7 @@ function useDahuaService() {
|
|
|
17942
17626
|
return response;
|
|
17943
17627
|
} catch (error2) {
|
|
17944
17628
|
loggerDahua.error(`[${value.host}] Error adding plate number:`, error2);
|
|
17945
|
-
throw new
|
|
17629
|
+
throw new import_node_server_utils75.BadRequestError(`Failed to add plate number: ${error2.message}`);
|
|
17946
17630
|
}
|
|
17947
17631
|
}
|
|
17948
17632
|
async function updatePlateNumber(value) {
|
|
@@ -17960,7 +17644,7 @@ function useDahuaService() {
|
|
|
17960
17644
|
});
|
|
17961
17645
|
const { error } = validation.validate(value);
|
|
17962
17646
|
if (error) {
|
|
17963
|
-
throw new
|
|
17647
|
+
throw new import_node_server_utils75.BadRequestError(`Validation error: ${error.message}`);
|
|
17964
17648
|
}
|
|
17965
17649
|
value.owner = value.owner || "unknown";
|
|
17966
17650
|
let _isOpenGate;
|
|
@@ -17979,7 +17663,7 @@ function useDahuaService() {
|
|
|
17979
17663
|
return response;
|
|
17980
17664
|
} catch (error2) {
|
|
17981
17665
|
loggerDahua.error(`[${value.host}] Error updating plate number:`, error2);
|
|
17982
|
-
throw new
|
|
17666
|
+
throw new import_node_server_utils75.BadRequestError(
|
|
17983
17667
|
`Failed to update plate number: ${error2.message}`
|
|
17984
17668
|
);
|
|
17985
17669
|
}
|
|
@@ -17994,7 +17678,7 @@ function useDahuaService() {
|
|
|
17994
17678
|
});
|
|
17995
17679
|
const { error } = validation.validate(value);
|
|
17996
17680
|
if (error) {
|
|
17997
|
-
throw new
|
|
17681
|
+
throw new import_node_server_utils75.BadRequestError(`Validation error: ${error.message}`);
|
|
17998
17682
|
}
|
|
17999
17683
|
try {
|
|
18000
17684
|
const response = await useDahuaDigest({
|
|
@@ -18018,7 +17702,7 @@ function useDahuaService() {
|
|
|
18018
17702
|
});
|
|
18019
17703
|
const { error } = validation.validate(value);
|
|
18020
17704
|
if (error) {
|
|
18021
|
-
throw new
|
|
17705
|
+
throw new import_node_server_utils75.BadRequestError(`Validation error: ${error.message}`);
|
|
18022
17706
|
}
|
|
18023
17707
|
try {
|
|
18024
17708
|
const response = await useDahuaDigest({
|
|
@@ -18050,7 +17734,7 @@ function useDahuaService() {
|
|
|
18050
17734
|
});
|
|
18051
17735
|
const { error } = validation.validate(value);
|
|
18052
17736
|
if (error) {
|
|
18053
|
-
throw new
|
|
17737
|
+
throw new import_node_server_utils75.BadRequestError(`Validation error: ${error.message}`);
|
|
18054
17738
|
}
|
|
18055
17739
|
value.owner = String(value.owner ?? "").substring(0, 15) || "unknown";
|
|
18056
17740
|
value.vehicleType = String(value.vehicleType ?? "").substring(0, 31) || "unknown";
|
|
@@ -18068,14 +17752,14 @@ function useDahuaService() {
|
|
|
18068
17752
|
return response;
|
|
18069
17753
|
} catch (error2) {
|
|
18070
17754
|
loggerDahua.error(`[${value.host}] Error bulk add plate number:`, error2);
|
|
18071
|
-
throw new
|
|
17755
|
+
throw new import_node_server_utils75.BadRequestError(
|
|
18072
17756
|
`Failed bulk adding plate number: ${error2.message}`
|
|
18073
17757
|
);
|
|
18074
17758
|
}
|
|
18075
17759
|
}
|
|
18076
17760
|
return {
|
|
18077
17761
|
getSnapshot,
|
|
18078
|
-
|
|
17762
|
+
listenToCamera,
|
|
18079
17763
|
addPlateNumber,
|
|
18080
17764
|
removePlateNumber,
|
|
18081
17765
|
updatePlateNumber,
|
|
@@ -18084,7 +17768,400 @@ function useDahuaService() {
|
|
|
18084
17768
|
};
|
|
18085
17769
|
}
|
|
18086
17770
|
|
|
17771
|
+
// src/repositories/site-camera.repo.ts
|
|
17772
|
+
function useSiteCameraRepo() {
|
|
17773
|
+
const db = import_node_server_utils76.useAtlas.getDb();
|
|
17774
|
+
if (!db) {
|
|
17775
|
+
throw new Error("Unable to connect to server.");
|
|
17776
|
+
}
|
|
17777
|
+
const namespace_collection = "site.cameras";
|
|
17778
|
+
const collection = db.collection(namespace_collection);
|
|
17779
|
+
const { delNamespace, getCache, setCache } = (0, import_node_server_utils76.useCache)(namespace_collection);
|
|
17780
|
+
async function createIndexes() {
|
|
17781
|
+
try {
|
|
17782
|
+
await collection.createIndexes([
|
|
17783
|
+
{
|
|
17784
|
+
key: { site: 1 }
|
|
17785
|
+
},
|
|
17786
|
+
{
|
|
17787
|
+
key: { type: 1 }
|
|
17788
|
+
},
|
|
17789
|
+
{
|
|
17790
|
+
key: { host: 1, type: 1, status: 1, site: 1 },
|
|
17791
|
+
unique: true,
|
|
17792
|
+
partialFilterExpression: { status: "active" }
|
|
17793
|
+
}
|
|
17794
|
+
]);
|
|
17795
|
+
return `Successfully created ${namespace_collection} indexes.`;
|
|
17796
|
+
} catch (error) {
|
|
17797
|
+
import_node_server_utils76.logger.log({
|
|
17798
|
+
level: "error",
|
|
17799
|
+
message: error.message
|
|
17800
|
+
});
|
|
17801
|
+
}
|
|
17802
|
+
}
|
|
17803
|
+
async function add(value, session) {
|
|
17804
|
+
try {
|
|
17805
|
+
value = MSiteCamera(value);
|
|
17806
|
+
const res = await collection.insertOne(value, { session });
|
|
17807
|
+
delCachedData();
|
|
17808
|
+
return res.insertedId;
|
|
17809
|
+
} catch (error) {
|
|
17810
|
+
import_node_server_utils76.logger.log({
|
|
17811
|
+
level: "error",
|
|
17812
|
+
message: error.message
|
|
17813
|
+
});
|
|
17814
|
+
const isDuplicated = error.message.includes("duplicate");
|
|
17815
|
+
if (isDuplicated) {
|
|
17816
|
+
throw new import_node_server_utils76.BadRequestError("ANPR already exist.");
|
|
17817
|
+
}
|
|
17818
|
+
if (error instanceof import_node_server_utils76.AppError) {
|
|
17819
|
+
throw error;
|
|
17820
|
+
} else {
|
|
17821
|
+
throw new Error("Failed to create ANPR.");
|
|
17822
|
+
}
|
|
17823
|
+
}
|
|
17824
|
+
}
|
|
17825
|
+
async function getAll(value) {
|
|
17826
|
+
value.page = value.page ? value.page - 1 : 0;
|
|
17827
|
+
value.limit = value.limit || 10;
|
|
17828
|
+
const cacheKeyOptions = {
|
|
17829
|
+
type: value.type,
|
|
17830
|
+
page: value.page,
|
|
17831
|
+
limit: value.limit,
|
|
17832
|
+
...value.direction && { direction: value.direction }
|
|
17833
|
+
};
|
|
17834
|
+
const query = {
|
|
17835
|
+
type: value.type,
|
|
17836
|
+
...value.direction && {
|
|
17837
|
+
direction: {
|
|
17838
|
+
$in: Array.isArray(value.direction) ? value.direction : [value.direction]
|
|
17839
|
+
}
|
|
17840
|
+
}
|
|
17841
|
+
};
|
|
17842
|
+
if (value.site) {
|
|
17843
|
+
cacheKeyOptions.site = value.site;
|
|
17844
|
+
try {
|
|
17845
|
+
value.site = new import_mongodb47.ObjectId(value.site);
|
|
17846
|
+
query.site = value.site;
|
|
17847
|
+
} catch (error) {
|
|
17848
|
+
throw new import_node_server_utils76.BadRequestError("Invalid site ID format");
|
|
17849
|
+
}
|
|
17850
|
+
}
|
|
17851
|
+
const cacheKey = (0, import_node_server_utils76.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
17852
|
+
const cachedData = await getCache(cacheKey);
|
|
17853
|
+
if (cachedData) {
|
|
17854
|
+
return cachedData;
|
|
17855
|
+
}
|
|
17856
|
+
try {
|
|
17857
|
+
const items = await collection.aggregate([
|
|
17858
|
+
{
|
|
17859
|
+
$match: query
|
|
17860
|
+
},
|
|
17861
|
+
{
|
|
17862
|
+
$skip: value.page * value.limit
|
|
17863
|
+
},
|
|
17864
|
+
{
|
|
17865
|
+
$limit: value.limit
|
|
17866
|
+
},
|
|
17867
|
+
{
|
|
17868
|
+
$project: {
|
|
17869
|
+
password: 0
|
|
17870
|
+
}
|
|
17871
|
+
}
|
|
17872
|
+
]).toArray();
|
|
17873
|
+
const length = await collection.countDocuments(query);
|
|
17874
|
+
const data = (0, import_node_server_utils76.paginate)(items, value.page, value.limit, length);
|
|
17875
|
+
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
17876
|
+
import_node_server_utils76.logger.info(`Cache set for key: ${cacheKey}`);
|
|
17877
|
+
}).catch((err) => {
|
|
17878
|
+
import_node_server_utils76.logger.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
17879
|
+
});
|
|
17880
|
+
return data;
|
|
17881
|
+
} catch (error) {
|
|
17882
|
+
if (error instanceof import_node_server_utils76.BadRequestError) {
|
|
17883
|
+
throw error;
|
|
17884
|
+
}
|
|
17885
|
+
throw new import_node_server_utils76.BadRequestError("Failed to retrieve site cameras.");
|
|
17886
|
+
}
|
|
17887
|
+
}
|
|
17888
|
+
async function findSiteCameras({ site, type }) {
|
|
17889
|
+
const pipeline = [];
|
|
17890
|
+
const cacheObject = {};
|
|
17891
|
+
if (type) {
|
|
17892
|
+
cacheObject.type = type;
|
|
17893
|
+
pipeline.push({ $match: { "anprs.type": type } });
|
|
17894
|
+
}
|
|
17895
|
+
if (site) {
|
|
17896
|
+
const _site = new import_mongodb47.ObjectId(site);
|
|
17897
|
+
cacheObject.site = site;
|
|
17898
|
+
pipeline.push({ $match: { site: _site } });
|
|
17899
|
+
}
|
|
17900
|
+
pipeline.push({
|
|
17901
|
+
$project: {
|
|
17902
|
+
_id: 1,
|
|
17903
|
+
site: 1,
|
|
17904
|
+
cameras: 1
|
|
17905
|
+
}
|
|
17906
|
+
});
|
|
17907
|
+
try {
|
|
17908
|
+
const items = await collection.aggregate(pipeline).toArray();
|
|
17909
|
+
return items;
|
|
17910
|
+
} catch (error) {
|
|
17911
|
+
if (error instanceof import_node_server_utils76.BadRequestError) {
|
|
17912
|
+
throw error;
|
|
17913
|
+
}
|
|
17914
|
+
throw new import_node_server_utils76.BadRequestError("Failed to retrieve site cameras.");
|
|
17915
|
+
}
|
|
17916
|
+
}
|
|
17917
|
+
function delCachedData() {
|
|
17918
|
+
delNamespace().then(() => {
|
|
17919
|
+
import_node_server_utils76.logger.log({
|
|
17920
|
+
level: "info",
|
|
17921
|
+
message: `Cache namespace cleared for ${namespace_collection}`
|
|
17922
|
+
});
|
|
17923
|
+
}).catch((err) => {
|
|
17924
|
+
import_node_server_utils76.logger.log({
|
|
17925
|
+
level: "error",
|
|
17926
|
+
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
17927
|
+
});
|
|
17928
|
+
});
|
|
17929
|
+
}
|
|
17930
|
+
async function getBySite(site, options = {}) {
|
|
17931
|
+
const _site = (0, import_node_server_utils76.toObjectId)(site);
|
|
17932
|
+
const cacheKeyOptions = {
|
|
17933
|
+
site,
|
|
17934
|
+
tag: "get-by-site",
|
|
17935
|
+
...options.category && { category: options.category },
|
|
17936
|
+
...options.type && { type: options.type },
|
|
17937
|
+
...options.guardPost && { guardPost: options.guardPost }
|
|
17938
|
+
};
|
|
17939
|
+
const cacheKey = (0, import_node_server_utils76.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
17940
|
+
const cachedData = await getCache(cacheKey);
|
|
17941
|
+
if (cachedData) {
|
|
17942
|
+
return cachedData;
|
|
17943
|
+
}
|
|
17944
|
+
try {
|
|
17945
|
+
delete cacheKeyOptions.tag;
|
|
17946
|
+
cacheKeyOptions.site = _site;
|
|
17947
|
+
const item = await collection.findOne(cacheKeyOptions);
|
|
17948
|
+
setCache(cacheKey, item, 15 * 60).then(() => {
|
|
17949
|
+
import_node_server_utils76.logger.log({
|
|
17950
|
+
level: "info",
|
|
17951
|
+
message: `Cache set for getBySite: ${cacheKey}`
|
|
17952
|
+
});
|
|
17953
|
+
}).catch((err) => {
|
|
17954
|
+
import_node_server_utils76.logger.log({
|
|
17955
|
+
level: "error",
|
|
17956
|
+
message: `Failed to set cache for getBySite: ${err.message}`
|
|
17957
|
+
});
|
|
17958
|
+
});
|
|
17959
|
+
return item;
|
|
17960
|
+
} catch (error) {
|
|
17961
|
+
if (error instanceof import_node_server_utils76.BadRequestError) {
|
|
17962
|
+
throw error;
|
|
17963
|
+
}
|
|
17964
|
+
throw new import_node_server_utils76.BadRequestError("Failed to retrieve site cameras.");
|
|
17965
|
+
}
|
|
17966
|
+
}
|
|
17967
|
+
async function getBySiteGuardPost(site, guardPost) {
|
|
17968
|
+
try {
|
|
17969
|
+
site = new import_mongodb47.ObjectId(site);
|
|
17970
|
+
} catch (error) {
|
|
17971
|
+
throw new import_node_server_utils76.BadRequestError("Invalid site ID format");
|
|
17972
|
+
}
|
|
17973
|
+
const cacheKeyOptions = {
|
|
17974
|
+
site,
|
|
17975
|
+
guardPost,
|
|
17976
|
+
tag: "get-by-site"
|
|
17977
|
+
};
|
|
17978
|
+
const cacheKey = (0, import_node_server_utils76.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
17979
|
+
const cachedData = await getCache(cacheKey);
|
|
17980
|
+
if (cachedData) {
|
|
17981
|
+
return cachedData;
|
|
17982
|
+
}
|
|
17983
|
+
try {
|
|
17984
|
+
delete cacheKeyOptions.tag;
|
|
17985
|
+
const item = await collection.findOne({
|
|
17986
|
+
site,
|
|
17987
|
+
guardPost: { $gt: guardPost }
|
|
17988
|
+
});
|
|
17989
|
+
setCache(cacheKey, item, 15 * 60).then(() => {
|
|
17990
|
+
import_node_server_utils76.logger.log({
|
|
17991
|
+
level: "info",
|
|
17992
|
+
message: `Cache set for getBySite: ${cacheKey}`
|
|
17993
|
+
});
|
|
17994
|
+
}).catch((err) => {
|
|
17995
|
+
import_node_server_utils76.logger.log({
|
|
17996
|
+
level: "error",
|
|
17997
|
+
message: `Failed to set cache for getBySite: ${err.message}`
|
|
17998
|
+
});
|
|
17999
|
+
});
|
|
18000
|
+
return item;
|
|
18001
|
+
} catch (error) {
|
|
18002
|
+
if (error instanceof import_node_server_utils76.BadRequestError) {
|
|
18003
|
+
throw error;
|
|
18004
|
+
}
|
|
18005
|
+
throw new import_node_server_utils76.BadRequestError("Failed to retrieve site cameras.");
|
|
18006
|
+
}
|
|
18007
|
+
}
|
|
18008
|
+
async function getBySites(site, options = {}) {
|
|
18009
|
+
try {
|
|
18010
|
+
site = new import_mongodb47.ObjectId(site);
|
|
18011
|
+
} catch (error) {
|
|
18012
|
+
throw new import_node_server_utils76.BadRequestError("Invalid site ID format");
|
|
18013
|
+
}
|
|
18014
|
+
const cacheKeyOptions = { site };
|
|
18015
|
+
if (options.category) {
|
|
18016
|
+
cacheKeyOptions.category = options.category;
|
|
18017
|
+
}
|
|
18018
|
+
if (options.type) {
|
|
18019
|
+
cacheKeyOptions.type = options.type;
|
|
18020
|
+
}
|
|
18021
|
+
if (options.direction) {
|
|
18022
|
+
cacheKeyOptions.direction = options.direction;
|
|
18023
|
+
}
|
|
18024
|
+
const cacheKey = (0, import_node_server_utils76.makeCacheKey)(namespace_collection, cacheKeyOptions);
|
|
18025
|
+
const cachedData = await getCache(cacheKey);
|
|
18026
|
+
if (cachedData) {
|
|
18027
|
+
return cachedData;
|
|
18028
|
+
}
|
|
18029
|
+
try {
|
|
18030
|
+
const items = await collection.find(cacheKeyOptions);
|
|
18031
|
+
setCache(cacheKey, items, 15 * 60).then(() => {
|
|
18032
|
+
import_node_server_utils76.logger.log({
|
|
18033
|
+
level: "info",
|
|
18034
|
+
message: `Cache set for getBySite: ${cacheKey}`
|
|
18035
|
+
});
|
|
18036
|
+
}).catch((err) => {
|
|
18037
|
+
import_node_server_utils76.logger.log({
|
|
18038
|
+
level: "error",
|
|
18039
|
+
message: `Failed to set cache for getBySite: ${err.message}`
|
|
18040
|
+
});
|
|
18041
|
+
});
|
|
18042
|
+
return items;
|
|
18043
|
+
} catch (error) {
|
|
18044
|
+
if (error instanceof import_node_server_utils76.BadRequestError) {
|
|
18045
|
+
throw error;
|
|
18046
|
+
}
|
|
18047
|
+
throw new import_node_server_utils76.BadRequestError("Failed to retrieve site cameras.");
|
|
18048
|
+
}
|
|
18049
|
+
}
|
|
18050
|
+
async function updateById(_id, value, session) {
|
|
18051
|
+
const { error } = schemaUpdateSiteCamera.validate(value);
|
|
18052
|
+
if (error) {
|
|
18053
|
+
import_node_server_utils76.logger.info(`Site Camera Management: ${error.message}`);
|
|
18054
|
+
throw new import_node_server_utils76.BadRequestError(error.message);
|
|
18055
|
+
}
|
|
18056
|
+
try {
|
|
18057
|
+
_id = new import_mongodb47.ObjectId(_id);
|
|
18058
|
+
} catch (error2) {
|
|
18059
|
+
throw new import_node_server_utils76.BadRequestError("Invalid camera ID format");
|
|
18060
|
+
}
|
|
18061
|
+
try {
|
|
18062
|
+
value.updatedAt = /* @__PURE__ */ new Date();
|
|
18063
|
+
const result = await collection.findOneAndUpdate({ _id }, { $set: value }, { session, returnDocument: "after" });
|
|
18064
|
+
if (!result?._id) {
|
|
18065
|
+
throw new import_node_server_utils76.BadRequestError("Site camera not found.");
|
|
18066
|
+
}
|
|
18067
|
+
const { listenToCamera } = useDahuaService();
|
|
18068
|
+
await listenToCamera(result);
|
|
18069
|
+
delCachedData();
|
|
18070
|
+
return "Successfully updated site camera.";
|
|
18071
|
+
} catch (error2) {
|
|
18072
|
+
import_node_server_utils76.logger.log({
|
|
18073
|
+
level: "error",
|
|
18074
|
+
message: error2.message
|
|
18075
|
+
});
|
|
18076
|
+
const isDuplicated = error2.message.includes("duplicate");
|
|
18077
|
+
if (isDuplicated) {
|
|
18078
|
+
throw new import_node_server_utils76.BadRequestError("ANPR already exist.");
|
|
18079
|
+
}
|
|
18080
|
+
if (error2 instanceof import_node_server_utils76.AppError) {
|
|
18081
|
+
throw error2;
|
|
18082
|
+
}
|
|
18083
|
+
throw new Error("Failed to update ANPR.");
|
|
18084
|
+
}
|
|
18085
|
+
}
|
|
18086
|
+
async function deleteById(_id, session) {
|
|
18087
|
+
try {
|
|
18088
|
+
_id = new import_mongodb47.ObjectId(_id);
|
|
18089
|
+
} catch (error) {
|
|
18090
|
+
throw new import_node_server_utils76.BadRequestError("Invalid camera ID format");
|
|
18091
|
+
}
|
|
18092
|
+
try {
|
|
18093
|
+
await collection.deleteOne({ _id }, { session });
|
|
18094
|
+
delCachedData();
|
|
18095
|
+
return "Successfully deleted site camera.";
|
|
18096
|
+
} catch (error) {
|
|
18097
|
+
import_node_server_utils76.logger.log({
|
|
18098
|
+
level: "error",
|
|
18099
|
+
message: error.message
|
|
18100
|
+
});
|
|
18101
|
+
if (error instanceof import_node_server_utils76.AppError) {
|
|
18102
|
+
throw error;
|
|
18103
|
+
}
|
|
18104
|
+
throw new Error("Failed to delete site camera.");
|
|
18105
|
+
}
|
|
18106
|
+
}
|
|
18107
|
+
async function getAllCameraWithPassword(value) {
|
|
18108
|
+
value.page = value.page ? value.page - 1 : 0;
|
|
18109
|
+
value.limit = value.limit || 10;
|
|
18110
|
+
const query = {
|
|
18111
|
+
type: value.type,
|
|
18112
|
+
...value.direction && {
|
|
18113
|
+
direction: {
|
|
18114
|
+
$in: Array.isArray(value.direction) ? value.direction : [value.direction]
|
|
18115
|
+
}
|
|
18116
|
+
}
|
|
18117
|
+
};
|
|
18118
|
+
if (value.site) {
|
|
18119
|
+
try {
|
|
18120
|
+
value.site = new import_mongodb47.ObjectId(value.site);
|
|
18121
|
+
query.site = value.site;
|
|
18122
|
+
} catch (error) {
|
|
18123
|
+
throw new import_node_server_utils76.BadRequestError("Invalid site ID format");
|
|
18124
|
+
}
|
|
18125
|
+
}
|
|
18126
|
+
try {
|
|
18127
|
+
const items = await collection.aggregate([
|
|
18128
|
+
{
|
|
18129
|
+
$match: query
|
|
18130
|
+
},
|
|
18131
|
+
{
|
|
18132
|
+
$skip: value.page * value.limit
|
|
18133
|
+
},
|
|
18134
|
+
{
|
|
18135
|
+
$limit: value.limit
|
|
18136
|
+
}
|
|
18137
|
+
]).toArray();
|
|
18138
|
+
const length = await collection.countDocuments(query);
|
|
18139
|
+
const data = (0, import_node_server_utils76.paginate)(items, value.page, value.limit, length);
|
|
18140
|
+
return data;
|
|
18141
|
+
} catch (error) {
|
|
18142
|
+
if (error instanceof import_node_server_utils76.BadRequestError) {
|
|
18143
|
+
throw error;
|
|
18144
|
+
}
|
|
18145
|
+
throw new import_node_server_utils76.BadRequestError("Failed to retrieve site cameras.");
|
|
18146
|
+
}
|
|
18147
|
+
}
|
|
18148
|
+
return {
|
|
18149
|
+
createIndexes,
|
|
18150
|
+
add,
|
|
18151
|
+
getAll,
|
|
18152
|
+
findSiteCameras,
|
|
18153
|
+
getBySite,
|
|
18154
|
+
getBySites,
|
|
18155
|
+
delCachedData,
|
|
18156
|
+
getBySiteGuardPost,
|
|
18157
|
+
updateById,
|
|
18158
|
+
deleteById,
|
|
18159
|
+
getAllCameraWithPassword
|
|
18160
|
+
};
|
|
18161
|
+
}
|
|
18162
|
+
|
|
18087
18163
|
// src/services/site.service.ts
|
|
18164
|
+
var import_joi41 = __toESM(require("joi"));
|
|
18088
18165
|
function useSiteService() {
|
|
18089
18166
|
const {
|
|
18090
18167
|
createSite: _createSite,
|
|
@@ -20124,14 +20201,7 @@ function useSiteCameraService() {
|
|
|
20124
20201
|
for (let index = 0; index < siteCameras.length; index++) {
|
|
20125
20202
|
const siteCamera = siteCameras[index];
|
|
20126
20203
|
if (siteCamera && siteCamera.status === "active" && siteCamera.host && siteCamera.username && siteCamera.password && siteCamera.site && siteCamera.guardPost) {
|
|
20127
|
-
dahuaService.
|
|
20128
|
-
siteCamera.host,
|
|
20129
|
-
siteCamera.username,
|
|
20130
|
-
siteCamera.password,
|
|
20131
|
-
siteCamera.site.toString(),
|
|
20132
|
-
`guard-post-${siteCamera.guardPost}`,
|
|
20133
|
-
siteCamera.direction
|
|
20134
|
-
);
|
|
20204
|
+
dahuaService.listenToCamera(siteCamera);
|
|
20135
20205
|
}
|
|
20136
20206
|
}
|
|
20137
20207
|
}
|
|
@@ -31317,14 +31387,36 @@ function useSiteBillingConfigurationController() {
|
|
|
31317
31387
|
// src/models/event-management.model.ts
|
|
31318
31388
|
var import_mongodb84 = require("mongodb");
|
|
31319
31389
|
var import_joi81 = __toESM(require("joi"));
|
|
31390
|
+
var EventStatus = /* @__PURE__ */ ((EventStatus2) => {
|
|
31391
|
+
EventStatus2["PLANNED"] = "planned";
|
|
31392
|
+
EventStatus2["IN_PROGRESS"] = "in_progress";
|
|
31393
|
+
EventStatus2["COMPLETED"] = "completed";
|
|
31394
|
+
return EventStatus2;
|
|
31395
|
+
})(EventStatus || {});
|
|
31396
|
+
var EventSort = /* @__PURE__ */ ((EventSort2) => {
|
|
31397
|
+
EventSort2["CREATED_AT"] = "createdAt";
|
|
31398
|
+
EventSort2["NAME"] = "name";
|
|
31399
|
+
EventSort2["ID"] = "_id";
|
|
31400
|
+
return EventSort2;
|
|
31401
|
+
})(EventSort || {});
|
|
31402
|
+
var EventOrder = /* @__PURE__ */ ((EventOrder2) => {
|
|
31403
|
+
EventOrder2["ASC"] = "asc";
|
|
31404
|
+
EventOrder2["DESC"] = "desc";
|
|
31405
|
+
return EventOrder2;
|
|
31406
|
+
})(EventOrder || {});
|
|
31407
|
+
var EventType = /* @__PURE__ */ ((EventType2) => {
|
|
31408
|
+
EventType2["TASK"] = "TASK";
|
|
31409
|
+
EventType2["EVENT"] = "EVENT";
|
|
31410
|
+
return EventType2;
|
|
31411
|
+
})(EventType || {});
|
|
31320
31412
|
var schemaEventManagement = import_joi81.default.object({
|
|
31321
31413
|
_id: import_joi81.default.string().optional().allow(null, ""),
|
|
31322
31414
|
site: import_joi81.default.string().required(),
|
|
31323
31415
|
title: import_joi81.default.string().required(),
|
|
31324
31416
|
description: import_joi81.default.string().optional().allow(""),
|
|
31325
31417
|
dateTime: import_joi81.default.date().iso().required(),
|
|
31326
|
-
status: import_joi81.default.string().optional().default("planned"),
|
|
31327
|
-
type: import_joi81.default.string().optional().default("TASK")
|
|
31418
|
+
status: import_joi81.default.string().optional().default("planned" /* PLANNED */),
|
|
31419
|
+
type: import_joi81.default.string().optional().default("TASK" /* TASK */)
|
|
31328
31420
|
});
|
|
31329
31421
|
var schemaUpdateEventManagement = import_joi81.default.object({
|
|
31330
31422
|
_id: import_joi81.default.string().hex().required(),
|
|
@@ -31355,8 +31447,8 @@ function MEventManagement(value) {
|
|
|
31355
31447
|
title: value.title,
|
|
31356
31448
|
description: value.description ?? "",
|
|
31357
31449
|
dateTime: new Date(value.dateTime),
|
|
31358
|
-
status: value.status ?? "planned"
|
|
31359
|
-
type: value.type ?? "TASK"
|
|
31450
|
+
status: value.status ?? "planned" /* PLANNED */,
|
|
31451
|
+
type: value.type ?? "TASK" /* TASK */,
|
|
31360
31452
|
createdAt: value.createdAt ?? /* @__PURE__ */ new Date(),
|
|
31361
31453
|
updatedAt: value.updatedAt,
|
|
31362
31454
|
deletedAt: value.deletedAt
|
|
@@ -31455,7 +31547,8 @@ function useEventManagementRepo() {
|
|
|
31455
31547
|
sort: JSON.stringify(sort),
|
|
31456
31548
|
page,
|
|
31457
31549
|
limit,
|
|
31458
|
-
...type && { type }
|
|
31550
|
+
...type && { type },
|
|
31551
|
+
...date && { dateTime: date }
|
|
31459
31552
|
};
|
|
31460
31553
|
if (search) {
|
|
31461
31554
|
query.$or = [{ title: { $regex: search, $options: "i" } }];
|
|
@@ -31658,8 +31751,8 @@ function useEventManagementService() {
|
|
|
31658
31751
|
} = useEventManagementRepo();
|
|
31659
31752
|
async function add(value) {
|
|
31660
31753
|
const session = import_node_server_utils147.useAtlas.getClient()?.startSession();
|
|
31661
|
-
session?.startTransaction();
|
|
31662
31754
|
try {
|
|
31755
|
+
session?.startTransaction();
|
|
31663
31756
|
await _add(value, session);
|
|
31664
31757
|
await session?.commitTransaction();
|
|
31665
31758
|
return "Successfully added event.";
|
|
@@ -31722,67 +31815,51 @@ function useEventManagementController() {
|
|
|
31722
31815
|
deleteEventManagementById: _deleteEventManagementById
|
|
31723
31816
|
} = useEventManagementRepo();
|
|
31724
31817
|
async function add(req, res, next) {
|
|
31725
|
-
const payload = { ...req.body };
|
|
31726
|
-
const { error } = schemaEventManagement.validate(payload, {
|
|
31727
|
-
abortEarly: false
|
|
31728
|
-
});
|
|
31729
|
-
if (error) {
|
|
31730
|
-
const messages = error.details.map((d) => d.message).join(", ");
|
|
31731
|
-
import_node_server_utils148.logger.log({ level: "error", message: messages });
|
|
31732
|
-
next(new import_node_server_utils148.BadRequestError(messages));
|
|
31733
|
-
return;
|
|
31734
|
-
}
|
|
31735
31818
|
try {
|
|
31736
|
-
const
|
|
31819
|
+
const { error, value } = schemaEventManagement.validate(req.body, {
|
|
31820
|
+
abortEarly: false
|
|
31821
|
+
});
|
|
31822
|
+
if (error) {
|
|
31823
|
+
const messages = error.details.map((d) => d.message).join(", ");
|
|
31824
|
+
import_node_server_utils148.logger.log({ level: "error", message: messages });
|
|
31825
|
+
next(new import_node_server_utils148.BadRequestError(messages));
|
|
31826
|
+
return;
|
|
31827
|
+
}
|
|
31828
|
+
const data = await _add(value);
|
|
31737
31829
|
res.status(201).json(data);
|
|
31738
31830
|
return;
|
|
31739
|
-
} catch (
|
|
31740
|
-
import_node_server_utils148.logger.log({ level: "error", message:
|
|
31741
|
-
next(
|
|
31831
|
+
} catch (error) {
|
|
31832
|
+
import_node_server_utils148.logger.log({ level: "error", message: error.message });
|
|
31833
|
+
next(error);
|
|
31742
31834
|
return;
|
|
31743
31835
|
}
|
|
31744
31836
|
}
|
|
31745
31837
|
async function getAll(req, res, next) {
|
|
31746
|
-
const allowedFields = ["createdAt", "name"];
|
|
31747
|
-
const allowedOrder = ["asc", "desc"];
|
|
31748
|
-
const validation = import_joi82.default.object({
|
|
31749
|
-
search: import_joi82.default.string().optional().allow("", null),
|
|
31750
|
-
page: import_joi82.default.number().integer().min(1).allow("", null).default(1),
|
|
31751
|
-
limit: import_joi82.default.number().integer().min(1).max(100).allow("", null).default(10),
|
|
31752
|
-
sort: import_joi82.default.string().pattern(/^([a-zA-Z0-9_]+)(,[a-zA-Z0-9_]+)*$/).optional().allow("", ...allowedFields),
|
|
31753
|
-
order: import_joi82.default.string().pattern(/^(asc|desc)(,(asc|desc))*$/).optional().allow("", ...allowedOrder),
|
|
31754
|
-
site: import_joi82.default.string().hex().required(),
|
|
31755
|
-
status: import_joi82.default.string().optional(),
|
|
31756
|
-
type: import_joi82.default.string().optional().valid("TASK", "EVENT").allow(null, ""),
|
|
31757
|
-
date: import_joi82.default.string().optional().allow(null, "")
|
|
31758
|
-
});
|
|
31759
|
-
const query = { ...req.query };
|
|
31760
|
-
const { error } = validation.validate(query, {
|
|
31761
|
-
abortEarly: false
|
|
31762
|
-
});
|
|
31763
|
-
if (error) {
|
|
31764
|
-
const messages = error.details.map((d) => d.message).join(", ");
|
|
31765
|
-
import_node_server_utils148.logger.log({ level: "error", message: messages });
|
|
31766
|
-
next(new import_node_server_utils148.BadRequestError(messages));
|
|
31767
|
-
return;
|
|
31768
|
-
}
|
|
31769
|
-
const search = req.query.search ?? "";
|
|
31770
|
-
const page = parseInt(req.query.page ?? "1");
|
|
31771
|
-
const limit = parseInt(req.query.limit ?? "10");
|
|
31772
|
-
const site = req.query.site ?? "";
|
|
31773
|
-
const status = req.query.status ?? "";
|
|
31774
|
-
const type = req.query.type ?? "";
|
|
31775
|
-
const date = req.query.date ?? "";
|
|
31776
|
-
const sortObj = {};
|
|
31777
|
-
const sortFields = String(req.query.sort).split(",");
|
|
31778
|
-
const sortOrders = String(req.query.order).split(",");
|
|
31779
|
-
sortFields.forEach((field, index) => {
|
|
31780
|
-
if (allowedFields.includes(field)) {
|
|
31781
|
-
const order = sortOrders[index] === "asc" ? 1 : -1;
|
|
31782
|
-
sortObj[field] = order;
|
|
31783
|
-
}
|
|
31784
|
-
});
|
|
31785
31838
|
try {
|
|
31839
|
+
const validation = import_joi82.default.object({
|
|
31840
|
+
search: import_joi82.default.string().optional().allow("", null),
|
|
31841
|
+
page: import_joi82.default.number().integer().min(1).allow("", null).default(1),
|
|
31842
|
+
limit: import_joi82.default.number().integer().min(1).max(100).allow("", null).default(10),
|
|
31843
|
+
sort: import_joi82.default.string().valid(...Object.values(EventSort)).default("_id" /* ID */),
|
|
31844
|
+
order: import_joi82.default.string().valid(...Object.values(EventOrder)).default("desc" /* DESC */),
|
|
31845
|
+
site: import_joi82.default.string().hex().length(24).required(),
|
|
31846
|
+
status: import_joi82.default.string().valid(...Object.values(EventStatus)).allow(null, ""),
|
|
31847
|
+
type: import_joi82.default.string().optional().valid(...Object.values(EventType)).allow(null, ""),
|
|
31848
|
+
date: import_joi82.default.string().optional().allow(null, "")
|
|
31849
|
+
});
|
|
31850
|
+
const { error, value } = validation.validate(req.query, {
|
|
31851
|
+
abortEarly: false
|
|
31852
|
+
});
|
|
31853
|
+
if (error) {
|
|
31854
|
+
const messages = error.details.map((d) => d.message).join(", ");
|
|
31855
|
+
import_node_server_utils148.logger.log({ level: "error", message: messages });
|
|
31856
|
+
next(new import_node_server_utils148.BadRequestError(messages));
|
|
31857
|
+
return;
|
|
31858
|
+
}
|
|
31859
|
+
const { search, page, limit, site, status, type, date, sort, order } = value;
|
|
31860
|
+
const sortObj = {
|
|
31861
|
+
[sort ?? "_id" /* ID */]: order === "asc" /* ASC */ ? 1 : -1
|
|
31862
|
+
};
|
|
31786
31863
|
const data = await _getAll({
|
|
31787
31864
|
search,
|
|
31788
31865
|
page,
|
|
@@ -31795,28 +31872,30 @@ function useEventManagementController() {
|
|
|
31795
31872
|
});
|
|
31796
31873
|
res.status(200).json(data);
|
|
31797
31874
|
return;
|
|
31798
|
-
} catch (
|
|
31799
|
-
import_node_server_utils148.logger.log({ level: "error", message:
|
|
31800
|
-
next(
|
|
31875
|
+
} catch (error) {
|
|
31876
|
+
import_node_server_utils148.logger.log({ level: "error", message: error.message });
|
|
31877
|
+
next(error);
|
|
31801
31878
|
return;
|
|
31802
31879
|
}
|
|
31803
31880
|
}
|
|
31804
31881
|
async function getEventManagementById(req, res, next) {
|
|
31805
|
-
const validation = import_joi82.default.string().hex().required();
|
|
31806
|
-
const _id = req.params.id;
|
|
31807
|
-
const { error } = validation.validate(_id);
|
|
31808
|
-
if (error) {
|
|
31809
|
-
import_node_server_utils148.logger.log({ level: "error", message: error.message });
|
|
31810
|
-
next(new import_node_server_utils148.BadRequestError(error.message));
|
|
31811
|
-
return;
|
|
31812
|
-
}
|
|
31813
31882
|
try {
|
|
31883
|
+
const schema2 = import_joi82.default.object({
|
|
31884
|
+
_id: import_joi82.default.string().hex().length(24).required()
|
|
31885
|
+
});
|
|
31886
|
+
const { error, value } = schema2.validate({ _id: req.params.id });
|
|
31887
|
+
if (error) {
|
|
31888
|
+
import_node_server_utils148.logger.log({ level: "error", message: error.message });
|
|
31889
|
+
next(new import_node_server_utils148.BadRequestError(error.message));
|
|
31890
|
+
return;
|
|
31891
|
+
}
|
|
31892
|
+
const { _id } = value;
|
|
31814
31893
|
const data = await _getEventManagementById(_id);
|
|
31815
31894
|
res.status(200).json(data);
|
|
31816
31895
|
return;
|
|
31817
|
-
} catch (
|
|
31818
|
-
import_node_server_utils148.logger.log({ level: "error", message:
|
|
31819
|
-
next(
|
|
31896
|
+
} catch (error) {
|
|
31897
|
+
import_node_server_utils148.logger.log({ level: "error", message: error.message });
|
|
31898
|
+
next(error);
|
|
31820
31899
|
return;
|
|
31821
31900
|
}
|
|
31822
31901
|
}
|
|
@@ -33264,6 +33343,15 @@ var formatEntryPassDate = (date) => {
|
|
|
33264
33343
|
const day = String(newDate.getDate()).padStart(2, "0");
|
|
33265
33344
|
return `${year}${month}${day}`;
|
|
33266
33345
|
};
|
|
33346
|
+
var entryPassDate = (TRDATE, TRTIME) => {
|
|
33347
|
+
const year = parseInt(TRDATE.substring(0, 4), 10);
|
|
33348
|
+
const month = parseInt(TRDATE.substring(4, 6), 10) - 1;
|
|
33349
|
+
const day = parseInt(TRDATE.substring(6, 8), 10);
|
|
33350
|
+
const hours = parseInt(TRTIME.substring(0, 2), 10);
|
|
33351
|
+
const minutes = parseInt(TRTIME.substring(2, 4), 10);
|
|
33352
|
+
const seconds = parseInt(TRTIME.substring(4, 6), 10);
|
|
33353
|
+
return new Date(year, month, day, hours, minutes, seconds);
|
|
33354
|
+
};
|
|
33267
33355
|
async function removeAccessGroup({
|
|
33268
33356
|
cardNo,
|
|
33269
33357
|
staffNo,
|
|
@@ -33296,6 +33384,16 @@ async function removeAccessGroup({
|
|
|
33296
33384
|
console.log(error);
|
|
33297
33385
|
}
|
|
33298
33386
|
}
|
|
33387
|
+
async function getTransactions(index, url) {
|
|
33388
|
+
try {
|
|
33389
|
+
const decrypt = decryptAcmUrl(url);
|
|
33390
|
+
const response = await import_axios.default.get(`${decrypt}transactions?from=${index}`);
|
|
33391
|
+
if (response.status === 200 || response.status === 201)
|
|
33392
|
+
return response.data;
|
|
33393
|
+
} catch (error) {
|
|
33394
|
+
return Promise.reject(error);
|
|
33395
|
+
}
|
|
33396
|
+
}
|
|
33299
33397
|
|
|
33300
33398
|
// src/repositories/access-management.repo.ts
|
|
33301
33399
|
var import_xml2js2 = require("xml2js");
|
|
@@ -35282,6 +35380,116 @@ function UseAccessManagementRepo() {
|
|
|
35282
35380
|
throw new Error(error.message);
|
|
35283
35381
|
}
|
|
35284
35382
|
}
|
|
35383
|
+
async function getTransactionsRepo({ page = 1, limit = 10, site, cardNo, url }) {
|
|
35384
|
+
page = page ? page - 1 : 0;
|
|
35385
|
+
site = new import_mongodb90.ObjectId(site);
|
|
35386
|
+
try {
|
|
35387
|
+
let index = await collectionName("access-card-transactions").findOne({}, { sort: { index: -1 } });
|
|
35388
|
+
index = index ? index.index : 0;
|
|
35389
|
+
const response = await getTransactions(index, url);
|
|
35390
|
+
if (response && Array.isArray(response.items) && response.items.length > 0) {
|
|
35391
|
+
let result2 = response.items.map((item) => ({
|
|
35392
|
+
id: item.id,
|
|
35393
|
+
data: JSON.parse(item.data),
|
|
35394
|
+
timestamp: item.timestamp
|
|
35395
|
+
}));
|
|
35396
|
+
result2 = result2.filter((item) => item.data.Event.ETYPE === "0" && item.data.Event.CARDNO !== "");
|
|
35397
|
+
if (result2.length > 0) {
|
|
35398
|
+
const transactions = result2.map(
|
|
35399
|
+
(item) => new MAccessCardTransaction({
|
|
35400
|
+
index: item.id,
|
|
35401
|
+
cardNo: item.data.Event.CARDNO,
|
|
35402
|
+
staffNo: item.data.Event.STAFFNO,
|
|
35403
|
+
staffName: item.data.Event.STAFFNAME,
|
|
35404
|
+
accessType: item.data.Event.DEVNAME,
|
|
35405
|
+
accessStatus: item.data.Event.TRCODE,
|
|
35406
|
+
description: item.data.Event.TRDESC,
|
|
35407
|
+
accessTime: entryPassDate(item.data.Event.TRDATE, item.data.Event.TRTIME),
|
|
35408
|
+
createdAt: /* @__PURE__ */ new Date()
|
|
35409
|
+
})
|
|
35410
|
+
);
|
|
35411
|
+
await collectionName("access-card-transactions").insertMany(transactions);
|
|
35412
|
+
}
|
|
35413
|
+
}
|
|
35414
|
+
const result = await collectionName("access-card-transactions").aggregate([
|
|
35415
|
+
{
|
|
35416
|
+
$match: { cardNo }
|
|
35417
|
+
},
|
|
35418
|
+
{
|
|
35419
|
+
$lookup: {
|
|
35420
|
+
from: "access-card",
|
|
35421
|
+
let: { cardNo: "$cardNo" },
|
|
35422
|
+
pipeline: [
|
|
35423
|
+
{
|
|
35424
|
+
$match: {
|
|
35425
|
+
$expr: {
|
|
35426
|
+
$eq: ["$cardNo", "$$cardNo"]
|
|
35427
|
+
}
|
|
35428
|
+
}
|
|
35429
|
+
},
|
|
35430
|
+
{
|
|
35431
|
+
$lookup: {
|
|
35432
|
+
from: "users",
|
|
35433
|
+
let: { userId: "$userId" },
|
|
35434
|
+
pipeline: [
|
|
35435
|
+
{
|
|
35436
|
+
$match: {
|
|
35437
|
+
$expr: {
|
|
35438
|
+
$eq: ["$_id", "$$userId"]
|
|
35439
|
+
}
|
|
35440
|
+
}
|
|
35441
|
+
},
|
|
35442
|
+
{
|
|
35443
|
+
$project: {
|
|
35444
|
+
_id: 1,
|
|
35445
|
+
givenName: 1,
|
|
35446
|
+
surname: 1
|
|
35447
|
+
}
|
|
35448
|
+
}
|
|
35449
|
+
],
|
|
35450
|
+
as: "user"
|
|
35451
|
+
}
|
|
35452
|
+
},
|
|
35453
|
+
{
|
|
35454
|
+
$project: {
|
|
35455
|
+
_id: 1,
|
|
35456
|
+
user: { $arrayElemAt: ["$user", 0] },
|
|
35457
|
+
type: 1,
|
|
35458
|
+
cardNo: 1
|
|
35459
|
+
}
|
|
35460
|
+
}
|
|
35461
|
+
],
|
|
35462
|
+
as: "accessCard"
|
|
35463
|
+
}
|
|
35464
|
+
},
|
|
35465
|
+
{
|
|
35466
|
+
$project: {
|
|
35467
|
+
_id: 1,
|
|
35468
|
+
cardNo: 1,
|
|
35469
|
+
staffNo: 1,
|
|
35470
|
+
staffName: 1,
|
|
35471
|
+
accessType: 1,
|
|
35472
|
+
accessStatus: 1,
|
|
35473
|
+
description: 1,
|
|
35474
|
+
accessTime: 1,
|
|
35475
|
+
createdAt: 1,
|
|
35476
|
+
accessCard: { $arrayElemAt: ["$accessCard", 0] }
|
|
35477
|
+
}
|
|
35478
|
+
},
|
|
35479
|
+
{
|
|
35480
|
+
$facet: {
|
|
35481
|
+
totalCount: [{ $count: "count" }],
|
|
35482
|
+
items: [{ $sort: { _id: -1 } }, { $skip: page * limit }, { $limit: limit }]
|
|
35483
|
+
}
|
|
35484
|
+
}
|
|
35485
|
+
]).toArray();
|
|
35486
|
+
const length = result[0].totalCount[0] ? result[0].totalCount[0].count : 0;
|
|
35487
|
+
const items = result[0].items;
|
|
35488
|
+
return (0, import_node_server_utils153.paginate)(items, page, limit, length);
|
|
35489
|
+
} catch (error) {
|
|
35490
|
+
return Promise.reject("Server internal error.");
|
|
35491
|
+
}
|
|
35492
|
+
}
|
|
35285
35493
|
return {
|
|
35286
35494
|
createIndexes,
|
|
35287
35495
|
createIndexForEntrypass,
|
|
@@ -35313,7 +35521,8 @@ function UseAccessManagementRepo() {
|
|
|
35313
35521
|
signQrCodeRepo,
|
|
35314
35522
|
checkoutVisitorRepo,
|
|
35315
35523
|
getBlockLevelAndUnitListRepo,
|
|
35316
|
-
indexCombination
|
|
35524
|
+
indexCombination,
|
|
35525
|
+
getTransactionsRepo
|
|
35317
35526
|
};
|
|
35318
35527
|
}
|
|
35319
35528
|
|
|
@@ -35353,7 +35562,8 @@ function useAccessManagementSvc() {
|
|
|
35353
35562
|
addVisitorAccessCardRepo,
|
|
35354
35563
|
signQrCodeRepo,
|
|
35355
35564
|
checkoutVisitorRepo,
|
|
35356
|
-
getBlockLevelAndUnitListRepo
|
|
35565
|
+
getBlockLevelAndUnitListRepo,
|
|
35566
|
+
getTransactionsRepo
|
|
35357
35567
|
} = UseAccessManagementRepo();
|
|
35358
35568
|
const addPhysicalCardSvc = async (payload) => {
|
|
35359
35569
|
try {
|
|
@@ -35645,6 +35855,14 @@ function useAccessManagementSvc() {
|
|
|
35645
35855
|
throw new Error(err.message);
|
|
35646
35856
|
}
|
|
35647
35857
|
};
|
|
35858
|
+
const getTransactionsSvc = async ({ page, limit, site, cardNo, url }) => {
|
|
35859
|
+
try {
|
|
35860
|
+
const response = await getTransactionsRepo({ page, limit, site, cardNo, url });
|
|
35861
|
+
return response;
|
|
35862
|
+
} catch (err) {
|
|
35863
|
+
return Promise.reject("Server internal error.");
|
|
35864
|
+
}
|
|
35865
|
+
};
|
|
35648
35866
|
return {
|
|
35649
35867
|
addPhysicalCardSvc,
|
|
35650
35868
|
addNonPhysicalCardSvc,
|
|
@@ -35679,7 +35897,8 @@ function useAccessManagementSvc() {
|
|
|
35679
35897
|
addVisitorAccessCardSvc,
|
|
35680
35898
|
signQrCodeSvc,
|
|
35681
35899
|
checkoutVisitorSvc,
|
|
35682
|
-
getBlockLevelAndUnitListSvc
|
|
35900
|
+
getBlockLevelAndUnitListSvc,
|
|
35901
|
+
getTransactionsSvc
|
|
35683
35902
|
};
|
|
35684
35903
|
}
|
|
35685
35904
|
|
|
@@ -35719,7 +35938,8 @@ function useAccessManagementController() {
|
|
|
35719
35938
|
addVisitorAccessCardSvc,
|
|
35720
35939
|
signQrCodeSvc,
|
|
35721
35940
|
checkoutVisitorSvc,
|
|
35722
|
-
getBlockLevelAndUnitListSvc
|
|
35941
|
+
getBlockLevelAndUnitListSvc,
|
|
35942
|
+
getTransactionsSvc
|
|
35723
35943
|
} = useAccessManagementSvc();
|
|
35724
35944
|
const addPhysicalCard = async (req, res) => {
|
|
35725
35945
|
try {
|
|
@@ -36481,6 +36701,31 @@ function useAccessManagementController() {
|
|
|
36481
36701
|
});
|
|
36482
36702
|
}
|
|
36483
36703
|
};
|
|
36704
|
+
const getTransactions2 = async (req, res) => {
|
|
36705
|
+
const { page = 1, limit = 10, site, cardNo, url } = req.query;
|
|
36706
|
+
const schema2 = import_joi85.default.object({
|
|
36707
|
+
page: import_joi85.default.number().required(),
|
|
36708
|
+
limit: import_joi85.default.number().optional().default(10),
|
|
36709
|
+
site: import_joi85.default.string().hex().required(),
|
|
36710
|
+
cardNo: import_joi85.default.string().required()
|
|
36711
|
+
});
|
|
36712
|
+
const { value, error } = schema2.validate({ page, limit, site, cardNo });
|
|
36713
|
+
if (error) {
|
|
36714
|
+
return res.status(400).json({ message: error.message });
|
|
36715
|
+
}
|
|
36716
|
+
try {
|
|
36717
|
+
const result = await getTransactionsSvc({
|
|
36718
|
+
page: Number(page),
|
|
36719
|
+
limit: Number(value.limit),
|
|
36720
|
+
site,
|
|
36721
|
+
cardNo,
|
|
36722
|
+
url
|
|
36723
|
+
});
|
|
36724
|
+
return res.json(result);
|
|
36725
|
+
} catch (error2) {
|
|
36726
|
+
return Promise.reject("Internal Server Error");
|
|
36727
|
+
}
|
|
36728
|
+
};
|
|
36484
36729
|
return {
|
|
36485
36730
|
addPhysicalCard,
|
|
36486
36731
|
addNonPhysicalCard,
|
|
@@ -36513,7 +36758,8 @@ function useAccessManagementController() {
|
|
|
36513
36758
|
signQrCode,
|
|
36514
36759
|
checkoutVisitor,
|
|
36515
36760
|
removeAccessCard,
|
|
36516
|
-
getBlockLevelAndUnitList
|
|
36761
|
+
getBlockLevelAndUnitList,
|
|
36762
|
+
getTransactions: getTransactions2
|
|
36517
36763
|
};
|
|
36518
36764
|
}
|
|
36519
36765
|
|
|
@@ -50328,6 +50574,10 @@ function useRoleControllerV2() {
|
|
|
50328
50574
|
EmailSender,
|
|
50329
50575
|
EntryOrder,
|
|
50330
50576
|
EntrySort,
|
|
50577
|
+
EventOrder,
|
|
50578
|
+
EventSort,
|
|
50579
|
+
EventStatus,
|
|
50580
|
+
EventType,
|
|
50331
50581
|
FacilitySort,
|
|
50332
50582
|
FacilityStatus,
|
|
50333
50583
|
GuestSort,
|
|
@@ -50440,6 +50690,7 @@ function useRoleControllerV2() {
|
|
|
50440
50690
|
events_namespace_collection,
|
|
50441
50691
|
facility_bookings_namespace_collection,
|
|
50442
50692
|
feedbackSchema,
|
|
50693
|
+
feedbacks2_namespace_collection,
|
|
50443
50694
|
feedbacks_namespace_collection,
|
|
50444
50695
|
formatDahuaDate,
|
|
50445
50696
|
getPeriodRangeWithPrevious,
|