@7365admin1/core 3.48.1-staging.181 → 3.48.1-staging.183

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -10450,6 +10450,7 @@ declare function useBuildingLevelRepo(): {
10450
10450
  getById: (_id: string | ObjectId, session?: ClientSession) => Promise<TBuildingLevel>;
10451
10451
  updateLevelById: (_id: string | ObjectId, value: TBuildingLevel, session?: ClientSession) => Promise<mongodb.UpdateResult<bson.Document>>;
10452
10452
  deleteById: (_id: string | ObjectId, session?: ClientSession) => Promise<string>;
10453
+ deactivateByBuilding: (buildingId: string | ObjectId, levelIds?: (string | ObjectId)[], session?: ClientSession) => Promise<mongodb.UpdateResult<bson.Document>>;
10453
10454
  bulkWriteLevels: (levels: TBuildingLevel[], session?: ClientSession) => Promise<{
10454
10455
  insertedIds: {
10455
10456
  [key: number]: any;
package/dist/index.js CHANGED
@@ -33693,7 +33693,7 @@ function useBuildingUnitRepo() {
33693
33693
  ...site && { site: (0, import_node_server_utils97.toObjectId)(site) },
33694
33694
  ...building && { building: (0, import_node_server_utils97.toObjectId)(building) }
33695
33695
  };
33696
- sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
33696
+ sort = Object.keys(sort).length > 0 ? sort : { block: 1, "level.name": 1, name: 1, _id: 1 };
33697
33697
  const cacheParams = {
33698
33698
  page,
33699
33699
  limit,
@@ -33757,38 +33757,44 @@ function useBuildingUnitRepo() {
33757
33757
  }
33758
33758
  ] : []
33759
33759
  ];
33760
- const items = await collection.aggregate([
33761
- ...pipeline,
33762
- {
33763
- $lookup: {
33764
- from: "sites",
33765
- let: { siteId: "$site" },
33766
- pipeline: [
33767
- {
33768
- $match: {
33769
- $expr: { $eq: ["$_id", "$$siteId"] },
33770
- status: "active" /* ACTIVE */
33771
- }
33772
- },
33773
- {
33774
- $project: {
33775
- name: 1,
33776
- description: 1,
33777
- orgId: 1,
33778
- metadata: 1,
33779
- status: 1,
33780
- address: 1
33760
+ const items = await collection.aggregate(
33761
+ [
33762
+ ...pipeline,
33763
+ {
33764
+ $lookup: {
33765
+ from: "sites",
33766
+ let: { siteId: "$site" },
33767
+ pipeline: [
33768
+ {
33769
+ $match: {
33770
+ $expr: { $eq: ["$_id", "$$siteId"] },
33771
+ status: "active" /* ACTIVE */
33772
+ }
33773
+ },
33774
+ {
33775
+ $project: {
33776
+ name: 1,
33777
+ description: 1,
33778
+ orgId: 1,
33779
+ metadata: 1,
33780
+ status: 1,
33781
+ address: 1
33782
+ }
33781
33783
  }
33782
- }
33783
- ],
33784
- as: "site"
33785
- }
33786
- },
33787
- { $set: { site: { $first: "$site" } } },
33788
- { $sort: sort },
33789
- { $skip: page * limit },
33790
- { $limit: limit }
33791
- ]).toArray();
33784
+ ],
33785
+ as: "site"
33786
+ }
33787
+ },
33788
+ { $set: { site: { $first: "$site" } } },
33789
+ { $sort: sort },
33790
+ { $skip: page * limit },
33791
+ { $limit: limit }
33792
+ ],
33793
+ // Sort units by block, then level name (natural: Lvl 2 before Lvl 10),
33794
+ // then unit name. Skip collation when a search regex is active (Mongo
33795
+ // rejects $regex + non-simple collation); browse view is what needs it.
33796
+ searchText ? {} : { collation: { locale: "en", numericOrdering: true } }
33797
+ ).toArray();
33792
33798
  const total = await collection.aggregate([...pipeline, { $count: "length" }]).toArray();
33793
33799
  const length = total[0]?.length ?? 0;
33794
33800
  const data = (0, import_node_server_utils97.paginate)(items, page, limit, length);
@@ -33827,7 +33833,7 @@ function useBuildingUnitRepo() {
33827
33833
  ...building && { building: (0, import_node_server_utils97.toObjectId)(building) },
33828
33834
  ...level && { level: (0, import_node_server_utils97.toObjectId)(level) }
33829
33835
  };
33830
- sort = Object.keys(sort).length > 0 ? sort : { _id: -1 };
33836
+ sort = Object.keys(sort).length > 0 ? sort : { block: 1, "level.name": 1, name: 1, _id: 1 };
33831
33837
  const cacheParams = {
33832
33838
  page,
33833
33839
  limit,
@@ -33855,35 +33861,40 @@ function useBuildingUnitRepo() {
33855
33861
  });
33856
33862
  return cached;
33857
33863
  }
33858
- const items = await collection.aggregate([
33859
- { $match: query2 },
33860
- {
33861
- $project: {
33862
- site: 1,
33863
- name: 1,
33864
- building: 1,
33865
- buildingName: 1,
33866
- block: 1,
33867
- level: 1,
33868
- unitNumber: 1,
33869
- createdAt: 1,
33870
- updatedAt: 1
33871
- }
33872
- },
33873
- {
33874
- $lookup: {
33875
- from: "building-levels",
33876
- localField: "level",
33877
- foreignField: "_id",
33878
- pipeline: [{ $project: { name: 1 } }],
33879
- as: "level"
33880
- }
33881
- },
33882
- { $set: { level: { $first: "$level" } } },
33883
- { $sort: sort },
33884
- { $skip: page * limit },
33885
- { $limit: limit }
33886
- ]).toArray();
33864
+ const items = await collection.aggregate(
33865
+ [
33866
+ { $match: query2 },
33867
+ {
33868
+ $project: {
33869
+ site: 1,
33870
+ name: 1,
33871
+ building: 1,
33872
+ buildingName: 1,
33873
+ block: 1,
33874
+ level: 1,
33875
+ unitNumber: 1,
33876
+ createdAt: 1,
33877
+ updatedAt: 1
33878
+ }
33879
+ },
33880
+ {
33881
+ $lookup: {
33882
+ from: "building-levels",
33883
+ localField: "level",
33884
+ foreignField: "_id",
33885
+ pipeline: [{ $project: { name: 1 } }],
33886
+ as: "level"
33887
+ }
33888
+ },
33889
+ { $set: { level: { $first: "$level" } } },
33890
+ { $sort: sort },
33891
+ { $skip: page * limit },
33892
+ { $limit: limit }
33893
+ ],
33894
+ // Natural sort by block, level name, unit name. Skip collation when a
33895
+ // $text search is active (Mongo rejects the combo); browse view needs it.
33896
+ search ? {} : { collation: { locale: "en", numericOrdering: true } }
33897
+ ).toArray();
33887
33898
  const length = await collection.countDocuments(query2);
33888
33899
  const data = (0, import_node_server_utils97.paginate)(items, page, limit, length);
33889
33900
  setCache(cacheKey, data, 600).then(() => {
@@ -36064,7 +36075,7 @@ function useBuildingRepo() {
36064
36075
  ]
36065
36076
  }
36066
36077
  };
36067
- sort = Object.keys(sort).length ? sort : { _id: -1 };
36078
+ sort = Object.keys(sort).length ? sort : { name: 1, _id: 1 };
36068
36079
  const cacheParams = {
36069
36080
  page,
36070
36081
  limit,
@@ -36087,36 +36098,44 @@ function useBuildingRepo() {
36087
36098
  });
36088
36099
  return cached;
36089
36100
  }
36090
- const items = await collection.aggregate([
36091
- { $match: query2 },
36092
- {
36093
- $lookup: {
36094
- from: "building-levels",
36095
- let: {
36096
- buildingId: "$_id",
36097
- buildingLevelIds: { $ifNull: ["$levels", []] }
36098
- },
36099
- pipeline: [
36100
- {
36101
- $match: {
36102
- status: "active" /* ACTIVE */,
36103
- $expr: {
36104
- $or: [
36105
- { $in: ["$_id", "$$buildingLevelIds"] },
36106
- { $eq: ["$blockId", "$$buildingId"] }
36107
- ]
36108
- }
36109
- }
36101
+ const items = await collection.aggregate(
36102
+ [
36103
+ { $match: query2 },
36104
+ {
36105
+ $lookup: {
36106
+ from: "building-levels",
36107
+ let: {
36108
+ buildingId: "$_id",
36109
+ buildingLevelIds: { $ifNull: ["$levels", []] }
36110
36110
  },
36111
- { $project: { _id: 1, name: 1 } }
36112
- ],
36113
- as: "levels"
36114
- }
36115
- },
36116
- { $sort: sort },
36117
- { $skip: page * limit },
36118
- { $limit: limit }
36119
- ]).toArray();
36111
+ pipeline: [
36112
+ {
36113
+ $match: {
36114
+ status: "active" /* ACTIVE */,
36115
+ $expr: {
36116
+ $or: [
36117
+ { $in: ["$_id", "$$buildingLevelIds"] },
36118
+ { $eq: ["$blockId", "$$buildingId"] }
36119
+ ]
36120
+ }
36121
+ }
36122
+ },
36123
+ { $project: { _id: 1, name: 1 } },
36124
+ { $sort: { name: 1 } }
36125
+ ],
36126
+ as: "levels"
36127
+ }
36128
+ },
36129
+ { $sort: sort },
36130
+ { $skip: page * limit },
36131
+ { $limit: limit }
36132
+ ],
36133
+ // numericOrdering => natural order (Blk 2 < Blk 100 < Blk 165, and
36134
+ // levels Lvl 2 before Lvl 10). Skip collation when a $text search is
36135
+ // active (Mongo rejects the combo); the default browse view is what
36136
+ // needs the natural ordering.
36137
+ searchText ? {} : { collation: { locale: "en", numericOrdering: true } }
36138
+ ).toArray();
36120
36139
  const length = await collection.countDocuments(query2);
36121
36140
  const data = (0, import_node_server_utils104.paginate)(items, page, limit, length);
36122
36141
  setCache(cacheKey, data, 600).then(() => {
@@ -36465,18 +36484,25 @@ function useBuildingRepo() {
36465
36484
  const cacheOptions = { ...query2 };
36466
36485
  const cacheKey = (0, import_node_server_utils104.makeCacheKey)(buildings_namespace_collection, cacheOptions);
36467
36486
  try {
36468
- const result = await collection.aggregate([
36469
- {
36470
- $match: {
36471
- ...query2
36472
- }
36473
- },
36474
- {
36475
- $project: {
36476
- name: 1
36477
- }
36478
- }
36479
- ]).toArray();
36487
+ const result = await collection.aggregate(
36488
+ [
36489
+ {
36490
+ $match: {
36491
+ ...query2
36492
+ }
36493
+ },
36494
+ {
36495
+ $project: {
36496
+ name: 1
36497
+ }
36498
+ },
36499
+ { $sort: { name: 1, _id: 1 } }
36500
+ ],
36501
+ // numericOrdering => natural order in the block dropdowns
36502
+ // (Blk 2 < Blk 10 < Blk 165). No search branch here, so collation
36503
+ // always applies.
36504
+ { collation: { locale: "en", numericOrdering: true } }
36505
+ ).toArray();
36480
36506
  return {
36481
36507
  success: true,
36482
36508
  message: "Buildings retrieved successfully",
@@ -36641,6 +36667,9 @@ function useBuildingLevelRepo() {
36641
36667
  const { delNamespace: delBuildingNamespace } = (0, import_node_server_utils107.useCache)(
36642
36668
  buildings_namespace_collection
36643
36669
  );
36670
+ const { delNamespace: delUnitNamespace } = (0, import_node_server_utils107.useCache)(
36671
+ building_units_namespace_collection
36672
+ );
36644
36673
  async function createIndexes() {
36645
36674
  try {
36646
36675
  await collection.createIndexes([
@@ -36680,6 +36709,12 @@ function useBuildingLevelRepo() {
36680
36709
  message: `Failed to clear buildings cache: ${err.message}`
36681
36710
  });
36682
36711
  });
36712
+ delUnitNamespace().catch((err) => {
36713
+ import_node_server_utils107.logger.log({
36714
+ level: "error",
36715
+ message: `Failed to clear building-units cache: ${err.message}`
36716
+ });
36717
+ });
36683
36718
  }
36684
36719
  async function add(value, session) {
36685
36720
  try {
@@ -36742,19 +36777,25 @@ function useBuildingLevelRepo() {
36742
36777
  });
36743
36778
  return cached;
36744
36779
  }
36745
- const sortStage = Object.keys(sort).length > 0 ? { $sort: sort } : { $sort: { latestDate: -1 } };
36746
- const items = await collection.aggregate([
36747
- { $match: query2 },
36748
- {
36749
- $addFields: {
36750
- latestDate: { $max: ["$updatedAt", "$createdAt"] }
36751
- }
36752
- },
36753
- sortStage,
36754
- { $unset: "latestDate" },
36755
- { $skip: page * limit },
36756
- { $limit: limit }
36757
- ]).toArray();
36780
+ const sortStage = Object.keys(sort).length > 0 ? { $sort: sort } : { $sort: { name: 1 } };
36781
+ const items = await collection.aggregate(
36782
+ [
36783
+ { $match: query2 },
36784
+ {
36785
+ $addFields: {
36786
+ latestDate: { $max: ["$updatedAt", "$createdAt"] }
36787
+ }
36788
+ },
36789
+ sortStage,
36790
+ { $unset: "latestDate" },
36791
+ { $skip: page * limit },
36792
+ { $limit: limit }
36793
+ ],
36794
+ // numericOrdering gives natural order (Lvl 2 before Lvl 10). Skip
36795
+ // collation when a $text search is active (Mongo rejects the combo);
36796
+ // ordering matters for the default browse list.
36797
+ search ? {} : { collation: { locale: "en", numericOrdering: true } }
36798
+ ).toArray();
36758
36799
  const length = await collection.countDocuments(query2);
36759
36800
  const data = (0, import_node_server_utils107.paginate)(items, page, limit, length);
36760
36801
  setCache(cacheKey, data, 600).then(() => {
@@ -36906,6 +36947,34 @@ function useBuildingLevelRepo() {
36906
36947
  }
36907
36948
  }
36908
36949
  }
36950
+ async function deactivateByBuilding(buildingId, levelIds = [], session) {
36951
+ let bId;
36952
+ try {
36953
+ bId = new import_mongodb70.ObjectId(buildingId);
36954
+ } catch {
36955
+ throw new import_node_server_utils107.BadRequestError("Invalid building ID.");
36956
+ }
36957
+ const or = [
36958
+ { blockId: bId },
36959
+ { blockId: String(bId) }
36960
+ ];
36961
+ const refIds = (levelIds || []).map((id) => {
36962
+ try {
36963
+ return new import_mongodb70.ObjectId(id);
36964
+ } catch {
36965
+ return null;
36966
+ }
36967
+ }).filter((x) => x !== null);
36968
+ if (refIds.length)
36969
+ or.push({ _id: { $in: refIds } });
36970
+ const res = await collection.updateMany(
36971
+ { status: "active" /* ACTIVE */, $or: or },
36972
+ { $set: { status: "deleted" /* DELETED */, deletedAt: /* @__PURE__ */ new Date() } },
36973
+ { session }
36974
+ );
36975
+ delCachedData();
36976
+ return res;
36977
+ }
36909
36978
  async function bulkWriteLevels(levels, session) {
36910
36979
  try {
36911
36980
  const operations = levels.map((level) => {
@@ -36999,18 +37068,22 @@ function useBuildingLevelRepo() {
36999
37068
  const levelIds = [
37000
37069
  ...new Set(levelsWithUnits.map((l) => l.level.toString()))
37001
37070
  ].map((id) => new import_mongodb70.ObjectId(id));
37002
- const result = await collection.aggregate([
37003
- {
37004
- $match: {
37005
- _id: { $in: levelIds }
37006
- }
37007
- },
37008
- {
37009
- $project: {
37010
- name: 1
37011
- }
37012
- }
37013
- ]).toArray();
37071
+ const result = await collection.aggregate(
37072
+ [
37073
+ {
37074
+ $match: {
37075
+ _id: { $in: levelIds }
37076
+ }
37077
+ },
37078
+ {
37079
+ $project: {
37080
+ name: 1
37081
+ }
37082
+ },
37083
+ { $sort: { name: 1 } }
37084
+ ],
37085
+ { collation: { locale: "en", numericOrdering: true } }
37086
+ ).toArray();
37014
37087
  return {
37015
37088
  success: true,
37016
37089
  message: "Building levels retrieved successfully",
@@ -37031,6 +37104,7 @@ function useBuildingLevelRepo() {
37031
37104
  getById,
37032
37105
  updateLevelById,
37033
37106
  deleteById,
37107
+ deactivateByBuilding,
37034
37108
  bulkWriteLevels,
37035
37109
  batchUpdateByIds,
37036
37110
  getBuildingLevelList,
@@ -37057,7 +37131,8 @@ function useBuildingService() {
37057
37131
  const { deleteFile } = useFileService();
37058
37132
  const {
37059
37133
  bulkWriteLevels: _bulkWriteLevels,
37060
- getActiveLevelsByIds: _getActiveLevelsByIds
37134
+ getActiveLevelsByIds: _getActiveLevelsByIds,
37135
+ deactivateByBuilding: _deactivateLevelsByBuilding
37061
37136
  } = useBuildingLevelRepo();
37062
37137
  function normalizeImportKey(value) {
37063
37138
  return String(value ?? "").trim().replace(/\s+/g, " ").toLowerCase();
@@ -37212,17 +37287,29 @@ function useBuildingService() {
37212
37287
  }
37213
37288
  }
37214
37289
  async function deleteById(id) {
37215
- const building = await getByBuilding(id);
37216
- if (building) {
37290
+ const existingUnit = await getByBuilding(id);
37291
+ if (existingUnit) {
37217
37292
  throw new import_node_server_utils108.BadRequestError(
37218
- "Cannot delete building with existing room/facility. Please delete room/facility first."
37293
+ "Cannot delete building with existing unit(s). Please delete the unit(s) first."
37219
37294
  );
37220
37295
  }
37296
+ const session = import_node_server_utils108.useAtlas.getClient()?.startSession();
37297
+ if (!session) {
37298
+ throw new import_node_server_utils108.BadRequestError("Database session not available.");
37299
+ }
37221
37300
  try {
37222
- await _deleteById(id);
37301
+ session.startTransaction();
37302
+ const building = await _getById(id);
37303
+ const levelIds = building?.levels ?? [];
37304
+ await _deleteById(id, session);
37305
+ await _deactivateLevelsByBuilding(id, levelIds, session);
37306
+ await session.commitTransaction();
37223
37307
  return "Building deleted successfully.";
37224
37308
  } catch (error) {
37309
+ await session.abortTransaction();
37225
37310
  throw error;
37311
+ } finally {
37312
+ session.endSession();
37226
37313
  }
37227
37314
  }
37228
37315
  async function uploadSpreadsheetBuilding(data, site) {
@@ -37637,7 +37724,7 @@ function useBuildingController() {
37637
37724
  search: import_joi53.default.string().optional().allow("", null),
37638
37725
  site: import_joi53.default.string().hex().optional().allow("", null),
37639
37726
  status: import_joi53.default.string().valid(...Object.values(BuildingStatus)).default("active" /* ACTIVE */),
37640
- sort: import_joi53.default.string().valid(...Object.values(SortFields)).default("_id" /* ID */),
37727
+ sort: import_joi53.default.string().valid(...Object.values(SortFields)).optional(),
37641
37728
  order: import_joi53.default.string().valid(...Object.values(SortOrder)).default("asc" /* ASC */)
37642
37729
  });
37643
37730
  const { error, value } = validation.validate(query2);
@@ -37652,9 +37739,7 @@ function useBuildingController() {
37652
37739
  const status = value.status;
37653
37740
  const site = value.site ?? "";
37654
37741
  const search = value.search ?? "";
37655
- const sortObj = {
37656
- [sort]: order === "asc" /* ASC */ ? 1 : -1
37657
- };
37742
+ const sortObj = sort ? { [sort]: order === "asc" /* ASC */ ? 1 : -1 } : {};
37658
37743
  try {
37659
37744
  const buildings = await _getAll({
37660
37745
  page,
@@ -38269,7 +38354,7 @@ function useBuildingUnitController() {
38269
38354
  site: import_joi54.default.string().hex().length(24).optional().allow("", null),
38270
38355
  building: import_joi54.default.string().hex().optional().allow("", null),
38271
38356
  status: import_joi54.default.string().valid(...Object.values(BuildingStatus)).default("active" /* ACTIVE */),
38272
- sort: import_joi54.default.string().valid(...Object.values(SortFields)).default("_id" /* ID */),
38357
+ sort: import_joi54.default.string().valid(...Object.values(SortFields)).optional(),
38273
38358
  order: import_joi54.default.string().valid(...Object.values(SortOrder)).default("asc" /* ASC */)
38274
38359
  });
38275
38360
  const query2 = {
@@ -38290,9 +38375,7 @@ function useBuildingUnitController() {
38290
38375
  const sort = value.sort;
38291
38376
  const order = value.order;
38292
38377
  const status = value.status;
38293
- const sortObj = {
38294
- [sort]: order === "asc" /* ASC */ ? -1 : 1
38295
- };
38378
+ const sortObj = sort ? { [sort]: order === "asc" /* ASC */ ? -1 : 1 } : {};
38296
38379
  const site = value.site;
38297
38380
  const building = value.building;
38298
38381
  const search = value.search;
@@ -38435,7 +38518,7 @@ function useBuildingUnitController() {
38435
38518
  site: import_joi54.default.string().hex().length(24).optional().allow("", null),
38436
38519
  building: import_joi54.default.string().hex().optional().allow("", null),
38437
38520
  status: import_joi54.default.string().valid(...Object.values(BuildingStatus)).default("active" /* ACTIVE */),
38438
- sort: import_joi54.default.string().valid(...Object.values(SortFields)).default("_id" /* ID */),
38521
+ sort: import_joi54.default.string().valid(...Object.values(SortFields)).optional(),
38439
38522
  order: import_joi54.default.string().valid(...Object.values(SortOrder)).default("asc" /* ASC */),
38440
38523
  level: import_joi54.default.string().hex().length(24).optional().allow(null, "")
38441
38524
  });
@@ -38457,9 +38540,7 @@ function useBuildingUnitController() {
38457
38540
  const sort = value.sort;
38458
38541
  const order = value.order;
38459
38542
  const status = value.status;
38460
- const sortObj = {
38461
- [sort]: order === "asc" /* ASC */ ? -1 : 1
38462
- };
38543
+ const sortObj = sort ? { [sort]: order === "asc" /* ASC */ ? -1 : 1 } : {};
38463
38544
  const site = value.site;
38464
38545
  const building = value.building;
38465
38546
  const search = value.search;