shuttlepro-shared 1.1.90 → 1.1.91

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.
@@ -1,8 +1,10 @@
1
1
  const workspaceRepository = require("./workspace.repository");
2
2
  const integrationRepository = require("./integration.repository");
3
3
  const descriptionTemplateRepository = require("./descriptionTemplates.repository");
4
+ const shipperRepository = require("./shipper.repository");
4
5
  exports.module = {
5
6
  workspaceRepository,
6
7
  integrationRepository,
7
8
  descriptionTemplateRepository,
9
+ shipperRepository,
8
10
  };
@@ -0,0 +1,77 @@
1
+ const { getRedisData, setRedisData } = require("../../config/redis");
2
+ const Shipper = require("../../models/Shipper");
3
+
4
+ const CACHE_KEY_ALL = "shippers_all";
5
+
6
+ const getCachedShippers = async () => {
7
+ let shippers = await getRedisData(CACHE_KEY_ALL);
8
+ if (!shippers) {
9
+ shippers = await Shipper.find().lean().exec();
10
+ await setRedisData(CACHE_KEY_ALL, JSON.stringify(shippers));
11
+ } else {
12
+ shippers = JSON.parse(shippers);
13
+ }
14
+ return shippers;
15
+ };
16
+
17
+ const updateCachedShippers = async () => {
18
+ const shippers = await Shipper.find().lean().exec();
19
+ await setRedisData(CACHE_KEY_ALL, JSON.stringify(shippers));
20
+ };
21
+
22
+ const createShipper = async (data) => {
23
+ const newShipper = new Shipper(data);
24
+ const savedShipper = await newShipper.save();
25
+ await updateCachedShippers();
26
+ return savedShipper;
27
+ };
28
+
29
+ const findShipperById = async (id) => {
30
+ const shippers = await getCachedShippers();
31
+ const shipper = shippers.find((s) => s._id.toString() === id) || null;
32
+
33
+ return shipper;
34
+ };
35
+
36
+ const findShipper = async (filter) => {
37
+ const shippers = await getCachedShippers();
38
+ return (
39
+ shippers.find((s) =>
40
+ Object.entries(filter).every(([key, value]) => s[key] === value)
41
+ ) || null
42
+ );
43
+ };
44
+
45
+ const findAllShippers = async (filter = {}) => {
46
+ const shippers = await getCachedShippers();
47
+ return shippers.filter((s) =>
48
+ Object.entries(filter).every(([key, value]) => s[key] === value)
49
+ );
50
+ };
51
+
52
+ const updateShipper = async (id, data) => {
53
+ const updatedShipper = await Shipper.findByIdAndUpdate(id, data, {
54
+ new: true,
55
+ }).exec();
56
+ if (updatedShipper) {
57
+ await updateCachedShippers();
58
+ }
59
+ return updatedShipper;
60
+ };
61
+
62
+ const deleteShipper = async (id) => {
63
+ const deletedShipper = await Shipper.findByIdAndDelete(id).exec();
64
+ if (deletedShipper) {
65
+ await updateCachedShippers();
66
+ }
67
+ return deletedShipper;
68
+ };
69
+
70
+ module.exports = {
71
+ createShipper,
72
+ findShipperById,
73
+ findShipper,
74
+ findAllShippers,
75
+ updateShipper,
76
+ deleteShipper,
77
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shuttlepro-shared",
3
- "version": "1.1.90",
3
+ "version": "1.1.91",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {