@7365admin1/core 3.26.0 → 3.28.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 +12 -0
- package/dist/index.d.ts +20 -1
- package/dist/index.js +352 -54
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +354 -54
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -8676,6 +8676,31 @@ function useUserRepo() {
|
|
|
8676
8676
|
throw new InternalServerError5("Failed to update user organization.");
|
|
8677
8677
|
}
|
|
8678
8678
|
}
|
|
8679
|
+
async function updateUserUnitById(id, value, session) {
|
|
8680
|
+
const _id = toObjectId(id);
|
|
8681
|
+
const update = {};
|
|
8682
|
+
if ("block" in value)
|
|
8683
|
+
update.block = value.block ?? null;
|
|
8684
|
+
if ("level" in value)
|
|
8685
|
+
update.level = value.level ?? null;
|
|
8686
|
+
if ("unitId" in value)
|
|
8687
|
+
update.unitId = value.unitId ?? null;
|
|
8688
|
+
if ("unitName" in value)
|
|
8689
|
+
update.unitName = value.unitName ?? "";
|
|
8690
|
+
if (Object.keys(update).length === 0) {
|
|
8691
|
+
return "No user unit fields to update.";
|
|
8692
|
+
}
|
|
8693
|
+
try {
|
|
8694
|
+
await collection.updateOne(
|
|
8695
|
+
{ _id },
|
|
8696
|
+
{ $set: { ...update, updatedAt: (/* @__PURE__ */ new Date()).toISOString() } },
|
|
8697
|
+
{ session }
|
|
8698
|
+
);
|
|
8699
|
+
return "Successfully updated user unit information.";
|
|
8700
|
+
} catch (error) {
|
|
8701
|
+
throw new InternalServerError5("Failed to update user unit information.");
|
|
8702
|
+
}
|
|
8703
|
+
}
|
|
8679
8704
|
return {
|
|
8680
8705
|
createIndex,
|
|
8681
8706
|
createTextIndex,
|
|
@@ -8694,7 +8719,8 @@ function useUserRepo() {
|
|
|
8694
8719
|
getUserByEmailStatus,
|
|
8695
8720
|
updateUserSIDById,
|
|
8696
8721
|
resetPassword,
|
|
8697
|
-
updateUserOrgById
|
|
8722
|
+
updateUserOrgById,
|
|
8723
|
+
updateUserUnitById
|
|
8698
8724
|
};
|
|
8699
8725
|
}
|
|
8700
8726
|
|
|
@@ -25161,6 +25187,10 @@ function usePersonRepo() {
|
|
|
25161
25187
|
}
|
|
25162
25188
|
async function getByNRIC(value) {
|
|
25163
25189
|
try {
|
|
25190
|
+
if (!value || value.trim() === "") {
|
|
25191
|
+
logger52.warn("getByNRIC called with an empty or invalid NRIC value.");
|
|
25192
|
+
return null;
|
|
25193
|
+
}
|
|
25164
25194
|
const cacheKey = makeCacheKey24(site_people_namespace_collection, {
|
|
25165
25195
|
nric: value
|
|
25166
25196
|
});
|
|
@@ -25209,8 +25239,11 @@ function usePersonRepo() {
|
|
|
25209
25239
|
unit
|
|
25210
25240
|
}, session) {
|
|
25211
25241
|
try {
|
|
25242
|
+
if (!unit || !ObjectId45.isValid(unit)) {
|
|
25243
|
+
throw new BadRequestError72("Invalid unit ID.");
|
|
25244
|
+
}
|
|
25212
25245
|
const query = {
|
|
25213
|
-
unit,
|
|
25246
|
+
unit: new ObjectId45(unit),
|
|
25214
25247
|
status,
|
|
25215
25248
|
...Array.isArray(type) && type.length > 0 && {
|
|
25216
25249
|
type: { $in: type }
|
|
@@ -26189,18 +26222,7 @@ function useBuildingUnitRepo() {
|
|
|
26189
26222
|
} catch (error) {
|
|
26190
26223
|
throw new BadRequestError74("Invalid ID.");
|
|
26191
26224
|
}
|
|
26192
|
-
const cacheKey = makeCacheKey25(building_units_namespace_collection, {
|
|
26193
|
-
_id: String(_id)
|
|
26194
|
-
});
|
|
26195
26225
|
try {
|
|
26196
|
-
const cached = await getCache(cacheKey);
|
|
26197
|
-
if (cached) {
|
|
26198
|
-
logger54.log({
|
|
26199
|
-
level: "info",
|
|
26200
|
-
message: `Cache hit for getById building unit: ${cacheKey}`
|
|
26201
|
-
});
|
|
26202
|
-
return cached;
|
|
26203
|
-
}
|
|
26204
26226
|
const result = await collection.findOne({
|
|
26205
26227
|
_id,
|
|
26206
26228
|
deletedAt: { $in: ["", null] }
|
|
@@ -26208,17 +26230,6 @@ function useBuildingUnitRepo() {
|
|
|
26208
26230
|
if (!result) {
|
|
26209
26231
|
throw new BadRequestError74("Building unit not found.");
|
|
26210
26232
|
}
|
|
26211
|
-
setCache(cacheKey, result, 300).then(() => {
|
|
26212
|
-
logger54.log({
|
|
26213
|
-
level: "info",
|
|
26214
|
-
message: `Cache set for building unit by id: ${cacheKey}`
|
|
26215
|
-
});
|
|
26216
|
-
}).catch((err) => {
|
|
26217
|
-
logger54.log({
|
|
26218
|
-
level: "error",
|
|
26219
|
-
message: `Failed to set cache for building unit by id: ${err.message}`
|
|
26220
|
-
});
|
|
26221
|
-
});
|
|
26222
26233
|
return result;
|
|
26223
26234
|
} catch (error) {
|
|
26224
26235
|
if (error instanceof AppError11) {
|
|
@@ -34749,7 +34760,7 @@ function useVisitorTransactionService() {
|
|
|
34749
34760
|
const unit = await _getUnitById(value.unit);
|
|
34750
34761
|
value.unitName = unit?.name;
|
|
34751
34762
|
}
|
|
34752
|
-
if (allowedPersonTypes.includes(value?.type)) {
|
|
34763
|
+
if (allowedPersonTypes.includes(value?.type) && value?.nric) {
|
|
34753
34764
|
const nric = value?.nric || "";
|
|
34754
34765
|
const person = await getByNRIC(nric);
|
|
34755
34766
|
const existingCompanyName = person?.companyName?.includes(
|
|
@@ -34981,6 +34992,103 @@ function useVisitorTransactionService() {
|
|
|
34981
34992
|
let host;
|
|
34982
34993
|
let username;
|
|
34983
34994
|
let password;
|
|
34995
|
+
if (value.checkIn) {
|
|
34996
|
+
const parsed = new Date(value.checkIn);
|
|
34997
|
+
value.checkIn = isNaN(parsed.getTime()) ? null : parsed;
|
|
34998
|
+
}
|
|
34999
|
+
if (value.isMembersAdded != true && Array.isArray(value.members) && value.members.length > 0) {
|
|
35000
|
+
const chunkSize = 10;
|
|
35001
|
+
const {
|
|
35002
|
+
block,
|
|
35003
|
+
level,
|
|
35004
|
+
unit,
|
|
35005
|
+
site,
|
|
35006
|
+
org,
|
|
35007
|
+
type,
|
|
35008
|
+
company,
|
|
35009
|
+
remarks,
|
|
35010
|
+
contractorType,
|
|
35011
|
+
unitName
|
|
35012
|
+
} = value;
|
|
35013
|
+
for (let i = 0; i < value.members.length; i += chunkSize) {
|
|
35014
|
+
const chunk = value.members.slice(i, i + chunkSize);
|
|
35015
|
+
await Promise.all(
|
|
35016
|
+
chunk.map(async (member) => {
|
|
35017
|
+
const clonedMember = structuredClone(member);
|
|
35018
|
+
const { visitorPass, passKeys } = clonedMember;
|
|
35019
|
+
const preparedVisitorPass = Array.isArray(visitorPass) ? visitorPass.map((item) => ({
|
|
35020
|
+
...item,
|
|
35021
|
+
receivedDate: /* @__PURE__ */ new Date(),
|
|
35022
|
+
status: "Not Returned" /* NOT_RETURNED */,
|
|
35023
|
+
lastUpdate: null,
|
|
35024
|
+
remarks: ""
|
|
35025
|
+
})) : [];
|
|
35026
|
+
const preparedPassKeys = Array.isArray(passKeys) ? passKeys.map((item) => ({
|
|
35027
|
+
...item,
|
|
35028
|
+
receivedDate: /* @__PURE__ */ new Date(),
|
|
35029
|
+
status: "Not Returned" /* NOT_RETURNED */,
|
|
35030
|
+
lastUpdate: null,
|
|
35031
|
+
remarks: ""
|
|
35032
|
+
})) : [];
|
|
35033
|
+
const visitorId = await _add(
|
|
35034
|
+
{
|
|
35035
|
+
...clonedMember,
|
|
35036
|
+
block,
|
|
35037
|
+
level,
|
|
35038
|
+
unit,
|
|
35039
|
+
unitName,
|
|
35040
|
+
site,
|
|
35041
|
+
org,
|
|
35042
|
+
type,
|
|
35043
|
+
company,
|
|
35044
|
+
remarks,
|
|
35045
|
+
contractorType,
|
|
35046
|
+
checkIn: value.checkIn,
|
|
35047
|
+
visitorPass: preparedVisitorPass,
|
|
35048
|
+
passKeys: preparedPassKeys,
|
|
35049
|
+
status: "registered" /* REGISTERED */
|
|
35050
|
+
},
|
|
35051
|
+
session
|
|
35052
|
+
);
|
|
35053
|
+
console.log("visitorId service", visitorId);
|
|
35054
|
+
for (const item of preparedVisitorPass) {
|
|
35055
|
+
await KeyRepo.checkPassOrKeyAvailability(item.keyId, "Pass", session);
|
|
35056
|
+
await KeyRepo.updateKeyById(
|
|
35057
|
+
item.keyId,
|
|
35058
|
+
{
|
|
35059
|
+
status: "In Use" /* IN_USE */,
|
|
35060
|
+
updatedBy: value.createdBy
|
|
35061
|
+
},
|
|
35062
|
+
value.site,
|
|
35063
|
+
session,
|
|
35064
|
+
void 0,
|
|
35065
|
+
visitorId,
|
|
35066
|
+
void 0,
|
|
35067
|
+
true
|
|
35068
|
+
);
|
|
35069
|
+
}
|
|
35070
|
+
for (const item of preparedPassKeys) {
|
|
35071
|
+
await KeyRepo.checkPassOrKeyAvailability(item.keyId, "Key", session);
|
|
35072
|
+
await KeyRepo.updateKeyById(
|
|
35073
|
+
item.keyId,
|
|
35074
|
+
{
|
|
35075
|
+
status: "In Use" /* IN_USE */,
|
|
35076
|
+
updatedBy: value.createdBy,
|
|
35077
|
+
visitorId
|
|
35078
|
+
},
|
|
35079
|
+
value.site,
|
|
35080
|
+
session,
|
|
35081
|
+
void 0,
|
|
35082
|
+
visitorId,
|
|
35083
|
+
void 0,
|
|
35084
|
+
true
|
|
35085
|
+
);
|
|
35086
|
+
}
|
|
35087
|
+
})
|
|
35088
|
+
);
|
|
35089
|
+
}
|
|
35090
|
+
value.isMembersAdded = true;
|
|
35091
|
+
}
|
|
34984
35092
|
if (value.site && value.plateNumber) {
|
|
34985
35093
|
try {
|
|
34986
35094
|
camera = await _getVisitorsInBySite(value.site);
|
|
@@ -35014,10 +35122,6 @@ function useVisitorTransactionService() {
|
|
|
35014
35122
|
if (found === 1)
|
|
35015
35123
|
throw new BadRequestError100("This plate number is blocklisted");
|
|
35016
35124
|
}
|
|
35017
|
-
if (value.checkIn) {
|
|
35018
|
-
const parsed = new Date(value.checkIn);
|
|
35019
|
-
value.checkIn = isNaN(parsed.getTime()) ? null : parsed;
|
|
35020
|
-
}
|
|
35021
35125
|
if (value.checkOut) {
|
|
35022
35126
|
const parsed = new Date(value.checkOut);
|
|
35023
35127
|
value.checkOut = isNaN(parsed.getTime()) ? null : parsed;
|
|
@@ -36078,7 +36182,8 @@ function usePersonService() {
|
|
|
36078
36182
|
getUserByEmail,
|
|
36079
36183
|
updateUserFieldById: _updateUserFieldById,
|
|
36080
36184
|
getUserById,
|
|
36081
|
-
updateUserOrgById: _updateUserOrgById
|
|
36185
|
+
updateUserOrgById: _updateUserOrgById,
|
|
36186
|
+
updateUserUnitById: _updateUserUnitById
|
|
36082
36187
|
} = useUserRepo();
|
|
36083
36188
|
const { add: addMember } = useMemberRepo();
|
|
36084
36189
|
const { getById: _getUnitById, updateById: updateUnitById } = useBuildingUnitRepo();
|
|
@@ -36225,6 +36330,10 @@ function usePersonService() {
|
|
|
36225
36330
|
value.unit = new ObjectId66(value.unit);
|
|
36226
36331
|
}
|
|
36227
36332
|
const isOrgChanged = value.org && person.org?.toString() !== value.org.toString();
|
|
36333
|
+
const toKey = (v) => v === null || v === void 0 || v === "" ? "" : v.toString();
|
|
36334
|
+
const isBlockChanged = "block" in value && toKey(value.block) !== toKey(person.block);
|
|
36335
|
+
const isLevelChanged = "level" in value && toKey(value.level) !== toKey(person.level);
|
|
36336
|
+
const isUnitChanged = "unit" in value && toKey(value.unit) !== toKey(person.unit);
|
|
36228
36337
|
await _updateById(_id, value, session);
|
|
36229
36338
|
if (isOrgChanged && person.user) {
|
|
36230
36339
|
await _updateUserOrgById(
|
|
@@ -36233,6 +36342,26 @@ function usePersonService() {
|
|
|
36233
36342
|
session
|
|
36234
36343
|
);
|
|
36235
36344
|
}
|
|
36345
|
+
if ((isBlockChanged || isLevelChanged || isUnitChanged) && person.user) {
|
|
36346
|
+
const userUnitPayload = {};
|
|
36347
|
+
if (isBlockChanged) {
|
|
36348
|
+
userUnitPayload.block = value.block ?? null;
|
|
36349
|
+
}
|
|
36350
|
+
if (isLevelChanged) {
|
|
36351
|
+
userUnitPayload.level = value.level ?? null;
|
|
36352
|
+
}
|
|
36353
|
+
if (isUnitChanged) {
|
|
36354
|
+
userUnitPayload.unitId = value.unit ?? null;
|
|
36355
|
+
if ("unitName" in value) {
|
|
36356
|
+
userUnitPayload.unitName = value.unitName ?? "";
|
|
36357
|
+
}
|
|
36358
|
+
}
|
|
36359
|
+
await _updateUserUnitById(
|
|
36360
|
+
person.user.toString(),
|
|
36361
|
+
userUnitPayload,
|
|
36362
|
+
session
|
|
36363
|
+
);
|
|
36364
|
+
}
|
|
36236
36365
|
if (value.unit && (isNameUpdated || isOwnerChanged || value.isOwner)) {
|
|
36237
36366
|
const unit = await _getUnitById(value.unit.toString());
|
|
36238
36367
|
if (unit) {
|
|
@@ -36262,6 +36391,16 @@ function usePersonService() {
|
|
|
36262
36391
|
}
|
|
36263
36392
|
}
|
|
36264
36393
|
}
|
|
36394
|
+
if (isUnitChanged && person.isOwner && person.unit) {
|
|
36395
|
+
const previousUnit = await _getUnitById(person.unit.toString());
|
|
36396
|
+
if (previousUnit && previousUnit._id && previousUnit.owner?.toString() === person?._id?.toString()) {
|
|
36397
|
+
await updateUnitById(
|
|
36398
|
+
previousUnit._id.toString(),
|
|
36399
|
+
{ owner: "", ownerName: "" },
|
|
36400
|
+
session
|
|
36401
|
+
);
|
|
36402
|
+
}
|
|
36403
|
+
}
|
|
36265
36404
|
await session.commitTransaction();
|
|
36266
36405
|
return "Person updated successfully.";
|
|
36267
36406
|
} catch (error) {
|
|
@@ -45303,7 +45442,6 @@ function useSiteUnitBillingService() {
|
|
|
45303
45442
|
const { getAll, updateById: _updateById } = useSiteBillingItemRepo();
|
|
45304
45443
|
const { getBuildingUnitsWithOwner: _getBuildingUnitsWithOwner } = useBuildingUnitRepo();
|
|
45305
45444
|
async function processBilling() {
|
|
45306
|
-
console.log("Starting billing process...");
|
|
45307
45445
|
const billing_items = await getAll({
|
|
45308
45446
|
search: "",
|
|
45309
45447
|
page: 1,
|
|
@@ -45346,7 +45484,7 @@ function useSiteUnitBillingService() {
|
|
|
45346
45484
|
const buildUnitBilling = (unit) => ({
|
|
45347
45485
|
site: billing_item.site.toString(),
|
|
45348
45486
|
org: billing_item.org.toString(),
|
|
45349
|
-
billItem: billing_item._id,
|
|
45487
|
+
billItem: billing_item._id?.toString(),
|
|
45350
45488
|
billName: billing_item.name,
|
|
45351
45489
|
unitId: unit._id.toString(),
|
|
45352
45490
|
unit: unit.blockName + " / " + unit.levelName + " / " + unit.name,
|
|
@@ -45380,6 +45518,9 @@ function useSiteUnitBillingService() {
|
|
|
45380
45518
|
level: "error",
|
|
45381
45519
|
message: `Failed to proccess cron billing: ${error.message}`
|
|
45382
45520
|
});
|
|
45521
|
+
if (session.inTransaction()) {
|
|
45522
|
+
await session.abortTransaction();
|
|
45523
|
+
}
|
|
45383
45524
|
continue;
|
|
45384
45525
|
} finally {
|
|
45385
45526
|
session.endSession();
|
|
@@ -45434,6 +45575,7 @@ function useSiteUnitBillingService() {
|
|
|
45434
45575
|
const billingMonth = Number(billing_item.month);
|
|
45435
45576
|
const billingDay = Number(billing_item.date);
|
|
45436
45577
|
if (billing_item.frequency === "monthly" /* MONTHLY */) {
|
|
45578
|
+
console.log("billing_item", billing_item.name);
|
|
45437
45579
|
return todayDate === billingDay;
|
|
45438
45580
|
}
|
|
45439
45581
|
if (billing_item.frequency === "quarterly" /* QAURTERLY */) {
|
|
@@ -46569,7 +46711,7 @@ function UseAccessManagementRepo() {
|
|
|
46569
46711
|
$lookup: {
|
|
46570
46712
|
from: "building-levels",
|
|
46571
46713
|
localField: "_id",
|
|
46572
|
-
foreignField: "
|
|
46714
|
+
foreignField: "blockId",
|
|
46573
46715
|
pipeline: [
|
|
46574
46716
|
{ $match: { status: { $ne: "deleted" } } },
|
|
46575
46717
|
{
|
|
@@ -46590,7 +46732,7 @@ function UseAccessManagementRepo() {
|
|
|
46590
46732
|
{
|
|
46591
46733
|
$project: {
|
|
46592
46734
|
_id: 1,
|
|
46593
|
-
|
|
46735
|
+
name: 1,
|
|
46594
46736
|
units: 1
|
|
46595
46737
|
}
|
|
46596
46738
|
}
|
|
@@ -46751,7 +46893,7 @@ function UseAccessManagementRepo() {
|
|
|
46751
46893
|
$project: {
|
|
46752
46894
|
_id: "$level.units._id",
|
|
46753
46895
|
name: "$level.units.name",
|
|
46754
|
-
level: { _id: "$level._id", level: "$level.
|
|
46896
|
+
level: { _id: "$level._id", level: "$level.name" },
|
|
46755
46897
|
block: { _id: "$_id", name: "$name", block: "$block" },
|
|
46756
46898
|
site: "$site",
|
|
46757
46899
|
unit_owner: { $arrayElemAt: ["$unitOwner", 0] },
|
|
@@ -47875,7 +48017,8 @@ function UseAccessManagementRepo() {
|
|
|
47875
48017
|
remarks: 1,
|
|
47876
48018
|
requestDate: 1,
|
|
47877
48019
|
createdAt: 1,
|
|
47878
|
-
updatedAt: 1
|
|
48020
|
+
updatedAt: 1,
|
|
48021
|
+
userCred: 1
|
|
47879
48022
|
}
|
|
47880
48023
|
}
|
|
47881
48024
|
);
|
|
@@ -48171,7 +48314,7 @@ function UseAccessManagementRepo() {
|
|
|
48171
48314
|
const unitId = new ObjectId97(params.unitId);
|
|
48172
48315
|
const quantity = params.quantity;
|
|
48173
48316
|
const type = params.type;
|
|
48174
|
-
const visitorId = new ObjectId97(params.visitorId);
|
|
48317
|
+
const visitorId = new ObjectId97(params.user?.visitorId);
|
|
48175
48318
|
const nfcCards = params.nfcCards;
|
|
48176
48319
|
const acm_url = params.acm_url;
|
|
48177
48320
|
let cards = [];
|
|
@@ -48218,7 +48361,10 @@ function UseAccessManagementRepo() {
|
|
|
48218
48361
|
const updateFields = {
|
|
48219
48362
|
staffNo: `STAFF-${cardId.toString().slice(-10)}`,
|
|
48220
48363
|
updatedAt: /* @__PURE__ */ new Date(),
|
|
48221
|
-
userId: updatedVisitor
|
|
48364
|
+
userId: updatedVisitor,
|
|
48365
|
+
userCred: {
|
|
48366
|
+
...params.user
|
|
48367
|
+
}
|
|
48222
48368
|
};
|
|
48223
48369
|
if (type === "QRCODE" /* QR */) {
|
|
48224
48370
|
updateFields.one_time = true;
|
|
@@ -48360,7 +48506,7 @@ function UseAccessManagementRepo() {
|
|
|
48360
48506
|
$lookup: {
|
|
48361
48507
|
from: "building-levels",
|
|
48362
48508
|
localField: "_id",
|
|
48363
|
-
foreignField: "
|
|
48509
|
+
foreignField: "blockId",
|
|
48364
48510
|
as: "levels",
|
|
48365
48511
|
pipeline: [
|
|
48366
48512
|
{
|
|
@@ -50224,7 +50370,7 @@ function useAccessManagementController() {
|
|
|
50224
50370
|
quantity = 1,
|
|
50225
50371
|
type,
|
|
50226
50372
|
nfcCards,
|
|
50227
|
-
|
|
50373
|
+
user,
|
|
50228
50374
|
acm_url
|
|
50229
50375
|
} = req.body;
|
|
50230
50376
|
const schema2 = Joi87.object({
|
|
@@ -50239,7 +50385,7 @@ function useAccessManagementController() {
|
|
|
50239
50385
|
})
|
|
50240
50386
|
).required(),
|
|
50241
50387
|
acm_url: Joi87.string().required(),
|
|
50242
|
-
|
|
50388
|
+
user: Joi87.object().optional().allow("", null)
|
|
50243
50389
|
});
|
|
50244
50390
|
const { error } = schema2.validate({
|
|
50245
50391
|
site,
|
|
@@ -50247,7 +50393,7 @@ function useAccessManagementController() {
|
|
|
50247
50393
|
quantity,
|
|
50248
50394
|
type,
|
|
50249
50395
|
nfcCards,
|
|
50250
|
-
|
|
50396
|
+
user,
|
|
50251
50397
|
acm_url
|
|
50252
50398
|
});
|
|
50253
50399
|
if (error) {
|
|
@@ -50259,7 +50405,7 @@ function useAccessManagementController() {
|
|
|
50259
50405
|
quantity,
|
|
50260
50406
|
type,
|
|
50261
50407
|
nfcCards,
|
|
50262
|
-
|
|
50408
|
+
user,
|
|
50263
50409
|
acm_url
|
|
50264
50410
|
});
|
|
50265
50411
|
return res.status(200).json({ message: "Success", data: result });
|
|
@@ -50660,6 +50806,7 @@ import {
|
|
|
50660
50806
|
InternalServerError as InternalServerError49,
|
|
50661
50807
|
logger as logger126,
|
|
50662
50808
|
makeCacheKey as makeCacheKey47,
|
|
50809
|
+
NotFoundError as NotFoundError36,
|
|
50663
50810
|
paginate as paginate41,
|
|
50664
50811
|
useAtlas as useAtlas79,
|
|
50665
50812
|
useCache as useCache49
|
|
@@ -50854,11 +51001,24 @@ function useNfcPatrolTagRepo() {
|
|
|
50854
51001
|
});
|
|
50855
51002
|
});
|
|
50856
51003
|
}
|
|
51004
|
+
async function getById(_id, session) {
|
|
51005
|
+
try {
|
|
51006
|
+
_id = typeof _id === "string" ? new ObjectId99(_id) : _id;
|
|
51007
|
+
} catch {
|
|
51008
|
+
throw new BadRequestError147("Invalid NFC Patrol Tag ID.");
|
|
51009
|
+
}
|
|
51010
|
+
const tag = await collection.findOne({ _id }, { session });
|
|
51011
|
+
if (!tag) {
|
|
51012
|
+
throw new NotFoundError36("NFC Patrol Tag not found.");
|
|
51013
|
+
}
|
|
51014
|
+
return tag;
|
|
51015
|
+
}
|
|
50857
51016
|
return {
|
|
50858
51017
|
createIndexes,
|
|
50859
51018
|
add,
|
|
50860
51019
|
getAll,
|
|
50861
|
-
updateNfcPatrolTagBySite
|
|
51020
|
+
updateNfcPatrolTagBySite,
|
|
51021
|
+
getById
|
|
50862
51022
|
};
|
|
50863
51023
|
}
|
|
50864
51024
|
|
|
@@ -50967,7 +51127,7 @@ import { BadRequestError as BadRequestError149, logger as logger127 } from "@736
|
|
|
50967
51127
|
import Joi89 from "joi";
|
|
50968
51128
|
function useNfcPatrolTagController() {
|
|
50969
51129
|
const { add: _add, updateNfcPatrolTagBySite: _updateNfcPatrolTagBySite } = useNfcPatrolTagService();
|
|
50970
|
-
const { getAll: _getAll } = useNfcPatrolTagRepo();
|
|
51130
|
+
const { getAll: _getAll, getById: _getById } = useNfcPatrolTagRepo();
|
|
50971
51131
|
async function add(req, res, next) {
|
|
50972
51132
|
const cookies = req.headers.cookie ? req.headers.cookie.split(";").map((cookie) => cookie.trim().split("=")).reduce(
|
|
50973
51133
|
(acc, [key, value2]) => ({ ...acc, [key]: value2 }),
|
|
@@ -51057,10 +51217,34 @@ function useNfcPatrolTagController() {
|
|
|
51057
51217
|
return;
|
|
51058
51218
|
}
|
|
51059
51219
|
}
|
|
51220
|
+
async function getById(req, res, next) {
|
|
51221
|
+
const validation = Joi89.object({
|
|
51222
|
+
id: Joi89.string().length(24).hex().required()
|
|
51223
|
+
});
|
|
51224
|
+
const { error } = validation.validate(req.params, {
|
|
51225
|
+
abortEarly: false
|
|
51226
|
+
});
|
|
51227
|
+
if (error) {
|
|
51228
|
+
const messages = error.details.map((d) => d.message).join(", ");
|
|
51229
|
+
logger127.log({ level: "error", message: messages });
|
|
51230
|
+
next(new BadRequestError149(messages));
|
|
51231
|
+
return;
|
|
51232
|
+
}
|
|
51233
|
+
try {
|
|
51234
|
+
const tag = await _getById(req.params.id);
|
|
51235
|
+
res.status(200).json(tag);
|
|
51236
|
+
return;
|
|
51237
|
+
} catch (error2) {
|
|
51238
|
+
logger127.log({ level: "error", message: error2.message });
|
|
51239
|
+
next(error2);
|
|
51240
|
+
return;
|
|
51241
|
+
}
|
|
51242
|
+
}
|
|
51060
51243
|
return {
|
|
51061
51244
|
add,
|
|
51062
51245
|
getAll,
|
|
51063
|
-
updateNfcPatrolTagBySite
|
|
51246
|
+
updateNfcPatrolTagBySite,
|
|
51247
|
+
getById
|
|
51064
51248
|
};
|
|
51065
51249
|
}
|
|
51066
51250
|
|
|
@@ -57597,7 +57781,7 @@ var schemaNfcPatrolLog = Joi110.object({
|
|
|
57597
57781
|
checkPoints: Joi110.array().items(
|
|
57598
57782
|
Joi110.object({
|
|
57599
57783
|
nfcTag_id: Joi110.string().length(24).hex().required(),
|
|
57600
|
-
nfcTag_name: Joi110.string().
|
|
57784
|
+
nfcTag_name: Joi110.string().required(),
|
|
57601
57785
|
travelTime: Joi110.number().integer().min(0).required(),
|
|
57602
57786
|
startDateTime: Joi110.date().required().messages({
|
|
57603
57787
|
"date.base": "startDateTime must be a valid date or ISO string"
|
|
@@ -57605,7 +57789,7 @@ var schemaNfcPatrolLog = Joi110.object({
|
|
|
57605
57789
|
endDateTime: Joi110.date().required().messages({
|
|
57606
57790
|
"date.base": "endDateTime must be a valid date or ISO string"
|
|
57607
57791
|
}),
|
|
57608
|
-
status: Joi110.string().valid("Completed", "Skipped").required(),
|
|
57792
|
+
status: Joi110.string().valid("Pending", "Completed", "Skipped").required(),
|
|
57609
57793
|
skippedRemarks: Joi110.string().required()
|
|
57610
57794
|
})
|
|
57611
57795
|
).min(0).required(),
|
|
@@ -57790,19 +57974,62 @@ function useNfcPatrolLogRepo() {
|
|
|
57790
57974
|
});
|
|
57791
57975
|
});
|
|
57792
57976
|
}
|
|
57977
|
+
async function getById(id, session) {
|
|
57978
|
+
try {
|
|
57979
|
+
id = new ObjectId121(id);
|
|
57980
|
+
} catch {
|
|
57981
|
+
throw new BadRequestError179("Invalid Log ID.");
|
|
57982
|
+
}
|
|
57983
|
+
return collection.findOne(
|
|
57984
|
+
{ _id: id },
|
|
57985
|
+
{ session }
|
|
57986
|
+
);
|
|
57987
|
+
}
|
|
57988
|
+
async function updateCheckpoint(id, checkpointIndex, payload, session) {
|
|
57989
|
+
try {
|
|
57990
|
+
id = new ObjectId121(id);
|
|
57991
|
+
} catch {
|
|
57992
|
+
throw new BadRequestError179("Invalid Log ID.");
|
|
57993
|
+
}
|
|
57994
|
+
const setData = {
|
|
57995
|
+
[`checkPoints.${checkpointIndex}.status`]: payload.action === "complete" ? "Completed" : "Skipped",
|
|
57996
|
+
[`checkPoints.${checkpointIndex}.endDateTime`]: /* @__PURE__ */ new Date()
|
|
57997
|
+
};
|
|
57998
|
+
if (payload.action === "skip") {
|
|
57999
|
+
setData[`checkPoints.${checkpointIndex}.skippedRemarks`] = payload.skippedRemarks;
|
|
58000
|
+
}
|
|
58001
|
+
const res = await collection.updateOne(
|
|
58002
|
+
{ _id: id },
|
|
58003
|
+
{
|
|
58004
|
+
$set: setData
|
|
58005
|
+
},
|
|
58006
|
+
{ session }
|
|
58007
|
+
);
|
|
58008
|
+
delCachedData();
|
|
58009
|
+
return res;
|
|
58010
|
+
}
|
|
57793
58011
|
return {
|
|
57794
58012
|
createIndexes,
|
|
57795
58013
|
add,
|
|
57796
|
-
getAllBySite
|
|
58014
|
+
getAllBySite,
|
|
58015
|
+
getById,
|
|
58016
|
+
updateCheckpoint
|
|
57797
58017
|
};
|
|
57798
58018
|
}
|
|
57799
58019
|
|
|
57800
58020
|
// src/services/nfc-patrol-log.service.ts
|
|
57801
58021
|
import {
|
|
58022
|
+
BadRequestError as BadRequestError180,
|
|
57802
58023
|
useAtlas as useAtlas100
|
|
57803
58024
|
} from "@7365admin1/node-server-utils";
|
|
57804
58025
|
function useNfcPatrolLogService() {
|
|
57805
|
-
const {
|
|
58026
|
+
const {
|
|
58027
|
+
add: _add,
|
|
58028
|
+
getById,
|
|
58029
|
+
updateCheckpoint
|
|
58030
|
+
} = useNfcPatrolLogRepo();
|
|
58031
|
+
const routeRepo = useNfcPatrolRouteRepo();
|
|
58032
|
+
const tagRepo = useNfcPatrolTagRepo();
|
|
57806
58033
|
async function add(value) {
|
|
57807
58034
|
const session = useAtlas100.getClient()?.startSession();
|
|
57808
58035
|
session?.startTransaction();
|
|
@@ -57817,8 +58044,54 @@ function useNfcPatrolLogService() {
|
|
|
57817
58044
|
session?.endSession();
|
|
57818
58045
|
}
|
|
57819
58046
|
}
|
|
58047
|
+
async function completeCheckpoint(logId, payload) {
|
|
58048
|
+
const session = useAtlas100.getClient()?.startSession();
|
|
58049
|
+
session?.startTransaction();
|
|
58050
|
+
try {
|
|
58051
|
+
const log = await getById(logId, session);
|
|
58052
|
+
if (!log) {
|
|
58053
|
+
throw new BadRequestError180("Patrol Log not found.");
|
|
58054
|
+
}
|
|
58055
|
+
const logCheckpoint = log.checkPoints[payload.checkpointIndex];
|
|
58056
|
+
if (!logCheckpoint) {
|
|
58057
|
+
throw new BadRequestError180("Checkpoint log not found.");
|
|
58058
|
+
}
|
|
58059
|
+
if (logCheckpoint.status === "Completed") {
|
|
58060
|
+
throw new BadRequestError180("Checkpoint already completed.");
|
|
58061
|
+
}
|
|
58062
|
+
if (payload.action === "skip" && !payload.skippedRemarks?.trim()) {
|
|
58063
|
+
throw new BadRequestError180("Skipped remarks are required.");
|
|
58064
|
+
}
|
|
58065
|
+
if (payload.action === "complete") {
|
|
58066
|
+
const tag = await tagRepo.getById(logCheckpoint.nfcTag_id);
|
|
58067
|
+
if (!tag) {
|
|
58068
|
+
throw new BadRequestError180("NFC Tag not found.");
|
|
58069
|
+
}
|
|
58070
|
+
if (!tag.tagID) {
|
|
58071
|
+
throw new BadRequestError180("NFC Tag has not been configured.");
|
|
58072
|
+
}
|
|
58073
|
+
if (tag.tagID !== payload.uid) {
|
|
58074
|
+
throw new BadRequestError180("Invalid NFC Tag.");
|
|
58075
|
+
}
|
|
58076
|
+
}
|
|
58077
|
+
await updateCheckpoint(
|
|
58078
|
+
logId,
|
|
58079
|
+
payload.checkpointIndex,
|
|
58080
|
+
payload,
|
|
58081
|
+
session
|
|
58082
|
+
);
|
|
58083
|
+
await session?.commitTransaction();
|
|
58084
|
+
return "Checkpoint updated.";
|
|
58085
|
+
} catch (error) {
|
|
58086
|
+
await session?.abortTransaction();
|
|
58087
|
+
throw error;
|
|
58088
|
+
} finally {
|
|
58089
|
+
await session?.endSession();
|
|
58090
|
+
}
|
|
58091
|
+
}
|
|
57820
58092
|
return {
|
|
57821
|
-
add
|
|
58093
|
+
add,
|
|
58094
|
+
completeCheckpoint
|
|
57822
58095
|
};
|
|
57823
58096
|
}
|
|
57824
58097
|
|
|
@@ -57829,8 +58102,8 @@ import {
|
|
|
57829
58102
|
} from "@7365admin1/node-server-utils";
|
|
57830
58103
|
import Joi111 from "joi";
|
|
57831
58104
|
function useNfcPatrolLogController() {
|
|
57832
|
-
const { add: _add } = useNfcPatrolLogService();
|
|
57833
|
-
const { getAllBySite: _getAllBySite } = useNfcPatrolLogRepo();
|
|
58105
|
+
const { add: _add, completeCheckpoint: _completeCheckpoint } = useNfcPatrolLogService();
|
|
58106
|
+
const { getAllBySite: _getAllBySite, getById: _getById } = useNfcPatrolLogRepo();
|
|
57834
58107
|
async function add(req, res, next) {
|
|
57835
58108
|
const payload = { ...req.body };
|
|
57836
58109
|
try {
|
|
@@ -57892,9 +58165,36 @@ function useNfcPatrolLogController() {
|
|
|
57892
58165
|
return;
|
|
57893
58166
|
}
|
|
57894
58167
|
}
|
|
58168
|
+
async function completeCheckpoint(req, res, next) {
|
|
58169
|
+
const { id } = req.params;
|
|
58170
|
+
const payload = {
|
|
58171
|
+
checkpointIndex: req.body.checkpointIndex,
|
|
58172
|
+
uid: req.body.uid,
|
|
58173
|
+
action: req.body.action,
|
|
58174
|
+
skippedRemarks: req.body.skippedRemarks
|
|
58175
|
+
};
|
|
58176
|
+
try {
|
|
58177
|
+
const data = await _completeCheckpoint(id, payload);
|
|
58178
|
+
res.status(200).json(data);
|
|
58179
|
+
return;
|
|
58180
|
+
} catch (error) {
|
|
58181
|
+
logger159.error(error.message);
|
|
58182
|
+
next(error);
|
|
58183
|
+
}
|
|
58184
|
+
}
|
|
58185
|
+
async function getLog(req, res, next) {
|
|
58186
|
+
try {
|
|
58187
|
+
const log = await _getById(req.params.id);
|
|
58188
|
+
res.json(log);
|
|
58189
|
+
} catch (error) {
|
|
58190
|
+
next(error);
|
|
58191
|
+
}
|
|
58192
|
+
}
|
|
57895
58193
|
return {
|
|
57896
58194
|
add,
|
|
57897
|
-
getAllBySite
|
|
58195
|
+
getAllBySite,
|
|
58196
|
+
completeCheckpoint,
|
|
58197
|
+
getLog
|
|
57898
58198
|
};
|
|
57899
58199
|
}
|
|
57900
58200
|
|