@iservice365/module-hygiene 1.0.1 → 1.0.2

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
@@ -2834,7 +2834,8 @@ import {
2834
2834
  BadRequestError as BadRequestError16,
2835
2835
  useCache as useCache5,
2836
2836
  logger as logger17,
2837
- makeCacheKey as makeCacheKey5
2837
+ makeCacheKey as makeCacheKey5,
2838
+ NotFoundError as NotFoundError4
2838
2839
  } from "@iservice365/node-server-utils";
2839
2840
  function useSupplyRepository() {
2840
2841
  const db = useAtlas7.getDb();
@@ -2951,6 +2952,48 @@ function useSupplyRepository() {
2951
2952
  throw error;
2952
2953
  }
2953
2954
  }
2955
+ async function getSupplyById(_id) {
2956
+ try {
2957
+ _id = new ObjectId10(_id);
2958
+ } catch (error) {
2959
+ throw new BadRequestError16("Invalid supply ID format.");
2960
+ }
2961
+ const query = {
2962
+ _id,
2963
+ status: { $ne: "deleted" }
2964
+ };
2965
+ const cacheKey = makeCacheKey5(namespace_collection, {
2966
+ _id: _id.toString()
2967
+ });
2968
+ const cachedData = await getCache(cacheKey);
2969
+ if (cachedData) {
2970
+ logger17.info(`Cache hit for key: ${cacheKey}`);
2971
+ return cachedData;
2972
+ }
2973
+ try {
2974
+ const data = await collection.aggregate([
2975
+ { $match: query },
2976
+ {
2977
+ $project: {
2978
+ name: 1,
2979
+ unitOfMeasurement: 1,
2980
+ qty: 1
2981
+ }
2982
+ }
2983
+ ]).toArray();
2984
+ if (!data || data.length === 0) {
2985
+ throw new NotFoundError4("Supply not found.");
2986
+ }
2987
+ setCache(cacheKey, data[0], 15 * 60).then(() => {
2988
+ logger17.info(`Cache set for key: ${cacheKey}`);
2989
+ }).catch((err) => {
2990
+ logger17.error(`Failed to set cache for key: ${cacheKey}`, err);
2991
+ });
2992
+ return data[0];
2993
+ } catch (error) {
2994
+ throw error;
2995
+ }
2996
+ }
2954
2997
  async function updateSupply(_id, value, session) {
2955
2998
  try {
2956
2999
  _id = new ObjectId10(_id);
@@ -3023,6 +3066,7 @@ function useSupplyRepository() {
3023
3066
  createUniqueIndex,
3024
3067
  createSupply,
3025
3068
  getSupplies,
3069
+ getSupplyById,
3026
3070
  updateSupply,
3027
3071
  deleteSupply
3028
3072
  };
@@ -3035,6 +3079,7 @@ function useSupplyController() {
3035
3079
  const {
3036
3080
  createSupply: _createSupply,
3037
3081
  getSupplies: _getSupplies,
3082
+ getSupplyById: _getSupplyById,
3038
3083
  updateSupply: _updateSupply,
3039
3084
  deleteSupply: _deleteSupply
3040
3085
  } = useSupplyRepository();
@@ -3089,6 +3134,25 @@ function useSupplyController() {
3089
3134
  return;
3090
3135
  }
3091
3136
  }
3137
+ async function getSupplyById(req, res, next) {
3138
+ const validation = Joi10.string().hex().required();
3139
+ const _id = req.params.id;
3140
+ const { error } = validation.validate(_id);
3141
+ if (error) {
3142
+ logger18.log({ level: "error", message: error.message });
3143
+ next(new BadRequestError17(error.message));
3144
+ return;
3145
+ }
3146
+ try {
3147
+ const data = await _getSupplyById(_id);
3148
+ res.json(data);
3149
+ return;
3150
+ } catch (error2) {
3151
+ logger18.log({ level: "error", message: error2.message });
3152
+ next(error2);
3153
+ return;
3154
+ }
3155
+ }
3092
3156
  async function updateSupply(req, res, next) {
3093
3157
  const payload = { id: req.params.id, ...req.body };
3094
3158
  const validation = Joi10.object({
@@ -3138,6 +3202,7 @@ function useSupplyController() {
3138
3202
  return {
3139
3203
  createSupply,
3140
3204
  getSupplies,
3205
+ getSupplyById,
3141
3206
  updateSupply,
3142
3207
  deleteSupply
3143
3208
  };