@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.mjs
CHANGED
|
@@ -289,15 +289,22 @@ import {
|
|
|
289
289
|
logger as logger3
|
|
290
290
|
} from "@7365admin1/node-server-utils";
|
|
291
291
|
var feedbacks_namespace_collection = "feedbacks";
|
|
292
|
+
var feedbacks2_namespace_collection = "feedbacks2";
|
|
292
293
|
function useFeedbackRepo() {
|
|
293
294
|
const db = useAtlas2.getDb();
|
|
294
295
|
if (!db) {
|
|
295
296
|
throw new InternalServerError2("Unable to connect to server.");
|
|
296
297
|
}
|
|
297
298
|
const collection = db.collection(feedbacks_namespace_collection);
|
|
299
|
+
const feedbacks2Collection = db.collection(feedbacks2_namespace_collection);
|
|
298
300
|
const { delNamespace, getCache, setCache } = useCache2(
|
|
299
301
|
feedbacks_namespace_collection
|
|
300
302
|
);
|
|
303
|
+
const {
|
|
304
|
+
delNamespace: delNamespaceFeedbacks2,
|
|
305
|
+
getCache: getCacheFeedbacks2,
|
|
306
|
+
setCache: setCacheFeedbacks2
|
|
307
|
+
} = useCache2(feedbacks2_namespace_collection);
|
|
301
308
|
const { delNamespace: _delDashboardNameSpace } = useCache2("dashboard");
|
|
302
309
|
async function createIndex() {
|
|
303
310
|
try {
|
|
@@ -758,7 +765,7 @@ function useFeedbackRepo() {
|
|
|
758
765
|
updatedAt: /* @__PURE__ */ new Date(),
|
|
759
766
|
deletedAt: /* @__PURE__ */ new Date()
|
|
760
767
|
};
|
|
761
|
-
const res = await
|
|
768
|
+
const res = await feedbacks2Collection.updateOne(
|
|
762
769
|
{ _id },
|
|
763
770
|
{ $set: updateValue },
|
|
764
771
|
{ session }
|
|
@@ -766,13 +773,13 @@ function useFeedbackRepo() {
|
|
|
766
773
|
if (res.modifiedCount === 0) {
|
|
767
774
|
throw new InternalServerError2("Unable to delete feedback.");
|
|
768
775
|
}
|
|
769
|
-
|
|
776
|
+
delNamespaceFeedbacks2().then(() => {
|
|
770
777
|
logger3.info(
|
|
771
|
-
`Cache cleared for namespace: ${
|
|
778
|
+
`Cache cleared for namespace: ${feedbacks2_namespace_collection}`
|
|
772
779
|
);
|
|
773
780
|
}).catch((err) => {
|
|
774
781
|
logger3.error(
|
|
775
|
-
`Failed to clear cache for namespace: ${
|
|
782
|
+
`Failed to clear cache for namespace: ${feedbacks2_namespace_collection}`,
|
|
776
783
|
err
|
|
777
784
|
);
|
|
778
785
|
});
|
|
@@ -12828,14 +12835,14 @@ import {
|
|
|
12828
12835
|
|
|
12829
12836
|
// src/repositories/site-camera.repo.ts
|
|
12830
12837
|
import {
|
|
12831
|
-
AppError as
|
|
12832
|
-
BadRequestError as
|
|
12833
|
-
logger as
|
|
12834
|
-
makeCacheKey as
|
|
12835
|
-
paginate as
|
|
12836
|
-
toObjectId as
|
|
12837
|
-
useAtlas as
|
|
12838
|
-
useCache as
|
|
12838
|
+
AppError as AppError11,
|
|
12839
|
+
BadRequestError as BadRequestError74,
|
|
12840
|
+
logger as logger54,
|
|
12841
|
+
makeCacheKey as makeCacheKey25,
|
|
12842
|
+
paginate as paginate21,
|
|
12843
|
+
toObjectId as toObjectId11,
|
|
12844
|
+
useAtlas as useAtlas35,
|
|
12845
|
+
useCache as useCache26
|
|
12839
12846
|
} from "@7365admin1/node-server-utils";
|
|
12840
12847
|
|
|
12841
12848
|
// src/models/site-camera.model.ts
|
|
@@ -12901,412 +12908,32 @@ function MSiteCamera(value) {
|
|
|
12901
12908
|
}
|
|
12902
12909
|
|
|
12903
12910
|
// src/repositories/site-camera.repo.ts
|
|
12904
|
-
import { ObjectId as
|
|
12905
|
-
function useSiteCameraRepo() {
|
|
12906
|
-
const db = useAtlas30.getDb();
|
|
12907
|
-
if (!db) {
|
|
12908
|
-
throw new Error("Unable to connect to server.");
|
|
12909
|
-
}
|
|
12910
|
-
const namespace_collection = "site.cameras";
|
|
12911
|
-
const collection = db.collection(namespace_collection);
|
|
12912
|
-
const { delNamespace, getCache, setCache } = useCache23(namespace_collection);
|
|
12913
|
-
async function createIndexes() {
|
|
12914
|
-
try {
|
|
12915
|
-
await collection.createIndexes([
|
|
12916
|
-
{
|
|
12917
|
-
key: { site: 1 }
|
|
12918
|
-
},
|
|
12919
|
-
{
|
|
12920
|
-
key: { type: 1 }
|
|
12921
|
-
},
|
|
12922
|
-
{
|
|
12923
|
-
key: { host: 1, type: 1, status: 1, site: 1 },
|
|
12924
|
-
unique: true,
|
|
12925
|
-
partialFilterExpression: { status: "active" }
|
|
12926
|
-
}
|
|
12927
|
-
]);
|
|
12928
|
-
return `Successfully created ${namespace_collection} indexes.`;
|
|
12929
|
-
} catch (error) {
|
|
12930
|
-
logger47.log({
|
|
12931
|
-
level: "error",
|
|
12932
|
-
message: error.message
|
|
12933
|
-
});
|
|
12934
|
-
}
|
|
12935
|
-
}
|
|
12936
|
-
async function add(value, session) {
|
|
12937
|
-
try {
|
|
12938
|
-
value = MSiteCamera(value);
|
|
12939
|
-
const res = await collection.insertOne(value, { session });
|
|
12940
|
-
delCachedData();
|
|
12941
|
-
return res.insertedId;
|
|
12942
|
-
} catch (error) {
|
|
12943
|
-
logger47.log({
|
|
12944
|
-
level: "error",
|
|
12945
|
-
message: error.message
|
|
12946
|
-
});
|
|
12947
|
-
const isDuplicated = error.message.includes("duplicate");
|
|
12948
|
-
if (isDuplicated) {
|
|
12949
|
-
throw new BadRequestError65("ANPR already exist.");
|
|
12950
|
-
}
|
|
12951
|
-
if (error instanceof AppError8) {
|
|
12952
|
-
throw error;
|
|
12953
|
-
} else {
|
|
12954
|
-
throw new Error("Failed to create ANPR.");
|
|
12955
|
-
}
|
|
12956
|
-
}
|
|
12957
|
-
}
|
|
12958
|
-
async function getAll(value) {
|
|
12959
|
-
value.page = value.page ? value.page - 1 : 0;
|
|
12960
|
-
value.limit = value.limit || 10;
|
|
12961
|
-
const cacheKeyOptions = {
|
|
12962
|
-
type: value.type,
|
|
12963
|
-
page: value.page,
|
|
12964
|
-
limit: value.limit,
|
|
12965
|
-
...value.direction && { direction: value.direction }
|
|
12966
|
-
};
|
|
12967
|
-
const query = {
|
|
12968
|
-
type: value.type,
|
|
12969
|
-
...value.direction && {
|
|
12970
|
-
direction: {
|
|
12971
|
-
$in: Array.isArray(value.direction) ? value.direction : [value.direction]
|
|
12972
|
-
}
|
|
12973
|
-
}
|
|
12974
|
-
};
|
|
12975
|
-
if (value.site) {
|
|
12976
|
-
cacheKeyOptions.site = value.site;
|
|
12977
|
-
try {
|
|
12978
|
-
value.site = new ObjectId38(value.site);
|
|
12979
|
-
query.site = value.site;
|
|
12980
|
-
} catch (error) {
|
|
12981
|
-
throw new BadRequestError65("Invalid site ID format");
|
|
12982
|
-
}
|
|
12983
|
-
}
|
|
12984
|
-
const cacheKey = makeCacheKey22(namespace_collection, cacheKeyOptions);
|
|
12985
|
-
const cachedData = await getCache(cacheKey);
|
|
12986
|
-
if (cachedData) {
|
|
12987
|
-
return cachedData;
|
|
12988
|
-
}
|
|
12989
|
-
try {
|
|
12990
|
-
const items = await collection.aggregate([
|
|
12991
|
-
{
|
|
12992
|
-
$match: query
|
|
12993
|
-
},
|
|
12994
|
-
{
|
|
12995
|
-
$skip: value.page * value.limit
|
|
12996
|
-
},
|
|
12997
|
-
{
|
|
12998
|
-
$limit: value.limit
|
|
12999
|
-
},
|
|
13000
|
-
{
|
|
13001
|
-
$project: {
|
|
13002
|
-
password: 0
|
|
13003
|
-
}
|
|
13004
|
-
}
|
|
13005
|
-
]).toArray();
|
|
13006
|
-
const length = await collection.countDocuments(query);
|
|
13007
|
-
const data = paginate17(items, value.page, value.limit, length);
|
|
13008
|
-
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
13009
|
-
logger47.info(`Cache set for key: ${cacheKey}`);
|
|
13010
|
-
}).catch((err) => {
|
|
13011
|
-
logger47.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
13012
|
-
});
|
|
13013
|
-
return data;
|
|
13014
|
-
} catch (error) {
|
|
13015
|
-
if (error instanceof BadRequestError65) {
|
|
13016
|
-
throw error;
|
|
13017
|
-
}
|
|
13018
|
-
throw new BadRequestError65("Failed to retrieve site cameras.");
|
|
13019
|
-
}
|
|
13020
|
-
}
|
|
13021
|
-
async function findSiteCameras({ site, type }) {
|
|
13022
|
-
const pipeline = [];
|
|
13023
|
-
const cacheObject = {};
|
|
13024
|
-
if (type) {
|
|
13025
|
-
cacheObject.type = type;
|
|
13026
|
-
pipeline.push({ $match: { "anprs.type": type } });
|
|
13027
|
-
}
|
|
13028
|
-
if (site) {
|
|
13029
|
-
const _site = new ObjectId38(site);
|
|
13030
|
-
cacheObject.site = site;
|
|
13031
|
-
pipeline.push({ $match: { site: _site } });
|
|
13032
|
-
}
|
|
13033
|
-
pipeline.push({
|
|
13034
|
-
$project: {
|
|
13035
|
-
_id: 1,
|
|
13036
|
-
site: 1,
|
|
13037
|
-
cameras: 1
|
|
13038
|
-
}
|
|
13039
|
-
});
|
|
13040
|
-
try {
|
|
13041
|
-
const items = await collection.aggregate(pipeline).toArray();
|
|
13042
|
-
return items;
|
|
13043
|
-
} catch (error) {
|
|
13044
|
-
if (error instanceof BadRequestError65) {
|
|
13045
|
-
throw error;
|
|
13046
|
-
}
|
|
13047
|
-
throw new BadRequestError65("Failed to retrieve site cameras.");
|
|
13048
|
-
}
|
|
13049
|
-
}
|
|
13050
|
-
function delCachedData() {
|
|
13051
|
-
delNamespace().then(() => {
|
|
13052
|
-
logger47.log({
|
|
13053
|
-
level: "info",
|
|
13054
|
-
message: `Cache namespace cleared for ${namespace_collection}`
|
|
13055
|
-
});
|
|
13056
|
-
}).catch((err) => {
|
|
13057
|
-
logger47.log({
|
|
13058
|
-
level: "error",
|
|
13059
|
-
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
13060
|
-
});
|
|
13061
|
-
});
|
|
13062
|
-
}
|
|
13063
|
-
async function getBySite(site, options = {}) {
|
|
13064
|
-
const _site = toObjectId7(site);
|
|
13065
|
-
const cacheKeyOptions = {
|
|
13066
|
-
site,
|
|
13067
|
-
tag: "get-by-site",
|
|
13068
|
-
...options.category && { category: options.category },
|
|
13069
|
-
...options.type && { type: options.type },
|
|
13070
|
-
...options.guardPost && { guardPost: options.guardPost }
|
|
13071
|
-
};
|
|
13072
|
-
const cacheKey = makeCacheKey22(namespace_collection, cacheKeyOptions);
|
|
13073
|
-
const cachedData = await getCache(cacheKey);
|
|
13074
|
-
if (cachedData) {
|
|
13075
|
-
return cachedData;
|
|
13076
|
-
}
|
|
13077
|
-
try {
|
|
13078
|
-
delete cacheKeyOptions.tag;
|
|
13079
|
-
cacheKeyOptions.site = _site;
|
|
13080
|
-
const item = await collection.findOne(cacheKeyOptions);
|
|
13081
|
-
setCache(cacheKey, item, 15 * 60).then(() => {
|
|
13082
|
-
logger47.log({
|
|
13083
|
-
level: "info",
|
|
13084
|
-
message: `Cache set for getBySite: ${cacheKey}`
|
|
13085
|
-
});
|
|
13086
|
-
}).catch((err) => {
|
|
13087
|
-
logger47.log({
|
|
13088
|
-
level: "error",
|
|
13089
|
-
message: `Failed to set cache for getBySite: ${err.message}`
|
|
13090
|
-
});
|
|
13091
|
-
});
|
|
13092
|
-
return item;
|
|
13093
|
-
} catch (error) {
|
|
13094
|
-
if (error instanceof BadRequestError65) {
|
|
13095
|
-
throw error;
|
|
13096
|
-
}
|
|
13097
|
-
throw new BadRequestError65("Failed to retrieve site cameras.");
|
|
13098
|
-
}
|
|
13099
|
-
}
|
|
13100
|
-
async function getBySiteGuardPost(site, guardPost) {
|
|
13101
|
-
try {
|
|
13102
|
-
site = new ObjectId38(site);
|
|
13103
|
-
} catch (error) {
|
|
13104
|
-
throw new BadRequestError65("Invalid site ID format");
|
|
13105
|
-
}
|
|
13106
|
-
const cacheKeyOptions = {
|
|
13107
|
-
site,
|
|
13108
|
-
guardPost,
|
|
13109
|
-
tag: "get-by-site"
|
|
13110
|
-
};
|
|
13111
|
-
const cacheKey = makeCacheKey22(namespace_collection, cacheKeyOptions);
|
|
13112
|
-
const cachedData = await getCache(cacheKey);
|
|
13113
|
-
if (cachedData) {
|
|
13114
|
-
return cachedData;
|
|
13115
|
-
}
|
|
13116
|
-
try {
|
|
13117
|
-
delete cacheKeyOptions.tag;
|
|
13118
|
-
const item = await collection.findOne({
|
|
13119
|
-
site,
|
|
13120
|
-
guardPost: { $gt: guardPost }
|
|
13121
|
-
});
|
|
13122
|
-
setCache(cacheKey, item, 15 * 60).then(() => {
|
|
13123
|
-
logger47.log({
|
|
13124
|
-
level: "info",
|
|
13125
|
-
message: `Cache set for getBySite: ${cacheKey}`
|
|
13126
|
-
});
|
|
13127
|
-
}).catch((err) => {
|
|
13128
|
-
logger47.log({
|
|
13129
|
-
level: "error",
|
|
13130
|
-
message: `Failed to set cache for getBySite: ${err.message}`
|
|
13131
|
-
});
|
|
13132
|
-
});
|
|
13133
|
-
return item;
|
|
13134
|
-
} catch (error) {
|
|
13135
|
-
if (error instanceof BadRequestError65) {
|
|
13136
|
-
throw error;
|
|
13137
|
-
}
|
|
13138
|
-
throw new BadRequestError65("Failed to retrieve site cameras.");
|
|
13139
|
-
}
|
|
13140
|
-
}
|
|
13141
|
-
async function getBySites(site, options = {}) {
|
|
13142
|
-
try {
|
|
13143
|
-
site = new ObjectId38(site);
|
|
13144
|
-
} catch (error) {
|
|
13145
|
-
throw new BadRequestError65("Invalid site ID format");
|
|
13146
|
-
}
|
|
13147
|
-
const cacheKeyOptions = { site };
|
|
13148
|
-
if (options.category) {
|
|
13149
|
-
cacheKeyOptions.category = options.category;
|
|
13150
|
-
}
|
|
13151
|
-
if (options.type) {
|
|
13152
|
-
cacheKeyOptions.type = options.type;
|
|
13153
|
-
}
|
|
13154
|
-
if (options.direction) {
|
|
13155
|
-
cacheKeyOptions.direction = options.direction;
|
|
13156
|
-
}
|
|
13157
|
-
const cacheKey = makeCacheKey22(namespace_collection, cacheKeyOptions);
|
|
13158
|
-
const cachedData = await getCache(cacheKey);
|
|
13159
|
-
if (cachedData) {
|
|
13160
|
-
return cachedData;
|
|
13161
|
-
}
|
|
13162
|
-
try {
|
|
13163
|
-
const items = await collection.find(cacheKeyOptions);
|
|
13164
|
-
setCache(cacheKey, items, 15 * 60).then(() => {
|
|
13165
|
-
logger47.log({
|
|
13166
|
-
level: "info",
|
|
13167
|
-
message: `Cache set for getBySite: ${cacheKey}`
|
|
13168
|
-
});
|
|
13169
|
-
}).catch((err) => {
|
|
13170
|
-
logger47.log({
|
|
13171
|
-
level: "error",
|
|
13172
|
-
message: `Failed to set cache for getBySite: ${err.message}`
|
|
13173
|
-
});
|
|
13174
|
-
});
|
|
13175
|
-
return items;
|
|
13176
|
-
} catch (error) {
|
|
13177
|
-
if (error instanceof BadRequestError65) {
|
|
13178
|
-
throw error;
|
|
13179
|
-
}
|
|
13180
|
-
throw new BadRequestError65("Failed to retrieve site cameras.");
|
|
13181
|
-
}
|
|
13182
|
-
}
|
|
13183
|
-
async function updateById(_id, value, session) {
|
|
13184
|
-
const { error } = schemaUpdateSiteCamera.validate(value);
|
|
13185
|
-
if (error) {
|
|
13186
|
-
logger47.info(`Site Camera Management: ${error.message}`);
|
|
13187
|
-
throw new BadRequestError65(error.message);
|
|
13188
|
-
}
|
|
13189
|
-
try {
|
|
13190
|
-
_id = new ObjectId38(_id);
|
|
13191
|
-
} catch (error2) {
|
|
13192
|
-
throw new BadRequestError65("Invalid camera ID format");
|
|
13193
|
-
}
|
|
13194
|
-
try {
|
|
13195
|
-
await collection.updateOne({ _id }, { $set: value }, { session });
|
|
13196
|
-
delCachedData();
|
|
13197
|
-
return "Successfully updated site camera.";
|
|
13198
|
-
} catch (error2) {
|
|
13199
|
-
logger47.log({
|
|
13200
|
-
level: "error",
|
|
13201
|
-
message: error2.message
|
|
13202
|
-
});
|
|
13203
|
-
const isDuplicated = error2.message.includes("duplicate");
|
|
13204
|
-
if (isDuplicated) {
|
|
13205
|
-
throw new BadRequestError65("ANPR already exist.");
|
|
13206
|
-
}
|
|
13207
|
-
if (error2 instanceof AppError8) {
|
|
13208
|
-
throw error2;
|
|
13209
|
-
}
|
|
13210
|
-
throw new Error("Failed to update ANPR.");
|
|
13211
|
-
}
|
|
13212
|
-
}
|
|
13213
|
-
async function deleteById(_id, session) {
|
|
13214
|
-
try {
|
|
13215
|
-
_id = new ObjectId38(_id);
|
|
13216
|
-
} catch (error) {
|
|
13217
|
-
throw new BadRequestError65("Invalid camera ID format");
|
|
13218
|
-
}
|
|
13219
|
-
try {
|
|
13220
|
-
await collection.deleteOne({ _id }, { session });
|
|
13221
|
-
delCachedData();
|
|
13222
|
-
return "Successfully deleted site camera.";
|
|
13223
|
-
} catch (error) {
|
|
13224
|
-
logger47.log({
|
|
13225
|
-
level: "error",
|
|
13226
|
-
message: error.message
|
|
13227
|
-
});
|
|
13228
|
-
if (error instanceof AppError8) {
|
|
13229
|
-
throw error;
|
|
13230
|
-
}
|
|
13231
|
-
throw new Error("Failed to delete site camera.");
|
|
13232
|
-
}
|
|
13233
|
-
}
|
|
13234
|
-
async function getAllCameraWithPassword(value) {
|
|
13235
|
-
value.page = value.page ? value.page - 1 : 0;
|
|
13236
|
-
value.limit = value.limit || 10;
|
|
13237
|
-
const query = {
|
|
13238
|
-
type: value.type,
|
|
13239
|
-
...value.direction && {
|
|
13240
|
-
direction: {
|
|
13241
|
-
$in: Array.isArray(value.direction) ? value.direction : [value.direction]
|
|
13242
|
-
}
|
|
13243
|
-
}
|
|
13244
|
-
};
|
|
13245
|
-
if (value.site) {
|
|
13246
|
-
try {
|
|
13247
|
-
value.site = new ObjectId38(value.site);
|
|
13248
|
-
query.site = value.site;
|
|
13249
|
-
} catch (error) {
|
|
13250
|
-
throw new BadRequestError65("Invalid site ID format");
|
|
13251
|
-
}
|
|
13252
|
-
}
|
|
13253
|
-
try {
|
|
13254
|
-
const items = await collection.aggregate([
|
|
13255
|
-
{
|
|
13256
|
-
$match: query
|
|
13257
|
-
},
|
|
13258
|
-
{
|
|
13259
|
-
$skip: value.page * value.limit
|
|
13260
|
-
},
|
|
13261
|
-
{
|
|
13262
|
-
$limit: value.limit
|
|
13263
|
-
}
|
|
13264
|
-
]).toArray();
|
|
13265
|
-
const length = await collection.countDocuments(query);
|
|
13266
|
-
const data = paginate17(items, value.page, value.limit, length);
|
|
13267
|
-
return data;
|
|
13268
|
-
} catch (error) {
|
|
13269
|
-
if (error instanceof BadRequestError65) {
|
|
13270
|
-
throw error;
|
|
13271
|
-
}
|
|
13272
|
-
throw new BadRequestError65("Failed to retrieve site cameras.");
|
|
13273
|
-
}
|
|
13274
|
-
}
|
|
13275
|
-
return {
|
|
13276
|
-
createIndexes,
|
|
13277
|
-
add,
|
|
13278
|
-
getAll,
|
|
13279
|
-
findSiteCameras,
|
|
13280
|
-
getBySite,
|
|
13281
|
-
getBySites,
|
|
13282
|
-
delCachedData,
|
|
13283
|
-
getBySiteGuardPost,
|
|
13284
|
-
updateById,
|
|
13285
|
-
deleteById,
|
|
13286
|
-
getAllCameraWithPassword
|
|
13287
|
-
};
|
|
13288
|
-
}
|
|
12911
|
+
import { ObjectId as ObjectId47 } from "mongodb";
|
|
13289
12912
|
|
|
13290
|
-
// src/services/
|
|
13291
|
-
import
|
|
12913
|
+
// src/services/dahua.service.ts
|
|
12914
|
+
import { BadRequestError as BadRequestError73 } from "@7365admin1/node-server-utils";
|
|
12915
|
+
import { promises as fsAsync } from "fs";
|
|
12916
|
+
import Joi40 from "joi";
|
|
12917
|
+
import * as path from "path";
|
|
12918
|
+
import { request } from "urllib";
|
|
13292
12919
|
|
|
13293
12920
|
// src/repositories/visitor-transaction.repo.ts
|
|
13294
12921
|
import {
|
|
13295
|
-
BadRequestError as
|
|
12922
|
+
BadRequestError as BadRequestError66,
|
|
13296
12923
|
InternalServerError as InternalServerError24,
|
|
13297
|
-
paginate as
|
|
13298
|
-
toObjectId as
|
|
13299
|
-
useAtlas as
|
|
12924
|
+
paginate as paginate17,
|
|
12925
|
+
toObjectId as toObjectId7,
|
|
12926
|
+
useAtlas as useAtlas30
|
|
13300
12927
|
} from "@7365admin1/node-server-utils";
|
|
13301
12928
|
|
|
13302
12929
|
// src/models/visitor-transactions.model.ts
|
|
13303
12930
|
import Joi36 from "joi";
|
|
13304
|
-
import { ObjectId as
|
|
12931
|
+
import { ObjectId as ObjectId39 } from "mongodb";
|
|
13305
12932
|
|
|
13306
12933
|
// src/models/person.model.ts
|
|
13307
|
-
import { BadRequestError as
|
|
12934
|
+
import { BadRequestError as BadRequestError65 } from "@7365admin1/node-server-utils";
|
|
13308
12935
|
import Joi35 from "joi";
|
|
13309
|
-
import { ObjectId as
|
|
12936
|
+
import { ObjectId as ObjectId38 } from "mongodb";
|
|
13310
12937
|
var PersonTypes = /* @__PURE__ */ ((PersonTypes3) => {
|
|
13311
12938
|
PersonTypes3["WALK_IN"] = "walk-in";
|
|
13312
12939
|
PersonTypes3["DROP_OFF"] = "drop-off";
|
|
@@ -13399,25 +13026,25 @@ var schemaUpdatePerson = Joi35.object({
|
|
|
13399
13026
|
function MPerson(value) {
|
|
13400
13027
|
const { error } = schemaPerson.validate(value);
|
|
13401
13028
|
if (error) {
|
|
13402
|
-
throw new
|
|
13029
|
+
throw new BadRequestError65(error.details[0].message);
|
|
13403
13030
|
}
|
|
13404
13031
|
if (value._id && typeof value._id === "string") {
|
|
13405
13032
|
try {
|
|
13406
|
-
value._id = new
|
|
13033
|
+
value._id = new ObjectId38(value._id);
|
|
13407
13034
|
} catch (error2) {
|
|
13408
13035
|
throw new Error("Invalid ID.");
|
|
13409
13036
|
}
|
|
13410
13037
|
}
|
|
13411
13038
|
if (value.org && typeof value.org === "string") {
|
|
13412
13039
|
try {
|
|
13413
|
-
value.org = new
|
|
13040
|
+
value.org = new ObjectId38(value.org);
|
|
13414
13041
|
} catch (error2) {
|
|
13415
13042
|
throw new Error("Invalid org ID.");
|
|
13416
13043
|
}
|
|
13417
13044
|
}
|
|
13418
13045
|
if (value.site && typeof value.site === "string") {
|
|
13419
13046
|
try {
|
|
13420
|
-
value.site = new
|
|
13047
|
+
value.site = new ObjectId38(value.site);
|
|
13421
13048
|
} catch (error2) {
|
|
13422
13049
|
throw new Error("Invalid site ID.");
|
|
13423
13050
|
}
|
|
@@ -13426,9 +13053,9 @@ function MPerson(value) {
|
|
|
13426
13053
|
value.files = value.files.map((file) => {
|
|
13427
13054
|
if (file.id && typeof file.id === "string") {
|
|
13428
13055
|
try {
|
|
13429
|
-
file.id = new
|
|
13056
|
+
file.id = new ObjectId38(file.id);
|
|
13430
13057
|
} catch {
|
|
13431
|
-
throw new
|
|
13058
|
+
throw new BadRequestError65("Invalid file id format");
|
|
13432
13059
|
}
|
|
13433
13060
|
}
|
|
13434
13061
|
return file;
|
|
@@ -13436,7 +13063,7 @@ function MPerson(value) {
|
|
|
13436
13063
|
}
|
|
13437
13064
|
if (value.user && typeof value.user === "string") {
|
|
13438
13065
|
try {
|
|
13439
|
-
value.user = new
|
|
13066
|
+
value.user = new ObjectId38(value.user);
|
|
13440
13067
|
} catch (error2) {
|
|
13441
13068
|
throw new Error("Invalid user ID.");
|
|
13442
13069
|
}
|
|
@@ -13606,28 +13233,28 @@ function MVisitorTransaction(value) {
|
|
|
13606
13233
|
}
|
|
13607
13234
|
if (value._id && typeof value._id === "string") {
|
|
13608
13235
|
try {
|
|
13609
|
-
value._id = new
|
|
13236
|
+
value._id = new ObjectId39(value._id);
|
|
13610
13237
|
} catch (error2) {
|
|
13611
13238
|
throw new Error("Invalid ID.");
|
|
13612
13239
|
}
|
|
13613
13240
|
}
|
|
13614
13241
|
if (value.org && typeof value.org === "string") {
|
|
13615
13242
|
try {
|
|
13616
|
-
value.org = new
|
|
13243
|
+
value.org = new ObjectId39(value.org);
|
|
13617
13244
|
} catch (error2) {
|
|
13618
13245
|
throw new Error("Invalid org ID.");
|
|
13619
13246
|
}
|
|
13620
13247
|
}
|
|
13621
13248
|
if (value.site && typeof value.site === "string") {
|
|
13622
13249
|
try {
|
|
13623
|
-
value.site = new
|
|
13250
|
+
value.site = new ObjectId39(value.site);
|
|
13624
13251
|
} catch (error2) {
|
|
13625
13252
|
throw new Error("Invalid site ID.");
|
|
13626
13253
|
}
|
|
13627
13254
|
}
|
|
13628
13255
|
if (value.unit && typeof value.unit === "string") {
|
|
13629
13256
|
try {
|
|
13630
|
-
value.unit = new
|
|
13257
|
+
value.unit = new ObjectId39(value.unit);
|
|
13631
13258
|
} catch (error2) {
|
|
13632
13259
|
throw new Error("Invalid unit ID.");
|
|
13633
13260
|
}
|
|
@@ -13642,9 +13269,9 @@ function MVisitorTransaction(value) {
|
|
|
13642
13269
|
if (value.visitorPass && Array.isArray(value.visitorPass) && value.visitorPass.length > 0) {
|
|
13643
13270
|
value.visitorPass = value.visitorPass.map((v) => {
|
|
13644
13271
|
if (typeof v === "string")
|
|
13645
|
-
return { keyId: new
|
|
13272
|
+
return { keyId: new ObjectId39(v) };
|
|
13646
13273
|
if (v?.keyId && typeof v.keyId === "string") {
|
|
13647
|
-
return { keyId: new
|
|
13274
|
+
return { keyId: new ObjectId39(v.keyId) };
|
|
13648
13275
|
}
|
|
13649
13276
|
return v;
|
|
13650
13277
|
});
|
|
@@ -13652,16 +13279,16 @@ function MVisitorTransaction(value) {
|
|
|
13652
13279
|
if (value.passKeys && Array.isArray(value.passKeys) && value.passKeys.length > 0) {
|
|
13653
13280
|
value.passKeys = value.passKeys.map((p) => {
|
|
13654
13281
|
if (typeof p === "string")
|
|
13655
|
-
return { keyId: new
|
|
13282
|
+
return { keyId: new ObjectId39(p) };
|
|
13656
13283
|
if (p?.keyId && typeof p.keyId === "string") {
|
|
13657
|
-
return { keyId: new
|
|
13284
|
+
return { keyId: new ObjectId39(p.keyId) };
|
|
13658
13285
|
}
|
|
13659
13286
|
return p;
|
|
13660
13287
|
});
|
|
13661
13288
|
}
|
|
13662
13289
|
if (value.inviterId && typeof value.inviterId === "string") {
|
|
13663
13290
|
try {
|
|
13664
|
-
value.inviterId = new
|
|
13291
|
+
value.inviterId = new ObjectId39(value.inviterId);
|
|
13665
13292
|
} catch (error2) {
|
|
13666
13293
|
throw new Error("Invalid inviter ID.");
|
|
13667
13294
|
}
|
|
@@ -13718,10 +13345,10 @@ function MVisitorTransaction(value) {
|
|
|
13718
13345
|
}
|
|
13719
13346
|
|
|
13720
13347
|
// src/repositories/visitor-transaction.repo.ts
|
|
13721
|
-
import { ObjectId as
|
|
13348
|
+
import { ObjectId as ObjectId40 } from "mongodb";
|
|
13722
13349
|
var visitors_namespace_collection = "visitor.transactions";
|
|
13723
13350
|
function useVisitorTransactionRepo() {
|
|
13724
|
-
const db =
|
|
13351
|
+
const db = useAtlas30.getDb();
|
|
13725
13352
|
if (!db) {
|
|
13726
13353
|
throw new InternalServerError24("Unable to connect to server.");
|
|
13727
13354
|
}
|
|
@@ -13751,7 +13378,7 @@ function useVisitorTransactionRepo() {
|
|
|
13751
13378
|
console.log("Error in add visitor transaction:", error);
|
|
13752
13379
|
const isDuplicated = error.message.includes("duplicate");
|
|
13753
13380
|
if (isDuplicated) {
|
|
13754
|
-
throw new
|
|
13381
|
+
throw new BadRequestError66("Visitor already exists.");
|
|
13755
13382
|
}
|
|
13756
13383
|
throw error;
|
|
13757
13384
|
}
|
|
@@ -13787,8 +13414,8 @@ function useVisitorTransactionRepo() {
|
|
|
13787
13414
|
expectedCheckInFilter.$lte = new Date(dateTo);
|
|
13788
13415
|
}
|
|
13789
13416
|
const query = {
|
|
13790
|
-
...
|
|
13791
|
-
...
|
|
13417
|
+
...ObjectId40.isValid(org) && { org: new ObjectId40(org) },
|
|
13418
|
+
...ObjectId40.isValid(site) && { site: new ObjectId40(site) },
|
|
13792
13419
|
...Object.keys(checkInFilter).length > 0 && { checkIn: checkInFilter },
|
|
13793
13420
|
...Object.keys(expectedCheckInFilter).length > 0 && {
|
|
13794
13421
|
expectedCheckIn: expectedCheckInFilter
|
|
@@ -13973,14 +13600,14 @@ function useVisitorTransactionRepo() {
|
|
|
13973
13600
|
collection.aggregate([...basePipeline, { $count: "total" }]).toArray()
|
|
13974
13601
|
]);
|
|
13975
13602
|
const totalCount = countResult[0]?.total || 0;
|
|
13976
|
-
const data =
|
|
13603
|
+
const data = paginate17(items, page, limit, totalCount);
|
|
13977
13604
|
return data;
|
|
13978
13605
|
} catch (error) {
|
|
13979
13606
|
throw error;
|
|
13980
13607
|
}
|
|
13981
13608
|
}
|
|
13982
13609
|
async function getVisitorTransactionById(id) {
|
|
13983
|
-
const _id =
|
|
13610
|
+
const _id = toObjectId7(id);
|
|
13984
13611
|
try {
|
|
13985
13612
|
const basePipeline = [{ $match: { _id } }];
|
|
13986
13613
|
const [result] = await collection.aggregate([
|
|
@@ -14022,7 +13649,7 @@ function useVisitorTransactionRepo() {
|
|
|
14022
13649
|
}
|
|
14023
13650
|
}
|
|
14024
13651
|
async function getOpenByPlateNumber(plateNumber, site) {
|
|
14025
|
-
const _site = typeof site === "string" ?
|
|
13652
|
+
const _site = typeof site === "string" ? toObjectId7(site) : site;
|
|
14026
13653
|
const query = {
|
|
14027
13654
|
plateNumber,
|
|
14028
13655
|
site: _site,
|
|
@@ -14039,9 +13666,9 @@ function useVisitorTransactionRepo() {
|
|
|
14039
13666
|
}
|
|
14040
13667
|
async function updateById(_id, value, session) {
|
|
14041
13668
|
try {
|
|
14042
|
-
_id = new
|
|
13669
|
+
_id = new ObjectId40(_id);
|
|
14043
13670
|
} catch (error) {
|
|
14044
|
-
throw new
|
|
13671
|
+
throw new BadRequestError66("Invalid visitor transaction ID format.");
|
|
14045
13672
|
}
|
|
14046
13673
|
value.updatedAt = /* @__PURE__ */ new Date();
|
|
14047
13674
|
if (value.checkOut) {
|
|
@@ -14060,9 +13687,9 @@ function useVisitorTransactionRepo() {
|
|
|
14060
13687
|
}
|
|
14061
13688
|
async function deleteVisitorTransaction(_id) {
|
|
14062
13689
|
try {
|
|
14063
|
-
_id = new
|
|
13690
|
+
_id = new ObjectId40(_id);
|
|
14064
13691
|
} catch (error) {
|
|
14065
|
-
throw new
|
|
13692
|
+
throw new BadRequestError66("Invalid customer ID format.");
|
|
14066
13693
|
}
|
|
14067
13694
|
try {
|
|
14068
13695
|
const updateValue = {
|
|
@@ -14106,7 +13733,7 @@ function useVisitorTransactionRepo() {
|
|
|
14106
13733
|
}
|
|
14107
13734
|
}
|
|
14108
13735
|
async function getExpiredCheckedOutTransactionsBySite(siteId) {
|
|
14109
|
-
const site =
|
|
13736
|
+
const site = toObjectId7(siteId);
|
|
14110
13737
|
try {
|
|
14111
13738
|
const now = /* @__PURE__ */ new Date();
|
|
14112
13739
|
const expiredTransactions = await collection.find({
|
|
@@ -14127,7 +13754,7 @@ function useVisitorTransactionRepo() {
|
|
|
14127
13754
|
async function updateManyDahuaSyncStatus(ids, dahuaSyncStatus, session) {
|
|
14128
13755
|
try {
|
|
14129
13756
|
const objectIds = ids.map(
|
|
14130
|
-
(id) => typeof id === "string" ? new
|
|
13757
|
+
(id) => typeof id === "string" ? new ObjectId40(id) : id
|
|
14131
13758
|
);
|
|
14132
13759
|
const res = await collection.updateMany(
|
|
14133
13760
|
{ _id: { $in: objectIds } },
|
|
@@ -14158,10 +13785,22 @@ function useVisitorTransactionRepo() {
|
|
|
14158
13785
|
};
|
|
14159
13786
|
}
|
|
14160
13787
|
|
|
13788
|
+
// src/repositories/vehicle.repo.ts
|
|
13789
|
+
import {
|
|
13790
|
+
AppError as AppError8,
|
|
13791
|
+
BadRequestError as BadRequestError68,
|
|
13792
|
+
InternalServerError as InternalServerError25,
|
|
13793
|
+
logger as logger49,
|
|
13794
|
+
NotFoundError as NotFoundError17,
|
|
13795
|
+
paginate as paginate18,
|
|
13796
|
+
toObjectId as toObjectId8,
|
|
13797
|
+
useAtlas as useAtlas31
|
|
13798
|
+
} from "@7365admin1/node-server-utils";
|
|
13799
|
+
|
|
14161
13800
|
// src/models/vehicle.model.ts
|
|
14162
|
-
import { BadRequestError as
|
|
13801
|
+
import { BadRequestError as BadRequestError67, logger as logger48 } from "@7365admin1/node-server-utils";
|
|
14163
13802
|
import Joi37 from "joi";
|
|
14164
|
-
import { ObjectId as
|
|
13803
|
+
import { ObjectId as ObjectId41 } from "mongodb";
|
|
14165
13804
|
var VehicleType = /* @__PURE__ */ ((VehicleType2) => {
|
|
14166
13805
|
VehicleType2["WHITELIST"] = "whitelist";
|
|
14167
13806
|
VehicleType2["BLOCKLIST"] = "blocklist";
|
|
@@ -14238,35 +13877,35 @@ var vehicleSchema = Joi37.object({
|
|
|
14238
13877
|
function MVehicle(value) {
|
|
14239
13878
|
const { error } = vehicleSchema.validate(value);
|
|
14240
13879
|
if (error) {
|
|
14241
|
-
|
|
14242
|
-
throw new
|
|
13880
|
+
logger48.log({ level: "error", message: error.message });
|
|
13881
|
+
throw new BadRequestError67(error.message);
|
|
14243
13882
|
}
|
|
14244
13883
|
if (value.org) {
|
|
14245
13884
|
try {
|
|
14246
|
-
value.org = new
|
|
13885
|
+
value.org = new ObjectId41(value.org);
|
|
14247
13886
|
} catch (error2) {
|
|
14248
|
-
throw new
|
|
13887
|
+
throw new BadRequestError67("Invalid org ID format.");
|
|
14249
13888
|
}
|
|
14250
13889
|
}
|
|
14251
13890
|
if (value.site) {
|
|
14252
13891
|
try {
|
|
14253
|
-
value.site = new
|
|
13892
|
+
value.site = new ObjectId41(value.site);
|
|
14254
13893
|
} catch (error2) {
|
|
14255
|
-
throw new
|
|
13894
|
+
throw new BadRequestError67("Invalid site ID format.");
|
|
14256
13895
|
}
|
|
14257
13896
|
}
|
|
14258
13897
|
if (value.unit) {
|
|
14259
13898
|
try {
|
|
14260
|
-
value.unit = new
|
|
13899
|
+
value.unit = new ObjectId41(value.unit);
|
|
14261
13900
|
} catch (error2) {
|
|
14262
|
-
throw new
|
|
13901
|
+
throw new BadRequestError67("Invalid building unit ID format.");
|
|
14263
13902
|
}
|
|
14264
13903
|
}
|
|
14265
13904
|
if (value.peopleId) {
|
|
14266
13905
|
try {
|
|
14267
|
-
value.peopleId = new
|
|
13906
|
+
value.peopleId = new ObjectId41(value.peopleId);
|
|
14268
13907
|
} catch (error2) {
|
|
14269
|
-
throw new
|
|
13908
|
+
throw new BadRequestError67("Invalid building unit ID format.");
|
|
14270
13909
|
}
|
|
14271
13910
|
}
|
|
14272
13911
|
const createdAtDate = value.createdAt ? new Date(value.createdAt) : /* @__PURE__ */ new Date();
|
|
@@ -14314,20 +13953,20 @@ var schemaVehicleTransaction = Joi37.object({
|
|
|
14314
13953
|
function MVehicleTransaction(value) {
|
|
14315
13954
|
const { error } = schemaVehicleTransaction.validate(value);
|
|
14316
13955
|
if (error) {
|
|
14317
|
-
|
|
14318
|
-
throw new
|
|
13956
|
+
logger48.log({ level: "error", message: error.message });
|
|
13957
|
+
throw new BadRequestError67(error.message);
|
|
14319
13958
|
}
|
|
14320
13959
|
if (value._id) {
|
|
14321
13960
|
try {
|
|
14322
|
-
value._id = new
|
|
13961
|
+
value._id = new ObjectId41(value._id);
|
|
14323
13962
|
} catch (error2) {
|
|
14324
|
-
throw new
|
|
13963
|
+
throw new BadRequestError67("Invalid ID.");
|
|
14325
13964
|
}
|
|
14326
13965
|
}
|
|
14327
13966
|
try {
|
|
14328
|
-
value.site = new
|
|
13967
|
+
value.site = new ObjectId41(value.site);
|
|
14329
13968
|
} catch (error2) {
|
|
14330
|
-
throw new
|
|
13969
|
+
throw new BadRequestError67("Invalid site ID.");
|
|
14331
13970
|
}
|
|
14332
13971
|
return {
|
|
14333
13972
|
_id: value._id,
|
|
@@ -14343,30 +13982,13 @@ function MVehicleTransaction(value) {
|
|
|
14343
13982
|
};
|
|
14344
13983
|
}
|
|
14345
13984
|
|
|
14346
|
-
// src/services/dahua.service.ts
|
|
14347
|
-
import { BadRequestError as BadRequestError74 } from "@7365admin1/node-server-utils";
|
|
14348
|
-
import { promises as fsAsync } from "fs";
|
|
14349
|
-
import Joi40 from "joi";
|
|
14350
|
-
import * as path from "path";
|
|
14351
|
-
import { request } from "urllib";
|
|
14352
|
-
|
|
14353
13985
|
// src/repositories/vehicle.repo.ts
|
|
14354
|
-
import {
|
|
14355
|
-
AppError as AppError9,
|
|
14356
|
-
BadRequestError as BadRequestError69,
|
|
14357
|
-
InternalServerError as InternalServerError25,
|
|
14358
|
-
logger as logger50,
|
|
14359
|
-
NotFoundError as NotFoundError17,
|
|
14360
|
-
paginate as paginate19,
|
|
14361
|
-
toObjectId as toObjectId9,
|
|
14362
|
-
useAtlas as useAtlas32
|
|
14363
|
-
} from "@7365admin1/node-server-utils";
|
|
14364
|
-
import { ObjectId as ObjectId43 } from "mongodb";
|
|
13986
|
+
import { ObjectId as ObjectId42 } from "mongodb";
|
|
14365
13987
|
import Joi38 from "joi";
|
|
14366
13988
|
var vehicles_namespace_collection = "vehicles";
|
|
14367
13989
|
function useVehicleRepo() {
|
|
14368
13990
|
vehicles_namespace_collection;
|
|
14369
|
-
const db =
|
|
13991
|
+
const db = useAtlas31.getDb();
|
|
14370
13992
|
if (!db) {
|
|
14371
13993
|
throw new InternalServerError25("Unable to connect to server.");
|
|
14372
13994
|
}
|
|
@@ -14404,11 +14026,11 @@ function useVehicleRepo() {
|
|
|
14404
14026
|
const res = await collection.insertOne(value, { session });
|
|
14405
14027
|
return res.insertedId;
|
|
14406
14028
|
} catch (error) {
|
|
14407
|
-
|
|
14029
|
+
logger49.log({
|
|
14408
14030
|
level: "error",
|
|
14409
14031
|
message: error.message
|
|
14410
14032
|
});
|
|
14411
|
-
if (error instanceof
|
|
14033
|
+
if (error instanceof AppError8) {
|
|
14412
14034
|
throw error;
|
|
14413
14035
|
} else {
|
|
14414
14036
|
throw new Error("Failed to create vehicle.");
|
|
@@ -14589,7 +14211,7 @@ function useVehicleRepo() {
|
|
|
14589
14211
|
const regexCountResult = await collection.aggregate(buildGroupedCountPipeline(regexQuery)).toArray();
|
|
14590
14212
|
length = regexCountResult[0]?.total || 0;
|
|
14591
14213
|
}
|
|
14592
|
-
const data =
|
|
14214
|
+
const data = paginate18(items, page, limit, length);
|
|
14593
14215
|
return data;
|
|
14594
14216
|
} catch (error) {
|
|
14595
14217
|
throw error;
|
|
@@ -14597,9 +14219,9 @@ function useVehicleRepo() {
|
|
|
14597
14219
|
}
|
|
14598
14220
|
async function getSeasonPassTypes(site) {
|
|
14599
14221
|
try {
|
|
14600
|
-
site = new
|
|
14222
|
+
site = new ObjectId42(site);
|
|
14601
14223
|
} catch (error) {
|
|
14602
|
-
throw new
|
|
14224
|
+
throw new BadRequestError68("Invalid site ID format.");
|
|
14603
14225
|
}
|
|
14604
14226
|
try {
|
|
14605
14227
|
const categories = await collection.aggregate([
|
|
@@ -14622,17 +14244,17 @@ function useVehicleRepo() {
|
|
|
14622
14244
|
]).toArray();
|
|
14623
14245
|
return categories;
|
|
14624
14246
|
} catch (error) {
|
|
14625
|
-
if (error instanceof
|
|
14247
|
+
if (error instanceof BadRequestError68) {
|
|
14626
14248
|
throw error;
|
|
14627
14249
|
}
|
|
14628
|
-
throw new
|
|
14250
|
+
throw new BadRequestError68("Failed to retrieve season pass types.");
|
|
14629
14251
|
}
|
|
14630
14252
|
}
|
|
14631
14253
|
async function getVehicleById(_id) {
|
|
14632
14254
|
try {
|
|
14633
|
-
_id = new
|
|
14255
|
+
_id = new ObjectId42(_id);
|
|
14634
14256
|
} catch (error) {
|
|
14635
|
-
throw new
|
|
14257
|
+
throw new BadRequestError68("Invalid vehicle ID format.");
|
|
14636
14258
|
}
|
|
14637
14259
|
try {
|
|
14638
14260
|
const data = await collection.aggregate([
|
|
@@ -14812,7 +14434,7 @@ function useVehicleRepo() {
|
|
|
14812
14434
|
async function getByPlaceNumber(value) {
|
|
14813
14435
|
const { error } = Joi38.string().required().validate(value);
|
|
14814
14436
|
if (error) {
|
|
14815
|
-
throw new
|
|
14437
|
+
throw new BadRequestError68(error.details[0].message);
|
|
14816
14438
|
}
|
|
14817
14439
|
try {
|
|
14818
14440
|
const data = await collection.findOne({
|
|
@@ -14829,9 +14451,9 @@ function useVehicleRepo() {
|
|
|
14829
14451
|
}
|
|
14830
14452
|
async function updateVehicleById(_id, value, session) {
|
|
14831
14453
|
try {
|
|
14832
|
-
_id = new
|
|
14454
|
+
_id = new ObjectId42(_id);
|
|
14833
14455
|
} catch (error) {
|
|
14834
|
-
throw new
|
|
14456
|
+
throw new BadRequestError68("Invalid vehicle ID format.");
|
|
14835
14457
|
}
|
|
14836
14458
|
try {
|
|
14837
14459
|
const updateValue = {
|
|
@@ -14853,9 +14475,9 @@ function useVehicleRepo() {
|
|
|
14853
14475
|
}
|
|
14854
14476
|
async function deleteVehicle(_id, session) {
|
|
14855
14477
|
try {
|
|
14856
|
-
_id = new
|
|
14478
|
+
_id = new ObjectId42(_id);
|
|
14857
14479
|
} catch (error) {
|
|
14858
|
-
throw new
|
|
14480
|
+
throw new BadRequestError68("Invalid vehicle ID format.");
|
|
14859
14481
|
}
|
|
14860
14482
|
try {
|
|
14861
14483
|
const updateValue = {
|
|
@@ -14877,7 +14499,7 @@ function useVehicleRepo() {
|
|
|
14877
14499
|
async function getVehicleByPlateNumber(plateNumber) {
|
|
14878
14500
|
const { error } = Joi38.string().required().validate(plateNumber);
|
|
14879
14501
|
if (error) {
|
|
14880
|
-
throw new
|
|
14502
|
+
throw new BadRequestError68(error.details[0].message);
|
|
14881
14503
|
}
|
|
14882
14504
|
try {
|
|
14883
14505
|
const data = await collection.findOne(
|
|
@@ -14899,7 +14521,7 @@ function useVehicleRepo() {
|
|
|
14899
14521
|
}) {
|
|
14900
14522
|
page = page > 0 ? page - 1 : 0;
|
|
14901
14523
|
if (!nric) {
|
|
14902
|
-
throw new
|
|
14524
|
+
throw new BadRequestError68("NRIC is required.");
|
|
14903
14525
|
}
|
|
14904
14526
|
const _nric = nric.trim();
|
|
14905
14527
|
const query = {
|
|
@@ -14926,7 +14548,7 @@ function useVehicleRepo() {
|
|
|
14926
14548
|
}
|
|
14927
14549
|
]).toArray();
|
|
14928
14550
|
const length = await collection.countDocuments(query);
|
|
14929
|
-
const data =
|
|
14551
|
+
const data = paginate18(items, page, limit, length);
|
|
14930
14552
|
return data;
|
|
14931
14553
|
} catch (error) {
|
|
14932
14554
|
throw error;
|
|
@@ -14958,7 +14580,7 @@ function useVehicleRepo() {
|
|
|
14958
14580
|
page = page > 0 ? page - 1 : 0;
|
|
14959
14581
|
const skip = page * limit;
|
|
14960
14582
|
try {
|
|
14961
|
-
const unit =
|
|
14583
|
+
const unit = toObjectId8(unitId);
|
|
14962
14584
|
const query = {
|
|
14963
14585
|
unit
|
|
14964
14586
|
};
|
|
@@ -14973,7 +14595,7 @@ function useVehicleRepo() {
|
|
|
14973
14595
|
limit
|
|
14974
14596
|
}).toArray();
|
|
14975
14597
|
const length = await collection.countDocuments(query);
|
|
14976
|
-
const data =
|
|
14598
|
+
const data = paginate18(items, page, limit, length);
|
|
14977
14599
|
return data;
|
|
14978
14600
|
} catch (error) {
|
|
14979
14601
|
throw error;
|
|
@@ -15009,7 +14631,7 @@ function useVehicleRepo() {
|
|
|
15009
14631
|
return {
|
|
15010
14632
|
updateOne: {
|
|
15011
14633
|
filter: {
|
|
15012
|
-
site: new
|
|
14634
|
+
site: new ObjectId42(vehicle.site),
|
|
15013
14635
|
plateNumber,
|
|
15014
14636
|
$or: [
|
|
15015
14637
|
{ deletedAt: "" },
|
|
@@ -15020,9 +14642,9 @@ function useVehicleRepo() {
|
|
|
15020
14642
|
update: {
|
|
15021
14643
|
$set: {
|
|
15022
14644
|
...rest,
|
|
15023
|
-
site: new
|
|
15024
|
-
unit: vehicle.unit ? new
|
|
15025
|
-
org: vehicle.org ? new
|
|
14645
|
+
site: new ObjectId42(vehicle.site),
|
|
14646
|
+
unit: vehicle.unit ? new ObjectId42(vehicle.unit) : null,
|
|
14647
|
+
org: vehicle.org ? new ObjectId42(vehicle.org) : null,
|
|
15026
14648
|
plateNumber,
|
|
15027
14649
|
updatedAt: now
|
|
15028
14650
|
},
|
|
@@ -15042,11 +14664,11 @@ function useVehicleRepo() {
|
|
|
15042
14664
|
upsertedIds: res.upsertedIds
|
|
15043
14665
|
};
|
|
15044
14666
|
} catch (error) {
|
|
15045
|
-
|
|
14667
|
+
logger49.log({
|
|
15046
14668
|
level: "error",
|
|
15047
14669
|
message: error.message
|
|
15048
14670
|
});
|
|
15049
|
-
if (error instanceof
|
|
14671
|
+
if (error instanceof AppError8) {
|
|
15050
14672
|
throw error;
|
|
15051
14673
|
}
|
|
15052
14674
|
throw new Error("Failed to bulk upsert vehicles.");
|
|
@@ -15073,32 +14695,32 @@ function useVehicleRepo() {
|
|
|
15073
14695
|
|
|
15074
14696
|
// src/services/vehicle.service.ts
|
|
15075
14697
|
import {
|
|
15076
|
-
BadRequestError as
|
|
15077
|
-
logger as
|
|
15078
|
-
useAtlas as
|
|
14698
|
+
BadRequestError as BadRequestError72,
|
|
14699
|
+
logger as logger53,
|
|
14700
|
+
useAtlas as useAtlas34
|
|
15079
14701
|
} from "@7365admin1/node-server-utils";
|
|
15080
14702
|
|
|
15081
14703
|
// src/repositories/person.repo.ts
|
|
15082
14704
|
import {
|
|
15083
|
-
BadRequestError as
|
|
14705
|
+
BadRequestError as BadRequestError69,
|
|
15084
14706
|
InternalServerError as InternalServerError26,
|
|
15085
|
-
logger as
|
|
15086
|
-
makeCacheKey as
|
|
15087
|
-
paginate as
|
|
15088
|
-
useAtlas as
|
|
15089
|
-
useCache as
|
|
15090
|
-
AppError as
|
|
15091
|
-
toObjectId as
|
|
14707
|
+
logger as logger50,
|
|
14708
|
+
makeCacheKey as makeCacheKey23,
|
|
14709
|
+
paginate as paginate19,
|
|
14710
|
+
useAtlas as useAtlas32,
|
|
14711
|
+
useCache as useCache24,
|
|
14712
|
+
AppError as AppError9,
|
|
14713
|
+
toObjectId as toObjectId9
|
|
15092
14714
|
} from "@7365admin1/node-server-utils";
|
|
15093
|
-
import { ObjectId as
|
|
14715
|
+
import { ObjectId as ObjectId43 } from "mongodb";
|
|
15094
14716
|
var site_people_namespace_collection = "site.people";
|
|
15095
14717
|
function usePersonRepo() {
|
|
15096
|
-
const db =
|
|
14718
|
+
const db = useAtlas32.getDb();
|
|
15097
14719
|
if (!db) {
|
|
15098
14720
|
throw new InternalServerError26("Unable to connect to server.");
|
|
15099
14721
|
}
|
|
15100
14722
|
const collection = db.collection(site_people_namespace_collection);
|
|
15101
|
-
const { delNamespace, getCache, setCache } =
|
|
14723
|
+
const { delNamespace, getCache, setCache } = useCache24(
|
|
15102
14724
|
site_people_namespace_collection
|
|
15103
14725
|
);
|
|
15104
14726
|
async function createIndexes() {
|
|
@@ -15132,11 +14754,11 @@ function usePersonRepo() {
|
|
|
15132
14754
|
value = MPerson(value);
|
|
15133
14755
|
const res = await collection.insertOne(value, { session });
|
|
15134
14756
|
delNamespace().then(() => {
|
|
15135
|
-
|
|
14757
|
+
logger50.info(
|
|
15136
14758
|
`Cache cleared for namespace: ${site_people_namespace_collection}`
|
|
15137
14759
|
);
|
|
15138
14760
|
}).catch((err) => {
|
|
15139
|
-
|
|
14761
|
+
logger50.error(
|
|
15140
14762
|
`Failed to clear cache for namespace: ${site_people_namespace_collection}`,
|
|
15141
14763
|
err
|
|
15142
14764
|
);
|
|
@@ -15145,7 +14767,7 @@ function usePersonRepo() {
|
|
|
15145
14767
|
} catch (error) {
|
|
15146
14768
|
const isDuplicated = error.message.includes("duplicate");
|
|
15147
14769
|
if (isDuplicated) {
|
|
15148
|
-
throw new
|
|
14770
|
+
throw new BadRequestError69("Person already exists.");
|
|
15149
14771
|
}
|
|
15150
14772
|
throw error;
|
|
15151
14773
|
}
|
|
@@ -15182,8 +14804,8 @@ function usePersonRepo() {
|
|
|
15182
14804
|
{ "plates.plateNumber": { $regex: search, $options: "i" } }
|
|
15183
14805
|
]
|
|
15184
14806
|
},
|
|
15185
|
-
...
|
|
15186
|
-
...
|
|
14807
|
+
...ObjectId43.isValid(org) && { org: new ObjectId43(org) },
|
|
14808
|
+
...ObjectId43.isValid(site) && { site: new ObjectId43(site) },
|
|
15187
14809
|
...PERSON_TYPES.includes(type) && { type }
|
|
15188
14810
|
};
|
|
15189
14811
|
sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
|
|
@@ -15199,13 +14821,13 @@ function usePersonRepo() {
|
|
|
15199
14821
|
...query.site && { site: query.site.toString() },
|
|
15200
14822
|
...PERSON_TYPES.includes(type) && { type }
|
|
15201
14823
|
};
|
|
15202
|
-
const cacheKey =
|
|
14824
|
+
const cacheKey = makeCacheKey23(
|
|
15203
14825
|
site_people_namespace_collection,
|
|
15204
14826
|
cacheOptions
|
|
15205
14827
|
);
|
|
15206
14828
|
const cachedData = await getCache(cacheKey);
|
|
15207
14829
|
if (cachedData) {
|
|
15208
|
-
|
|
14830
|
+
logger50.info(`Cache hit for key: ${cacheKey}`);
|
|
15209
14831
|
return cachedData;
|
|
15210
14832
|
}
|
|
15211
14833
|
try {
|
|
@@ -15237,9 +14859,9 @@ function usePersonRepo() {
|
|
|
15237
14859
|
collection.aggregate([{ $match: query }, { $count: "total" }], { session }).toArray()
|
|
15238
14860
|
]);
|
|
15239
14861
|
const totalCount = countResult[0]?.total || 0;
|
|
15240
|
-
const data =
|
|
15241
|
-
setCache(cacheKey, data, 15 * 60).then(() =>
|
|
15242
|
-
(err) =>
|
|
14862
|
+
const data = paginate19(items, page, limit, totalCount);
|
|
14863
|
+
setCache(cacheKey, data, 15 * 60).then(() => logger50.info(`Cache set for key: ${cacheKey}`)).catch(
|
|
14864
|
+
(err) => logger50.error(`Failed to set cache for key: ${cacheKey}`, err)
|
|
15243
14865
|
);
|
|
15244
14866
|
return data;
|
|
15245
14867
|
} catch (error) {
|
|
@@ -15248,9 +14870,9 @@ function usePersonRepo() {
|
|
|
15248
14870
|
}
|
|
15249
14871
|
async function updateById(_id, value, session) {
|
|
15250
14872
|
try {
|
|
15251
|
-
_id = new
|
|
14873
|
+
_id = new ObjectId43(_id);
|
|
15252
14874
|
} catch (error) {
|
|
15253
|
-
throw new
|
|
14875
|
+
throw new BadRequestError69("Invalid person transaction ID format.");
|
|
15254
14876
|
}
|
|
15255
14877
|
value.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
15256
14878
|
try {
|
|
@@ -15260,11 +14882,11 @@ function usePersonRepo() {
|
|
|
15260
14882
|
{ session }
|
|
15261
14883
|
);
|
|
15262
14884
|
delNamespace().then(() => {
|
|
15263
|
-
|
|
14885
|
+
logger50.info(
|
|
15264
14886
|
`Cache cleared for namespace: ${site_people_namespace_collection}`
|
|
15265
14887
|
);
|
|
15266
14888
|
}).catch((err) => {
|
|
15267
|
-
|
|
14889
|
+
logger50.error(
|
|
15268
14890
|
`Failed to clear cache for namespace: ${site_people_namespace_collection}`,
|
|
15269
14891
|
err
|
|
15270
14892
|
);
|
|
@@ -15276,9 +14898,9 @@ function usePersonRepo() {
|
|
|
15276
14898
|
}
|
|
15277
14899
|
async function deleteById(_id) {
|
|
15278
14900
|
try {
|
|
15279
|
-
_id = new
|
|
14901
|
+
_id = new ObjectId43(_id);
|
|
15280
14902
|
} catch (error) {
|
|
15281
|
-
throw new
|
|
14903
|
+
throw new BadRequestError69("Invalid customer ID format.");
|
|
15282
14904
|
}
|
|
15283
14905
|
try {
|
|
15284
14906
|
const updateValue = {
|
|
@@ -15291,11 +14913,11 @@ function usePersonRepo() {
|
|
|
15291
14913
|
throw new InternalServerError26("Unable to delete visitor transaction.");
|
|
15292
14914
|
}
|
|
15293
14915
|
delNamespace().then(() => {
|
|
15294
|
-
|
|
14916
|
+
logger50.info(
|
|
15295
14917
|
`Cache cleared for namespace: ${site_people_namespace_collection}`
|
|
15296
14918
|
);
|
|
15297
14919
|
}).catch((err) => {
|
|
15298
|
-
|
|
14920
|
+
logger50.error(
|
|
15299
14921
|
`Failed to clear cache for namespace: ${site_people_namespace_collection}`,
|
|
15300
14922
|
err
|
|
15301
14923
|
);
|
|
@@ -15307,17 +14929,17 @@ function usePersonRepo() {
|
|
|
15307
14929
|
}
|
|
15308
14930
|
async function getById(_id) {
|
|
15309
14931
|
try {
|
|
15310
|
-
_id = new
|
|
14932
|
+
_id = new ObjectId43(_id);
|
|
15311
14933
|
} catch (error) {
|
|
15312
|
-
throw new
|
|
14934
|
+
throw new BadRequestError69("Invalid person ID.");
|
|
15313
14935
|
}
|
|
15314
|
-
const cacheKey =
|
|
14936
|
+
const cacheKey = makeCacheKey23(site_people_namespace_collection, {
|
|
15315
14937
|
_id: String(_id)
|
|
15316
14938
|
});
|
|
15317
14939
|
try {
|
|
15318
14940
|
const cached = await getCache(cacheKey);
|
|
15319
14941
|
if (cached) {
|
|
15320
|
-
|
|
14942
|
+
logger50.log({
|
|
15321
14943
|
level: "info",
|
|
15322
14944
|
message: `Cache hit for getById person: ${cacheKey}`
|
|
15323
14945
|
});
|
|
@@ -15327,19 +14949,19 @@ function usePersonRepo() {
|
|
|
15327
14949
|
_id
|
|
15328
14950
|
});
|
|
15329
14951
|
setCache(cacheKey, result, 300).then(() => {
|
|
15330
|
-
|
|
14952
|
+
logger50.log({
|
|
15331
14953
|
level: "info",
|
|
15332
14954
|
message: `Cache set for person by id: ${cacheKey}`
|
|
15333
14955
|
});
|
|
15334
14956
|
}).catch((err) => {
|
|
15335
|
-
|
|
14957
|
+
logger50.log({
|
|
15336
14958
|
level: "error",
|
|
15337
14959
|
message: `Failed to set cache for person by id: ${err.message}`
|
|
15338
14960
|
});
|
|
15339
14961
|
});
|
|
15340
14962
|
return result;
|
|
15341
14963
|
} catch (error) {
|
|
15342
|
-
if (error instanceof
|
|
14964
|
+
if (error instanceof AppError9) {
|
|
15343
14965
|
throw error;
|
|
15344
14966
|
} else {
|
|
15345
14967
|
throw new InternalServerError26("Failed to get person by id.");
|
|
@@ -15347,14 +14969,14 @@ function usePersonRepo() {
|
|
|
15347
14969
|
}
|
|
15348
14970
|
}
|
|
15349
14971
|
async function getPersonByPlateNumber(plateNumber) {
|
|
15350
|
-
const cacheKey =
|
|
14972
|
+
const cacheKey = makeCacheKey23(site_people_namespace_collection, {
|
|
15351
14973
|
plateNumber,
|
|
15352
14974
|
key: "get-person-plate-number"
|
|
15353
14975
|
});
|
|
15354
14976
|
try {
|
|
15355
14977
|
const cacheData = await getCache(cacheKey);
|
|
15356
14978
|
if (cacheData) {
|
|
15357
|
-
|
|
14979
|
+
logger50.info(`Cache hit for key: ${cacheKey}`);
|
|
15358
14980
|
return cacheData;
|
|
15359
14981
|
}
|
|
15360
14982
|
const data = await collection.findOne(
|
|
@@ -15362,9 +14984,9 @@ function usePersonRepo() {
|
|
|
15362
14984
|
{ sort: { _id: -1 } }
|
|
15363
14985
|
);
|
|
15364
14986
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
15365
|
-
|
|
14987
|
+
logger50.info(`Cache set for key: ${cacheKey}`);
|
|
15366
14988
|
}).catch((err) => {
|
|
15367
|
-
|
|
14989
|
+
logger50.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
15368
14990
|
});
|
|
15369
14991
|
return data;
|
|
15370
14992
|
} catch (error) {
|
|
@@ -15375,19 +14997,19 @@ function usePersonRepo() {
|
|
|
15375
14997
|
}
|
|
15376
14998
|
async function getByNRIC(value) {
|
|
15377
14999
|
try {
|
|
15378
|
-
const cacheKey =
|
|
15000
|
+
const cacheKey = makeCacheKey23(site_people_namespace_collection, {
|
|
15379
15001
|
nric: value
|
|
15380
15002
|
});
|
|
15381
15003
|
const cachedData = await getCache(cacheKey);
|
|
15382
15004
|
if (cachedData) {
|
|
15383
|
-
|
|
15005
|
+
logger50.info(`Cache hit for key: ${cacheKey}`);
|
|
15384
15006
|
return cachedData;
|
|
15385
15007
|
}
|
|
15386
15008
|
const data = await collection.findOne({ nric: value });
|
|
15387
15009
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
15388
|
-
|
|
15010
|
+
logger50.info(`Cache set for key: ${cacheKey}`);
|
|
15389
15011
|
}).catch((err) => {
|
|
15390
|
-
|
|
15012
|
+
logger50.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
15391
15013
|
});
|
|
15392
15014
|
return data;
|
|
15393
15015
|
} catch (error) {
|
|
@@ -15396,19 +15018,19 @@ function usePersonRepo() {
|
|
|
15396
15018
|
}
|
|
15397
15019
|
async function getPersonByPhoneNumber(value) {
|
|
15398
15020
|
try {
|
|
15399
|
-
const cacheKey =
|
|
15021
|
+
const cacheKey = makeCacheKey23(site_people_namespace_collection, {
|
|
15400
15022
|
contact: value
|
|
15401
15023
|
});
|
|
15402
15024
|
const cachedData = await getCache(cacheKey);
|
|
15403
15025
|
if (cachedData) {
|
|
15404
|
-
|
|
15026
|
+
logger50.info(`Cache hit for key: ${cacheKey}`);
|
|
15405
15027
|
return cachedData;
|
|
15406
15028
|
}
|
|
15407
15029
|
const data = await collection.findOne({ contact: value });
|
|
15408
15030
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
15409
|
-
|
|
15031
|
+
logger50.info(`Cache set for key: ${cacheKey}`);
|
|
15410
15032
|
}).catch((err) => {
|
|
15411
|
-
|
|
15033
|
+
logger50.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
15412
15034
|
});
|
|
15413
15035
|
return data;
|
|
15414
15036
|
} catch (error) {
|
|
@@ -15422,7 +15044,7 @@ function usePersonRepo() {
|
|
|
15422
15044
|
type = [],
|
|
15423
15045
|
unit
|
|
15424
15046
|
}, session) {
|
|
15425
|
-
const cacheKey =
|
|
15047
|
+
const cacheKey = makeCacheKey23(site_people_namespace_collection, {
|
|
15426
15048
|
unit: JSON.stringify(unit),
|
|
15427
15049
|
status,
|
|
15428
15050
|
type,
|
|
@@ -15430,7 +15052,7 @@ function usePersonRepo() {
|
|
|
15430
15052
|
});
|
|
15431
15053
|
const cacheData = await getCache(cacheKey);
|
|
15432
15054
|
if (cacheData) {
|
|
15433
|
-
|
|
15055
|
+
logger50.info(`Cache hit for key: ${cacheKey}`);
|
|
15434
15056
|
return cacheData;
|
|
15435
15057
|
}
|
|
15436
15058
|
try {
|
|
@@ -15443,9 +15065,9 @@ function usePersonRepo() {
|
|
|
15443
15065
|
};
|
|
15444
15066
|
const data = await collection.find(query).sort({ _id: -1 }).toArray();
|
|
15445
15067
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
15446
|
-
|
|
15068
|
+
logger50.info(`Cache set for key: ${cacheKey}`);
|
|
15447
15069
|
}).catch((err) => {
|
|
15448
|
-
|
|
15070
|
+
logger50.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
15449
15071
|
});
|
|
15450
15072
|
return data;
|
|
15451
15073
|
} catch (error) {
|
|
@@ -15456,12 +15078,12 @@ function usePersonRepo() {
|
|
|
15456
15078
|
}
|
|
15457
15079
|
async function getCompany(search) {
|
|
15458
15080
|
try {
|
|
15459
|
-
const cacheKey =
|
|
15081
|
+
const cacheKey = makeCacheKey23(site_people_namespace_collection, {
|
|
15460
15082
|
company: search
|
|
15461
15083
|
});
|
|
15462
15084
|
const cachedData = await getCache(cacheKey);
|
|
15463
15085
|
if (cachedData) {
|
|
15464
|
-
|
|
15086
|
+
logger50.info(`Cache hit for key: ${cacheKey}`);
|
|
15465
15087
|
return cachedData;
|
|
15466
15088
|
}
|
|
15467
15089
|
const baseQuery = {
|
|
@@ -15505,9 +15127,9 @@ function usePersonRepo() {
|
|
|
15505
15127
|
]).toArray();
|
|
15506
15128
|
}
|
|
15507
15129
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
15508
|
-
|
|
15130
|
+
logger50.info(`Cache set for key: ${cacheKey}`);
|
|
15509
15131
|
}).catch((err) => {
|
|
15510
|
-
|
|
15132
|
+
logger50.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
15511
15133
|
});
|
|
15512
15134
|
return data;
|
|
15513
15135
|
} catch (error) {
|
|
@@ -15515,14 +15137,14 @@ function usePersonRepo() {
|
|
|
15515
15137
|
}
|
|
15516
15138
|
}
|
|
15517
15139
|
async function getPeopleByPlateNumber(plateNumber) {
|
|
15518
|
-
const cacheKey =
|
|
15140
|
+
const cacheKey = makeCacheKey23(site_people_namespace_collection, {
|
|
15519
15141
|
plateNumber,
|
|
15520
15142
|
key: "get-people-plate-number"
|
|
15521
15143
|
});
|
|
15522
15144
|
try {
|
|
15523
15145
|
const cacheData = await getCache(cacheKey);
|
|
15524
15146
|
if (cacheData) {
|
|
15525
|
-
|
|
15147
|
+
logger50.info(`Cache hit for key: ${cacheKey}`);
|
|
15526
15148
|
return cacheData;
|
|
15527
15149
|
}
|
|
15528
15150
|
const data = await collection.find(
|
|
@@ -15530,9 +15152,9 @@ function usePersonRepo() {
|
|
|
15530
15152
|
{ sort: { _id: -1 } }
|
|
15531
15153
|
).toArray();
|
|
15532
15154
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
15533
|
-
|
|
15155
|
+
logger50.info(`Cache set for key: ${cacheKey}`);
|
|
15534
15156
|
}).catch((err) => {
|
|
15535
|
-
|
|
15157
|
+
logger50.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
15536
15158
|
});
|
|
15537
15159
|
return data;
|
|
15538
15160
|
} catch (error) {
|
|
@@ -15551,7 +15173,7 @@ function usePersonRepo() {
|
|
|
15551
15173
|
sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
|
|
15552
15174
|
const query = {
|
|
15553
15175
|
...nric && { nric: normalizedNric },
|
|
15554
|
-
site: typeof site === "string" ? new
|
|
15176
|
+
site: typeof site === "string" ? new ObjectId43(site) : site
|
|
15555
15177
|
};
|
|
15556
15178
|
const cacheOptions = {
|
|
15557
15179
|
site,
|
|
@@ -15561,22 +15183,22 @@ function usePersonRepo() {
|
|
|
15561
15183
|
sort: JSON.stringify(sort)
|
|
15562
15184
|
};
|
|
15563
15185
|
try {
|
|
15564
|
-
const cacheKey =
|
|
15186
|
+
const cacheKey = makeCacheKey23(
|
|
15565
15187
|
site_people_namespace_collection,
|
|
15566
15188
|
cacheOptions
|
|
15567
15189
|
);
|
|
15568
15190
|
const cachedData = await getCache(cacheKey);
|
|
15569
15191
|
if (cachedData) {
|
|
15570
|
-
|
|
15192
|
+
logger50.info(`Cache hit for key: ${cacheKey}`);
|
|
15571
15193
|
return cachedData;
|
|
15572
15194
|
}
|
|
15573
15195
|
const items = await collection.find(query).sort(sort).skip(page * limit).limit(limit).toArray();
|
|
15574
15196
|
const length = await collection.countDocuments(query);
|
|
15575
|
-
const data =
|
|
15197
|
+
const data = paginate19(items, page, limit, length);
|
|
15576
15198
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
15577
|
-
|
|
15199
|
+
logger50.info(`Cache set for key: ${cacheKey}`);
|
|
15578
15200
|
}).catch((err) => {
|
|
15579
|
-
|
|
15201
|
+
logger50.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
15580
15202
|
});
|
|
15581
15203
|
return data;
|
|
15582
15204
|
} catch (error) {
|
|
@@ -15586,7 +15208,7 @@ function usePersonRepo() {
|
|
|
15586
15208
|
async function pushVehicleById(id, plate, session) {
|
|
15587
15209
|
const { plateNumber, recNo } = plate;
|
|
15588
15210
|
const updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
15589
|
-
const _id =
|
|
15211
|
+
const _id = toObjectId9(id);
|
|
15590
15212
|
try {
|
|
15591
15213
|
const updateExisting = await collection.updateOne(
|
|
15592
15214
|
{
|
|
@@ -15644,14 +15266,14 @@ function usePersonRepo() {
|
|
|
15644
15266
|
}
|
|
15645
15267
|
}
|
|
15646
15268
|
async function getByUserId(userId) {
|
|
15647
|
-
const user =
|
|
15648
|
-
const cacheKey =
|
|
15269
|
+
const user = toObjectId9(userId);
|
|
15270
|
+
const cacheKey = makeCacheKey23(site_people_namespace_collection, {
|
|
15649
15271
|
user: userId
|
|
15650
15272
|
});
|
|
15651
15273
|
try {
|
|
15652
15274
|
const cached = await getCache(cacheKey);
|
|
15653
15275
|
if (cached) {
|
|
15654
|
-
|
|
15276
|
+
logger50.log({
|
|
15655
15277
|
level: "info",
|
|
15656
15278
|
message: `Cache hit for getByUserId person: ${cacheKey}`
|
|
15657
15279
|
});
|
|
@@ -15661,19 +15283,19 @@ function usePersonRepo() {
|
|
|
15661
15283
|
user
|
|
15662
15284
|
});
|
|
15663
15285
|
setCache(cacheKey, result, 15 * 60).then(() => {
|
|
15664
|
-
|
|
15286
|
+
logger50.log({
|
|
15665
15287
|
level: "info",
|
|
15666
15288
|
message: `Cache set for person by user: ${cacheKey}`
|
|
15667
15289
|
});
|
|
15668
15290
|
}).catch((err) => {
|
|
15669
|
-
|
|
15291
|
+
logger50.log({
|
|
15670
15292
|
level: "error",
|
|
15671
15293
|
message: `Failed to set cache for person by user: ${err.message}`
|
|
15672
15294
|
});
|
|
15673
15295
|
});
|
|
15674
15296
|
return result;
|
|
15675
15297
|
} catch (error) {
|
|
15676
|
-
if (error instanceof
|
|
15298
|
+
if (error instanceof AppError9) {
|
|
15677
15299
|
throw error;
|
|
15678
15300
|
} else {
|
|
15679
15301
|
throw new InternalServerError26("Failed to get person by user.");
|
|
@@ -15683,9 +15305,9 @@ function usePersonRepo() {
|
|
|
15683
15305
|
async function reviewResidentPerson(_id, value, session) {
|
|
15684
15306
|
value.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
15685
15307
|
try {
|
|
15686
|
-
_id = new
|
|
15308
|
+
_id = new ObjectId43(_id);
|
|
15687
15309
|
} catch (error) {
|
|
15688
|
-
throw new
|
|
15310
|
+
throw new BadRequestError69("Invalid ID format.");
|
|
15689
15311
|
}
|
|
15690
15312
|
try {
|
|
15691
15313
|
const res = await collection.updateOne(
|
|
@@ -15697,11 +15319,11 @@ function usePersonRepo() {
|
|
|
15697
15319
|
throw new InternalServerError26(`Unable to ${value.status} person.`);
|
|
15698
15320
|
}
|
|
15699
15321
|
delNamespace().then(() => {
|
|
15700
|
-
|
|
15322
|
+
logger50.info(
|
|
15701
15323
|
`Cache cleared for namespace: ${site_people_namespace_collection}`
|
|
15702
15324
|
);
|
|
15703
15325
|
}).catch((err) => {
|
|
15704
|
-
|
|
15326
|
+
logger50.error(
|
|
15705
15327
|
`Failed to clear cache for namespace: ${site_people_namespace_collection}`,
|
|
15706
15328
|
err
|
|
15707
15329
|
);
|
|
@@ -15734,25 +15356,25 @@ function usePersonRepo() {
|
|
|
15734
15356
|
}
|
|
15735
15357
|
|
|
15736
15358
|
// src/services/vehicle.service.ts
|
|
15737
|
-
import { ObjectId as
|
|
15359
|
+
import { ObjectId as ObjectId46 } from "mongodb";
|
|
15738
15360
|
|
|
15739
15361
|
// src/repositories/building-unit.repository.ts
|
|
15740
15362
|
import {
|
|
15741
|
-
AppError as
|
|
15742
|
-
BadRequestError as
|
|
15363
|
+
AppError as AppError10,
|
|
15364
|
+
BadRequestError as BadRequestError71,
|
|
15743
15365
|
InternalServerError as InternalServerError27,
|
|
15744
|
-
logger as
|
|
15745
|
-
makeCacheKey as
|
|
15746
|
-
paginate as
|
|
15747
|
-
toObjectId as
|
|
15748
|
-
useAtlas as
|
|
15749
|
-
useCache as
|
|
15366
|
+
logger as logger52,
|
|
15367
|
+
makeCacheKey as makeCacheKey24,
|
|
15368
|
+
paginate as paginate20,
|
|
15369
|
+
toObjectId as toObjectId10,
|
|
15370
|
+
useAtlas as useAtlas33,
|
|
15371
|
+
useCache as useCache25
|
|
15750
15372
|
} from "@7365admin1/node-server-utils";
|
|
15751
15373
|
|
|
15752
15374
|
// src/models/building.model.ts
|
|
15753
|
-
import { BadRequestError as
|
|
15375
|
+
import { BadRequestError as BadRequestError70, logger as logger51 } from "@7365admin1/node-server-utils";
|
|
15754
15376
|
import Joi39 from "joi";
|
|
15755
|
-
import { ObjectId as
|
|
15377
|
+
import { ObjectId as ObjectId44 } from "mongodb";
|
|
15756
15378
|
var SortFields = /* @__PURE__ */ ((SortFields2) => {
|
|
15757
15379
|
SortFields2["ID"] = "_id";
|
|
15758
15380
|
SortFields2["CREATED_AT"] = "createdAt";
|
|
@@ -15841,20 +15463,20 @@ var schemaUpdateOptions = Joi39.object({
|
|
|
15841
15463
|
function MBuilding(value) {
|
|
15842
15464
|
const { error } = schemaBuilding.validate(value);
|
|
15843
15465
|
if (error) {
|
|
15844
|
-
|
|
15845
|
-
throw new
|
|
15466
|
+
logger51.info(`Building Model: ${error.message}`);
|
|
15467
|
+
throw new BadRequestError70(error.message);
|
|
15846
15468
|
}
|
|
15847
15469
|
if (value._id && typeof value._id === "string") {
|
|
15848
15470
|
try {
|
|
15849
|
-
value._id = new
|
|
15471
|
+
value._id = new ObjectId44(value._id);
|
|
15850
15472
|
} catch (error2) {
|
|
15851
|
-
throw new
|
|
15473
|
+
throw new BadRequestError70("Invalid _id format");
|
|
15852
15474
|
}
|
|
15853
15475
|
}
|
|
15854
15476
|
try {
|
|
15855
|
-
value.site = new
|
|
15477
|
+
value.site = new ObjectId44(value.site);
|
|
15856
15478
|
} catch (error2) {
|
|
15857
|
-
throw new
|
|
15479
|
+
throw new BadRequestError70("Invalid site format");
|
|
15858
15480
|
}
|
|
15859
15481
|
return {
|
|
15860
15482
|
_id: value._id ?? void 0,
|
|
@@ -15873,40 +15495,40 @@ function MBuilding(value) {
|
|
|
15873
15495
|
function MBuildingUnit(value) {
|
|
15874
15496
|
const { error } = schemaBuildingUnit.validate(value);
|
|
15875
15497
|
if (error) {
|
|
15876
|
-
|
|
15877
|
-
throw new
|
|
15498
|
+
logger51.info(`Building Unit Model: ${error.message}`);
|
|
15499
|
+
throw new BadRequestError70(error.message);
|
|
15878
15500
|
}
|
|
15879
15501
|
if (value._id && typeof value._id === "string") {
|
|
15880
15502
|
try {
|
|
15881
|
-
value._id = new
|
|
15503
|
+
value._id = new ObjectId44(value._id);
|
|
15882
15504
|
} catch (error2) {
|
|
15883
|
-
throw new
|
|
15505
|
+
throw new BadRequestError70("Invalid ID");
|
|
15884
15506
|
}
|
|
15885
15507
|
}
|
|
15886
15508
|
try {
|
|
15887
|
-
value.site = new
|
|
15509
|
+
value.site = new ObjectId44(value.site);
|
|
15888
15510
|
} catch (error2) {
|
|
15889
|
-
throw new
|
|
15511
|
+
throw new BadRequestError70("Invalid site ID");
|
|
15890
15512
|
}
|
|
15891
15513
|
try {
|
|
15892
|
-
value.building = new
|
|
15514
|
+
value.building = new ObjectId44(value.building);
|
|
15893
15515
|
} catch (error2) {
|
|
15894
|
-
throw new
|
|
15516
|
+
throw new BadRequestError70("Invalid building ID");
|
|
15895
15517
|
}
|
|
15896
15518
|
if (value.owner && typeof value.owner === "string") {
|
|
15897
15519
|
try {
|
|
15898
|
-
value.owner = new
|
|
15520
|
+
value.owner = new ObjectId44(value.owner);
|
|
15899
15521
|
} catch (error2) {
|
|
15900
|
-
throw new
|
|
15522
|
+
throw new BadRequestError70("Invalid Owner ID");
|
|
15901
15523
|
}
|
|
15902
15524
|
}
|
|
15903
15525
|
if (value.billing && Array.isArray(value.billing)) {
|
|
15904
15526
|
value.billing = value.billing.map((billing) => {
|
|
15905
15527
|
if (billing._id && typeof billing._id === "string") {
|
|
15906
15528
|
try {
|
|
15907
|
-
billing._id = new
|
|
15529
|
+
billing._id = new ObjectId44(billing._id);
|
|
15908
15530
|
} catch {
|
|
15909
|
-
throw new
|
|
15531
|
+
throw new BadRequestError70("Invalid billing id format");
|
|
15910
15532
|
}
|
|
15911
15533
|
}
|
|
15912
15534
|
return billing;
|
|
@@ -15938,15 +15560,15 @@ function MBuildingUnit(value) {
|
|
|
15938
15560
|
}
|
|
15939
15561
|
|
|
15940
15562
|
// src/repositories/building-unit.repository.ts
|
|
15941
|
-
import { ObjectId as
|
|
15563
|
+
import { ObjectId as ObjectId45 } from "mongodb";
|
|
15942
15564
|
function useBuildingUnitRepo() {
|
|
15943
|
-
const db =
|
|
15565
|
+
const db = useAtlas33.getDb();
|
|
15944
15566
|
if (!db) {
|
|
15945
15567
|
throw new Error("Unable to connect to server.");
|
|
15946
15568
|
}
|
|
15947
15569
|
const namespace_collection = "building-units";
|
|
15948
15570
|
const collection = db.collection(namespace_collection);
|
|
15949
|
-
const { getCache, setCache, delNamespace, delCache } =
|
|
15571
|
+
const { getCache, setCache, delNamespace, delCache } = useCache25(namespace_collection);
|
|
15950
15572
|
async function createIndexes() {
|
|
15951
15573
|
try {
|
|
15952
15574
|
await collection.createIndexes([
|
|
@@ -15970,12 +15592,12 @@ function useBuildingUnitRepo() {
|
|
|
15970
15592
|
}
|
|
15971
15593
|
function delCachedData() {
|
|
15972
15594
|
delNamespace().then(() => {
|
|
15973
|
-
|
|
15595
|
+
logger52.log({
|
|
15974
15596
|
level: "info",
|
|
15975
15597
|
message: `Cache namespace cleared for ${namespace_collection}`
|
|
15976
15598
|
});
|
|
15977
15599
|
}).catch((err) => {
|
|
15978
|
-
|
|
15600
|
+
logger52.log({
|
|
15979
15601
|
level: "error",
|
|
15980
15602
|
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
15981
15603
|
});
|
|
@@ -16000,15 +15622,15 @@ function useBuildingUnitRepo() {
|
|
|
16000
15622
|
delCachedData();
|
|
16001
15623
|
return res.insertedId;
|
|
16002
15624
|
} catch (error) {
|
|
16003
|
-
|
|
15625
|
+
logger52.log({
|
|
16004
15626
|
level: "error",
|
|
16005
15627
|
message: error.message
|
|
16006
15628
|
});
|
|
16007
15629
|
const isDuplicated = error.message.includes("duplicate");
|
|
16008
15630
|
if (isDuplicated) {
|
|
16009
|
-
throw new
|
|
15631
|
+
throw new BadRequestError71("Building unit already exists.");
|
|
16010
15632
|
}
|
|
16011
|
-
if (error instanceof
|
|
15633
|
+
if (error instanceof AppError10) {
|
|
16012
15634
|
throw error;
|
|
16013
15635
|
} else {
|
|
16014
15636
|
throw new Error("Failed to create building unit.");
|
|
@@ -16018,27 +15640,27 @@ function useBuildingUnitRepo() {
|
|
|
16018
15640
|
async function updateById(_id, value, session) {
|
|
16019
15641
|
const { error } = schemaUpdateOptions.validate(value);
|
|
16020
15642
|
if (error) {
|
|
16021
|
-
throw new
|
|
15643
|
+
throw new BadRequestError71(error.message);
|
|
16022
15644
|
}
|
|
16023
15645
|
try {
|
|
16024
|
-
_id = new
|
|
15646
|
+
_id = new ObjectId45(_id);
|
|
16025
15647
|
} catch (error2) {
|
|
16026
|
-
throw new
|
|
15648
|
+
throw new BadRequestError71("Invalid ID.");
|
|
16027
15649
|
}
|
|
16028
15650
|
if (value.billing && Array.isArray(value.billing)) {
|
|
16029
15651
|
value.billing = value.billing.map((billing) => {
|
|
16030
15652
|
if (billing._id && typeof billing._id === "string") {
|
|
16031
15653
|
try {
|
|
16032
|
-
billing._id = new
|
|
15654
|
+
billing._id = new ObjectId45(billing._id);
|
|
16033
15655
|
} catch {
|
|
16034
|
-
throw new
|
|
15656
|
+
throw new BadRequestError71("Invalid billing id format");
|
|
16035
15657
|
}
|
|
16036
15658
|
}
|
|
16037
15659
|
return billing;
|
|
16038
15660
|
});
|
|
16039
15661
|
}
|
|
16040
15662
|
if (value.owner) {
|
|
16041
|
-
value.owner = new
|
|
15663
|
+
value.owner = new ObjectId45(value.owner);
|
|
16042
15664
|
}
|
|
16043
15665
|
try {
|
|
16044
15666
|
const res = await collection.updateOne(
|
|
@@ -16049,15 +15671,15 @@ function useBuildingUnitRepo() {
|
|
|
16049
15671
|
delCachedData();
|
|
16050
15672
|
return res;
|
|
16051
15673
|
} catch (error2) {
|
|
16052
|
-
|
|
15674
|
+
logger52.log({
|
|
16053
15675
|
level: "error",
|
|
16054
15676
|
message: error2.message
|
|
16055
15677
|
});
|
|
16056
15678
|
const isDuplicated = error2.message.includes("duplicate");
|
|
16057
15679
|
if (isDuplicated) {
|
|
16058
|
-
throw new
|
|
15680
|
+
throw new BadRequestError71("Building unit already exists.");
|
|
16059
15681
|
}
|
|
16060
|
-
if (error2 instanceof
|
|
15682
|
+
if (error2 instanceof AppError10) {
|
|
16061
15683
|
throw error2;
|
|
16062
15684
|
} else {
|
|
16063
15685
|
throw new Error("Failed to create building unit.");
|
|
@@ -16067,12 +15689,12 @@ function useBuildingUnitRepo() {
|
|
|
16067
15689
|
async function updateByBuildingId(building, value, session) {
|
|
16068
15690
|
const { error } = schemaUpdateOptions.validate(value);
|
|
16069
15691
|
if (error) {
|
|
16070
|
-
throw new
|
|
15692
|
+
throw new BadRequestError71(error.message);
|
|
16071
15693
|
}
|
|
16072
15694
|
try {
|
|
16073
|
-
building = new
|
|
15695
|
+
building = new ObjectId45(building);
|
|
16074
15696
|
} catch (error2) {
|
|
16075
|
-
throw new
|
|
15697
|
+
throw new BadRequestError71("Invalid building ID.");
|
|
16076
15698
|
}
|
|
16077
15699
|
try {
|
|
16078
15700
|
const res = await collection.updateMany(
|
|
@@ -16083,15 +15705,15 @@ function useBuildingUnitRepo() {
|
|
|
16083
15705
|
delCachedData();
|
|
16084
15706
|
return res;
|
|
16085
15707
|
} catch (error2) {
|
|
16086
|
-
|
|
15708
|
+
logger52.log({
|
|
16087
15709
|
level: "error",
|
|
16088
15710
|
message: error2.message
|
|
16089
15711
|
});
|
|
16090
15712
|
const isDuplicated = error2.message.includes("duplicate");
|
|
16091
15713
|
if (isDuplicated) {
|
|
16092
|
-
throw new
|
|
15714
|
+
throw new BadRequestError71("Building unit already exists.");
|
|
16093
15715
|
}
|
|
16094
|
-
if (error2 instanceof
|
|
15716
|
+
if (error2 instanceof AppError10) {
|
|
16095
15717
|
throw error2;
|
|
16096
15718
|
} else {
|
|
16097
15719
|
throw new Error("Failed to create building unit.");
|
|
@@ -16111,8 +15733,8 @@ function useBuildingUnitRepo() {
|
|
|
16111
15733
|
const query = {
|
|
16112
15734
|
status,
|
|
16113
15735
|
...search && { $text: { $search: search } },
|
|
16114
|
-
...site && { site:
|
|
16115
|
-
...building && { building:
|
|
15736
|
+
...site && { site: toObjectId10(site) },
|
|
15737
|
+
...building && { building: toObjectId10(building) }
|
|
16116
15738
|
};
|
|
16117
15739
|
sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
|
|
16118
15740
|
const cacheParams = {
|
|
@@ -16124,15 +15746,15 @@ function useBuildingUnitRepo() {
|
|
|
16124
15746
|
...building && { building },
|
|
16125
15747
|
...status && { status }
|
|
16126
15748
|
};
|
|
16127
|
-
const cacheKey =
|
|
16128
|
-
|
|
15749
|
+
const cacheKey = makeCacheKey24(namespace_collection, cacheParams);
|
|
15750
|
+
logger52.log({
|
|
16129
15751
|
level: "info",
|
|
16130
15752
|
message: `Cache key for getAll building units: ${cacheKey}`
|
|
16131
15753
|
});
|
|
16132
15754
|
try {
|
|
16133
15755
|
const cached = await getCache(cacheKey);
|
|
16134
15756
|
if (cached) {
|
|
16135
|
-
|
|
15757
|
+
logger52.log({
|
|
16136
15758
|
level: "info",
|
|
16137
15759
|
message: `Cache hit for getAll building units: ${cacheKey}`
|
|
16138
15760
|
});
|
|
@@ -16171,35 +15793,35 @@ function useBuildingUnitRepo() {
|
|
|
16171
15793
|
{ $limit: limit }
|
|
16172
15794
|
]).toArray();
|
|
16173
15795
|
const length = await collection.countDocuments(query);
|
|
16174
|
-
const data =
|
|
15796
|
+
const data = paginate20(items, page, limit, length);
|
|
16175
15797
|
setCache(cacheKey, data, 600).then(() => {
|
|
16176
|
-
|
|
15798
|
+
logger52.log({
|
|
16177
15799
|
level: "info",
|
|
16178
15800
|
message: `Cache set for getAll building units: ${cacheKey}`
|
|
16179
15801
|
});
|
|
16180
15802
|
}).catch((err) => {
|
|
16181
|
-
|
|
15803
|
+
logger52.log({
|
|
16182
15804
|
level: "error",
|
|
16183
15805
|
message: `Failed to set cache for getAll building units: ${err.message}`
|
|
16184
15806
|
});
|
|
16185
15807
|
});
|
|
16186
15808
|
return data;
|
|
16187
15809
|
} catch (error) {
|
|
16188
|
-
|
|
15810
|
+
logger52.log({ level: "error", message: `${error}` });
|
|
16189
15811
|
throw error;
|
|
16190
15812
|
}
|
|
16191
15813
|
}
|
|
16192
15814
|
async function getById(_id, session) {
|
|
16193
15815
|
try {
|
|
16194
|
-
_id = new
|
|
15816
|
+
_id = new ObjectId45(_id);
|
|
16195
15817
|
} catch (error) {
|
|
16196
|
-
throw new
|
|
15818
|
+
throw new BadRequestError71("Invalid ID.");
|
|
16197
15819
|
}
|
|
16198
|
-
const cacheKey =
|
|
15820
|
+
const cacheKey = makeCacheKey24(namespace_collection, { _id: String(_id) });
|
|
16199
15821
|
try {
|
|
16200
15822
|
const cached = await getCache(cacheKey);
|
|
16201
15823
|
if (cached) {
|
|
16202
|
-
|
|
15824
|
+
logger52.log({
|
|
16203
15825
|
level: "info",
|
|
16204
15826
|
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
16205
15827
|
});
|
|
@@ -16210,22 +15832,22 @@ function useBuildingUnitRepo() {
|
|
|
16210
15832
|
deletedAt: { $in: ["", null] }
|
|
16211
15833
|
});
|
|
16212
15834
|
if (!result) {
|
|
16213
|
-
throw new
|
|
15835
|
+
throw new BadRequestError71("Building unit not found.");
|
|
16214
15836
|
}
|
|
16215
15837
|
setCache(cacheKey, result, 300).then(() => {
|
|
16216
|
-
|
|
15838
|
+
logger52.log({
|
|
16217
15839
|
level: "info",
|
|
16218
15840
|
message: `Cache set for building unit by id: ${cacheKey}`
|
|
16219
15841
|
});
|
|
16220
15842
|
}).catch((err) => {
|
|
16221
|
-
|
|
15843
|
+
logger52.log({
|
|
16222
15844
|
level: "error",
|
|
16223
15845
|
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
16224
15846
|
});
|
|
16225
15847
|
});
|
|
16226
15848
|
return result;
|
|
16227
15849
|
} catch (error) {
|
|
16228
|
-
if (error instanceof
|
|
15850
|
+
if (error instanceof AppError10) {
|
|
16229
15851
|
throw error;
|
|
16230
15852
|
} else {
|
|
16231
15853
|
throw new InternalServerError27("Failed to get building unit.");
|
|
@@ -16234,18 +15856,18 @@ function useBuildingUnitRepo() {
|
|
|
16234
15856
|
}
|
|
16235
15857
|
async function getByBuildingLevel(building, level) {
|
|
16236
15858
|
try {
|
|
16237
|
-
building = new
|
|
15859
|
+
building = new ObjectId45(building);
|
|
16238
15860
|
} catch (error) {
|
|
16239
|
-
throw new
|
|
15861
|
+
throw new BadRequestError71("Invalid building ID.");
|
|
16240
15862
|
}
|
|
16241
|
-
const cacheKey =
|
|
15863
|
+
const cacheKey = makeCacheKey24(namespace_collection, {
|
|
16242
15864
|
building: String(building),
|
|
16243
15865
|
level
|
|
16244
15866
|
});
|
|
16245
15867
|
try {
|
|
16246
15868
|
const cached = await getCache(cacheKey);
|
|
16247
15869
|
if (cached) {
|
|
16248
|
-
|
|
15870
|
+
logger52.log({
|
|
16249
15871
|
level: "info",
|
|
16250
15872
|
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
16251
15873
|
});
|
|
@@ -16257,19 +15879,19 @@ function useBuildingUnitRepo() {
|
|
|
16257
15879
|
status: "active"
|
|
16258
15880
|
});
|
|
16259
15881
|
setCache(cacheKey, result, 300).then(() => {
|
|
16260
|
-
|
|
15882
|
+
logger52.log({
|
|
16261
15883
|
level: "info",
|
|
16262
15884
|
message: `Cache set for building unit by id: ${cacheKey}`
|
|
16263
15885
|
});
|
|
16264
15886
|
}).catch((err) => {
|
|
16265
|
-
|
|
15887
|
+
logger52.log({
|
|
16266
15888
|
level: "error",
|
|
16267
15889
|
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
16268
15890
|
});
|
|
16269
15891
|
});
|
|
16270
15892
|
return result;
|
|
16271
15893
|
} catch (error) {
|
|
16272
|
-
if (error instanceof
|
|
15894
|
+
if (error instanceof AppError10) {
|
|
16273
15895
|
throw error;
|
|
16274
15896
|
} else {
|
|
16275
15897
|
throw new InternalServerError27("Failed to get building unit.");
|
|
@@ -16278,9 +15900,9 @@ function useBuildingUnitRepo() {
|
|
|
16278
15900
|
}
|
|
16279
15901
|
async function updateLevelByBuildingLevel(building, level, newLevel, session) {
|
|
16280
15902
|
try {
|
|
16281
|
-
building = new
|
|
15903
|
+
building = new ObjectId45(building);
|
|
16282
15904
|
} catch (error) {
|
|
16283
|
-
throw new
|
|
15905
|
+
throw new BadRequestError71("Invalid building ID.");
|
|
16284
15906
|
}
|
|
16285
15907
|
try {
|
|
16286
15908
|
const result = await collection.updateMany(
|
|
@@ -16299,7 +15921,7 @@ function useBuildingUnitRepo() {
|
|
|
16299
15921
|
delCachedData();
|
|
16300
15922
|
return result;
|
|
16301
15923
|
} catch (error) {
|
|
16302
|
-
if (error instanceof
|
|
15924
|
+
if (error instanceof AppError10) {
|
|
16303
15925
|
throw error;
|
|
16304
15926
|
} else {
|
|
16305
15927
|
throw new InternalServerError27("Failed to update building unit level.");
|
|
@@ -16308,17 +15930,17 @@ function useBuildingUnitRepo() {
|
|
|
16308
15930
|
}
|
|
16309
15931
|
async function getByBuilding(building) {
|
|
16310
15932
|
try {
|
|
16311
|
-
building = new
|
|
15933
|
+
building = new ObjectId45(building);
|
|
16312
15934
|
} catch (error) {
|
|
16313
|
-
throw new
|
|
15935
|
+
throw new BadRequestError71("Invalid building ID.");
|
|
16314
15936
|
}
|
|
16315
|
-
const cacheKey =
|
|
15937
|
+
const cacheKey = makeCacheKey24(namespace_collection, {
|
|
16316
15938
|
building: String(building)
|
|
16317
15939
|
});
|
|
16318
15940
|
try {
|
|
16319
15941
|
const cached = await getCache(cacheKey);
|
|
16320
15942
|
if (cached) {
|
|
16321
|
-
|
|
15943
|
+
logger52.log({
|
|
16322
15944
|
level: "info",
|
|
16323
15945
|
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
16324
15946
|
});
|
|
@@ -16329,19 +15951,19 @@ function useBuildingUnitRepo() {
|
|
|
16329
15951
|
status: "active"
|
|
16330
15952
|
});
|
|
16331
15953
|
setCache(cacheKey, result, 300).then(() => {
|
|
16332
|
-
|
|
15954
|
+
logger52.log({
|
|
16333
15955
|
level: "info",
|
|
16334
15956
|
message: `Cache set for building unit by id: ${cacheKey}`
|
|
16335
15957
|
});
|
|
16336
15958
|
}).catch((err) => {
|
|
16337
|
-
|
|
15959
|
+
logger52.log({
|
|
16338
15960
|
level: "error",
|
|
16339
15961
|
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
16340
15962
|
});
|
|
16341
15963
|
});
|
|
16342
15964
|
return result;
|
|
16343
15965
|
} catch (error) {
|
|
16344
|
-
if (error instanceof
|
|
15966
|
+
if (error instanceof AppError10) {
|
|
16345
15967
|
throw error;
|
|
16346
15968
|
} else {
|
|
16347
15969
|
throw new InternalServerError27("Failed to get building unit.");
|
|
@@ -16350,9 +15972,9 @@ function useBuildingUnitRepo() {
|
|
|
16350
15972
|
}
|
|
16351
15973
|
async function deleteById(_id, session) {
|
|
16352
15974
|
try {
|
|
16353
|
-
_id = new
|
|
15975
|
+
_id = new ObjectId45(_id);
|
|
16354
15976
|
} catch (error) {
|
|
16355
|
-
throw new
|
|
15977
|
+
throw new BadRequestError71("Invalid ID.");
|
|
16356
15978
|
}
|
|
16357
15979
|
try {
|
|
16358
15980
|
const res = await collection.updateOne(
|
|
@@ -16363,11 +15985,11 @@ function useBuildingUnitRepo() {
|
|
|
16363
15985
|
delCachedData();
|
|
16364
15986
|
return "Room/Facility deleted successfully.";
|
|
16365
15987
|
} catch (error) {
|
|
16366
|
-
|
|
15988
|
+
logger52.log({
|
|
16367
15989
|
level: "error",
|
|
16368
15990
|
message: error.message
|
|
16369
15991
|
});
|
|
16370
|
-
if (error instanceof
|
|
15992
|
+
if (error instanceof AppError10) {
|
|
16371
15993
|
throw error;
|
|
16372
15994
|
} else {
|
|
16373
15995
|
throw new Error("Failed to deleted room/facility.");
|
|
@@ -16376,9 +15998,9 @@ function useBuildingUnitRepo() {
|
|
|
16376
15998
|
}
|
|
16377
15999
|
async function getBuildingUnits(site, block, level) {
|
|
16378
16000
|
try {
|
|
16379
|
-
site = new
|
|
16001
|
+
site = new ObjectId45(site);
|
|
16380
16002
|
} catch (error) {
|
|
16381
|
-
throw new
|
|
16003
|
+
throw new BadRequestError71("Invalid site ID format.");
|
|
16382
16004
|
}
|
|
16383
16005
|
const query = { site, block, level };
|
|
16384
16006
|
const cacheOptions = {
|
|
@@ -16386,22 +16008,22 @@ function useBuildingUnitRepo() {
|
|
|
16386
16008
|
block,
|
|
16387
16009
|
level
|
|
16388
16010
|
};
|
|
16389
|
-
const cacheKey =
|
|
16011
|
+
const cacheKey = makeCacheKey24(namespace_collection, cacheOptions);
|
|
16390
16012
|
const cachedData = await getCache(cacheKey);
|
|
16391
16013
|
if (cachedData) {
|
|
16392
|
-
|
|
16014
|
+
logger52.info(`Cache hit for key: ${cacheKey}`);
|
|
16393
16015
|
return cachedData;
|
|
16394
16016
|
}
|
|
16395
16017
|
try {
|
|
16396
16018
|
const data = await collection.aggregate([{ $match: query }, { $project: { name: 1 } }]).toArray();
|
|
16397
16019
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
16398
|
-
|
|
16020
|
+
logger52.info(`Cache set for key: ${cacheKey}`);
|
|
16399
16021
|
}).catch((err) => {
|
|
16400
|
-
|
|
16022
|
+
logger52.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
16401
16023
|
});
|
|
16402
16024
|
return data;
|
|
16403
16025
|
} catch (error) {
|
|
16404
|
-
if (error instanceof
|
|
16026
|
+
if (error instanceof AppError10) {
|
|
16405
16027
|
throw error;
|
|
16406
16028
|
} else {
|
|
16407
16029
|
throw new InternalServerError27("Failed to get building units.");
|
|
@@ -16410,9 +16032,9 @@ function useBuildingUnitRepo() {
|
|
|
16410
16032
|
}
|
|
16411
16033
|
async function getBuildingUnitsWithOwner(site, unitIds) {
|
|
16412
16034
|
try {
|
|
16413
|
-
site = new
|
|
16035
|
+
site = new ObjectId45(site);
|
|
16414
16036
|
} catch (error) {
|
|
16415
|
-
throw new
|
|
16037
|
+
throw new BadRequestError71("Invalid site ID format.");
|
|
16416
16038
|
}
|
|
16417
16039
|
const query = {
|
|
16418
16040
|
site,
|
|
@@ -16424,22 +16046,22 @@ function useBuildingUnitRepo() {
|
|
|
16424
16046
|
const cacheOptions = {
|
|
16425
16047
|
site: site.toString()
|
|
16426
16048
|
};
|
|
16427
|
-
const cacheKey =
|
|
16049
|
+
const cacheKey = makeCacheKey24(namespace_collection, cacheOptions);
|
|
16428
16050
|
const cachedData = await getCache(cacheKey);
|
|
16429
16051
|
if (cachedData) {
|
|
16430
|
-
|
|
16052
|
+
logger52.info(`Cache hit for key: ${cacheKey}`);
|
|
16431
16053
|
return cachedData;
|
|
16432
16054
|
}
|
|
16433
16055
|
try {
|
|
16434
16056
|
const data = await collection.aggregate([{ $match: query }]).toArray();
|
|
16435
16057
|
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
16436
|
-
|
|
16058
|
+
logger52.info(`Cache set for key: ${cacheKey}`);
|
|
16437
16059
|
}).catch((err) => {
|
|
16438
|
-
|
|
16060
|
+
logger52.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
16439
16061
|
});
|
|
16440
16062
|
return data;
|
|
16441
16063
|
} catch (error) {
|
|
16442
|
-
if (error instanceof
|
|
16064
|
+
if (error instanceof AppError10) {
|
|
16443
16065
|
throw error;
|
|
16444
16066
|
} else {
|
|
16445
16067
|
throw new InternalServerError27(
|
|
@@ -16451,9 +16073,9 @@ function useBuildingUnitRepo() {
|
|
|
16451
16073
|
async function getUnitByBlockLevelUnitNumber(block, level, unitNumber, site, session) {
|
|
16452
16074
|
let _site;
|
|
16453
16075
|
try {
|
|
16454
|
-
_site = new
|
|
16076
|
+
_site = new ObjectId45(site);
|
|
16455
16077
|
} catch (error) {
|
|
16456
|
-
throw new
|
|
16078
|
+
throw new BadRequestError71("Invalid ID.");
|
|
16457
16079
|
}
|
|
16458
16080
|
const cacheOptions = {
|
|
16459
16081
|
block,
|
|
@@ -16472,11 +16094,11 @@ function useBuildingUnitRepo() {
|
|
|
16472
16094
|
{ deletedAt: "" }
|
|
16473
16095
|
]
|
|
16474
16096
|
};
|
|
16475
|
-
const cacheKey =
|
|
16097
|
+
const cacheKey = makeCacheKey24(namespace_collection, cacheOptions);
|
|
16476
16098
|
try {
|
|
16477
16099
|
const cached = await getCache(cacheKey);
|
|
16478
16100
|
if (cached) {
|
|
16479
|
-
|
|
16101
|
+
logger52.log({
|
|
16480
16102
|
level: "info",
|
|
16481
16103
|
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
16482
16104
|
});
|
|
@@ -16486,22 +16108,22 @@ function useBuildingUnitRepo() {
|
|
|
16486
16108
|
session
|
|
16487
16109
|
});
|
|
16488
16110
|
if (!result) {
|
|
16489
|
-
throw new
|
|
16111
|
+
throw new BadRequestError71("Building unit not found.");
|
|
16490
16112
|
}
|
|
16491
16113
|
setCache(cacheKey, result, 300).then(() => {
|
|
16492
|
-
|
|
16114
|
+
logger52.log({
|
|
16493
16115
|
level: "info",
|
|
16494
16116
|
message: `Cache set for building unit by id: ${cacheKey}`
|
|
16495
16117
|
});
|
|
16496
16118
|
}).catch((err) => {
|
|
16497
|
-
|
|
16119
|
+
logger52.log({
|
|
16498
16120
|
level: "error",
|
|
16499
16121
|
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
16500
16122
|
});
|
|
16501
16123
|
});
|
|
16502
16124
|
return result;
|
|
16503
16125
|
} catch (error) {
|
|
16504
|
-
if (error instanceof
|
|
16126
|
+
if (error instanceof AppError10) {
|
|
16505
16127
|
throw error;
|
|
16506
16128
|
} else {
|
|
16507
16129
|
throw new InternalServerError27("Failed to get building unit.");
|
|
@@ -16573,7 +16195,7 @@ function useVehicleService() {
|
|
|
16573
16195
|
async function add(value, session) {
|
|
16574
16196
|
const isExternalSession = !!session;
|
|
16575
16197
|
if (!session) {
|
|
16576
|
-
session = await
|
|
16198
|
+
session = await useAtlas34.getClient()?.startSession();
|
|
16577
16199
|
if (!session) {
|
|
16578
16200
|
throw new Error("Unable to start session for vehicle service.");
|
|
16579
16201
|
}
|
|
@@ -16584,11 +16206,11 @@ function useVehicleService() {
|
|
|
16584
16206
|
_getById(value.org)
|
|
16585
16207
|
]);
|
|
16586
16208
|
if (!org)
|
|
16587
|
-
throw new
|
|
16209
|
+
throw new BadRequestError72("Org not found");
|
|
16588
16210
|
if (existingPlateNumber)
|
|
16589
|
-
throw new
|
|
16211
|
+
throw new BadRequestError72("Vehicle plate number already exists");
|
|
16590
16212
|
if (!Object.values(OrgNature).includes(org.nature)) {
|
|
16591
|
-
throw new
|
|
16213
|
+
throw new BadRequestError72(
|
|
16592
16214
|
"This organization is not allowed to add vehicles."
|
|
16593
16215
|
);
|
|
16594
16216
|
}
|
|
@@ -16655,7 +16277,7 @@ function useVehicleService() {
|
|
|
16655
16277
|
page++;
|
|
16656
16278
|
} while (page <= pages);
|
|
16657
16279
|
if (!siteCameras.length) {
|
|
16658
|
-
throw new
|
|
16280
|
+
throw new BadRequestError72("No site cameras found.");
|
|
16659
16281
|
}
|
|
16660
16282
|
}
|
|
16661
16283
|
for (const plateNumber of plateNumbers) {
|
|
@@ -16691,7 +16313,7 @@ function useVehicleService() {
|
|
|
16691
16313
|
};
|
|
16692
16314
|
const dahuaResponse = await _addPlateNumber(dahuaPayload);
|
|
16693
16315
|
if (dahuaResponse?.statusCode !== 200) {
|
|
16694
|
-
throw new
|
|
16316
|
+
throw new BadRequestError72(
|
|
16695
16317
|
`Failed to add plate number to ANPR ${_type}`
|
|
16696
16318
|
);
|
|
16697
16319
|
}
|
|
@@ -16712,7 +16334,7 @@ function useVehicleService() {
|
|
|
16712
16334
|
};
|
|
16713
16335
|
const dahuaResponse = await _updatePlateNumber(dahuaPayload);
|
|
16714
16336
|
if (dahuaResponse?.statusCode !== 200) {
|
|
16715
|
-
throw new
|
|
16337
|
+
throw new BadRequestError72(
|
|
16716
16338
|
`Failed to update plate number to ANPR ${_type}`
|
|
16717
16339
|
);
|
|
16718
16340
|
}
|
|
@@ -16738,7 +16360,7 @@ function useVehicleService() {
|
|
|
16738
16360
|
}
|
|
16739
16361
|
return message;
|
|
16740
16362
|
} catch (error) {
|
|
16741
|
-
|
|
16363
|
+
logger53.error("Error in vehicle service add:", error);
|
|
16742
16364
|
if (!isExternalSession) {
|
|
16743
16365
|
await session.abortTransaction();
|
|
16744
16366
|
}
|
|
@@ -16750,7 +16372,7 @@ function useVehicleService() {
|
|
|
16750
16372
|
}
|
|
16751
16373
|
}
|
|
16752
16374
|
async function deleteVehicle(_id, recno, site, type, bypass = false) {
|
|
16753
|
-
const session =
|
|
16375
|
+
const session = useAtlas34.getClient()?.startSession();
|
|
16754
16376
|
if (!session) {
|
|
16755
16377
|
throw new Error("Unable to start session for vehicle service.");
|
|
16756
16378
|
}
|
|
@@ -16773,7 +16395,7 @@ function useVehicleService() {
|
|
|
16773
16395
|
page++;
|
|
16774
16396
|
} while (page < pages);
|
|
16775
16397
|
if (!siteCameras.length) {
|
|
16776
|
-
throw new
|
|
16398
|
+
throw new BadRequestError72("No site cameras found.");
|
|
16777
16399
|
}
|
|
16778
16400
|
for (const camera of siteCameras) {
|
|
16779
16401
|
const host = camera.host;
|
|
@@ -16788,7 +16410,7 @@ function useVehicleService() {
|
|
|
16788
16410
|
};
|
|
16789
16411
|
const dahuaResponse = await _removePlateNumber(dahuaPayload);
|
|
16790
16412
|
if (!bypass && dahuaResponse?.statusCode !== 200) {
|
|
16791
|
-
throw new
|
|
16413
|
+
throw new BadRequestError72(
|
|
16792
16414
|
`Failed to remove plate number to ANPR from ${type}`
|
|
16793
16415
|
);
|
|
16794
16416
|
}
|
|
@@ -16800,7 +16422,7 @@ function useVehicleService() {
|
|
|
16800
16422
|
await session.commitTransaction();
|
|
16801
16423
|
return `Vehicle plate number deleted from ${type} record successfully.`;
|
|
16802
16424
|
} catch (error) {
|
|
16803
|
-
|
|
16425
|
+
logger53.error("Error in vehicle service delete:", error);
|
|
16804
16426
|
await session.abortTransaction();
|
|
16805
16427
|
throw error;
|
|
16806
16428
|
} finally {
|
|
@@ -16808,16 +16430,16 @@ function useVehicleService() {
|
|
|
16808
16430
|
}
|
|
16809
16431
|
}
|
|
16810
16432
|
async function approveVehicleById(id, orgId, siteId) {
|
|
16811
|
-
const session =
|
|
16433
|
+
const session = useAtlas34.getClient()?.startSession();
|
|
16812
16434
|
if (!session) {
|
|
16813
16435
|
throw new Error("Unable to start session for vehicle service.");
|
|
16814
16436
|
}
|
|
16815
16437
|
const org = await _getById(orgId);
|
|
16816
16438
|
if (!org)
|
|
16817
|
-
throw new
|
|
16439
|
+
throw new BadRequestError72("Org not found");
|
|
16818
16440
|
const allowedNatures2 = "property_management_agency" /* PROPERTY_MANAGEMENT_AGENCY */;
|
|
16819
16441
|
if (!allowedNatures2.includes(org.nature)) {
|
|
16820
|
-
throw new
|
|
16442
|
+
throw new BadRequestError72(
|
|
16821
16443
|
"Only property management can approve vehicles."
|
|
16822
16444
|
);
|
|
16823
16445
|
}
|
|
@@ -16825,7 +16447,7 @@ function useVehicleService() {
|
|
|
16825
16447
|
const plate = vehicle.plates.find((p) => p._id.toString() === id);
|
|
16826
16448
|
const _plateNumber = plate?.plateNumber;
|
|
16827
16449
|
if (!vehicle) {
|
|
16828
|
-
throw new
|
|
16450
|
+
throw new BadRequestError72("Vehicle not found");
|
|
16829
16451
|
}
|
|
16830
16452
|
const owner = vehicle.name;
|
|
16831
16453
|
const hasStart = typeof vehicle.start === "string" ? vehicle.start.trim() !== "" : !!vehicle.start;
|
|
@@ -16867,7 +16489,7 @@ function useVehicleService() {
|
|
|
16867
16489
|
page++;
|
|
16868
16490
|
} while (page < pages);
|
|
16869
16491
|
if (!siteCameras.length) {
|
|
16870
|
-
throw new
|
|
16492
|
+
throw new BadRequestError72("No site cameras found.");
|
|
16871
16493
|
}
|
|
16872
16494
|
for (const camera of siteCameras) {
|
|
16873
16495
|
const { host, username, password } = camera;
|
|
@@ -16883,7 +16505,7 @@ function useVehicleService() {
|
|
|
16883
16505
|
};
|
|
16884
16506
|
const dahuaResponse = await _addPlateNumber(dahuaPayload);
|
|
16885
16507
|
if (dahuaResponse?.statusCode !== 200) {
|
|
16886
|
-
throw new
|
|
16508
|
+
throw new BadRequestError72("Failed to add plate number to ANPR");
|
|
16887
16509
|
}
|
|
16888
16510
|
const responseData = dahuaResponse?.data.toString("utf-8");
|
|
16889
16511
|
value.recNo = responseData.split("=")[1]?.trim();
|
|
@@ -16908,7 +16530,7 @@ function useVehicleService() {
|
|
|
16908
16530
|
await session.commitTransaction();
|
|
16909
16531
|
return "Vehicle plate number approved and added successfully.";
|
|
16910
16532
|
} catch (error) {
|
|
16911
|
-
|
|
16533
|
+
logger53.error("Error in vehicle service approving and adding:", error);
|
|
16912
16534
|
await session.abortTransaction();
|
|
16913
16535
|
throw error;
|
|
16914
16536
|
} finally {
|
|
@@ -16916,7 +16538,7 @@ function useVehicleService() {
|
|
|
16916
16538
|
}
|
|
16917
16539
|
}
|
|
16918
16540
|
async function processDeletingExpiredVehicles() {
|
|
16919
|
-
const session =
|
|
16541
|
+
const session = useAtlas34.getClient()?.startSession();
|
|
16920
16542
|
if (!session) {
|
|
16921
16543
|
throw new Error("Unable to start session for vehicle service.");
|
|
16922
16544
|
}
|
|
@@ -16943,7 +16565,7 @@ function useVehicleService() {
|
|
|
16943
16565
|
page++;
|
|
16944
16566
|
} while (page < pages);
|
|
16945
16567
|
if (!siteCameras.length) {
|
|
16946
|
-
throw new
|
|
16568
|
+
throw new BadRequestError72("No site cameras found.");
|
|
16947
16569
|
}
|
|
16948
16570
|
for (const camera of siteCameras) {
|
|
16949
16571
|
const host = camera.host;
|
|
@@ -16963,7 +16585,7 @@ function useVehicleService() {
|
|
|
16963
16585
|
await session.commitTransaction();
|
|
16964
16586
|
return `Expired Vehicle plate numbers deleted successfully.`;
|
|
16965
16587
|
} catch (error) {
|
|
16966
|
-
|
|
16588
|
+
logger53.error(
|
|
16967
16589
|
"Error in vehicle service process deleting expired vehicles:",
|
|
16968
16590
|
error
|
|
16969
16591
|
);
|
|
@@ -16974,16 +16596,16 @@ function useVehicleService() {
|
|
|
16974
16596
|
}
|
|
16975
16597
|
}
|
|
16976
16598
|
async function reactivateVehicleById(id, orgId, siteId) {
|
|
16977
|
-
const session =
|
|
16599
|
+
const session = useAtlas34.getClient()?.startSession();
|
|
16978
16600
|
if (!session) {
|
|
16979
16601
|
throw new Error("Unable to start session for vehicle service.");
|
|
16980
16602
|
}
|
|
16981
16603
|
const org = await _getById(orgId);
|
|
16982
16604
|
if (!org)
|
|
16983
|
-
throw new
|
|
16605
|
+
throw new BadRequestError72("Org not found");
|
|
16984
16606
|
const allowedNatures2 = "property_management_agency" /* PROPERTY_MANAGEMENT_AGENCY */;
|
|
16985
16607
|
if (!allowedNatures2.includes(org.nature)) {
|
|
16986
|
-
throw new
|
|
16608
|
+
throw new BadRequestError72(
|
|
16987
16609
|
"Only property management can reactivate vehicles."
|
|
16988
16610
|
);
|
|
16989
16611
|
}
|
|
@@ -16991,7 +16613,7 @@ function useVehicleService() {
|
|
|
16991
16613
|
const plate = vehicle.plates.find((p) => p._id.toString() === id);
|
|
16992
16614
|
const _plateNumber = plate?.plateNumber;
|
|
16993
16615
|
if (!vehicle) {
|
|
16994
|
-
throw new
|
|
16616
|
+
throw new BadRequestError72("Vehicle not found");
|
|
16995
16617
|
}
|
|
16996
16618
|
const owner = vehicle.name;
|
|
16997
16619
|
let startDate = /* @__PURE__ */ new Date();
|
|
@@ -17021,7 +16643,7 @@ function useVehicleService() {
|
|
|
17021
16643
|
page++;
|
|
17022
16644
|
} while (page < pages);
|
|
17023
16645
|
if (!siteCameras.length) {
|
|
17024
|
-
throw new
|
|
16646
|
+
throw new BadRequestError72("No site cameras found.");
|
|
17025
16647
|
}
|
|
17026
16648
|
for (const camera of siteCameras) {
|
|
17027
16649
|
const { host, username, password } = camera;
|
|
@@ -17037,7 +16659,7 @@ function useVehicleService() {
|
|
|
17037
16659
|
};
|
|
17038
16660
|
const dahuaResponse = await _addPlateNumber(dahuaPayload);
|
|
17039
16661
|
if (dahuaResponse?.statusCode !== 200) {
|
|
17040
|
-
throw new
|
|
16662
|
+
throw new BadRequestError72("Failed to add plate number to ANPR");
|
|
17041
16663
|
}
|
|
17042
16664
|
const responseData = dahuaResponse?.data.toString("utf-8");
|
|
17043
16665
|
value.recNo = responseData.split("=")[1]?.trim();
|
|
@@ -17063,7 +16685,7 @@ function useVehicleService() {
|
|
|
17063
16685
|
await session.commitTransaction();
|
|
17064
16686
|
return "Vehicle reactivated successfully.";
|
|
17065
16687
|
} catch (error) {
|
|
17066
|
-
|
|
16688
|
+
logger53.error("Error in vehicle service reactivation:", error);
|
|
17067
16689
|
await session.abortTransaction();
|
|
17068
16690
|
throw error;
|
|
17069
16691
|
} finally {
|
|
@@ -17071,7 +16693,7 @@ function useVehicleService() {
|
|
|
17071
16693
|
}
|
|
17072
16694
|
}
|
|
17073
16695
|
async function updateVehicleById(_id, value) {
|
|
17074
|
-
const session =
|
|
16696
|
+
const session = useAtlas34.getClient()?.startSession();
|
|
17075
16697
|
if (!session) {
|
|
17076
16698
|
throw new Error("Unable to start session for vehicle service.");
|
|
17077
16699
|
}
|
|
@@ -17106,7 +16728,7 @@ function useVehicleService() {
|
|
|
17106
16728
|
page++;
|
|
17107
16729
|
} while (page < pages);
|
|
17108
16730
|
if (!siteCameras.length) {
|
|
17109
|
-
throw new
|
|
16731
|
+
throw new BadRequestError72("No site cameras found.");
|
|
17110
16732
|
}
|
|
17111
16733
|
for (const camera of siteCameras) {
|
|
17112
16734
|
const { host, username, password } = camera;
|
|
@@ -17123,7 +16745,7 @@ function useVehicleService() {
|
|
|
17123
16745
|
removePlateNumber
|
|
17124
16746
|
);
|
|
17125
16747
|
if (responseForDeletion?.statusCode !== 200) {
|
|
17126
|
-
throw new
|
|
16748
|
+
throw new BadRequestError72(
|
|
17127
16749
|
"Failed to delete plate number to ANPR"
|
|
17128
16750
|
);
|
|
17129
16751
|
}
|
|
@@ -17140,7 +16762,7 @@ function useVehicleService() {
|
|
|
17140
16762
|
};
|
|
17141
16763
|
const dahuaResponse = await _addPlateNumber(dahuaPayload);
|
|
17142
16764
|
if (dahuaResponse?.statusCode !== 200) {
|
|
17143
|
-
throw new
|
|
16765
|
+
throw new BadRequestError72(
|
|
17144
16766
|
"Failed to update plate number to ANPR"
|
|
17145
16767
|
);
|
|
17146
16768
|
}
|
|
@@ -17160,7 +16782,7 @@ function useVehicleService() {
|
|
|
17160
16782
|
};
|
|
17161
16783
|
const dahuaResponse = await _updatePlateNumber(dahuaPayload);
|
|
17162
16784
|
if (dahuaResponse?.statusCode !== 200) {
|
|
17163
|
-
throw new
|
|
16785
|
+
throw new BadRequestError72(
|
|
17164
16786
|
"Failed to update plate number to ANPR"
|
|
17165
16787
|
);
|
|
17166
16788
|
}
|
|
@@ -17174,7 +16796,7 @@ function useVehicleService() {
|
|
|
17174
16796
|
...start && { start },
|
|
17175
16797
|
...end && { end },
|
|
17176
16798
|
...unit && {
|
|
17177
|
-
unit: typeof unit === "string" ? new
|
|
16799
|
+
unit: typeof unit === "string" ? new ObjectId46(unit) : unit
|
|
17178
16800
|
},
|
|
17179
16801
|
...value.recNo && { recNo: value.recNo },
|
|
17180
16802
|
...type && { type: value.type }
|
|
@@ -17182,7 +16804,7 @@ function useVehicleService() {
|
|
|
17182
16804
|
await _updateVehicleById(_id, formattedValue, session);
|
|
17183
16805
|
await session.commitTransaction();
|
|
17184
16806
|
} catch (error) {
|
|
17185
|
-
|
|
16807
|
+
logger53.error("Error in vehicle service update:", error);
|
|
17186
16808
|
await session.abortTransaction();
|
|
17187
16809
|
throw error;
|
|
17188
16810
|
} finally {
|
|
@@ -17204,7 +16826,7 @@ function useVehicleService() {
|
|
|
17204
16826
|
item.unit,
|
|
17205
16827
|
site
|
|
17206
16828
|
);
|
|
17207
|
-
const unitId = unit && unit._id &&
|
|
16829
|
+
const unitId = unit && unit._id && ObjectId46.isValid(unit._id) ? unit._id.toString() : unit?._id;
|
|
17208
16830
|
return {
|
|
17209
16831
|
...item,
|
|
17210
16832
|
org,
|
|
@@ -17317,6 +16939,7 @@ var loggerDahua = winston.createLogger({
|
|
|
17317
16939
|
});
|
|
17318
16940
|
|
|
17319
16941
|
// src/services/dahua.service.ts
|
|
16942
|
+
var cameraRegistry = /* @__PURE__ */ new Map();
|
|
17320
16943
|
function useDahuaDigest({
|
|
17321
16944
|
host = "",
|
|
17322
16945
|
username = "",
|
|
@@ -17665,7 +17288,33 @@ function useDahuaService() {
|
|
|
17665
17288
|
throw error;
|
|
17666
17289
|
}
|
|
17667
17290
|
}
|
|
17668
|
-
async function
|
|
17291
|
+
async function listenToCamera(camera) {
|
|
17292
|
+
if (!camera?._id) {
|
|
17293
|
+
loggerDahua.error(`Camera _id is required to listen to camera.`);
|
|
17294
|
+
throw new BadRequestError73("Camera _id is required to listen to camera.");
|
|
17295
|
+
}
|
|
17296
|
+
const cameraId = camera._id.toString();
|
|
17297
|
+
if (cameraRegistry.has(cameraId)) {
|
|
17298
|
+
loggerDahua.info(`[${camera.host}] Stopping existing connection for update.`);
|
|
17299
|
+
cameraRegistry.get(cameraId)?.abort();
|
|
17300
|
+
cameraRegistry.delete(cameraId);
|
|
17301
|
+
}
|
|
17302
|
+
const controller = new AbortController();
|
|
17303
|
+
cameraRegistry.set(cameraId, controller);
|
|
17304
|
+
getTrafficJunction(
|
|
17305
|
+
camera.host,
|
|
17306
|
+
camera.username,
|
|
17307
|
+
camera.password,
|
|
17308
|
+
camera.site.toString(),
|
|
17309
|
+
`guard-post-${camera.guardPost}`,
|
|
17310
|
+
camera.direction,
|
|
17311
|
+
cameraId,
|
|
17312
|
+
controller.signal
|
|
17313
|
+
);
|
|
17314
|
+
}
|
|
17315
|
+
async function getTrafficJunction(host = "", username = "", password = "", site = "", gate = "", designation = "", cameraId, signal) {
|
|
17316
|
+
if (signal.aborted)
|
|
17317
|
+
return;
|
|
17669
17318
|
try {
|
|
17670
17319
|
const response = await useDahuaDigest({
|
|
17671
17320
|
host,
|
|
@@ -17689,32 +17338,58 @@ function useDahuaService() {
|
|
|
17689
17338
|
password
|
|
17690
17339
|
);
|
|
17691
17340
|
bufferQueue.setStream(response.res);
|
|
17341
|
+
const onAbort = () => {
|
|
17342
|
+
loggerDahua.info(`[${site}]-[${host}] Abort triggered. Cleaning up...`);
|
|
17343
|
+
if (response.res && !response.res.destroyed) {
|
|
17344
|
+
response.res.once("error", (err) => {
|
|
17345
|
+
if (err?.code === "UND_ERR_ABORTED") {
|
|
17346
|
+
loggerDahua.debug(`[${site}]-[${host}] Stream aborted successfully.`);
|
|
17347
|
+
} else {
|
|
17348
|
+
loggerDahua.error(`[${site}]-[${host}] Stream error during abort:`, err);
|
|
17349
|
+
}
|
|
17350
|
+
});
|
|
17351
|
+
response.res.destroy();
|
|
17352
|
+
}
|
|
17353
|
+
};
|
|
17354
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
17692
17355
|
if ([400, 401, 403, 500].includes(response.statusCode)) {
|
|
17693
17356
|
loggerDahua.error(
|
|
17694
|
-
`[${
|
|
17357
|
+
`[${host}] Connection error:`,
|
|
17695
17358
|
response.statusCode
|
|
17696
17359
|
);
|
|
17697
|
-
throw new
|
|
17360
|
+
throw new BadRequestError73(
|
|
17698
17361
|
`Failed to connect to ANPR: ${response.statusCode}`
|
|
17699
17362
|
);
|
|
17700
17363
|
}
|
|
17701
17364
|
if ([200, 201, 202].includes(response.statusCode)) {
|
|
17702
|
-
loggerDahua.info(`[${
|
|
17703
|
-
console.log(`[${
|
|
17365
|
+
loggerDahua.info(`[${host}] Successfully connected to ANPR.`);
|
|
17366
|
+
console.log(`[${host}] Successfully connected to ANPR.`);
|
|
17704
17367
|
}
|
|
17705
17368
|
response.res.on("data", (chunk) => {
|
|
17369
|
+
if (signal.aborted) {
|
|
17370
|
+
response.res.destroy();
|
|
17371
|
+
return;
|
|
17372
|
+
}
|
|
17706
17373
|
bufferQueue.enqueue(chunk);
|
|
17707
17374
|
});
|
|
17708
17375
|
const handleDisconnect = async (reason, err) => {
|
|
17376
|
+
signal.removeEventListener("abort", onAbort);
|
|
17377
|
+
if (bufferQueue.destroy)
|
|
17378
|
+
bufferQueue.destroy();
|
|
17379
|
+
if (response.res.destroy)
|
|
17380
|
+
response.res.destroy();
|
|
17381
|
+
if (signal.aborted) {
|
|
17382
|
+
loggerDahua.info(`[${site}]-[${host}] Loop terminated. Not reconnecting.`);
|
|
17383
|
+
return;
|
|
17384
|
+
}
|
|
17709
17385
|
loggerDahua.error(
|
|
17710
|
-
`[${site}][${
|
|
17386
|
+
`[${site}]-[${host}] ${reason}`,
|
|
17711
17387
|
err ? err.code || err : ""
|
|
17712
17388
|
);
|
|
17713
|
-
bufferQueue.destroy();
|
|
17714
|
-
if (response.res.destroy)
|
|
17715
|
-
response.res.destroy();
|
|
17716
17389
|
await new Promise((res) => setTimeout(res, 5e3));
|
|
17717
|
-
|
|
17390
|
+
if (signal.aborted)
|
|
17391
|
+
return;
|
|
17392
|
+
getTrafficJunction(host, username, password, site, gate, designation, cameraId, signal);
|
|
17718
17393
|
};
|
|
17719
17394
|
response.res.on(
|
|
17720
17395
|
"end",
|
|
@@ -17729,12 +17404,16 @@ function useDahuaService() {
|
|
|
17729
17404
|
(err) => handleDisconnect("Connection error:", err)
|
|
17730
17405
|
);
|
|
17731
17406
|
} catch (error) {
|
|
17407
|
+
if (signal.aborted)
|
|
17408
|
+
return;
|
|
17732
17409
|
loggerDahua.error(
|
|
17733
17410
|
`[${site}][${gate}] Initial connect error:`,
|
|
17734
17411
|
error.code || error
|
|
17735
17412
|
);
|
|
17736
17413
|
await new Promise((res) => setTimeout(res, 5e3));
|
|
17737
|
-
|
|
17414
|
+
if (signal.aborted)
|
|
17415
|
+
return;
|
|
17416
|
+
getTrafficJunction(host, username, password, site, gate, designation, cameraId, signal);
|
|
17738
17417
|
}
|
|
17739
17418
|
}
|
|
17740
17419
|
async function addPlateNumber(value) {
|
|
@@ -17751,7 +17430,7 @@ function useDahuaService() {
|
|
|
17751
17430
|
});
|
|
17752
17431
|
const { error } = validation.validate(value);
|
|
17753
17432
|
if (error) {
|
|
17754
|
-
throw new
|
|
17433
|
+
throw new BadRequestError73(`Validation error: ${error.message}`);
|
|
17755
17434
|
}
|
|
17756
17435
|
value.owner = String(value.owner ?? "").substring(0, 15) || "unknown";
|
|
17757
17436
|
const _openGate = String(value.isOpenGate);
|
|
@@ -17767,7 +17446,7 @@ function useDahuaService() {
|
|
|
17767
17446
|
return response;
|
|
17768
17447
|
} catch (error2) {
|
|
17769
17448
|
loggerDahua.error(`[${value.host}] Error adding plate number:`, error2);
|
|
17770
|
-
throw new
|
|
17449
|
+
throw new BadRequestError73(`Failed to add plate number: ${error2.message}`);
|
|
17771
17450
|
}
|
|
17772
17451
|
}
|
|
17773
17452
|
async function updatePlateNumber(value) {
|
|
@@ -17785,7 +17464,7 @@ function useDahuaService() {
|
|
|
17785
17464
|
});
|
|
17786
17465
|
const { error } = validation.validate(value);
|
|
17787
17466
|
if (error) {
|
|
17788
|
-
throw new
|
|
17467
|
+
throw new BadRequestError73(`Validation error: ${error.message}`);
|
|
17789
17468
|
}
|
|
17790
17469
|
value.owner = value.owner || "unknown";
|
|
17791
17470
|
let _isOpenGate;
|
|
@@ -17804,7 +17483,7 @@ function useDahuaService() {
|
|
|
17804
17483
|
return response;
|
|
17805
17484
|
} catch (error2) {
|
|
17806
17485
|
loggerDahua.error(`[${value.host}] Error updating plate number:`, error2);
|
|
17807
|
-
throw new
|
|
17486
|
+
throw new BadRequestError73(
|
|
17808
17487
|
`Failed to update plate number: ${error2.message}`
|
|
17809
17488
|
);
|
|
17810
17489
|
}
|
|
@@ -17819,7 +17498,7 @@ function useDahuaService() {
|
|
|
17819
17498
|
});
|
|
17820
17499
|
const { error } = validation.validate(value);
|
|
17821
17500
|
if (error) {
|
|
17822
|
-
throw new
|
|
17501
|
+
throw new BadRequestError73(`Validation error: ${error.message}`);
|
|
17823
17502
|
}
|
|
17824
17503
|
try {
|
|
17825
17504
|
const response = await useDahuaDigest({
|
|
@@ -17843,7 +17522,7 @@ function useDahuaService() {
|
|
|
17843
17522
|
});
|
|
17844
17523
|
const { error } = validation.validate(value);
|
|
17845
17524
|
if (error) {
|
|
17846
|
-
throw new
|
|
17525
|
+
throw new BadRequestError73(`Validation error: ${error.message}`);
|
|
17847
17526
|
}
|
|
17848
17527
|
try {
|
|
17849
17528
|
const response = await useDahuaDigest({
|
|
@@ -17875,7 +17554,7 @@ function useDahuaService() {
|
|
|
17875
17554
|
});
|
|
17876
17555
|
const { error } = validation.validate(value);
|
|
17877
17556
|
if (error) {
|
|
17878
|
-
throw new
|
|
17557
|
+
throw new BadRequestError73(`Validation error: ${error.message}`);
|
|
17879
17558
|
}
|
|
17880
17559
|
value.owner = String(value.owner ?? "").substring(0, 15) || "unknown";
|
|
17881
17560
|
value.vehicleType = String(value.vehicleType ?? "").substring(0, 31) || "unknown";
|
|
@@ -17893,14 +17572,14 @@ function useDahuaService() {
|
|
|
17893
17572
|
return response;
|
|
17894
17573
|
} catch (error2) {
|
|
17895
17574
|
loggerDahua.error(`[${value.host}] Error bulk add plate number:`, error2);
|
|
17896
|
-
throw new
|
|
17575
|
+
throw new BadRequestError73(
|
|
17897
17576
|
`Failed bulk adding plate number: ${error2.message}`
|
|
17898
17577
|
);
|
|
17899
17578
|
}
|
|
17900
17579
|
}
|
|
17901
17580
|
return {
|
|
17902
17581
|
getSnapshot,
|
|
17903
|
-
|
|
17582
|
+
listenToCamera,
|
|
17904
17583
|
addPlateNumber,
|
|
17905
17584
|
removePlateNumber,
|
|
17906
17585
|
updatePlateNumber,
|
|
@@ -17909,7 +17588,400 @@ function useDahuaService() {
|
|
|
17909
17588
|
};
|
|
17910
17589
|
}
|
|
17911
17590
|
|
|
17591
|
+
// src/repositories/site-camera.repo.ts
|
|
17592
|
+
function useSiteCameraRepo() {
|
|
17593
|
+
const db = useAtlas35.getDb();
|
|
17594
|
+
if (!db) {
|
|
17595
|
+
throw new Error("Unable to connect to server.");
|
|
17596
|
+
}
|
|
17597
|
+
const namespace_collection = "site.cameras";
|
|
17598
|
+
const collection = db.collection(namespace_collection);
|
|
17599
|
+
const { delNamespace, getCache, setCache } = useCache26(namespace_collection);
|
|
17600
|
+
async function createIndexes() {
|
|
17601
|
+
try {
|
|
17602
|
+
await collection.createIndexes([
|
|
17603
|
+
{
|
|
17604
|
+
key: { site: 1 }
|
|
17605
|
+
},
|
|
17606
|
+
{
|
|
17607
|
+
key: { type: 1 }
|
|
17608
|
+
},
|
|
17609
|
+
{
|
|
17610
|
+
key: { host: 1, type: 1, status: 1, site: 1 },
|
|
17611
|
+
unique: true,
|
|
17612
|
+
partialFilterExpression: { status: "active" }
|
|
17613
|
+
}
|
|
17614
|
+
]);
|
|
17615
|
+
return `Successfully created ${namespace_collection} indexes.`;
|
|
17616
|
+
} catch (error) {
|
|
17617
|
+
logger54.log({
|
|
17618
|
+
level: "error",
|
|
17619
|
+
message: error.message
|
|
17620
|
+
});
|
|
17621
|
+
}
|
|
17622
|
+
}
|
|
17623
|
+
async function add(value, session) {
|
|
17624
|
+
try {
|
|
17625
|
+
value = MSiteCamera(value);
|
|
17626
|
+
const res = await collection.insertOne(value, { session });
|
|
17627
|
+
delCachedData();
|
|
17628
|
+
return res.insertedId;
|
|
17629
|
+
} catch (error) {
|
|
17630
|
+
logger54.log({
|
|
17631
|
+
level: "error",
|
|
17632
|
+
message: error.message
|
|
17633
|
+
});
|
|
17634
|
+
const isDuplicated = error.message.includes("duplicate");
|
|
17635
|
+
if (isDuplicated) {
|
|
17636
|
+
throw new BadRequestError74("ANPR already exist.");
|
|
17637
|
+
}
|
|
17638
|
+
if (error instanceof AppError11) {
|
|
17639
|
+
throw error;
|
|
17640
|
+
} else {
|
|
17641
|
+
throw new Error("Failed to create ANPR.");
|
|
17642
|
+
}
|
|
17643
|
+
}
|
|
17644
|
+
}
|
|
17645
|
+
async function getAll(value) {
|
|
17646
|
+
value.page = value.page ? value.page - 1 : 0;
|
|
17647
|
+
value.limit = value.limit || 10;
|
|
17648
|
+
const cacheKeyOptions = {
|
|
17649
|
+
type: value.type,
|
|
17650
|
+
page: value.page,
|
|
17651
|
+
limit: value.limit,
|
|
17652
|
+
...value.direction && { direction: value.direction }
|
|
17653
|
+
};
|
|
17654
|
+
const query = {
|
|
17655
|
+
type: value.type,
|
|
17656
|
+
...value.direction && {
|
|
17657
|
+
direction: {
|
|
17658
|
+
$in: Array.isArray(value.direction) ? value.direction : [value.direction]
|
|
17659
|
+
}
|
|
17660
|
+
}
|
|
17661
|
+
};
|
|
17662
|
+
if (value.site) {
|
|
17663
|
+
cacheKeyOptions.site = value.site;
|
|
17664
|
+
try {
|
|
17665
|
+
value.site = new ObjectId47(value.site);
|
|
17666
|
+
query.site = value.site;
|
|
17667
|
+
} catch (error) {
|
|
17668
|
+
throw new BadRequestError74("Invalid site ID format");
|
|
17669
|
+
}
|
|
17670
|
+
}
|
|
17671
|
+
const cacheKey = makeCacheKey25(namespace_collection, cacheKeyOptions);
|
|
17672
|
+
const cachedData = await getCache(cacheKey);
|
|
17673
|
+
if (cachedData) {
|
|
17674
|
+
return cachedData;
|
|
17675
|
+
}
|
|
17676
|
+
try {
|
|
17677
|
+
const items = await collection.aggregate([
|
|
17678
|
+
{
|
|
17679
|
+
$match: query
|
|
17680
|
+
},
|
|
17681
|
+
{
|
|
17682
|
+
$skip: value.page * value.limit
|
|
17683
|
+
},
|
|
17684
|
+
{
|
|
17685
|
+
$limit: value.limit
|
|
17686
|
+
},
|
|
17687
|
+
{
|
|
17688
|
+
$project: {
|
|
17689
|
+
password: 0
|
|
17690
|
+
}
|
|
17691
|
+
}
|
|
17692
|
+
]).toArray();
|
|
17693
|
+
const length = await collection.countDocuments(query);
|
|
17694
|
+
const data = paginate21(items, value.page, value.limit, length);
|
|
17695
|
+
setCache(cacheKey, data, 15 * 60).then(() => {
|
|
17696
|
+
logger54.info(`Cache set for key: ${cacheKey}`);
|
|
17697
|
+
}).catch((err) => {
|
|
17698
|
+
logger54.error(`Failed to set cache for key: ${cacheKey}`, err);
|
|
17699
|
+
});
|
|
17700
|
+
return data;
|
|
17701
|
+
} catch (error) {
|
|
17702
|
+
if (error instanceof BadRequestError74) {
|
|
17703
|
+
throw error;
|
|
17704
|
+
}
|
|
17705
|
+
throw new BadRequestError74("Failed to retrieve site cameras.");
|
|
17706
|
+
}
|
|
17707
|
+
}
|
|
17708
|
+
async function findSiteCameras({ site, type }) {
|
|
17709
|
+
const pipeline = [];
|
|
17710
|
+
const cacheObject = {};
|
|
17711
|
+
if (type) {
|
|
17712
|
+
cacheObject.type = type;
|
|
17713
|
+
pipeline.push({ $match: { "anprs.type": type } });
|
|
17714
|
+
}
|
|
17715
|
+
if (site) {
|
|
17716
|
+
const _site = new ObjectId47(site);
|
|
17717
|
+
cacheObject.site = site;
|
|
17718
|
+
pipeline.push({ $match: { site: _site } });
|
|
17719
|
+
}
|
|
17720
|
+
pipeline.push({
|
|
17721
|
+
$project: {
|
|
17722
|
+
_id: 1,
|
|
17723
|
+
site: 1,
|
|
17724
|
+
cameras: 1
|
|
17725
|
+
}
|
|
17726
|
+
});
|
|
17727
|
+
try {
|
|
17728
|
+
const items = await collection.aggregate(pipeline).toArray();
|
|
17729
|
+
return items;
|
|
17730
|
+
} catch (error) {
|
|
17731
|
+
if (error instanceof BadRequestError74) {
|
|
17732
|
+
throw error;
|
|
17733
|
+
}
|
|
17734
|
+
throw new BadRequestError74("Failed to retrieve site cameras.");
|
|
17735
|
+
}
|
|
17736
|
+
}
|
|
17737
|
+
function delCachedData() {
|
|
17738
|
+
delNamespace().then(() => {
|
|
17739
|
+
logger54.log({
|
|
17740
|
+
level: "info",
|
|
17741
|
+
message: `Cache namespace cleared for ${namespace_collection}`
|
|
17742
|
+
});
|
|
17743
|
+
}).catch((err) => {
|
|
17744
|
+
logger54.log({
|
|
17745
|
+
level: "error",
|
|
17746
|
+
message: `Failed to clear cache namespace for ${namespace_collection}: ${err.message}`
|
|
17747
|
+
});
|
|
17748
|
+
});
|
|
17749
|
+
}
|
|
17750
|
+
async function getBySite(site, options = {}) {
|
|
17751
|
+
const _site = toObjectId11(site);
|
|
17752
|
+
const cacheKeyOptions = {
|
|
17753
|
+
site,
|
|
17754
|
+
tag: "get-by-site",
|
|
17755
|
+
...options.category && { category: options.category },
|
|
17756
|
+
...options.type && { type: options.type },
|
|
17757
|
+
...options.guardPost && { guardPost: options.guardPost }
|
|
17758
|
+
};
|
|
17759
|
+
const cacheKey = makeCacheKey25(namespace_collection, cacheKeyOptions);
|
|
17760
|
+
const cachedData = await getCache(cacheKey);
|
|
17761
|
+
if (cachedData) {
|
|
17762
|
+
return cachedData;
|
|
17763
|
+
}
|
|
17764
|
+
try {
|
|
17765
|
+
delete cacheKeyOptions.tag;
|
|
17766
|
+
cacheKeyOptions.site = _site;
|
|
17767
|
+
const item = await collection.findOne(cacheKeyOptions);
|
|
17768
|
+
setCache(cacheKey, item, 15 * 60).then(() => {
|
|
17769
|
+
logger54.log({
|
|
17770
|
+
level: "info",
|
|
17771
|
+
message: `Cache set for getBySite: ${cacheKey}`
|
|
17772
|
+
});
|
|
17773
|
+
}).catch((err) => {
|
|
17774
|
+
logger54.log({
|
|
17775
|
+
level: "error",
|
|
17776
|
+
message: `Failed to set cache for getBySite: ${err.message}`
|
|
17777
|
+
});
|
|
17778
|
+
});
|
|
17779
|
+
return item;
|
|
17780
|
+
} catch (error) {
|
|
17781
|
+
if (error instanceof BadRequestError74) {
|
|
17782
|
+
throw error;
|
|
17783
|
+
}
|
|
17784
|
+
throw new BadRequestError74("Failed to retrieve site cameras.");
|
|
17785
|
+
}
|
|
17786
|
+
}
|
|
17787
|
+
async function getBySiteGuardPost(site, guardPost) {
|
|
17788
|
+
try {
|
|
17789
|
+
site = new ObjectId47(site);
|
|
17790
|
+
} catch (error) {
|
|
17791
|
+
throw new BadRequestError74("Invalid site ID format");
|
|
17792
|
+
}
|
|
17793
|
+
const cacheKeyOptions = {
|
|
17794
|
+
site,
|
|
17795
|
+
guardPost,
|
|
17796
|
+
tag: "get-by-site"
|
|
17797
|
+
};
|
|
17798
|
+
const cacheKey = makeCacheKey25(namespace_collection, cacheKeyOptions);
|
|
17799
|
+
const cachedData = await getCache(cacheKey);
|
|
17800
|
+
if (cachedData) {
|
|
17801
|
+
return cachedData;
|
|
17802
|
+
}
|
|
17803
|
+
try {
|
|
17804
|
+
delete cacheKeyOptions.tag;
|
|
17805
|
+
const item = await collection.findOne({
|
|
17806
|
+
site,
|
|
17807
|
+
guardPost: { $gt: guardPost }
|
|
17808
|
+
});
|
|
17809
|
+
setCache(cacheKey, item, 15 * 60).then(() => {
|
|
17810
|
+
logger54.log({
|
|
17811
|
+
level: "info",
|
|
17812
|
+
message: `Cache set for getBySite: ${cacheKey}`
|
|
17813
|
+
});
|
|
17814
|
+
}).catch((err) => {
|
|
17815
|
+
logger54.log({
|
|
17816
|
+
level: "error",
|
|
17817
|
+
message: `Failed to set cache for getBySite: ${err.message}`
|
|
17818
|
+
});
|
|
17819
|
+
});
|
|
17820
|
+
return item;
|
|
17821
|
+
} catch (error) {
|
|
17822
|
+
if (error instanceof BadRequestError74) {
|
|
17823
|
+
throw error;
|
|
17824
|
+
}
|
|
17825
|
+
throw new BadRequestError74("Failed to retrieve site cameras.");
|
|
17826
|
+
}
|
|
17827
|
+
}
|
|
17828
|
+
async function getBySites(site, options = {}) {
|
|
17829
|
+
try {
|
|
17830
|
+
site = new ObjectId47(site);
|
|
17831
|
+
} catch (error) {
|
|
17832
|
+
throw new BadRequestError74("Invalid site ID format");
|
|
17833
|
+
}
|
|
17834
|
+
const cacheKeyOptions = { site };
|
|
17835
|
+
if (options.category) {
|
|
17836
|
+
cacheKeyOptions.category = options.category;
|
|
17837
|
+
}
|
|
17838
|
+
if (options.type) {
|
|
17839
|
+
cacheKeyOptions.type = options.type;
|
|
17840
|
+
}
|
|
17841
|
+
if (options.direction) {
|
|
17842
|
+
cacheKeyOptions.direction = options.direction;
|
|
17843
|
+
}
|
|
17844
|
+
const cacheKey = makeCacheKey25(namespace_collection, cacheKeyOptions);
|
|
17845
|
+
const cachedData = await getCache(cacheKey);
|
|
17846
|
+
if (cachedData) {
|
|
17847
|
+
return cachedData;
|
|
17848
|
+
}
|
|
17849
|
+
try {
|
|
17850
|
+
const items = await collection.find(cacheKeyOptions);
|
|
17851
|
+
setCache(cacheKey, items, 15 * 60).then(() => {
|
|
17852
|
+
logger54.log({
|
|
17853
|
+
level: "info",
|
|
17854
|
+
message: `Cache set for getBySite: ${cacheKey}`
|
|
17855
|
+
});
|
|
17856
|
+
}).catch((err) => {
|
|
17857
|
+
logger54.log({
|
|
17858
|
+
level: "error",
|
|
17859
|
+
message: `Failed to set cache for getBySite: ${err.message}`
|
|
17860
|
+
});
|
|
17861
|
+
});
|
|
17862
|
+
return items;
|
|
17863
|
+
} catch (error) {
|
|
17864
|
+
if (error instanceof BadRequestError74) {
|
|
17865
|
+
throw error;
|
|
17866
|
+
}
|
|
17867
|
+
throw new BadRequestError74("Failed to retrieve site cameras.");
|
|
17868
|
+
}
|
|
17869
|
+
}
|
|
17870
|
+
async function updateById(_id, value, session) {
|
|
17871
|
+
const { error } = schemaUpdateSiteCamera.validate(value);
|
|
17872
|
+
if (error) {
|
|
17873
|
+
logger54.info(`Site Camera Management: ${error.message}`);
|
|
17874
|
+
throw new BadRequestError74(error.message);
|
|
17875
|
+
}
|
|
17876
|
+
try {
|
|
17877
|
+
_id = new ObjectId47(_id);
|
|
17878
|
+
} catch (error2) {
|
|
17879
|
+
throw new BadRequestError74("Invalid camera ID format");
|
|
17880
|
+
}
|
|
17881
|
+
try {
|
|
17882
|
+
value.updatedAt = /* @__PURE__ */ new Date();
|
|
17883
|
+
const result = await collection.findOneAndUpdate({ _id }, { $set: value }, { session, returnDocument: "after" });
|
|
17884
|
+
if (!result?._id) {
|
|
17885
|
+
throw new BadRequestError74("Site camera not found.");
|
|
17886
|
+
}
|
|
17887
|
+
const { listenToCamera } = useDahuaService();
|
|
17888
|
+
await listenToCamera(result);
|
|
17889
|
+
delCachedData();
|
|
17890
|
+
return "Successfully updated site camera.";
|
|
17891
|
+
} catch (error2) {
|
|
17892
|
+
logger54.log({
|
|
17893
|
+
level: "error",
|
|
17894
|
+
message: error2.message
|
|
17895
|
+
});
|
|
17896
|
+
const isDuplicated = error2.message.includes("duplicate");
|
|
17897
|
+
if (isDuplicated) {
|
|
17898
|
+
throw new BadRequestError74("ANPR already exist.");
|
|
17899
|
+
}
|
|
17900
|
+
if (error2 instanceof AppError11) {
|
|
17901
|
+
throw error2;
|
|
17902
|
+
}
|
|
17903
|
+
throw new Error("Failed to update ANPR.");
|
|
17904
|
+
}
|
|
17905
|
+
}
|
|
17906
|
+
async function deleteById(_id, session) {
|
|
17907
|
+
try {
|
|
17908
|
+
_id = new ObjectId47(_id);
|
|
17909
|
+
} catch (error) {
|
|
17910
|
+
throw new BadRequestError74("Invalid camera ID format");
|
|
17911
|
+
}
|
|
17912
|
+
try {
|
|
17913
|
+
await collection.deleteOne({ _id }, { session });
|
|
17914
|
+
delCachedData();
|
|
17915
|
+
return "Successfully deleted site camera.";
|
|
17916
|
+
} catch (error) {
|
|
17917
|
+
logger54.log({
|
|
17918
|
+
level: "error",
|
|
17919
|
+
message: error.message
|
|
17920
|
+
});
|
|
17921
|
+
if (error instanceof AppError11) {
|
|
17922
|
+
throw error;
|
|
17923
|
+
}
|
|
17924
|
+
throw new Error("Failed to delete site camera.");
|
|
17925
|
+
}
|
|
17926
|
+
}
|
|
17927
|
+
async function getAllCameraWithPassword(value) {
|
|
17928
|
+
value.page = value.page ? value.page - 1 : 0;
|
|
17929
|
+
value.limit = value.limit || 10;
|
|
17930
|
+
const query = {
|
|
17931
|
+
type: value.type,
|
|
17932
|
+
...value.direction && {
|
|
17933
|
+
direction: {
|
|
17934
|
+
$in: Array.isArray(value.direction) ? value.direction : [value.direction]
|
|
17935
|
+
}
|
|
17936
|
+
}
|
|
17937
|
+
};
|
|
17938
|
+
if (value.site) {
|
|
17939
|
+
try {
|
|
17940
|
+
value.site = new ObjectId47(value.site);
|
|
17941
|
+
query.site = value.site;
|
|
17942
|
+
} catch (error) {
|
|
17943
|
+
throw new BadRequestError74("Invalid site ID format");
|
|
17944
|
+
}
|
|
17945
|
+
}
|
|
17946
|
+
try {
|
|
17947
|
+
const items = await collection.aggregate([
|
|
17948
|
+
{
|
|
17949
|
+
$match: query
|
|
17950
|
+
},
|
|
17951
|
+
{
|
|
17952
|
+
$skip: value.page * value.limit
|
|
17953
|
+
},
|
|
17954
|
+
{
|
|
17955
|
+
$limit: value.limit
|
|
17956
|
+
}
|
|
17957
|
+
]).toArray();
|
|
17958
|
+
const length = await collection.countDocuments(query);
|
|
17959
|
+
const data = paginate21(items, value.page, value.limit, length);
|
|
17960
|
+
return data;
|
|
17961
|
+
} catch (error) {
|
|
17962
|
+
if (error instanceof BadRequestError74) {
|
|
17963
|
+
throw error;
|
|
17964
|
+
}
|
|
17965
|
+
throw new BadRequestError74("Failed to retrieve site cameras.");
|
|
17966
|
+
}
|
|
17967
|
+
}
|
|
17968
|
+
return {
|
|
17969
|
+
createIndexes,
|
|
17970
|
+
add,
|
|
17971
|
+
getAll,
|
|
17972
|
+
findSiteCameras,
|
|
17973
|
+
getBySite,
|
|
17974
|
+
getBySites,
|
|
17975
|
+
delCachedData,
|
|
17976
|
+
getBySiteGuardPost,
|
|
17977
|
+
updateById,
|
|
17978
|
+
deleteById,
|
|
17979
|
+
getAllCameraWithPassword
|
|
17980
|
+
};
|
|
17981
|
+
}
|
|
17982
|
+
|
|
17912
17983
|
// src/services/site.service.ts
|
|
17984
|
+
import Joi41 from "joi";
|
|
17913
17985
|
function useSiteService() {
|
|
17914
17986
|
const {
|
|
17915
17987
|
createSite: _createSite,
|
|
@@ -19976,14 +20048,7 @@ function useSiteCameraService() {
|
|
|
19976
20048
|
for (let index = 0; index < siteCameras.length; index++) {
|
|
19977
20049
|
const siteCamera = siteCameras[index];
|
|
19978
20050
|
if (siteCamera && siteCamera.status === "active" && siteCamera.host && siteCamera.username && siteCamera.password && siteCamera.site && siteCamera.guardPost) {
|
|
19979
|
-
dahuaService.
|
|
19980
|
-
siteCamera.host,
|
|
19981
|
-
siteCamera.username,
|
|
19982
|
-
siteCamera.password,
|
|
19983
|
-
siteCamera.site.toString(),
|
|
19984
|
-
`guard-post-${siteCamera.guardPost}`,
|
|
19985
|
-
siteCamera.direction
|
|
19986
|
-
);
|
|
20051
|
+
dahuaService.listenToCamera(siteCamera);
|
|
19987
20052
|
}
|
|
19988
20053
|
}
|
|
19989
20054
|
}
|
|
@@ -31342,14 +31407,36 @@ function useSiteBillingConfigurationController() {
|
|
|
31342
31407
|
// src/models/event-management.model.ts
|
|
31343
31408
|
import { ObjectId as ObjectId84 } from "mongodb";
|
|
31344
31409
|
import Joi81 from "joi";
|
|
31410
|
+
var EventStatus = /* @__PURE__ */ ((EventStatus2) => {
|
|
31411
|
+
EventStatus2["PLANNED"] = "planned";
|
|
31412
|
+
EventStatus2["IN_PROGRESS"] = "in_progress";
|
|
31413
|
+
EventStatus2["COMPLETED"] = "completed";
|
|
31414
|
+
return EventStatus2;
|
|
31415
|
+
})(EventStatus || {});
|
|
31416
|
+
var EventSort = /* @__PURE__ */ ((EventSort2) => {
|
|
31417
|
+
EventSort2["CREATED_AT"] = "createdAt";
|
|
31418
|
+
EventSort2["NAME"] = "name";
|
|
31419
|
+
EventSort2["ID"] = "_id";
|
|
31420
|
+
return EventSort2;
|
|
31421
|
+
})(EventSort || {});
|
|
31422
|
+
var EventOrder = /* @__PURE__ */ ((EventOrder2) => {
|
|
31423
|
+
EventOrder2["ASC"] = "asc";
|
|
31424
|
+
EventOrder2["DESC"] = "desc";
|
|
31425
|
+
return EventOrder2;
|
|
31426
|
+
})(EventOrder || {});
|
|
31427
|
+
var EventType = /* @__PURE__ */ ((EventType2) => {
|
|
31428
|
+
EventType2["TASK"] = "TASK";
|
|
31429
|
+
EventType2["EVENT"] = "EVENT";
|
|
31430
|
+
return EventType2;
|
|
31431
|
+
})(EventType || {});
|
|
31345
31432
|
var schemaEventManagement = Joi81.object({
|
|
31346
31433
|
_id: Joi81.string().optional().allow(null, ""),
|
|
31347
31434
|
site: Joi81.string().required(),
|
|
31348
31435
|
title: Joi81.string().required(),
|
|
31349
31436
|
description: Joi81.string().optional().allow(""),
|
|
31350
31437
|
dateTime: Joi81.date().iso().required(),
|
|
31351
|
-
status: Joi81.string().optional().default("planned"),
|
|
31352
|
-
type: Joi81.string().optional().default("TASK")
|
|
31438
|
+
status: Joi81.string().optional().default("planned" /* PLANNED */),
|
|
31439
|
+
type: Joi81.string().optional().default("TASK" /* TASK */)
|
|
31353
31440
|
});
|
|
31354
31441
|
var schemaUpdateEventManagement = Joi81.object({
|
|
31355
31442
|
_id: Joi81.string().hex().required(),
|
|
@@ -31380,8 +31467,8 @@ function MEventManagement(value) {
|
|
|
31380
31467
|
title: value.title,
|
|
31381
31468
|
description: value.description ?? "",
|
|
31382
31469
|
dateTime: new Date(value.dateTime),
|
|
31383
|
-
status: value.status ?? "planned"
|
|
31384
|
-
type: value.type ?? "TASK"
|
|
31470
|
+
status: value.status ?? "planned" /* PLANNED */,
|
|
31471
|
+
type: value.type ?? "TASK" /* TASK */,
|
|
31385
31472
|
createdAt: value.createdAt ?? /* @__PURE__ */ new Date(),
|
|
31386
31473
|
updatedAt: value.updatedAt,
|
|
31387
31474
|
deletedAt: value.deletedAt
|
|
@@ -31489,7 +31576,8 @@ function useEventManagementRepo() {
|
|
|
31489
31576
|
sort: JSON.stringify(sort),
|
|
31490
31577
|
page,
|
|
31491
31578
|
limit,
|
|
31492
|
-
...type && { type }
|
|
31579
|
+
...type && { type },
|
|
31580
|
+
...date && { dateTime: date }
|
|
31493
31581
|
};
|
|
31494
31582
|
if (search) {
|
|
31495
31583
|
query.$or = [{ title: { $regex: search, $options: "i" } }];
|
|
@@ -31692,8 +31780,8 @@ function useEventManagementService() {
|
|
|
31692
31780
|
} = useEventManagementRepo();
|
|
31693
31781
|
async function add(value) {
|
|
31694
31782
|
const session = useAtlas73.getClient()?.startSession();
|
|
31695
|
-
session?.startTransaction();
|
|
31696
31783
|
try {
|
|
31784
|
+
session?.startTransaction();
|
|
31697
31785
|
await _add(value, session);
|
|
31698
31786
|
await session?.commitTransaction();
|
|
31699
31787
|
return "Successfully added event.";
|
|
@@ -31756,67 +31844,51 @@ function useEventManagementController() {
|
|
|
31756
31844
|
deleteEventManagementById: _deleteEventManagementById
|
|
31757
31845
|
} = useEventManagementRepo();
|
|
31758
31846
|
async function add(req, res, next) {
|
|
31759
|
-
const payload = { ...req.body };
|
|
31760
|
-
const { error } = schemaEventManagement.validate(payload, {
|
|
31761
|
-
abortEarly: false
|
|
31762
|
-
});
|
|
31763
|
-
if (error) {
|
|
31764
|
-
const messages = error.details.map((d) => d.message).join(", ");
|
|
31765
|
-
logger115.log({ level: "error", message: messages });
|
|
31766
|
-
next(new BadRequestError137(messages));
|
|
31767
|
-
return;
|
|
31768
|
-
}
|
|
31769
31847
|
try {
|
|
31770
|
-
const
|
|
31848
|
+
const { error, value } = schemaEventManagement.validate(req.body, {
|
|
31849
|
+
abortEarly: false
|
|
31850
|
+
});
|
|
31851
|
+
if (error) {
|
|
31852
|
+
const messages = error.details.map((d) => d.message).join(", ");
|
|
31853
|
+
logger115.log({ level: "error", message: messages });
|
|
31854
|
+
next(new BadRequestError137(messages));
|
|
31855
|
+
return;
|
|
31856
|
+
}
|
|
31857
|
+
const data = await _add(value);
|
|
31771
31858
|
res.status(201).json(data);
|
|
31772
31859
|
return;
|
|
31773
|
-
} catch (
|
|
31774
|
-
logger115.log({ level: "error", message:
|
|
31775
|
-
next(
|
|
31860
|
+
} catch (error) {
|
|
31861
|
+
logger115.log({ level: "error", message: error.message });
|
|
31862
|
+
next(error);
|
|
31776
31863
|
return;
|
|
31777
31864
|
}
|
|
31778
31865
|
}
|
|
31779
31866
|
async function getAll(req, res, next) {
|
|
31780
|
-
const allowedFields = ["createdAt", "name"];
|
|
31781
|
-
const allowedOrder = ["asc", "desc"];
|
|
31782
|
-
const validation = Joi82.object({
|
|
31783
|
-
search: Joi82.string().optional().allow("", null),
|
|
31784
|
-
page: Joi82.number().integer().min(1).allow("", null).default(1),
|
|
31785
|
-
limit: Joi82.number().integer().min(1).max(100).allow("", null).default(10),
|
|
31786
|
-
sort: Joi82.string().pattern(/^([a-zA-Z0-9_]+)(,[a-zA-Z0-9_]+)*$/).optional().allow("", ...allowedFields),
|
|
31787
|
-
order: Joi82.string().pattern(/^(asc|desc)(,(asc|desc))*$/).optional().allow("", ...allowedOrder),
|
|
31788
|
-
site: Joi82.string().hex().required(),
|
|
31789
|
-
status: Joi82.string().optional(),
|
|
31790
|
-
type: Joi82.string().optional().valid("TASK", "EVENT").allow(null, ""),
|
|
31791
|
-
date: Joi82.string().optional().allow(null, "")
|
|
31792
|
-
});
|
|
31793
|
-
const query = { ...req.query };
|
|
31794
|
-
const { error } = validation.validate(query, {
|
|
31795
|
-
abortEarly: false
|
|
31796
|
-
});
|
|
31797
|
-
if (error) {
|
|
31798
|
-
const messages = error.details.map((d) => d.message).join(", ");
|
|
31799
|
-
logger115.log({ level: "error", message: messages });
|
|
31800
|
-
next(new BadRequestError137(messages));
|
|
31801
|
-
return;
|
|
31802
|
-
}
|
|
31803
|
-
const search = req.query.search ?? "";
|
|
31804
|
-
const page = parseInt(req.query.page ?? "1");
|
|
31805
|
-
const limit = parseInt(req.query.limit ?? "10");
|
|
31806
|
-
const site = req.query.site ?? "";
|
|
31807
|
-
const status = req.query.status ?? "";
|
|
31808
|
-
const type = req.query.type ?? "";
|
|
31809
|
-
const date = req.query.date ?? "";
|
|
31810
|
-
const sortObj = {};
|
|
31811
|
-
const sortFields = String(req.query.sort).split(",");
|
|
31812
|
-
const sortOrders = String(req.query.order).split(",");
|
|
31813
|
-
sortFields.forEach((field, index) => {
|
|
31814
|
-
if (allowedFields.includes(field)) {
|
|
31815
|
-
const order = sortOrders[index] === "asc" ? 1 : -1;
|
|
31816
|
-
sortObj[field] = order;
|
|
31817
|
-
}
|
|
31818
|
-
});
|
|
31819
31867
|
try {
|
|
31868
|
+
const validation = Joi82.object({
|
|
31869
|
+
search: Joi82.string().optional().allow("", null),
|
|
31870
|
+
page: Joi82.number().integer().min(1).allow("", null).default(1),
|
|
31871
|
+
limit: Joi82.number().integer().min(1).max(100).allow("", null).default(10),
|
|
31872
|
+
sort: Joi82.string().valid(...Object.values(EventSort)).default("_id" /* ID */),
|
|
31873
|
+
order: Joi82.string().valid(...Object.values(EventOrder)).default("desc" /* DESC */),
|
|
31874
|
+
site: Joi82.string().hex().length(24).required(),
|
|
31875
|
+
status: Joi82.string().valid(...Object.values(EventStatus)).allow(null, ""),
|
|
31876
|
+
type: Joi82.string().optional().valid(...Object.values(EventType)).allow(null, ""),
|
|
31877
|
+
date: Joi82.string().optional().allow(null, "")
|
|
31878
|
+
});
|
|
31879
|
+
const { error, value } = validation.validate(req.query, {
|
|
31880
|
+
abortEarly: false
|
|
31881
|
+
});
|
|
31882
|
+
if (error) {
|
|
31883
|
+
const messages = error.details.map((d) => d.message).join(", ");
|
|
31884
|
+
logger115.log({ level: "error", message: messages });
|
|
31885
|
+
next(new BadRequestError137(messages));
|
|
31886
|
+
return;
|
|
31887
|
+
}
|
|
31888
|
+
const { search, page, limit, site, status, type, date, sort, order } = value;
|
|
31889
|
+
const sortObj = {
|
|
31890
|
+
[sort ?? "_id" /* ID */]: order === "asc" /* ASC */ ? 1 : -1
|
|
31891
|
+
};
|
|
31820
31892
|
const data = await _getAll({
|
|
31821
31893
|
search,
|
|
31822
31894
|
page,
|
|
@@ -31829,28 +31901,30 @@ function useEventManagementController() {
|
|
|
31829
31901
|
});
|
|
31830
31902
|
res.status(200).json(data);
|
|
31831
31903
|
return;
|
|
31832
|
-
} catch (
|
|
31833
|
-
logger115.log({ level: "error", message:
|
|
31834
|
-
next(
|
|
31904
|
+
} catch (error) {
|
|
31905
|
+
logger115.log({ level: "error", message: error.message });
|
|
31906
|
+
next(error);
|
|
31835
31907
|
return;
|
|
31836
31908
|
}
|
|
31837
31909
|
}
|
|
31838
31910
|
async function getEventManagementById(req, res, next) {
|
|
31839
|
-
const validation = Joi82.string().hex().required();
|
|
31840
|
-
const _id = req.params.id;
|
|
31841
|
-
const { error } = validation.validate(_id);
|
|
31842
|
-
if (error) {
|
|
31843
|
-
logger115.log({ level: "error", message: error.message });
|
|
31844
|
-
next(new BadRequestError137(error.message));
|
|
31845
|
-
return;
|
|
31846
|
-
}
|
|
31847
31911
|
try {
|
|
31912
|
+
const schema2 = Joi82.object({
|
|
31913
|
+
_id: Joi82.string().hex().length(24).required()
|
|
31914
|
+
});
|
|
31915
|
+
const { error, value } = schema2.validate({ _id: req.params.id });
|
|
31916
|
+
if (error) {
|
|
31917
|
+
logger115.log({ level: "error", message: error.message });
|
|
31918
|
+
next(new BadRequestError137(error.message));
|
|
31919
|
+
return;
|
|
31920
|
+
}
|
|
31921
|
+
const { _id } = value;
|
|
31848
31922
|
const data = await _getEventManagementById(_id);
|
|
31849
31923
|
res.status(200).json(data);
|
|
31850
31924
|
return;
|
|
31851
|
-
} catch (
|
|
31852
|
-
logger115.log({ level: "error", message:
|
|
31853
|
-
next(
|
|
31925
|
+
} catch (error) {
|
|
31926
|
+
logger115.log({ level: "error", message: error.message });
|
|
31927
|
+
next(error);
|
|
31854
31928
|
return;
|
|
31855
31929
|
}
|
|
31856
31930
|
}
|
|
@@ -33315,6 +33389,15 @@ var formatEntryPassDate = (date) => {
|
|
|
33315
33389
|
const day = String(newDate.getDate()).padStart(2, "0");
|
|
33316
33390
|
return `${year}${month}${day}`;
|
|
33317
33391
|
};
|
|
33392
|
+
var entryPassDate = (TRDATE, TRTIME) => {
|
|
33393
|
+
const year = parseInt(TRDATE.substring(0, 4), 10);
|
|
33394
|
+
const month = parseInt(TRDATE.substring(4, 6), 10) - 1;
|
|
33395
|
+
const day = parseInt(TRDATE.substring(6, 8), 10);
|
|
33396
|
+
const hours = parseInt(TRTIME.substring(0, 2), 10);
|
|
33397
|
+
const minutes = parseInt(TRTIME.substring(2, 4), 10);
|
|
33398
|
+
const seconds = parseInt(TRTIME.substring(4, 6), 10);
|
|
33399
|
+
return new Date(year, month, day, hours, minutes, seconds);
|
|
33400
|
+
};
|
|
33318
33401
|
async function removeAccessGroup({
|
|
33319
33402
|
cardNo,
|
|
33320
33403
|
staffNo,
|
|
@@ -33347,6 +33430,16 @@ async function removeAccessGroup({
|
|
|
33347
33430
|
console.log(error);
|
|
33348
33431
|
}
|
|
33349
33432
|
}
|
|
33433
|
+
async function getTransactions(index, url) {
|
|
33434
|
+
try {
|
|
33435
|
+
const decrypt = decryptAcmUrl(url);
|
|
33436
|
+
const response = await axios.get(`${decrypt}transactions?from=${index}`);
|
|
33437
|
+
if (response.status === 200 || response.status === 201)
|
|
33438
|
+
return response.data;
|
|
33439
|
+
} catch (error) {
|
|
33440
|
+
return Promise.reject(error);
|
|
33441
|
+
}
|
|
33442
|
+
}
|
|
33350
33443
|
|
|
33351
33444
|
// src/repositories/access-management.repo.ts
|
|
33352
33445
|
import { parseStringPromise as parseStringPromise2 } from "xml2js";
|
|
@@ -35333,6 +35426,116 @@ function UseAccessManagementRepo() {
|
|
|
35333
35426
|
throw new Error(error.message);
|
|
35334
35427
|
}
|
|
35335
35428
|
}
|
|
35429
|
+
async function getTransactionsRepo({ page = 1, limit = 10, site, cardNo, url }) {
|
|
35430
|
+
page = page ? page - 1 : 0;
|
|
35431
|
+
site = new ObjectId90(site);
|
|
35432
|
+
try {
|
|
35433
|
+
let index = await collectionName("access-card-transactions").findOne({}, { sort: { index: -1 } });
|
|
35434
|
+
index = index ? index.index : 0;
|
|
35435
|
+
const response = await getTransactions(index, url);
|
|
35436
|
+
if (response && Array.isArray(response.items) && response.items.length > 0) {
|
|
35437
|
+
let result2 = response.items.map((item) => ({
|
|
35438
|
+
id: item.id,
|
|
35439
|
+
data: JSON.parse(item.data),
|
|
35440
|
+
timestamp: item.timestamp
|
|
35441
|
+
}));
|
|
35442
|
+
result2 = result2.filter((item) => item.data.Event.ETYPE === "0" && item.data.Event.CARDNO !== "");
|
|
35443
|
+
if (result2.length > 0) {
|
|
35444
|
+
const transactions = result2.map(
|
|
35445
|
+
(item) => new MAccessCardTransaction({
|
|
35446
|
+
index: item.id,
|
|
35447
|
+
cardNo: item.data.Event.CARDNO,
|
|
35448
|
+
staffNo: item.data.Event.STAFFNO,
|
|
35449
|
+
staffName: item.data.Event.STAFFNAME,
|
|
35450
|
+
accessType: item.data.Event.DEVNAME,
|
|
35451
|
+
accessStatus: item.data.Event.TRCODE,
|
|
35452
|
+
description: item.data.Event.TRDESC,
|
|
35453
|
+
accessTime: entryPassDate(item.data.Event.TRDATE, item.data.Event.TRTIME),
|
|
35454
|
+
createdAt: /* @__PURE__ */ new Date()
|
|
35455
|
+
})
|
|
35456
|
+
);
|
|
35457
|
+
await collectionName("access-card-transactions").insertMany(transactions);
|
|
35458
|
+
}
|
|
35459
|
+
}
|
|
35460
|
+
const result = await collectionName("access-card-transactions").aggregate([
|
|
35461
|
+
{
|
|
35462
|
+
$match: { cardNo }
|
|
35463
|
+
},
|
|
35464
|
+
{
|
|
35465
|
+
$lookup: {
|
|
35466
|
+
from: "access-card",
|
|
35467
|
+
let: { cardNo: "$cardNo" },
|
|
35468
|
+
pipeline: [
|
|
35469
|
+
{
|
|
35470
|
+
$match: {
|
|
35471
|
+
$expr: {
|
|
35472
|
+
$eq: ["$cardNo", "$$cardNo"]
|
|
35473
|
+
}
|
|
35474
|
+
}
|
|
35475
|
+
},
|
|
35476
|
+
{
|
|
35477
|
+
$lookup: {
|
|
35478
|
+
from: "users",
|
|
35479
|
+
let: { userId: "$userId" },
|
|
35480
|
+
pipeline: [
|
|
35481
|
+
{
|
|
35482
|
+
$match: {
|
|
35483
|
+
$expr: {
|
|
35484
|
+
$eq: ["$_id", "$$userId"]
|
|
35485
|
+
}
|
|
35486
|
+
}
|
|
35487
|
+
},
|
|
35488
|
+
{
|
|
35489
|
+
$project: {
|
|
35490
|
+
_id: 1,
|
|
35491
|
+
givenName: 1,
|
|
35492
|
+
surname: 1
|
|
35493
|
+
}
|
|
35494
|
+
}
|
|
35495
|
+
],
|
|
35496
|
+
as: "user"
|
|
35497
|
+
}
|
|
35498
|
+
},
|
|
35499
|
+
{
|
|
35500
|
+
$project: {
|
|
35501
|
+
_id: 1,
|
|
35502
|
+
user: { $arrayElemAt: ["$user", 0] },
|
|
35503
|
+
type: 1,
|
|
35504
|
+
cardNo: 1
|
|
35505
|
+
}
|
|
35506
|
+
}
|
|
35507
|
+
],
|
|
35508
|
+
as: "accessCard"
|
|
35509
|
+
}
|
|
35510
|
+
},
|
|
35511
|
+
{
|
|
35512
|
+
$project: {
|
|
35513
|
+
_id: 1,
|
|
35514
|
+
cardNo: 1,
|
|
35515
|
+
staffNo: 1,
|
|
35516
|
+
staffName: 1,
|
|
35517
|
+
accessType: 1,
|
|
35518
|
+
accessStatus: 1,
|
|
35519
|
+
description: 1,
|
|
35520
|
+
accessTime: 1,
|
|
35521
|
+
createdAt: 1,
|
|
35522
|
+
accessCard: { $arrayElemAt: ["$accessCard", 0] }
|
|
35523
|
+
}
|
|
35524
|
+
},
|
|
35525
|
+
{
|
|
35526
|
+
$facet: {
|
|
35527
|
+
totalCount: [{ $count: "count" }],
|
|
35528
|
+
items: [{ $sort: { _id: -1 } }, { $skip: page * limit }, { $limit: limit }]
|
|
35529
|
+
}
|
|
35530
|
+
}
|
|
35531
|
+
]).toArray();
|
|
35532
|
+
const length = result[0].totalCount[0] ? result[0].totalCount[0].count : 0;
|
|
35533
|
+
const items = result[0].items;
|
|
35534
|
+
return paginate39(items, page, limit, length);
|
|
35535
|
+
} catch (error) {
|
|
35536
|
+
return Promise.reject("Server internal error.");
|
|
35537
|
+
}
|
|
35538
|
+
}
|
|
35336
35539
|
return {
|
|
35337
35540
|
createIndexes,
|
|
35338
35541
|
createIndexForEntrypass,
|
|
@@ -35364,7 +35567,8 @@ function UseAccessManagementRepo() {
|
|
|
35364
35567
|
signQrCodeRepo,
|
|
35365
35568
|
checkoutVisitorRepo,
|
|
35366
35569
|
getBlockLevelAndUnitListRepo,
|
|
35367
|
-
indexCombination
|
|
35570
|
+
indexCombination,
|
|
35571
|
+
getTransactionsRepo
|
|
35368
35572
|
};
|
|
35369
35573
|
}
|
|
35370
35574
|
|
|
@@ -35404,7 +35608,8 @@ function useAccessManagementSvc() {
|
|
|
35404
35608
|
addVisitorAccessCardRepo,
|
|
35405
35609
|
signQrCodeRepo,
|
|
35406
35610
|
checkoutVisitorRepo,
|
|
35407
|
-
getBlockLevelAndUnitListRepo
|
|
35611
|
+
getBlockLevelAndUnitListRepo,
|
|
35612
|
+
getTransactionsRepo
|
|
35408
35613
|
} = UseAccessManagementRepo();
|
|
35409
35614
|
const addPhysicalCardSvc = async (payload) => {
|
|
35410
35615
|
try {
|
|
@@ -35696,6 +35901,14 @@ function useAccessManagementSvc() {
|
|
|
35696
35901
|
throw new Error(err.message);
|
|
35697
35902
|
}
|
|
35698
35903
|
};
|
|
35904
|
+
const getTransactionsSvc = async ({ page, limit, site, cardNo, url }) => {
|
|
35905
|
+
try {
|
|
35906
|
+
const response = await getTransactionsRepo({ page, limit, site, cardNo, url });
|
|
35907
|
+
return response;
|
|
35908
|
+
} catch (err) {
|
|
35909
|
+
return Promise.reject("Server internal error.");
|
|
35910
|
+
}
|
|
35911
|
+
};
|
|
35699
35912
|
return {
|
|
35700
35913
|
addPhysicalCardSvc,
|
|
35701
35914
|
addNonPhysicalCardSvc,
|
|
@@ -35730,7 +35943,8 @@ function useAccessManagementSvc() {
|
|
|
35730
35943
|
addVisitorAccessCardSvc,
|
|
35731
35944
|
signQrCodeSvc,
|
|
35732
35945
|
checkoutVisitorSvc,
|
|
35733
|
-
getBlockLevelAndUnitListSvc
|
|
35946
|
+
getBlockLevelAndUnitListSvc,
|
|
35947
|
+
getTransactionsSvc
|
|
35734
35948
|
};
|
|
35735
35949
|
}
|
|
35736
35950
|
|
|
@@ -35770,7 +35984,8 @@ function useAccessManagementController() {
|
|
|
35770
35984
|
addVisitorAccessCardSvc,
|
|
35771
35985
|
signQrCodeSvc,
|
|
35772
35986
|
checkoutVisitorSvc,
|
|
35773
|
-
getBlockLevelAndUnitListSvc
|
|
35987
|
+
getBlockLevelAndUnitListSvc,
|
|
35988
|
+
getTransactionsSvc
|
|
35774
35989
|
} = useAccessManagementSvc();
|
|
35775
35990
|
const addPhysicalCard = async (req, res) => {
|
|
35776
35991
|
try {
|
|
@@ -36532,6 +36747,31 @@ function useAccessManagementController() {
|
|
|
36532
36747
|
});
|
|
36533
36748
|
}
|
|
36534
36749
|
};
|
|
36750
|
+
const getTransactions2 = async (req, res) => {
|
|
36751
|
+
const { page = 1, limit = 10, site, cardNo, url } = req.query;
|
|
36752
|
+
const schema2 = Joi85.object({
|
|
36753
|
+
page: Joi85.number().required(),
|
|
36754
|
+
limit: Joi85.number().optional().default(10),
|
|
36755
|
+
site: Joi85.string().hex().required(),
|
|
36756
|
+
cardNo: Joi85.string().required()
|
|
36757
|
+
});
|
|
36758
|
+
const { value, error } = schema2.validate({ page, limit, site, cardNo });
|
|
36759
|
+
if (error) {
|
|
36760
|
+
return res.status(400).json({ message: error.message });
|
|
36761
|
+
}
|
|
36762
|
+
try {
|
|
36763
|
+
const result = await getTransactionsSvc({
|
|
36764
|
+
page: Number(page),
|
|
36765
|
+
limit: Number(value.limit),
|
|
36766
|
+
site,
|
|
36767
|
+
cardNo,
|
|
36768
|
+
url
|
|
36769
|
+
});
|
|
36770
|
+
return res.json(result);
|
|
36771
|
+
} catch (error2) {
|
|
36772
|
+
return Promise.reject("Internal Server Error");
|
|
36773
|
+
}
|
|
36774
|
+
};
|
|
36535
36775
|
return {
|
|
36536
36776
|
addPhysicalCard,
|
|
36537
36777
|
addNonPhysicalCard,
|
|
@@ -36564,7 +36804,8 @@ function useAccessManagementController() {
|
|
|
36564
36804
|
signQrCode,
|
|
36565
36805
|
checkoutVisitor,
|
|
36566
36806
|
removeAccessCard,
|
|
36567
|
-
getBlockLevelAndUnitList
|
|
36807
|
+
getBlockLevelAndUnitList,
|
|
36808
|
+
getTransactions: getTransactions2
|
|
36568
36809
|
};
|
|
36569
36810
|
}
|
|
36570
36811
|
|
|
@@ -50612,6 +50853,10 @@ export {
|
|
|
50612
50853
|
EmailSender,
|
|
50613
50854
|
EntryOrder,
|
|
50614
50855
|
EntrySort,
|
|
50856
|
+
EventOrder,
|
|
50857
|
+
EventSort,
|
|
50858
|
+
EventStatus,
|
|
50859
|
+
EventType,
|
|
50615
50860
|
FacilitySort,
|
|
50616
50861
|
FacilityStatus,
|
|
50617
50862
|
GuestSort,
|
|
@@ -50724,6 +50969,7 @@ export {
|
|
|
50724
50969
|
events_namespace_collection,
|
|
50725
50970
|
facility_bookings_namespace_collection,
|
|
50726
50971
|
feedbackSchema,
|
|
50972
|
+
feedbacks2_namespace_collection,
|
|
50727
50973
|
feedbacks_namespace_collection,
|
|
50728
50974
|
formatDahuaDate,
|
|
50729
50975
|
getPeriodRangeWithPrevious,
|