@goweekdays/core 1.2.5 → 1.3.1

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
@@ -25752,6 +25752,27 @@ function usePriceController() {
25752
25752
  import { BadRequestError as BadRequestError51 } from "@goweekdays/utils";
25753
25753
  import Joi28 from "joi";
25754
25754
  import { ObjectId as ObjectId34 } from "mongodb";
25755
+ var schemaPropertyUpdate = Joi28.object({
25756
+ propertyName: Joi28.string().trim().required(),
25757
+ propertyType: Joi28.string().trim().required(),
25758
+ propertyDescription: Joi28.string().trim().required(),
25759
+ streetAddress: Joi28.string().trim().required(),
25760
+ barangay: Joi28.string().trim().required(),
25761
+ city: Joi28.string().trim().required(),
25762
+ province: Joi28.string().trim().required(),
25763
+ region: Joi28.string().trim().required(),
25764
+ zipCode: Joi28.string().trim().optional().allow(""),
25765
+ latitude: Joi28.string().trim().optional().allow(""),
25766
+ longitude: Joi28.string().trim().optional().allow(""),
25767
+ ownerId: Joi28.string().trim().optional().allow(""),
25768
+ managerName: Joi28.string().trim().optional().allow(""),
25769
+ managerEmail: Joi28.string().trim().email().optional().allow(""),
25770
+ managerPhone: Joi28.string().trim().optional().allow(""),
25771
+ amenities: Joi28.array().items(Joi28.string().trim()).optional(),
25772
+ numberOfFloors: Joi28.string().trim().required(),
25773
+ floorLabels: Joi28.string().optional().allow(""),
25774
+ stayType: Joi28.string().trim().required()
25775
+ });
25755
25776
  var schemaProperty = Joi28.object({
25756
25777
  _id: Joi28.string().optional(),
25757
25778
  propertyName: Joi28.string().trim().required(),
@@ -25772,20 +25793,8 @@ var schemaProperty = Joi28.object({
25772
25793
  amenities: Joi28.array().items(Joi28.string().trim()).optional(),
25773
25794
  numberOfFloors: Joi28.string().trim().required(),
25774
25795
  floorLabels: Joi28.string().optional().allow(""),
25775
- coverPhoto: Joi28.object({
25776
- name: Joi28.string().trim().optional().allow(""),
25777
- size: Joi28.string().trim().optional().allow(""),
25778
- type: Joi28.string().trim().optional().allow(""),
25779
- webkitRelativePath: Joi28.string().trim().optional().allow("")
25780
- }),
25781
- photoGallery: Joi28.array().items(
25782
- Joi28.object({
25783
- name: Joi28.string().trim().optional().allow(""),
25784
- size: Joi28.string().trim().optional().allow(""),
25785
- type: Joi28.string().trim().optional().allow(""),
25786
- webkitRelativePath: Joi28.string().trim().optional().allow("")
25787
- })
25788
- ).required(),
25796
+ coverPhoto: Joi28.string().optional().allow("", null),
25797
+ photoGallery: Joi28.array().items(Joi28.string()),
25789
25798
  stayType: Joi28.string().trim().required(),
25790
25799
  createdAt: Joi28.date().optional(),
25791
25800
  updatedAt: Joi28.date().optional(),
@@ -25826,6 +25835,7 @@ function modelProperty(value) {
25826
25835
  coverPhoto: value.coverPhoto,
25827
25836
  photoGallery: value.photoGallery ?? [],
25828
25837
  stayType: value.stayType,
25838
+ status: value.status ?? "active",
25829
25839
  createdAt: value.createdAt ?? /* @__PURE__ */ new Date(),
25830
25840
  updatedAt: value.updatedAt,
25831
25841
  deletedAt: value.deletedAt
@@ -25839,7 +25849,8 @@ import {
25839
25849
  useCache as useCache18,
25840
25850
  makeCacheKey as makeCacheKey17,
25841
25851
  logger as logger26,
25842
- InternalServerError as InternalServerError24
25852
+ InternalServerError as InternalServerError24,
25853
+ paginate as paginate12
25843
25854
  } from "@goweekdays/utils";
25844
25855
  import { ObjectId as ObjectId35 } from "mongodb";
25845
25856
  function usePropertyRepo() {
@@ -25897,18 +25908,25 @@ function usePropertyRepo() {
25897
25908
  async function updateById(_id, value, session) {
25898
25909
  try {
25899
25910
  _id = new ObjectId35(_id);
25900
- } catch (error) {
25911
+ } catch (error2) {
25901
25912
  throw new BadRequestError52("Invalid ID.");
25902
25913
  }
25914
+ const { error } = schemaPropertyUpdate.validate(value);
25915
+ if (error) {
25916
+ throw new BadRequestError52(
25917
+ `Invalid property data: ${error.message}. Repository validation.`
25918
+ );
25919
+ }
25903
25920
  try {
25921
+ value.updatedAt = /* @__PURE__ */ new Date();
25904
25922
  const res = await collection.updateOne(
25905
25923
  { _id },
25906
- { $set: { value, updatedAt: /* @__PURE__ */ new Date() } },
25924
+ { $set: value },
25907
25925
  { session }
25908
25926
  );
25909
25927
  delCachedData();
25910
25928
  return res;
25911
- } catch (error) {
25929
+ } catch (error2) {
25912
25930
  throw new InternalServerError24("Failed to update property.");
25913
25931
  }
25914
25932
  }
@@ -25926,8 +25944,25 @@ function usePropertyRepo() {
25926
25944
  throw new InternalServerError24("Failed to delete property.");
25927
25945
  }
25928
25946
  }
25929
- async function getAll() {
25930
- const cacheKey = makeCacheKey17(namespace_collection, { action: "getAll" });
25947
+ async function getAll({
25948
+ page = 1,
25949
+ limit = 20,
25950
+ status = "active",
25951
+ search = ""
25952
+ } = {}) {
25953
+ page = page > 0 ? page - 1 : 0;
25954
+ const query = { status };
25955
+ const cacheKeyOptions = {
25956
+ action: "getAll",
25957
+ page,
25958
+ limit,
25959
+ status
25960
+ };
25961
+ if (search) {
25962
+ query.$text = { $search: search };
25963
+ cacheKeyOptions.search = search;
25964
+ }
25965
+ const cacheKey = makeCacheKey17(namespace_collection, cacheKeyOptions);
25931
25966
  try {
25932
25967
  const cached = await getCache(cacheKey);
25933
25968
  if (cached) {
@@ -25937,8 +25972,14 @@ function usePropertyRepo() {
25937
25972
  });
25938
25973
  return cached;
25939
25974
  }
25940
- const properties = await collection.find({}).toArray();
25941
- setCache(cacheKey, properties, 300).then(() => {
25975
+ const items = await collection.aggregate([
25976
+ { $match: query },
25977
+ { $skip: page * limit },
25978
+ { $limit: limit }
25979
+ ]).toArray();
25980
+ const length = await collection.countDocuments(query);
25981
+ const data = paginate12(items, page, limit, length);
25982
+ setCache(cacheKey, data, 300).then(() => {
25942
25983
  logger26.log({
25943
25984
  level: "info",
25944
25985
  message: `Cache set for getAll: ${cacheKey}`
@@ -25949,7 +25990,7 @@ function usePropertyRepo() {
25949
25990
  message: `Failed to set cache: ${err.message}`
25950
25991
  });
25951
25992
  });
25952
- return properties;
25993
+ return data;
25953
25994
  } catch (error) {
25954
25995
  throw new InternalServerError24(
25955
25996
  `Failed to fetch properties: ${error.message}`
@@ -26015,7 +26056,8 @@ function usePropertyController() {
26015
26056
  add: _add,
26016
26057
  updateById: _updateById,
26017
26058
  getAll: _getAll,
26018
- getById: _getById
26059
+ getById: _getById,
26060
+ deleteById: _deleteById
26019
26061
  } = usePropertyRepo();
26020
26062
  async function add(req, res, next) {
26021
26063
  const value = req.body;
@@ -26034,32 +26076,15 @@ function usePropertyController() {
26034
26076
  }
26035
26077
  async function updateById(req, res, next) {
26036
26078
  const _id = req.params.id ?? "";
26037
- const value = { _id, ...req.body };
26038
- const validation = Joi29.object({
26039
- _id: Joi29.string().required(),
26040
- name: Joi29.string().trim().required(),
26041
- type: Joi29.string().trim().required(),
26042
- description: Joi29.string().trim().required(),
26043
- street: Joi29.string().trim().required(),
26044
- barangay: Joi29.string().trim().required(),
26045
- cityMunicipality: Joi29.string().trim().required(),
26046
- province: Joi29.string().trim().required(),
26047
- region: Joi29.string().trim().required(),
26048
- zipCode: Joi29.string().trim().optional().allow(""),
26049
- latitude: Joi29.string().trim().optional().allow(""),
26050
- longitude: Joi29.string().trim().optional().allow(""),
26051
- ownerId: Joi29.string().trim().optional().allow(""),
26052
- ownerName: Joi29.string().trim().optional().allow(""),
26053
- email: Joi29.string().trim().email().optional().allow(""),
26054
- phone: Joi29.string().trim().optional().allow(""),
26055
- amenities: Joi29.array().items(Joi29.string().trim()).optional(),
26056
- floorNumber: Joi29.string().trim().required(),
26057
- floorLabel: Joi29.string().optional().allow(""),
26058
- coverPhoto: Joi29.string().trim().required(),
26059
- photoGallery: Joi29.array().items(Joi29.string().trim()).required(),
26060
- stayType: Joi29.string().trim().required()
26079
+ const idValidation = Joi29.object({
26080
+ _id: Joi29.string().required()
26061
26081
  });
26062
- const { error } = validation.validate(value);
26082
+ const { error: idValidationError } = idValidation.validate({ _id });
26083
+ if (idValidationError) {
26084
+ throw new BadRequestError53(`Invalid ID`);
26085
+ }
26086
+ const value = req.body;
26087
+ const { error } = schemaPropertyUpdate.validate(value);
26063
26088
  if (error) {
26064
26089
  throw new BadRequestError53(`Invalid property data: ${error.message}`);
26065
26090
  }
@@ -26071,12 +26096,23 @@ function usePropertyController() {
26071
26096
  }
26072
26097
  }
26073
26098
  async function getAll(req, res, next) {
26099
+ const validation = Joi29.object({
26100
+ page: Joi29.number().integer().min(1).default(1),
26101
+ limit: Joi29.number().integer().min(1).max(100).default(10),
26102
+ search: Joi29.string().trim().allow("").default(""),
26103
+ status: Joi29.string().default("active")
26104
+ });
26105
+ const { error, value } = validation.validate(req.query);
26106
+ if (error) {
26107
+ next(new BadRequestError53(error.message));
26108
+ return;
26109
+ }
26074
26110
  try {
26075
- const properties = await _getAll();
26111
+ const properties = await _getAll(value);
26076
26112
  res.json(properties);
26077
26113
  return;
26078
- } catch (error) {
26079
- next(error);
26114
+ } catch (error2) {
26115
+ next(error2);
26080
26116
  }
26081
26117
  }
26082
26118
  async function getById(req, res, next) {
@@ -26093,11 +26129,26 @@ function usePropertyController() {
26093
26129
  next(error);
26094
26130
  }
26095
26131
  }
26132
+ async function deleteById(req, res, next) {
26133
+ const id = req.params.id;
26134
+ if (!id) {
26135
+ next(new BadRequestError53("Property ID is required."));
26136
+ return;
26137
+ }
26138
+ try {
26139
+ const message = await _deleteById(id);
26140
+ res.json(message);
26141
+ return;
26142
+ } catch (error) {
26143
+ next(error);
26144
+ }
26145
+ }
26096
26146
  return {
26097
26147
  add,
26098
26148
  updateById,
26099
26149
  getAll,
26100
- getById
26150
+ getById,
26151
+ deleteById
26101
26152
  };
26102
26153
  }
26103
26154
 
@@ -26427,6 +26478,7 @@ export {
26427
26478
  modelProperty,
26428
26479
  schema,
26429
26480
  schemaProperty,
26481
+ schemaPropertyUpdate,
26430
26482
  useAddressController,
26431
26483
  useAddressRepo,
26432
26484
  useAuthController,