shuttlepro-shared 1.1.91 → 1.1.92
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.
|
@@ -2,9 +2,11 @@ const workspaceRepository = require("./workspace.repository");
|
|
|
2
2
|
const integrationRepository = require("./integration.repository");
|
|
3
3
|
const descriptionTemplateRepository = require("./descriptionTemplates.repository");
|
|
4
4
|
const shipperRepository = require("./shipper.repository");
|
|
5
|
+
const labelRepository = require("./label.repository");
|
|
5
6
|
exports.module = {
|
|
6
7
|
workspaceRepository,
|
|
7
8
|
integrationRepository,
|
|
8
9
|
descriptionTemplateRepository,
|
|
9
10
|
shipperRepository,
|
|
11
|
+
labelRepository,
|
|
10
12
|
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
const { getRedisData, setRedisData } = require("../../config/redis");
|
|
2
|
+
const Label = require("../../models/Label");
|
|
3
|
+
|
|
4
|
+
const CACHE_KEY_ALL = "labels_all";
|
|
5
|
+
|
|
6
|
+
const getCachedLabels = async () => {
|
|
7
|
+
let labels = await getRedisData(CACHE_KEY_ALL);
|
|
8
|
+
if (!labels) {
|
|
9
|
+
labels = await Label.find().lean().exec();
|
|
10
|
+
await setRedisData(CACHE_KEY_ALL, JSON.stringify(labels));
|
|
11
|
+
} else {
|
|
12
|
+
labels = JSON.parse(labels);
|
|
13
|
+
}
|
|
14
|
+
return labels;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const updateCachedLabels = async () => {
|
|
18
|
+
const labels = await Label.find().lean().exec();
|
|
19
|
+
await setRedisData(CACHE_KEY_ALL, JSON.stringify(labels));
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const createLabel = async (data) => {
|
|
23
|
+
const newLabel = new Label(data);
|
|
24
|
+
const savedLabel = await newLabel.save();
|
|
25
|
+
await updateCachedLabels();
|
|
26
|
+
return savedLabel;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const findLabelById = async (id) => {
|
|
30
|
+
const labels = await getCachedLabels();
|
|
31
|
+
return labels.find((l) => l._id.toString() === id) || null;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const findLabel = async (filter) => {
|
|
35
|
+
const labels = await getCachedLabels();
|
|
36
|
+
return (
|
|
37
|
+
labels.find((l) =>
|
|
38
|
+
Object.entries(filter).every(([key, value]) => l[key] === value)
|
|
39
|
+
) || null
|
|
40
|
+
);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const findAllLabels = async (filter = {}) => {
|
|
44
|
+
const labels = await getCachedLabels();
|
|
45
|
+
return labels.filter((l) =>
|
|
46
|
+
Object.entries(filter).every(([key, value]) => l[key] === value)
|
|
47
|
+
);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const updateLabelById = async (id, data) => {
|
|
51
|
+
const updatedLabel = await Label.findByIdAndUpdate(id, data, {
|
|
52
|
+
new: true,
|
|
53
|
+
}).exec();
|
|
54
|
+
if (updatedLabel) {
|
|
55
|
+
await updateCachedLabels();
|
|
56
|
+
}
|
|
57
|
+
return updatedLabel;
|
|
58
|
+
};
|
|
59
|
+
const updateLabel = async (filter, data) => {
|
|
60
|
+
const updatedLabel = await Label.findOneAndUpdate(filter, data, {
|
|
61
|
+
new: true,
|
|
62
|
+
}).exec();
|
|
63
|
+
if (updatedLabel) {
|
|
64
|
+
await updateCachedLabels();
|
|
65
|
+
}
|
|
66
|
+
return updatedLabel;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const deleteLabel = async (id) => {
|
|
70
|
+
const deletedLabel = await Label.findByIdAndDelete(id).exec();
|
|
71
|
+
if (deletedLabel) {
|
|
72
|
+
await updateCachedLabels();
|
|
73
|
+
}
|
|
74
|
+
return deletedLabel;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
module.exports = {
|
|
78
|
+
createLabel,
|
|
79
|
+
findLabelById,
|
|
80
|
+
findLabel,
|
|
81
|
+
findAllLabels,
|
|
82
|
+
updateLabel,
|
|
83
|
+
deleteLabel,
|
|
84
|
+
updateLabelById,
|
|
85
|
+
};
|