@7365admin1/core 2.54.0 → 2.56.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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @iservice365/core
2
2
 
3
+ ## 2.56.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 024c2b6: get latest hotfix changes
8
+
9
+ ## 2.55.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 259a720: get latest changes
14
+
3
15
  ## 2.54.0
4
16
 
5
17
  ### Minor Changes
package/dist/index.d.ts CHANGED
@@ -1856,7 +1856,7 @@ declare function useBuildingRepo(): {
1856
1856
  updateById: (_id: ObjectId | string, value: Pick<TBuilding, "name" | "block" | "levels" | "buildingFiles">, session?: ClientSession) => Promise<mongodb.UpdateResult<bson.Document>>;
1857
1857
  deleteById: (_id: string | ObjectId, session?: ClientSession) => Promise<mongodb.UpdateResult<bson.Document>>;
1858
1858
  getBuildingLevel: (site: string | ObjectId, block: number) => Promise<mongodb.WithId<bson.Document> | TBuilding | null>;
1859
- getBuildingLevelWithUnits: (site: string | ObjectId, block: number) => Promise<mongodb.WithId<bson.Document> | TBuilding>;
1859
+ getBuildingLevelWithUnits: (site: string | ObjectId, block: number | string | ObjectId) => Promise<bson.Document[]>;
1860
1860
  };
1861
1861
 
1862
1862
  declare function useBuildingService(): {
@@ -4650,6 +4650,15 @@ declare function UseAccessManagementRepo(): {
4650
4650
  pages: number;
4651
4651
  pageRange: string;
4652
4652
  }>;
4653
+ assignMultipleCardsRepo: ({ assignees, unit, type, acm_url }: {
4654
+ assignees: string[] | ObjectId[];
4655
+ unit: string | ObjectId;
4656
+ type: string;
4657
+ acm_url: string;
4658
+ }) => Promise<string>;
4659
+ visitorCheckoutRepo: ({ userId }: {
4660
+ userId: string;
4661
+ }) => Promise<string>;
4653
4662
  };
4654
4663
 
4655
4664
  declare function useAccessManagementController(): {
@@ -4690,6 +4699,8 @@ declare function useAccessManagementController(): {
4690
4699
  }) => Promise<void>;
4691
4700
  getBlockLevelAndUnitList: (req: Request, res: Response) => Promise<Response<any, Record<string, any>>>;
4692
4701
  getTransactions: (req: Request, res: Response) => Promise<Response<any, Record<string, any>>>;
4702
+ assignMultipleCards: (req: Request, res: Response) => Promise<Response<any, Record<string, any>>>;
4703
+ visitorCheckout: (req: Request, res: Response) => Promise<Response<any, Record<string, any>>>;
4693
4704
  };
4694
4705
 
4695
4706
  declare const DEVICE_STATUS: {
package/dist/index.js CHANGED
@@ -19643,47 +19643,77 @@ function useBuildingRepo() {
19643
19643
  }
19644
19644
  const query = {
19645
19645
  site,
19646
- block
19646
+ _id: new import_mongodb49.ObjectId(block)
19647
19647
  };
19648
19648
  const cacheOptions = { ...query };
19649
19649
  const cacheKey = (0, import_node_server_utils82.makeCacheKey)(buildings_namespace_collection, cacheOptions);
19650
19650
  try {
19651
- const cached = await getCache(cacheKey);
19652
- if (cached) {
19653
- import_node_server_utils82.logger.log({
19654
- level: "info",
19655
- message: `Cache hit for getById building: ${cacheKey}`
19656
- });
19657
- return cached;
19658
- }
19659
- const result = await collection.findOne(query);
19651
+ console.log(query);
19652
+ const result = await collection.aggregate([
19653
+ {
19654
+ $match: {
19655
+ ...query
19656
+ }
19657
+ },
19658
+ {
19659
+ $lookup: {
19660
+ from: "building-levels",
19661
+ let: { buildingId: "$_id" },
19662
+ pipeline: [
19663
+ {
19664
+ $match: {
19665
+ $expr: { $eq: ["$block", "$$buildingId"] }
19666
+ }
19667
+ },
19668
+ {
19669
+ $lookup: {
19670
+ from: "building-units",
19671
+ let: { levelId: "$_id" },
19672
+ pipeline: [
19673
+ {
19674
+ $match: {
19675
+ $expr: {
19676
+ $and: [
19677
+ { $eq: ["$site", site] },
19678
+ { $eq: ["$block", "$$buildingId"] },
19679
+ { $eq: ["$level", "$$levelId"] },
19680
+ { $eq: ["$status", "active"] }
19681
+ ]
19682
+ }
19683
+ }
19684
+ }
19685
+ ],
19686
+ as: "buildingUnits"
19687
+ }
19688
+ },
19689
+ // discard levels that have no active units
19690
+ {
19691
+ $match: {
19692
+ $expr: { $gt: [{ $size: "$buildingUnits" }, 0] }
19693
+ }
19694
+ }
19695
+ ],
19696
+ as: "buildingLevels"
19697
+ }
19698
+ },
19699
+ {
19700
+ $project: {
19701
+ "_id": 1,
19702
+ "site": 1,
19703
+ "name": 1,
19704
+ "block": 1,
19705
+ "status": 1,
19706
+ "buildingFloorPlan": 1,
19707
+ "createdAt": 1,
19708
+ "updatedAt": 1,
19709
+ "deletedAt": 1,
19710
+ "levels": "$buildingLevels"
19711
+ }
19712
+ }
19713
+ ]).toArray();
19660
19714
  if (!result) {
19661
19715
  throw new import_node_server_utils82.BadRequestError("Building not found.");
19662
19716
  }
19663
- const validLevels = [];
19664
- for (const level of result.levels ?? []) {
19665
- const units = await _getBySiteBuildingLevel(
19666
- result.site,
19667
- result.block,
19668
- level
19669
- );
19670
- if (units) {
19671
- validLevels.push(level);
19672
- }
19673
- }
19674
- result.levels = validLevels;
19675
- console.log(result);
19676
- setCache(cacheKey, result, 300).then(() => {
19677
- import_node_server_utils82.logger.log({
19678
- level: "info",
19679
- message: `Cache set for building by id: ${cacheKey}`
19680
- });
19681
- }).catch((err) => {
19682
- import_node_server_utils82.logger.log({
19683
- level: "error",
19684
- message: `Failed to set cache for building by id: ${err.message}`
19685
- });
19686
- });
19687
19717
  return result;
19688
19718
  } catch (error) {
19689
19719
  if (error instanceof import_node_server_utils82.AppError) {
@@ -20143,14 +20173,13 @@ function useBuildingController() {
20143
20173
  let block = req.query.block;
20144
20174
  const validation = import_joi44.default.object({
20145
20175
  site: import_joi44.default.string().hex().required(),
20146
- block: import_joi44.default.number().required()
20176
+ block: import_joi44.default.string().required()
20147
20177
  });
20148
20178
  const { error } = validation.validate({ site, block });
20149
20179
  if (error) {
20150
20180
  next(new import_node_server_utils84.BadRequestError(error.message));
20151
20181
  return;
20152
20182
  }
20153
- block = parseInt(block) ?? 0;
20154
20183
  try {
20155
20184
  const siteBlock = await _getBuildingLevelWithUnits(site, block);
20156
20185
  res.json(siteBlock);
@@ -24026,7 +24055,10 @@ function useVisitorTransactionService() {
24026
24055
  nric: value.nric,
24027
24056
  start,
24028
24057
  end,
24029
- recNo: value.recNo
24058
+ recNo: value.recNo,
24059
+ ...value.block && { block: value.block },
24060
+ ...value.level && { level: value.level },
24061
+ ...value.unit && { unit: value.unit }
24030
24062
  };
24031
24063
  await addVehicle(vehiclePayload);
24032
24064
  } else {
@@ -32873,21 +32905,23 @@ function useEventManagementController() {
32873
32905
  }
32874
32906
  }
32875
32907
  async function deleteEventManagementById(req, res, next) {
32876
- const validation = import_joi82.default.string().hex().required();
32877
- const _id = req.params.id;
32878
- const { error } = validation.validate(_id);
32879
- if (error) {
32880
- import_node_server_utils148.logger.log({ level: "error", message: error.message });
32881
- next(new import_node_server_utils148.BadRequestError(error.message));
32882
- return;
32883
- }
32884
32908
  try {
32909
+ const schema2 = import_joi82.default.object({
32910
+ _id: import_joi82.default.string().hex().length(24).required()
32911
+ });
32912
+ const { error, value } = schema2.validate({ _id: req.params.id });
32913
+ if (error) {
32914
+ import_node_server_utils148.logger.log({ level: "error", message: error.message });
32915
+ next(new import_node_server_utils148.BadRequestError(error.message));
32916
+ return;
32917
+ }
32918
+ const { _id } = value;
32885
32919
  await _deleteEventManagementById(_id);
32886
32920
  res.status(200).json({ message: "Successfully deleted event." });
32887
32921
  return;
32888
- } catch (error2) {
32889
- import_node_server_utils148.logger.log({ level: "error", message: error2.message });
32890
- next(error2);
32922
+ } catch (error) {
32923
+ import_node_server_utils148.logger.log({ level: "error", message: error.message });
32924
+ next(error);
32891
32925
  return;
32892
32926
  }
32893
32927
  }
@@ -34865,7 +34899,7 @@ function UseAccessManagementRepo() {
34865
34899
  localField: "_id",
34866
34900
  foreignField: "level",
34867
34901
  pipeline: [
34868
- { $match: { status: { $ne: "deleted" } } },
34902
+ { $match: { status: { $ne: "deleted" }, site } },
34869
34903
  { $project: { _id: 1, name: 1 } }
34870
34904
  ],
34871
34905
  as: "units"
@@ -36458,7 +36492,111 @@ function UseAccessManagementRepo() {
36458
36492
  const items = result[0].items;
36459
36493
  return (0, import_node_server_utils153.paginate)(items, page, limit, length);
36460
36494
  } catch (error) {
36461
- return Promise.reject("Server internal error.");
36495
+ throw new Error(error.message);
36496
+ }
36497
+ }
36498
+ async function assignMultipleCardsRepo({
36499
+ assignees,
36500
+ unit,
36501
+ type,
36502
+ acm_url
36503
+ }) {
36504
+ const session = import_node_server_utils153.useAtlas.getClient()?.startSession();
36505
+ try {
36506
+ session?.startTransaction();
36507
+ if (assignees.length < 1) {
36508
+ throw new Error("No user to Assign.");
36509
+ }
36510
+ assignees = assignees.map((data) => new import_mongodb90.ObjectId(data));
36511
+ unit = new import_mongodb90.ObjectId(unit);
36512
+ let availableCards = [];
36513
+ let update = [];
36514
+ let cards = [];
36515
+ availableCards = await collection().aggregate([
36516
+ {
36517
+ $match: {
36518
+ $expr: {
36519
+ $and: [
36520
+ { $eq: ["$assignedUnit", unit] },
36521
+ { $eq: ["$userId", null] },
36522
+ { $eq: ["$type", type] },
36523
+ { $eq: ["$isActivated", true] }
36524
+ ]
36525
+ }
36526
+ }
36527
+ }
36528
+ ]).toArray();
36529
+ if (assignees.length > availableCards.length) {
36530
+ throw new Error(`Not enough ${type} cards available.`);
36531
+ }
36532
+ cards = availableCards?.slice(0, assignees.length).map((card, index) => {
36533
+ const userId = new import_mongodb90.ObjectId(assignees[index].toString());
36534
+ card.staffNo = userId.toString().slice(-10);
36535
+ return card;
36536
+ });
36537
+ update = availableCards.slice(0, assignees.length).map((card, index) => ({ _id: card._id, userId: new import_mongodb90.ObjectId(assignees[index]) }));
36538
+ const commands = cards.map((item, index) => {
36539
+ let ag = null;
36540
+ if (item.accessGroup !== void 0) {
36541
+ if (item.accessGroup?.length > 0) {
36542
+ ag = item.accessGroup.map((g) => `<GROUP_NAME>${g}</GROUP_NAME>`).join("\n ");
36543
+ }
36544
+ }
36545
+ const command = {
36546
+ commandId1: index * 2 + 1,
36547
+ commandId2: index * 2 + 2,
36548
+ staffName: `STAFF-${item._id.toString().slice(-10)}`,
36549
+ staffNo1: `STAFF-${item._id.toString().slice(-10)}`,
36550
+ staffNo2: `STAFF-${item._id.toString().slice(-10)}`,
36551
+ dateOfJoin: formatEntryPassDate(item.startDate),
36552
+ accessLevel: item.accessLevel ?? "0",
36553
+ cardNo: item.cardNo,
36554
+ pin: typeof item.pin === "string" && item.pin.trim() !== "" ? item.pin : "123456",
36555
+ startDate: formatEntryPassDate(item.startDate),
36556
+ endDate: formatEntryPassDate(item.endDate),
36557
+ cardType: 0,
36558
+ isActivated: item.isActivated ? 1 : 0,
36559
+ isAntiPassBack: item.isAntiPassBack ? 1 : 0,
36560
+ isLiftCard: item.isLiftCard ? 1 : 0,
36561
+ isLiftActivate: item.isLiftCard ? 1 : 0,
36562
+ liftAccessLevel: item.liftAccessLevel || 1,
36563
+ liftAccessStartDate: formatEntryPassDate(item.liftAccessStartDate) || "19770510",
36564
+ liftAccessEndDate: formatEntryPassDate(item.liftAccessEndDate) || "19770510",
36565
+ accessGroup: ag
36566
+ };
36567
+ return readTemplate(`${item.accessLevel !== null ? "add-card" : "add-card-lift"}`, { ...command });
36568
+ }).flat();
36569
+ const response = await sendCommand(commands.join("").toString(), acm_url);
36570
+ const result = await (0, import_xml2js2.parseStringPromise)(response, { explicitArray: false });
36571
+ console.log("status code", result.RESULT.$.STCODE);
36572
+ if (result && result.RESULT.$.STCODE !== "0") {
36573
+ throw new Error("Command failed, server error.");
36574
+ }
36575
+ for (const { _id, userId } of update) {
36576
+ await collection().updateOne({ _id }, { $set: { userId, staffNo: `STAFF-${userId.toString().slice(-10)}` } }, { session });
36577
+ }
36578
+ await session?.commitTransaction();
36579
+ return "Cards assigned successfully.";
36580
+ } catch (error) {
36581
+ await session?.abortTransaction();
36582
+ throw new Error(error.message);
36583
+ } finally {
36584
+ await session?.endSession();
36585
+ }
36586
+ }
36587
+ async function visitorCheckoutRepo({ userId }) {
36588
+ const session = import_node_server_utils153.useAtlas.getClient()?.startSession();
36589
+ try {
36590
+ session?.startTransaction();
36591
+ const id = new import_mongodb90.ObjectId(userId);
36592
+ await collection().updateMany({ userId: id }, { $set: { userId: null, status: "Available" } }, { session });
36593
+ session?.commitTransaction();
36594
+ return "Successful Checkout";
36595
+ } catch (error) {
36596
+ await session?.abortTransaction();
36597
+ throw new Error(error.message);
36598
+ } finally {
36599
+ await session?.endSession();
36462
36600
  }
36463
36601
  }
36464
36602
  return {
@@ -36493,7 +36631,9 @@ function UseAccessManagementRepo() {
36493
36631
  checkoutVisitorRepo,
36494
36632
  getBlockLevelAndUnitListRepo,
36495
36633
  indexCombination,
36496
- getTransactionsRepo
36634
+ getTransactionsRepo,
36635
+ assignMultipleCardsRepo,
36636
+ visitorCheckoutRepo
36497
36637
  };
36498
36638
  }
36499
36639
 
@@ -36534,7 +36674,9 @@ function useAccessManagementSvc() {
36534
36674
  signQrCodeRepo,
36535
36675
  checkoutVisitorRepo,
36536
36676
  getBlockLevelAndUnitListRepo,
36537
- getTransactionsRepo
36677
+ getTransactionsRepo,
36678
+ assignMultipleCardsRepo,
36679
+ visitorCheckoutRepo
36538
36680
  } = UseAccessManagementRepo();
36539
36681
  const addPhysicalCardSvc = async (payload) => {
36540
36682
  try {
@@ -36830,6 +36972,22 @@ function useAccessManagementSvc() {
36830
36972
  try {
36831
36973
  const response = await getTransactionsRepo({ page, limit, site, cardNo, url });
36832
36974
  return response;
36975
+ } catch (err) {
36976
+ throw new Error(err.message);
36977
+ }
36978
+ };
36979
+ const assignMultipleCardsSvc = async ({ assignees, unit, type, acm_url }) => {
36980
+ try {
36981
+ const response = await assignMultipleCardsRepo({ assignees, unit, type, acm_url });
36982
+ return response;
36983
+ } catch (err) {
36984
+ throw new Error(err.message);
36985
+ }
36986
+ };
36987
+ const visitorCheckoutSvc = async ({ userId }) => {
36988
+ try {
36989
+ const response = await visitorCheckoutRepo({ userId });
36990
+ return response;
36833
36991
  } catch (err) {
36834
36992
  return Promise.reject("Server internal error.");
36835
36993
  }
@@ -36869,7 +37027,9 @@ function useAccessManagementSvc() {
36869
37027
  signQrCodeSvc,
36870
37028
  checkoutVisitorSvc,
36871
37029
  getBlockLevelAndUnitListSvc,
36872
- getTransactionsSvc
37030
+ getTransactionsSvc,
37031
+ assignMultipleCardsSvc,
37032
+ visitorCheckoutSvc
36873
37033
  };
36874
37034
  }
36875
37035
 
@@ -36910,7 +37070,9 @@ function useAccessManagementController() {
36910
37070
  signQrCodeSvc,
36911
37071
  checkoutVisitorSvc,
36912
37072
  getBlockLevelAndUnitListSvc,
36913
- getTransactionsSvc
37073
+ getTransactionsSvc,
37074
+ assignMultipleCardsSvc,
37075
+ visitorCheckoutSvc
36914
37076
  } = useAccessManagementSvc();
36915
37077
  const addPhysicalCard = async (req, res) => {
36916
37078
  try {
@@ -37694,6 +37856,39 @@ function useAccessManagementController() {
37694
37856
  });
37695
37857
  return res.json(result);
37696
37858
  } catch (error2) {
37859
+ return res.status(400).json({
37860
+ data: null,
37861
+ message: error2.message
37862
+ });
37863
+ }
37864
+ };
37865
+ const assignMultipleCards = async (req, res) => {
37866
+ try {
37867
+ const { assignees, unit, type, acm_url } = req.body;
37868
+ const schema2 = import_joi85.default.object({
37869
+ assignees: import_joi85.default.array().items(import_joi85.default.string().hex()).required(),
37870
+ type: import_joi85.default.string().required(),
37871
+ unit: import_joi85.default.string().hex().required()
37872
+ });
37873
+ const { error } = schema2.validate({ assignees, type, unit });
37874
+ if (error) {
37875
+ throw new Error(`${error.message}`);
37876
+ }
37877
+ const result = await assignMultipleCardsSvc({ assignees, unit, type, acm_url });
37878
+ return res.status(200).json({ message: "Success", data: result });
37879
+ } catch (error) {
37880
+ return res.status(400).json({
37881
+ data: null,
37882
+ message: error.message
37883
+ });
37884
+ }
37885
+ };
37886
+ const visitorCheckout = async (req, res) => {
37887
+ try {
37888
+ const userId = req.params.userId;
37889
+ const result = await visitorCheckoutSvc({ userId });
37890
+ return res.status(200).json({ message: "Success", data: result });
37891
+ } catch (error) {
37697
37892
  return Promise.reject("Internal Server Error");
37698
37893
  }
37699
37894
  };
@@ -37730,7 +37925,9 @@ function useAccessManagementController() {
37730
37925
  checkoutVisitor,
37731
37926
  removeAccessCard,
37732
37927
  getBlockLevelAndUnitList,
37733
- getTransactions: getTransactions2
37928
+ getTransactions: getTransactions2,
37929
+ assignMultipleCards,
37930
+ visitorCheckout
37734
37931
  };
37735
37932
  }
37736
37933