@7365admin1/core 3.1.0 → 3.2.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,11 @@
1
1
  # @iservice365/core
2
2
 
3
+ ## 3.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 6a87d22: release new changes for building, incident report, etc.
8
+
3
9
  ## 3.1.0
4
10
 
5
11
  ### Minor Changes
package/dist/index.d.ts CHANGED
@@ -5433,9 +5433,9 @@ type TSiteInfo = {
5433
5433
  submittedBy?: string;
5434
5434
  };
5435
5435
  type TPlaceOfIncident = {
5436
- block?: number;
5437
- level?: string;
5438
- unit?: string | ObjectId;
5436
+ block?: string | ObjectId | null;
5437
+ level?: string | ObjectId | null;
5438
+ unit?: string | ObjectId | null;
5439
5439
  other?: string | null;
5440
5440
  isOther: boolean;
5441
5441
  incidentLocation: string;
@@ -5490,9 +5490,9 @@ type TAffectedInjured = {
5490
5490
  name: string;
5491
5491
  contact?: string;
5492
5492
  nric?: string;
5493
- block?: number;
5494
- level?: string;
5495
- unit?: string | ObjectId;
5493
+ block?: string | ObjectId | null;
5494
+ level?: string | ObjectId | null;
5495
+ unit?: string | ObjectId | null;
5496
5496
  other?: string | null;
5497
5497
  isOther: boolean;
5498
5498
  incidentLocation: string;
@@ -5501,9 +5501,9 @@ type TAffectedInjured = {
5501
5501
  };
5502
5502
  type TanyoneDamageToProperty = {
5503
5503
  description: string;
5504
- block: number;
5505
- level: string;
5506
- unit: string;
5504
+ block?: string | ObjectId | null;
5505
+ level?: string | ObjectId | null;
5506
+ unit?: string | ObjectId | null;
5507
5507
  blkLevelUnit: string;
5508
5508
  name: string;
5509
5509
  contact: string;
@@ -7348,6 +7348,7 @@ declare function MBuildingLevel(value: TBuildingLevel): {
7348
7348
 
7349
7349
  declare const building_level_namespace_collection = "building-levels";
7350
7350
  declare function useBuildingLevelRepo(): {
7351
+ createIndexes: () => Promise<void>;
7351
7352
  add: (value: TBuildingLevel, session?: ClientSession) => Promise<ObjectId>;
7352
7353
  getAll: ({ search, page, limit, sort, site, status, }?: {
7353
7354
  search?: string | undefined;
package/dist/index.js CHANGED
@@ -25443,6 +25443,9 @@ function useBuildingUnitRepo() {
25443
25443
  }
25444
25444
  const collection = db.collection(building_units_namespace_collection);
25445
25445
  const visitorCollection = db.collection(visitors_namespace_collection);
25446
+ const buildingUnitCollection = db.collection(
25447
+ site_people_namespace_collection
25448
+ );
25446
25449
  const { getCache, setCache, delNamespace, delCache } = (0, import_node_server_utils76.useCache)(
25447
25450
  building_units_namespace_collection
25448
25451
  );
@@ -25524,10 +25527,8 @@ function useBuildingUnitRepo() {
25524
25527
  } catch (error2) {
25525
25528
  throw new import_node_server_utils76.BadRequestError("Invalid ID.");
25526
25529
  }
25527
- try {
25530
+ if (value.level) {
25528
25531
  value.level = new import_mongodb47.ObjectId(value.level);
25529
- } catch (error2) {
25530
- throw new import_node_server_utils76.BadRequestError("Invalid level ID.");
25531
25532
  }
25532
25533
  if (value.billing && Array.isArray(value.billing)) {
25533
25534
  value.billing = value.billing.map((billing) => {
@@ -25975,6 +25976,12 @@ function useBuildingUnitRepo() {
25975
25976
  throw new import_node_server_utils76.BadRequestError("Invalid ID.");
25976
25977
  }
25977
25978
  try {
25979
+ const existingResidents = await buildingUnitCollection.find({ unit: _id }).toArray();
25980
+ if (existingResidents && existingResidents.length > 0) {
25981
+ throw new import_node_server_utils76.BadRequestError(
25982
+ `Cannot delete unit: ${existingResidents.length} resident/s is currently assigned.`
25983
+ );
25984
+ }
25978
25985
  const res = await collection.updateOne(
25979
25986
  { _id },
25980
25987
  { $set: { status: "deleted", deletedAt: /* @__PURE__ */ new Date() } },
@@ -27803,24 +27810,30 @@ function useBuildingRepo() {
27803
27810
  }
27804
27811
  }
27805
27812
  async function updateById(_id, value, session) {
27806
- const { error } = schemaBuildingUpdateOptions.validate(value);
27807
- if (error) {
27808
- import_node_server_utils83.logger.info(`Building Repository: ${error.message}`);
27809
- throw new import_node_server_utils83.BadRequestError(error.message);
27810
- }
27811
27813
  try {
27812
- _id = new import_mongodb50.ObjectId(_id);
27813
- } catch (error2) {
27814
- throw new import_node_server_utils83.BadRequestError("Invalid ID.");
27815
- }
27816
- if (value.levels && Array.isArray(value.levels)) {
27814
+ const { error } = schemaBuildingUpdateOptions.validate(value);
27815
+ if (error) {
27816
+ import_node_server_utils83.logger.info(`Building Repository: ${error.message}`);
27817
+ throw new import_node_server_utils83.BadRequestError(error.message);
27818
+ }
27817
27819
  try {
27818
- value.levels = value.levels.map((lvl) => new import_mongodb50.ObjectId(lvl));
27819
- } catch {
27820
- throw new import_node_server_utils83.BadRequestError("Invalid level ID format.");
27820
+ _id = new import_mongodb50.ObjectId(_id);
27821
+ } catch (error2) {
27822
+ throw new import_node_server_utils83.BadRequestError("Invalid ID.");
27821
27823
  }
27822
- }
27823
- try {
27824
+ if (value.levels && Array.isArray(value.levels)) {
27825
+ try {
27826
+ value.levels = value.levels.map((lvl) => new import_mongodb50.ObjectId(lvl));
27827
+ } catch {
27828
+ throw new import_node_server_utils83.BadRequestError("Invalid level ID format.");
27829
+ }
27830
+ }
27831
+ if (value.name)
27832
+ await buildingUnitCollection.updateMany(
27833
+ { building: _id, status: "active" /* ACTIVE */ },
27834
+ { $set: { buildingName: value.name } },
27835
+ { session }
27836
+ );
27824
27837
  const res = await collection.updateOne(
27825
27838
  { _id },
27826
27839
  { $set: value },
@@ -27828,17 +27841,17 @@ function useBuildingRepo() {
27828
27841
  );
27829
27842
  delCachedData();
27830
27843
  return res;
27831
- } catch (error2) {
27844
+ } catch (error) {
27832
27845
  import_node_server_utils83.logger.log({
27833
27846
  level: "error",
27834
- message: error2.message
27847
+ message: error.message
27835
27848
  });
27836
- const isDuplicated = error2.message.includes("duplicate");
27849
+ const isDuplicated = error.message.includes("duplicate");
27837
27850
  if (isDuplicated) {
27838
27851
  throw new import_node_server_utils83.BadRequestError("Building already exists.");
27839
27852
  }
27840
- if (error2 instanceof import_node_server_utils83.AppError) {
27841
- throw error2;
27853
+ if (error instanceof import_node_server_utils83.AppError) {
27854
+ throw error;
27842
27855
  } else {
27843
27856
  throw new Error("Failed to update building.");
27844
27857
  }
@@ -28430,6 +28443,32 @@ function useBuildingLevelRepo() {
28430
28443
  const { delNamespace: delBuildingNamespace } = (0, import_node_server_utils86.useCache)(
28431
28444
  buildings_namespace_collection
28432
28445
  );
28446
+ async function createIndexes() {
28447
+ try {
28448
+ await collection.createIndexes([
28449
+ // Ensure name is unique per blockId
28450
+ {
28451
+ key: { blockId: 1, name: 1 },
28452
+ unique: true,
28453
+ name: "unique_name_per_blockId"
28454
+ },
28455
+ // Other useful indexes
28456
+ { key: { site: 1 }, name: "idx_site" },
28457
+ { key: { blockId: 1 }, name: "idx_blockId" },
28458
+ { key: { status: 1 }, name: "idx_status" },
28459
+ { key: { createdAt: 1 }, name: "idx_createdAt" },
28460
+ // Text index for search
28461
+ {
28462
+ key: {
28463
+ name: "text"
28464
+ },
28465
+ name: "text_search_index"
28466
+ }
28467
+ ]);
28468
+ } catch (error) {
28469
+ throw new Error("Failed to create indexes on building levels.");
28470
+ }
28471
+ }
28433
28472
  function delCachedData() {
28434
28473
  delNamespace().then(() => {
28435
28474
  import_node_server_utils86.logger.log({
@@ -28775,7 +28814,7 @@ function useBuildingLevelRepo() {
28775
28814
  }
28776
28815
  }
28777
28816
  return {
28778
- // createIndexes,
28817
+ createIndexes,
28779
28818
  add,
28780
28819
  getAll,
28781
28820
  getById,
@@ -52947,9 +52986,9 @@ var affectedInjuredStatusSchema = import_joi101.default.object({
52947
52986
  contact: import_joi101.default.string().optional().allow(null, ""),
52948
52987
  nric: import_joi101.default.string().optional().allow(null, ""),
52949
52988
  remarks: import_joi101.default.string().optional().allow(null, ""),
52950
- block: import_joi101.default.number().optional().allow(null, ""),
52951
- level: import_joi101.default.string().optional().allow(null, ""),
52952
- unit: import_joi101.default.string().optional().allow(null, ""),
52989
+ block: import_joi101.default.string().hex().length(24).optional().allow(null, ""),
52990
+ level: import_joi101.default.string().hex().length(24).optional().allow(null, ""),
52991
+ unit: import_joi101.default.string().hex().length(24).optional().allow(null, ""),
52953
52992
  incidentLocation: import_joi101.default.string().optional().allow(null, ""),
52954
52993
  isOther: import_joi101.default.boolean().optional().allow(null, ""),
52955
52994
  other: import_joi101.default.string().optional().allow(null, ""),
@@ -52957,9 +52996,9 @@ var affectedInjuredStatusSchema = import_joi101.default.object({
52957
52996
  });
52958
52997
  var anyoneDamageToPropertyStatusSchema = import_joi101.default.object({
52959
52998
  description: import_joi101.default.string().optional().allow(null, ""),
52960
- block: import_joi101.default.number().optional().allow(null, ""),
52961
- level: import_joi101.default.string().optional().allow(null, ""),
52962
- unit: import_joi101.default.string().optional().allow(null, ""),
52999
+ block: import_joi101.default.string().hex().length(24).optional().allow(null, ""),
53000
+ level: import_joi101.default.string().hex().length(24).optional().allow(null, ""),
53001
+ unit: import_joi101.default.string().hex().length(24).optional().allow(null, ""),
52963
53002
  blkLevelUnit: import_joi101.default.string().optional().allow(null, ""),
52964
53003
  name: import_joi101.default.string().optional().allow(null, ""),
52965
53004
  contact: import_joi101.default.string().optional().allow(null, ""),
@@ -52982,9 +53021,9 @@ var schemaIncidentReport = import_joi101.default.object({
52982
53021
  submittedBy: import_joi101.default.string().optional().allow(null, "")
52983
53022
  }).required(),
52984
53023
  placeOfIncident: import_joi101.default.object({
52985
- block: import_joi101.default.number().optional().allow(null, ""),
52986
- level: import_joi101.default.string().optional().allow(null, ""),
52987
- unit: import_joi101.default.string().optional().allow(null, ""),
53024
+ block: import_joi101.default.string().hex().length(24).optional().allow(null, ""),
53025
+ level: import_joi101.default.string().hex().length(24).optional().allow(null, ""),
53026
+ unit: import_joi101.default.string().hex().length(24).optional().allow(null, ""),
52988
53027
  other: import_joi101.default.string().allow(null, "").optional().allow(null, ""),
52989
53028
  isOther: import_joi101.default.boolean().required(),
52990
53029
  incidentLocation: import_joi101.default.string().optional().allow(null, "")
@@ -53063,9 +53102,9 @@ var schemaUpdateIncidentReport = import_joi101.default.object({
53063
53102
  submittedBy: import_joi101.default.string().optional().allow(null, "")
53064
53103
  }).allow(null, ""),
53065
53104
  placeOfIncident: import_joi101.default.object({
53066
- block: import_joi101.default.number().optional().allow(null, ""),
53067
- level: import_joi101.default.string().optional().allow(null, ""),
53068
- unit: import_joi101.default.string().optional().allow(null, ""),
53105
+ block: import_joi101.default.string().hex().length(24).optional().allow(null, ""),
53106
+ level: import_joi101.default.string().hex().length(24).optional().allow(null, ""),
53107
+ unit: import_joi101.default.string().hex().length(24).optional().allow(null, ""),
53069
53108
  other: import_joi101.default.string().allow(null, "").optional().allow(null, ""),
53070
53109
  isOther: import_joi101.default.boolean().allow(null, ""),
53071
53110
  incidentLocation: import_joi101.default.string().optional().allow(null, "")
@@ -53173,6 +53212,82 @@ function MIncidentReport(value) {
53173
53212
  throw new Error("Invalid siteInfo.site ID.");
53174
53213
  }
53175
53214
  }
53215
+ const placeOfIncident = incidentInformation?.placeOfIncident;
53216
+ if (placeOfIncident) {
53217
+ if (placeOfIncident.block && typeof placeOfIncident.block === "string") {
53218
+ try {
53219
+ placeOfIncident.block = new import_mongodb109.ObjectId(placeOfIncident.block);
53220
+ } catch {
53221
+ throw new Error("Invalid placeOfIncident.block ID.");
53222
+ }
53223
+ }
53224
+ if (placeOfIncident.level && typeof placeOfIncident.level === "string") {
53225
+ try {
53226
+ placeOfIncident.level = new import_mongodb109.ObjectId(placeOfIncident.level);
53227
+ } catch {
53228
+ throw new Error("Invalid placeOfIncident.level ID.");
53229
+ }
53230
+ }
53231
+ if (placeOfIncident.unit && typeof placeOfIncident.unit === "string") {
53232
+ try {
53233
+ placeOfIncident.unit = new import_mongodb109.ObjectId(placeOfIncident.unit);
53234
+ } catch {
53235
+ throw new Error("Invalid placeOfIncident.unit ID.");
53236
+ }
53237
+ }
53238
+ }
53239
+ const affectedInjured = value.affectedEntities?.affectedInjured;
53240
+ if (Array.isArray(affectedInjured)) {
53241
+ affectedInjured.forEach((item) => {
53242
+ if (item.block && typeof item.block === "string") {
53243
+ try {
53244
+ item.block = new import_mongodb109.ObjectId(item.block);
53245
+ } catch {
53246
+ throw new Error("Invalid affectedInjured.block ID.");
53247
+ }
53248
+ }
53249
+ if (item.level && typeof item.level === "string") {
53250
+ try {
53251
+ item.level = new import_mongodb109.ObjectId(item.level);
53252
+ } catch {
53253
+ throw new Error("Invalid affectedInjured.level ID.");
53254
+ }
53255
+ }
53256
+ if (item.unit && typeof item.unit === "string") {
53257
+ try {
53258
+ item.unit = new import_mongodb109.ObjectId(item.unit);
53259
+ } catch {
53260
+ throw new Error("Invalid affectedInjured.unit ID.");
53261
+ }
53262
+ }
53263
+ });
53264
+ }
53265
+ const anyoneDamageToProperty = value.affectedEntities?.anyoneDamageToProperty;
53266
+ if (Array.isArray(anyoneDamageToProperty)) {
53267
+ anyoneDamageToProperty.forEach((item) => {
53268
+ if (item.block && typeof item.block === "string") {
53269
+ try {
53270
+ item.block = new import_mongodb109.ObjectId(item.block);
53271
+ } catch {
53272
+ throw new Error("Invalid anyoneDamageToProperty.block ID.");
53273
+ }
53274
+ }
53275
+ if (item.level && typeof item.level === "string") {
53276
+ try {
53277
+ item.level = new import_mongodb109.ObjectId(item.level);
53278
+ } catch {
53279
+ throw new Error("Invalid anyoneDamageToProperty.level ID.");
53280
+ }
53281
+ }
53282
+ if (item.unit && typeof item.unit === "string") {
53283
+ try {
53284
+ item.unit = new import_mongodb109.ObjectId(item.unit);
53285
+ } catch {
53286
+ throw new Error("Invalid anyoneDamageToProperty.unit ID.");
53287
+ }
53288
+ }
53289
+ });
53290
+ }
53176
53291
  if (incidentInformation?.incidentTypeAndTime?.dateOfIncident && typeof incidentInformation.incidentTypeAndTime.dateOfIncident !== "string") {
53177
53292
  incidentInformation.incidentTypeAndTime.dateOfIncident = incidentInformation.incidentTypeAndTime.dateOfIncident.toISOString();
53178
53293
  }
@@ -53508,6 +53623,82 @@ function useIncidentReportRepo() {
53508
53623
  throw new Error("Invalid siteInfo.site ID.");
53509
53624
  }
53510
53625
  }
53626
+ const placeOfIncident = incidentInformation?.placeOfIncident;
53627
+ if (placeOfIncident) {
53628
+ if (placeOfIncident.block && typeof placeOfIncident.block === "string") {
53629
+ try {
53630
+ placeOfIncident.block = new import_mongodb110.ObjectId(placeOfIncident.block);
53631
+ } catch {
53632
+ throw new Error("Invalid placeOfIncident.block ID.");
53633
+ }
53634
+ }
53635
+ if (placeOfIncident.level && typeof placeOfIncident.level === "string") {
53636
+ try {
53637
+ placeOfIncident.level = new import_mongodb110.ObjectId(placeOfIncident.level);
53638
+ } catch {
53639
+ throw new Error("Invalid placeOfIncident.level ID.");
53640
+ }
53641
+ }
53642
+ if (placeOfIncident.unit && typeof placeOfIncident.unit === "string") {
53643
+ try {
53644
+ placeOfIncident.unit = new import_mongodb110.ObjectId(placeOfIncident.unit);
53645
+ } catch {
53646
+ throw new Error("Invalid placeOfIncident.unit ID.");
53647
+ }
53648
+ }
53649
+ }
53650
+ const affectedInjured = value.affectedEntities?.affectedInjured;
53651
+ if (Array.isArray(affectedInjured)) {
53652
+ affectedInjured.forEach((item) => {
53653
+ if (item.block && typeof item.block === "string") {
53654
+ try {
53655
+ item.block = new import_mongodb110.ObjectId(item.block);
53656
+ } catch {
53657
+ throw new Error("Invalid affectedInjured.block ID.");
53658
+ }
53659
+ }
53660
+ if (item.level && typeof item.level === "string") {
53661
+ try {
53662
+ item.level = new import_mongodb110.ObjectId(item.level);
53663
+ } catch {
53664
+ throw new Error("Invalid affectedInjured.level ID.");
53665
+ }
53666
+ }
53667
+ if (item.unit && typeof item.unit === "string") {
53668
+ try {
53669
+ item.unit = new import_mongodb110.ObjectId(item.unit);
53670
+ } catch {
53671
+ throw new Error("Invalid affectedInjured.unit ID.");
53672
+ }
53673
+ }
53674
+ });
53675
+ }
53676
+ const anyoneDamageToProperty = value.affectedEntities?.anyoneDamageToProperty;
53677
+ if (Array.isArray(anyoneDamageToProperty)) {
53678
+ anyoneDamageToProperty.forEach((item) => {
53679
+ if (item.block && typeof item.block === "string") {
53680
+ try {
53681
+ item.block = new import_mongodb110.ObjectId(item.block);
53682
+ } catch {
53683
+ throw new Error("Invalid anyoneDamageToProperty.block ID.");
53684
+ }
53685
+ }
53686
+ if (item.level && typeof item.level === "string") {
53687
+ try {
53688
+ item.level = new import_mongodb110.ObjectId(item.level);
53689
+ } catch {
53690
+ throw new Error("Invalid anyoneDamageToProperty.level ID.");
53691
+ }
53692
+ }
53693
+ if (item.unit && typeof item.unit === "string") {
53694
+ try {
53695
+ item.unit = new import_mongodb110.ObjectId(item.unit);
53696
+ } catch {
53697
+ throw new Error("Invalid anyoneDamageToProperty.unit ID.");
53698
+ }
53699
+ }
53700
+ });
53701
+ }
53511
53702
  try {
53512
53703
  const res = await collection.updateOne(
53513
53704
  { _id },