@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/dist/index.mjs CHANGED
@@ -25241,6 +25241,9 @@ function useBuildingUnitRepo() {
25241
25241
  }
25242
25242
  const collection = db.collection(building_units_namespace_collection);
25243
25243
  const visitorCollection = db.collection(visitors_namespace_collection);
25244
+ const buildingUnitCollection = db.collection(
25245
+ site_people_namespace_collection
25246
+ );
25244
25247
  const { getCache, setCache, delNamespace, delCache } = useCache26(
25245
25248
  building_units_namespace_collection
25246
25249
  );
@@ -25322,10 +25325,8 @@ function useBuildingUnitRepo() {
25322
25325
  } catch (error2) {
25323
25326
  throw new BadRequestError74("Invalid ID.");
25324
25327
  }
25325
- try {
25328
+ if (value.level) {
25326
25329
  value.level = new ObjectId47(value.level);
25327
- } catch (error2) {
25328
- throw new BadRequestError74("Invalid level ID.");
25329
25330
  }
25330
25331
  if (value.billing && Array.isArray(value.billing)) {
25331
25332
  value.billing = value.billing.map((billing) => {
@@ -25773,6 +25774,12 @@ function useBuildingUnitRepo() {
25773
25774
  throw new BadRequestError74("Invalid ID.");
25774
25775
  }
25775
25776
  try {
25777
+ const existingResidents = await buildingUnitCollection.find({ unit: _id }).toArray();
25778
+ if (existingResidents && existingResidents.length > 0) {
25779
+ throw new BadRequestError74(
25780
+ `Cannot delete unit: ${existingResidents.length} resident/s is currently assigned.`
25781
+ );
25782
+ }
25776
25783
  const res = await collection.updateOne(
25777
25784
  { _id },
25778
25785
  { $set: { status: "deleted", deletedAt: /* @__PURE__ */ new Date() } },
@@ -27615,24 +27622,30 @@ function useBuildingRepo() {
27615
27622
  }
27616
27623
  }
27617
27624
  async function updateById(_id, value, session) {
27618
- const { error } = schemaBuildingUpdateOptions.validate(value);
27619
- if (error) {
27620
- logger61.info(`Building Repository: ${error.message}`);
27621
- throw new BadRequestError80(error.message);
27622
- }
27623
27625
  try {
27624
- _id = new ObjectId50(_id);
27625
- } catch (error2) {
27626
- throw new BadRequestError80("Invalid ID.");
27627
- }
27628
- if (value.levels && Array.isArray(value.levels)) {
27626
+ const { error } = schemaBuildingUpdateOptions.validate(value);
27627
+ if (error) {
27628
+ logger61.info(`Building Repository: ${error.message}`);
27629
+ throw new BadRequestError80(error.message);
27630
+ }
27629
27631
  try {
27630
- value.levels = value.levels.map((lvl) => new ObjectId50(lvl));
27631
- } catch {
27632
- throw new BadRequestError80("Invalid level ID format.");
27632
+ _id = new ObjectId50(_id);
27633
+ } catch (error2) {
27634
+ throw new BadRequestError80("Invalid ID.");
27633
27635
  }
27634
- }
27635
- try {
27636
+ if (value.levels && Array.isArray(value.levels)) {
27637
+ try {
27638
+ value.levels = value.levels.map((lvl) => new ObjectId50(lvl));
27639
+ } catch {
27640
+ throw new BadRequestError80("Invalid level ID format.");
27641
+ }
27642
+ }
27643
+ if (value.name)
27644
+ await buildingUnitCollection.updateMany(
27645
+ { building: _id, status: "active" /* ACTIVE */ },
27646
+ { $set: { buildingName: value.name } },
27647
+ { session }
27648
+ );
27636
27649
  const res = await collection.updateOne(
27637
27650
  { _id },
27638
27651
  { $set: value },
@@ -27640,17 +27653,17 @@ function useBuildingRepo() {
27640
27653
  );
27641
27654
  delCachedData();
27642
27655
  return res;
27643
- } catch (error2) {
27656
+ } catch (error) {
27644
27657
  logger61.log({
27645
27658
  level: "error",
27646
- message: error2.message
27659
+ message: error.message
27647
27660
  });
27648
- const isDuplicated = error2.message.includes("duplicate");
27661
+ const isDuplicated = error.message.includes("duplicate");
27649
27662
  if (isDuplicated) {
27650
27663
  throw new BadRequestError80("Building already exists.");
27651
27664
  }
27652
- if (error2 instanceof AppError13) {
27653
- throw error2;
27665
+ if (error instanceof AppError13) {
27666
+ throw error;
27654
27667
  } else {
27655
27668
  throw new Error("Failed to update building.");
27656
27669
  }
@@ -28257,6 +28270,32 @@ function useBuildingLevelRepo() {
28257
28270
  const { delNamespace: delBuildingNamespace } = useCache29(
28258
28271
  buildings_namespace_collection
28259
28272
  );
28273
+ async function createIndexes() {
28274
+ try {
28275
+ await collection.createIndexes([
28276
+ // Ensure name is unique per blockId
28277
+ {
28278
+ key: { blockId: 1, name: 1 },
28279
+ unique: true,
28280
+ name: "unique_name_per_blockId"
28281
+ },
28282
+ // Other useful indexes
28283
+ { key: { site: 1 }, name: "idx_site" },
28284
+ { key: { blockId: 1 }, name: "idx_blockId" },
28285
+ { key: { status: 1 }, name: "idx_status" },
28286
+ { key: { createdAt: 1 }, name: "idx_createdAt" },
28287
+ // Text index for search
28288
+ {
28289
+ key: {
28290
+ name: "text"
28291
+ },
28292
+ name: "text_search_index"
28293
+ }
28294
+ ]);
28295
+ } catch (error) {
28296
+ throw new Error("Failed to create indexes on building levels.");
28297
+ }
28298
+ }
28260
28299
  function delCachedData() {
28261
28300
  delNamespace().then(() => {
28262
28301
  logger63.log({
@@ -28602,7 +28641,7 @@ function useBuildingLevelRepo() {
28602
28641
  }
28603
28642
  }
28604
28643
  return {
28605
- // createIndexes,
28644
+ createIndexes,
28606
28645
  add,
28607
28646
  getAll,
28608
28647
  getById,
@@ -53050,9 +53089,9 @@ var affectedInjuredStatusSchema = Joi101.object({
53050
53089
  contact: Joi101.string().optional().allow(null, ""),
53051
53090
  nric: Joi101.string().optional().allow(null, ""),
53052
53091
  remarks: Joi101.string().optional().allow(null, ""),
53053
- block: Joi101.number().optional().allow(null, ""),
53054
- level: Joi101.string().optional().allow(null, ""),
53055
- unit: Joi101.string().optional().allow(null, ""),
53092
+ block: Joi101.string().hex().length(24).optional().allow(null, ""),
53093
+ level: Joi101.string().hex().length(24).optional().allow(null, ""),
53094
+ unit: Joi101.string().hex().length(24).optional().allow(null, ""),
53056
53095
  incidentLocation: Joi101.string().optional().allow(null, ""),
53057
53096
  isOther: Joi101.boolean().optional().allow(null, ""),
53058
53097
  other: Joi101.string().optional().allow(null, ""),
@@ -53060,9 +53099,9 @@ var affectedInjuredStatusSchema = Joi101.object({
53060
53099
  });
53061
53100
  var anyoneDamageToPropertyStatusSchema = Joi101.object({
53062
53101
  description: Joi101.string().optional().allow(null, ""),
53063
- block: Joi101.number().optional().allow(null, ""),
53064
- level: Joi101.string().optional().allow(null, ""),
53065
- unit: Joi101.string().optional().allow(null, ""),
53102
+ block: Joi101.string().hex().length(24).optional().allow(null, ""),
53103
+ level: Joi101.string().hex().length(24).optional().allow(null, ""),
53104
+ unit: Joi101.string().hex().length(24).optional().allow(null, ""),
53066
53105
  blkLevelUnit: Joi101.string().optional().allow(null, ""),
53067
53106
  name: Joi101.string().optional().allow(null, ""),
53068
53107
  contact: Joi101.string().optional().allow(null, ""),
@@ -53085,9 +53124,9 @@ var schemaIncidentReport = Joi101.object({
53085
53124
  submittedBy: Joi101.string().optional().allow(null, "")
53086
53125
  }).required(),
53087
53126
  placeOfIncident: Joi101.object({
53088
- block: Joi101.number().optional().allow(null, ""),
53089
- level: Joi101.string().optional().allow(null, ""),
53090
- unit: Joi101.string().optional().allow(null, ""),
53127
+ block: Joi101.string().hex().length(24).optional().allow(null, ""),
53128
+ level: Joi101.string().hex().length(24).optional().allow(null, ""),
53129
+ unit: Joi101.string().hex().length(24).optional().allow(null, ""),
53091
53130
  other: Joi101.string().allow(null, "").optional().allow(null, ""),
53092
53131
  isOther: Joi101.boolean().required(),
53093
53132
  incidentLocation: Joi101.string().optional().allow(null, "")
@@ -53166,9 +53205,9 @@ var schemaUpdateIncidentReport = Joi101.object({
53166
53205
  submittedBy: Joi101.string().optional().allow(null, "")
53167
53206
  }).allow(null, ""),
53168
53207
  placeOfIncident: Joi101.object({
53169
- block: Joi101.number().optional().allow(null, ""),
53170
- level: Joi101.string().optional().allow(null, ""),
53171
- unit: Joi101.string().optional().allow(null, ""),
53208
+ block: Joi101.string().hex().length(24).optional().allow(null, ""),
53209
+ level: Joi101.string().hex().length(24).optional().allow(null, ""),
53210
+ unit: Joi101.string().hex().length(24).optional().allow(null, ""),
53172
53211
  other: Joi101.string().allow(null, "").optional().allow(null, ""),
53173
53212
  isOther: Joi101.boolean().allow(null, ""),
53174
53213
  incidentLocation: Joi101.string().optional().allow(null, "")
@@ -53276,6 +53315,82 @@ function MIncidentReport(value) {
53276
53315
  throw new Error("Invalid siteInfo.site ID.");
53277
53316
  }
53278
53317
  }
53318
+ const placeOfIncident = incidentInformation?.placeOfIncident;
53319
+ if (placeOfIncident) {
53320
+ if (placeOfIncident.block && typeof placeOfIncident.block === "string") {
53321
+ try {
53322
+ placeOfIncident.block = new ObjectId109(placeOfIncident.block);
53323
+ } catch {
53324
+ throw new Error("Invalid placeOfIncident.block ID.");
53325
+ }
53326
+ }
53327
+ if (placeOfIncident.level && typeof placeOfIncident.level === "string") {
53328
+ try {
53329
+ placeOfIncident.level = new ObjectId109(placeOfIncident.level);
53330
+ } catch {
53331
+ throw new Error("Invalid placeOfIncident.level ID.");
53332
+ }
53333
+ }
53334
+ if (placeOfIncident.unit && typeof placeOfIncident.unit === "string") {
53335
+ try {
53336
+ placeOfIncident.unit = new ObjectId109(placeOfIncident.unit);
53337
+ } catch {
53338
+ throw new Error("Invalid placeOfIncident.unit ID.");
53339
+ }
53340
+ }
53341
+ }
53342
+ const affectedInjured = value.affectedEntities?.affectedInjured;
53343
+ if (Array.isArray(affectedInjured)) {
53344
+ affectedInjured.forEach((item) => {
53345
+ if (item.block && typeof item.block === "string") {
53346
+ try {
53347
+ item.block = new ObjectId109(item.block);
53348
+ } catch {
53349
+ throw new Error("Invalid affectedInjured.block ID.");
53350
+ }
53351
+ }
53352
+ if (item.level && typeof item.level === "string") {
53353
+ try {
53354
+ item.level = new ObjectId109(item.level);
53355
+ } catch {
53356
+ throw new Error("Invalid affectedInjured.level ID.");
53357
+ }
53358
+ }
53359
+ if (item.unit && typeof item.unit === "string") {
53360
+ try {
53361
+ item.unit = new ObjectId109(item.unit);
53362
+ } catch {
53363
+ throw new Error("Invalid affectedInjured.unit ID.");
53364
+ }
53365
+ }
53366
+ });
53367
+ }
53368
+ const anyoneDamageToProperty = value.affectedEntities?.anyoneDamageToProperty;
53369
+ if (Array.isArray(anyoneDamageToProperty)) {
53370
+ anyoneDamageToProperty.forEach((item) => {
53371
+ if (item.block && typeof item.block === "string") {
53372
+ try {
53373
+ item.block = new ObjectId109(item.block);
53374
+ } catch {
53375
+ throw new Error("Invalid anyoneDamageToProperty.block ID.");
53376
+ }
53377
+ }
53378
+ if (item.level && typeof item.level === "string") {
53379
+ try {
53380
+ item.level = new ObjectId109(item.level);
53381
+ } catch {
53382
+ throw new Error("Invalid anyoneDamageToProperty.level ID.");
53383
+ }
53384
+ }
53385
+ if (item.unit && typeof item.unit === "string") {
53386
+ try {
53387
+ item.unit = new ObjectId109(item.unit);
53388
+ } catch {
53389
+ throw new Error("Invalid anyoneDamageToProperty.unit ID.");
53390
+ }
53391
+ }
53392
+ });
53393
+ }
53279
53394
  if (incidentInformation?.incidentTypeAndTime?.dateOfIncident && typeof incidentInformation.incidentTypeAndTime.dateOfIncident !== "string") {
53280
53395
  incidentInformation.incidentTypeAndTime.dateOfIncident = incidentInformation.incidentTypeAndTime.dateOfIncident.toISOString();
53281
53396
  }
@@ -53624,6 +53739,82 @@ function useIncidentReportRepo() {
53624
53739
  throw new Error("Invalid siteInfo.site ID.");
53625
53740
  }
53626
53741
  }
53742
+ const placeOfIncident = incidentInformation?.placeOfIncident;
53743
+ if (placeOfIncident) {
53744
+ if (placeOfIncident.block && typeof placeOfIncident.block === "string") {
53745
+ try {
53746
+ placeOfIncident.block = new ObjectId110(placeOfIncident.block);
53747
+ } catch {
53748
+ throw new Error("Invalid placeOfIncident.block ID.");
53749
+ }
53750
+ }
53751
+ if (placeOfIncident.level && typeof placeOfIncident.level === "string") {
53752
+ try {
53753
+ placeOfIncident.level = new ObjectId110(placeOfIncident.level);
53754
+ } catch {
53755
+ throw new Error("Invalid placeOfIncident.level ID.");
53756
+ }
53757
+ }
53758
+ if (placeOfIncident.unit && typeof placeOfIncident.unit === "string") {
53759
+ try {
53760
+ placeOfIncident.unit = new ObjectId110(placeOfIncident.unit);
53761
+ } catch {
53762
+ throw new Error("Invalid placeOfIncident.unit ID.");
53763
+ }
53764
+ }
53765
+ }
53766
+ const affectedInjured = value.affectedEntities?.affectedInjured;
53767
+ if (Array.isArray(affectedInjured)) {
53768
+ affectedInjured.forEach((item) => {
53769
+ if (item.block && typeof item.block === "string") {
53770
+ try {
53771
+ item.block = new ObjectId110(item.block);
53772
+ } catch {
53773
+ throw new Error("Invalid affectedInjured.block ID.");
53774
+ }
53775
+ }
53776
+ if (item.level && typeof item.level === "string") {
53777
+ try {
53778
+ item.level = new ObjectId110(item.level);
53779
+ } catch {
53780
+ throw new Error("Invalid affectedInjured.level ID.");
53781
+ }
53782
+ }
53783
+ if (item.unit && typeof item.unit === "string") {
53784
+ try {
53785
+ item.unit = new ObjectId110(item.unit);
53786
+ } catch {
53787
+ throw new Error("Invalid affectedInjured.unit ID.");
53788
+ }
53789
+ }
53790
+ });
53791
+ }
53792
+ const anyoneDamageToProperty = value.affectedEntities?.anyoneDamageToProperty;
53793
+ if (Array.isArray(anyoneDamageToProperty)) {
53794
+ anyoneDamageToProperty.forEach((item) => {
53795
+ if (item.block && typeof item.block === "string") {
53796
+ try {
53797
+ item.block = new ObjectId110(item.block);
53798
+ } catch {
53799
+ throw new Error("Invalid anyoneDamageToProperty.block ID.");
53800
+ }
53801
+ }
53802
+ if (item.level && typeof item.level === "string") {
53803
+ try {
53804
+ item.level = new ObjectId110(item.level);
53805
+ } catch {
53806
+ throw new Error("Invalid anyoneDamageToProperty.level ID.");
53807
+ }
53808
+ }
53809
+ if (item.unit && typeof item.unit === "string") {
53810
+ try {
53811
+ item.unit = new ObjectId110(item.unit);
53812
+ } catch {
53813
+ throw new Error("Invalid anyoneDamageToProperty.unit ID.");
53814
+ }
53815
+ }
53816
+ });
53817
+ }
53627
53818
  try {
53628
53819
  const res = await collection.updateOne(
53629
53820
  { _id },