shuttlepro-shared 1.1.97 → 1.1.98
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/common/repositories/descriptionTemplates.repository.js +186 -0
- package/common/repositories/index.js +12 -0
- package/common/repositories/integration.repository.js +197 -0
- package/common/repositories/label.repository.js +85 -0
- package/common/repositories/shipper.repository.js +77 -0
- package/common/repositories/workspace.repository.js +95 -0
- package/config/bull.js +78 -0
- package/config/config.js +14 -0
- package/config/database.js +7 -0
- package/config/index.js +13 -0
- package/config/redis.js +160 -0
- package/config/socket.js +172 -0
- package/constants/index.js +15 -0
- package/index.js +8 -0
- package/models/AgentActivity.js +189 -0
- package/models/Assignment.js +23 -0
- package/models/BusinessDistribution.js +23 -0
- package/models/Card.js +144 -0
- package/models/CardComments.js +33 -0
- package/models/Chatbot.js +16 -0
- package/models/Checkpoint.js +50 -0
- package/models/City.js +17 -0
- package/models/Column.js +28 -0
- package/models/Conversation.js +84 -0
- package/models/Customer.js +35 -0
- package/models/DescriptionTemplate.js +22 -0
- package/models/Integration.js +53 -0
- package/models/Label.js +42 -0
- package/models/Message.js +47 -0
- package/models/Order.js +131 -0
- package/models/OrderProduct.js +37 -0
- package/models/Profile.js +127 -0
- package/models/Report.js +27 -0
- package/models/Shipper.js +52 -0
- package/models/Status.js +58 -0
- package/models/StatusType.js +10 -0
- package/models/Step.js +50 -0
- package/models/Type.js +25 -0
- package/models/Workspace.js +190 -0
- package/models.js +50 -0
- package/package.json +13 -2
- package/utils/decorator-factory.js +264 -0
- package/utils/logger.js +41 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
const { getRedisData, setRedisData } = require("../../config/redis");
|
|
2
|
+
// const DescriptionTemplate = require("../../../models/DescriptionTemplate");
|
|
3
|
+
const DescriptionTemplate = require("../../models/DescriptionTemplate");
|
|
4
|
+
|
|
5
|
+
const CACHE_KEY_ALL = "description_templates_all";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Get cached descriptionTemplates for all workspaces.
|
|
9
|
+
*/
|
|
10
|
+
const getCachedAllDescriptionTemplates = async () => {
|
|
11
|
+
let descriptionTemplates = await getRedisData(CACHE_KEY_ALL);
|
|
12
|
+
if (!descriptionTemplates) {
|
|
13
|
+
descriptionTemplates = await DescriptionTemplate.find({}).lean().exec();
|
|
14
|
+
await setRedisData(CACHE_KEY_ALL, descriptionTemplates);
|
|
15
|
+
}
|
|
16
|
+
return descriptionTemplates;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Update cached descriptionTemplates for all workspaces.
|
|
21
|
+
*/
|
|
22
|
+
const updateCachedAllDescriptionTemplates = async () => {
|
|
23
|
+
const descriptionTemplates = await DescriptionTemplate.find({}).lean().exec();
|
|
24
|
+
await setRedisData(CACHE_KEY_ALL, descriptionTemplates);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Get cached descriptionTemplates for a specific workspace.
|
|
29
|
+
* Now uses the all descriptionTemplates cache and filters by workspaceId.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Create a new DescriptionTemplate and update cache.
|
|
34
|
+
*/
|
|
35
|
+
const createTemplate = async (data) => {
|
|
36
|
+
const newTemplate = new DescriptionTemplate(data);
|
|
37
|
+
const saveDescriptionTemplate = await newTemplate.save();
|
|
38
|
+
|
|
39
|
+
// Update only the main cache
|
|
40
|
+
await updateCachedAllDescriptionTemplates();
|
|
41
|
+
|
|
42
|
+
return saveDescriptionTemplate;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Find an DescriptionTemplate by ID.
|
|
47
|
+
*/
|
|
48
|
+
const findTemplatesByWorkspaceId = async (workspaceId) => {
|
|
49
|
+
const allDescriptionTemplates = await getCachedAllDescriptionTemplates();
|
|
50
|
+
return allDescriptionTemplates.filter(
|
|
51
|
+
(desc) => desc.workspaceId === workspaceId
|
|
52
|
+
);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Find DescriptionTemplate by filter (supports both workspace & non-workspace).
|
|
57
|
+
*/
|
|
58
|
+
const findTemplateByFilter = async (
|
|
59
|
+
filter,
|
|
60
|
+
bodyFilter = {},
|
|
61
|
+
workspaceId = null
|
|
62
|
+
) => {
|
|
63
|
+
const allTemplates = await getCachedAllDescriptionTemplates();
|
|
64
|
+
|
|
65
|
+
// Filter by workspaceId if provided
|
|
66
|
+
let filteredTemplates = workspaceId
|
|
67
|
+
? allTemplates.filter((desc) => desc.workspaceId === workspaceId)
|
|
68
|
+
: allTemplates;
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
filteredTemplates.find(
|
|
72
|
+
(item) =>
|
|
73
|
+
// Check direct properties of item
|
|
74
|
+
Object.entries(filter).every(([key, value]) => item[key] === value) &&
|
|
75
|
+
// If bodyFilter is provided, check inside item.body
|
|
76
|
+
Object.entries(bodyFilter).every(
|
|
77
|
+
([key, value]) => item.body?.[key] === value
|
|
78
|
+
)
|
|
79
|
+
) || null
|
|
80
|
+
);
|
|
81
|
+
};
|
|
82
|
+
const createOrUpdateTemplate = async (filter, data) => {
|
|
83
|
+
const template = await DescriptionTemplate.findOneAndUpdate(filter, data, {
|
|
84
|
+
upsert: true,
|
|
85
|
+
new: true,
|
|
86
|
+
});
|
|
87
|
+
if (template) {
|
|
88
|
+
// Update only the main cache
|
|
89
|
+
await updateCachedAllDescriptionTemplates();
|
|
90
|
+
}
|
|
91
|
+
return template;
|
|
92
|
+
};
|
|
93
|
+
const updateTemplateById = async (id, data) => {
|
|
94
|
+
const updatedTemplate = await DescriptionTemplate.findByIdAndUpdate(
|
|
95
|
+
id,
|
|
96
|
+
data,
|
|
97
|
+
{
|
|
98
|
+
new: true,
|
|
99
|
+
}
|
|
100
|
+
).exec();
|
|
101
|
+
|
|
102
|
+
if (updatedTemplate) {
|
|
103
|
+
// Update only the main cache
|
|
104
|
+
await updateCachedAllDescriptionTemplates();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return updatedTemplate;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Update an DescriptionTemplate by filter.
|
|
112
|
+
*/
|
|
113
|
+
const updateTemplateByFilter = async (filter, data, workspaceId = null) => {
|
|
114
|
+
// Add workspace filter if provided
|
|
115
|
+
const queryFilter = workspaceId ? { ...filter, workspaceId } : filter;
|
|
116
|
+
|
|
117
|
+
const updatedTemplate = await DescriptionTemplate.findOneAndUpdate(
|
|
118
|
+
queryFilter,
|
|
119
|
+
data,
|
|
120
|
+
{
|
|
121
|
+
new: true,
|
|
122
|
+
}
|
|
123
|
+
).exec();
|
|
124
|
+
|
|
125
|
+
if (updatedTemplate) {
|
|
126
|
+
// Update only the main cache
|
|
127
|
+
await updateCachedAllDescriptionTemplates();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return updatedTemplate;
|
|
131
|
+
};
|
|
132
|
+
const updateManyTemplates = async (filter, data) => {
|
|
133
|
+
const queryFilter = workspaceId ? { ...filter, workspaceId } : filter;
|
|
134
|
+
|
|
135
|
+
const updatedTemplate = await DescriptionTemplate.updateMany(
|
|
136
|
+
queryFilter,
|
|
137
|
+
{ $set: { ...data } },
|
|
138
|
+
{
|
|
139
|
+
new: true,
|
|
140
|
+
}
|
|
141
|
+
).exec();
|
|
142
|
+
|
|
143
|
+
if (updatedTemplate) {
|
|
144
|
+
// Update only the main cache
|
|
145
|
+
await updateCachedAllDescriptionTemplates();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return updatedTemplate;
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Delete an DescriptionTemplate by ID.
|
|
152
|
+
*/
|
|
153
|
+
const deleteTemplate = async (id) => {
|
|
154
|
+
const deletedTemplate = await DescriptionTemplate.findByIdAndDelete(
|
|
155
|
+
id
|
|
156
|
+
).exec();
|
|
157
|
+
|
|
158
|
+
if (deletedTemplate) {
|
|
159
|
+
// Update only the main cache
|
|
160
|
+
await updateCachedAllDescriptionTemplates();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return deletedTemplate;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Delete all descriptionTemplates for a specific workspace.
|
|
168
|
+
*/
|
|
169
|
+
const deleteAllTemplatesByWorkspace = async (workspaceId) => {
|
|
170
|
+
await DescriptionTemplate.deleteMany({ workspaceId }).exec();
|
|
171
|
+
|
|
172
|
+
// Update only the main cache
|
|
173
|
+
await updateCachedAllDescriptionTemplates();
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
module.exports = {
|
|
177
|
+
createTemplate,
|
|
178
|
+
createOrUpdateTemplate,
|
|
179
|
+
findTemplateByFilter,
|
|
180
|
+
findTemplatesByWorkspaceId,
|
|
181
|
+
updateTemplateById,
|
|
182
|
+
updateTemplateByFilter,
|
|
183
|
+
updateManyTemplates,
|
|
184
|
+
deleteTemplate,
|
|
185
|
+
deleteAllTemplatesByWorkspace,
|
|
186
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const workspaceRepository = require("./workspace.repository");
|
|
2
|
+
const integrationRepository = require("./integration.repository");
|
|
3
|
+
const descriptionTemplateRepository = require("./descriptionTemplates.repository");
|
|
4
|
+
const shipperRepository = require("./shipper.repository");
|
|
5
|
+
const labelRepository = require("./label.repository");
|
|
6
|
+
exports.module = {
|
|
7
|
+
workspaceRepository,
|
|
8
|
+
integrationRepository,
|
|
9
|
+
descriptionTemplateRepository,
|
|
10
|
+
shipperRepository,
|
|
11
|
+
labelRepository,
|
|
12
|
+
};
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
const { getRedisData, setRedisData } = require("../../config/redis");
|
|
2
|
+
const Integration = require("../../models/Integration");
|
|
3
|
+
|
|
4
|
+
const CACHE_KEY_ALL = "integrations_all";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Get cached integrations for all workspaces.
|
|
8
|
+
*/
|
|
9
|
+
const getCachedAllIntegrations = async () => {
|
|
10
|
+
let integrations = await getRedisData(CACHE_KEY_ALL);
|
|
11
|
+
if (!integrations) {
|
|
12
|
+
integrations = await Integration.find({}).lean().exec();
|
|
13
|
+
await setRedisData(CACHE_KEY_ALL, integrations);
|
|
14
|
+
}
|
|
15
|
+
return integrations;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Update cached integrations for all workspaces.
|
|
20
|
+
*/
|
|
21
|
+
const updateCachedAllIntegrations = async () => {
|
|
22
|
+
const integrations = await Integration.find({}).lean().exec();
|
|
23
|
+
await setRedisData(CACHE_KEY_ALL, integrations);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Get cached integrations for a specific workspace.
|
|
28
|
+
* Now uses the all integrations cache and filters by workspaceId.
|
|
29
|
+
*/
|
|
30
|
+
const getCachedIntegrations = async (workspaceId) => {
|
|
31
|
+
const allIntegrations = await getCachedAllIntegrations();
|
|
32
|
+
return allIntegrations.filter((intg) => intg.workspaceId === workspaceId);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Create a new integration and update cache.
|
|
37
|
+
*/
|
|
38
|
+
const createIntegration = async (data) => {
|
|
39
|
+
const newIntegration = new Integration(data);
|
|
40
|
+
const savedIntegration = await newIntegration.save();
|
|
41
|
+
|
|
42
|
+
// Update only the main cache
|
|
43
|
+
await updateCachedAllIntegrations();
|
|
44
|
+
|
|
45
|
+
return savedIntegration;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Find an integration by ID.
|
|
50
|
+
*/
|
|
51
|
+
const findIntegrationsByWorkspaceId = async (workspaceId) => {
|
|
52
|
+
return await getCachedIntegrations(workspaceId);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Find integrations by workspace.
|
|
57
|
+
*/
|
|
58
|
+
const findIntegrationsByWorkspace = async (workspaceId, filter = {}) => {
|
|
59
|
+
const allIntegrations = await getCachedIntegrations(workspaceId);
|
|
60
|
+
|
|
61
|
+
return allIntegrations.filter(
|
|
62
|
+
(intg) =>
|
|
63
|
+
intg.workspaceId === workspaceId &&
|
|
64
|
+
Object.entries(filter).every(([key, value]) => intg[key] === value)
|
|
65
|
+
);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Find integration by filter (supports both workspace & non-workspace).
|
|
70
|
+
*/
|
|
71
|
+
const findIntegrationByFilter = async (filter, workspaceId = null) => {
|
|
72
|
+
const allIntegrations = await getCachedAllIntegrations();
|
|
73
|
+
|
|
74
|
+
let filteredIntegrations = allIntegrations;
|
|
75
|
+
if (workspaceId) {
|
|
76
|
+
filteredIntegrations = allIntegrations.filter(
|
|
77
|
+
(intg) => intg.workspaceId === workspaceId
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return (
|
|
82
|
+
filteredIntegrations.find((item) =>
|
|
83
|
+
Object.entries(filter).every(([key, value]) => item.body?.[key] === value)
|
|
84
|
+
) || null
|
|
85
|
+
);
|
|
86
|
+
};
|
|
87
|
+
const findAllIntegrationByFilter = async (
|
|
88
|
+
filter = {},
|
|
89
|
+
bodyFilter = {},
|
|
90
|
+
workspaceId = null
|
|
91
|
+
) => {
|
|
92
|
+
const allIntegrations = await getCachedAllIntegrations();
|
|
93
|
+
|
|
94
|
+
let filteredIntegrations = allIntegrations;
|
|
95
|
+
if (workspaceId) {
|
|
96
|
+
filteredIntegrations = allIntegrations.filter(
|
|
97
|
+
(intg) => intg.workspaceId === workspaceId
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
return (
|
|
101
|
+
filteredIntegrations.filter(
|
|
102
|
+
(item) =>
|
|
103
|
+
Object.entries(filter).every(([key, value]) => item[key] === value) &&
|
|
104
|
+
Object.entries(bodyFilter).every(
|
|
105
|
+
([key, value]) => item.body?.[key] === value
|
|
106
|
+
)
|
|
107
|
+
) || []
|
|
108
|
+
);
|
|
109
|
+
};
|
|
110
|
+
const bulkCreateAndUpdateIntegrations = async (operations) => {
|
|
111
|
+
await Integration.bulkWrite(operations);
|
|
112
|
+
await updateCachedAllIntegrations();
|
|
113
|
+
return operations;
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Find all integrations (without workspace filtering).
|
|
117
|
+
*/
|
|
118
|
+
const findAllIntegrations = async () => {
|
|
119
|
+
return await getCachedAllIntegrations();
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Update an integration by ID.
|
|
124
|
+
*/
|
|
125
|
+
const updateIntegration = async (id, data) => {
|
|
126
|
+
const updatedIntegration = await Integration.findByIdAndUpdate(id, data, {
|
|
127
|
+
new: true,
|
|
128
|
+
}).exec();
|
|
129
|
+
|
|
130
|
+
if (updatedIntegration) {
|
|
131
|
+
// Update only the main cache
|
|
132
|
+
await updateCachedAllIntegrations();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return updatedIntegration;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Update an integration by filter.
|
|
140
|
+
*/
|
|
141
|
+
const updateIntegrationByFilter = async (filter, data, workspaceId = null) => {
|
|
142
|
+
// Add workspace filter if provided
|
|
143
|
+
const queryFilter = workspaceId ? { ...filter, workspaceId } : filter;
|
|
144
|
+
|
|
145
|
+
const updatedIntegration = await Integration.findOneAndUpdate(
|
|
146
|
+
queryFilter,
|
|
147
|
+
data,
|
|
148
|
+
{
|
|
149
|
+
new: true,
|
|
150
|
+
}
|
|
151
|
+
).exec();
|
|
152
|
+
|
|
153
|
+
if (updatedIntegration) {
|
|
154
|
+
// Update only the main cache
|
|
155
|
+
await updateCachedAllIntegrations();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return updatedIntegration;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Delete an integration by ID.
|
|
163
|
+
*/
|
|
164
|
+
const deleteIntegration = async (id) => {
|
|
165
|
+
const deletedIntegration = await Integration.findByIdAndDelete(id).exec();
|
|
166
|
+
|
|
167
|
+
if (deletedIntegration) {
|
|
168
|
+
// Update only the main cache
|
|
169
|
+
await updateCachedAllIntegrations();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return deletedIntegration;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Delete all integrations for a specific workspace.
|
|
177
|
+
*/
|
|
178
|
+
const deleteAllIntegrationsByWorkspace = async (workspaceId) => {
|
|
179
|
+
await Integration.deleteMany({ workspaceId }).exec();
|
|
180
|
+
|
|
181
|
+
// Update only the main cache
|
|
182
|
+
await updateCachedAllIntegrations();
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
module.exports = {
|
|
186
|
+
createIntegration,
|
|
187
|
+
findIntegrationsByWorkspaceId,
|
|
188
|
+
findIntegrationsByWorkspace,
|
|
189
|
+
findIntegrationByFilter,
|
|
190
|
+
findAllIntegrations,
|
|
191
|
+
updateIntegration,
|
|
192
|
+
updateIntegrationByFilter,
|
|
193
|
+
deleteIntegration,
|
|
194
|
+
deleteAllIntegrationsByWorkspace,
|
|
195
|
+
findAllIntegrationByFilter,
|
|
196
|
+
bulkCreateAndUpdateIntegrations,
|
|
197
|
+
};
|
|
@@ -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
|
+
};
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
const { getRedisData, setRedisData } = require("../../config/redis");
|
|
2
|
+
const Workspace = require("../../models/Workspace");
|
|
3
|
+
|
|
4
|
+
const CACHE_KEY_ALL = "workspaces_all";
|
|
5
|
+
|
|
6
|
+
const getCachedWorkspaces = async () => {
|
|
7
|
+
let workspaces = await getRedisData(CACHE_KEY_ALL);
|
|
8
|
+
if (!workspaces) {
|
|
9
|
+
workspaces = await Workspace.find().exec();
|
|
10
|
+
await setRedisData(CACHE_KEY_ALL, JSON.stringify(workspaces));
|
|
11
|
+
} else {
|
|
12
|
+
workspaces = JSON.parse(workspaces);
|
|
13
|
+
}
|
|
14
|
+
return workspaces;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const updateCachedWorkspaces = async () => {
|
|
18
|
+
const workspaces = await Workspace.find().exec();
|
|
19
|
+
await setRedisData(CACHE_KEY_ALL, JSON.stringify(workspaces));
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const createWorkspace = async (data) => {
|
|
23
|
+
const newWorkspace = new Workspace(data);
|
|
24
|
+
const savedWorkspace = await newWorkspace.save();
|
|
25
|
+
await updateCachedWorkspaces();
|
|
26
|
+
return savedWorkspace;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const findWorkspaceById = async (id, select = []) => {
|
|
30
|
+
const workspaces = await getCachedWorkspaces();
|
|
31
|
+
const workspace = workspaces.find((ws) => ws._id.toString() === id) || null;
|
|
32
|
+
|
|
33
|
+
if (workspace && select.length > 0) {
|
|
34
|
+
return select.reduce((result, field) => {
|
|
35
|
+
if (workspace[field] !== undefined) {
|
|
36
|
+
result[field] = workspace[field];
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}, {});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return workspace;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const findWorkspace = async (filter) => {
|
|
46
|
+
const workspaces = await getCachedWorkspaces();
|
|
47
|
+
return (
|
|
48
|
+
workspaces.find((ws) =>
|
|
49
|
+
Object.entries(filter).every(([key, value]) => ws[key] === value)
|
|
50
|
+
) || null
|
|
51
|
+
);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const findWorkspaceByIdAndQueueTracking = async (id) => {
|
|
55
|
+
const workspaces = await getCachedWorkspaces();
|
|
56
|
+
return (
|
|
57
|
+
workspaces.find((ws) => ws._id.toString() === id && ws.queueTracking) ||
|
|
58
|
+
null
|
|
59
|
+
);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const findAllWorkspaces = async (filter = {}) => {
|
|
63
|
+
const workspaces = await getCachedWorkspaces();
|
|
64
|
+
return workspaces.filter((ws) =>
|
|
65
|
+
Object.entries(filter).every(([key, value]) => ws[key] === value)
|
|
66
|
+
);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const updateWorkspace = async (id, data) => {
|
|
70
|
+
const updatedWorkspace = await Workspace.findByIdAndUpdate(id, data, {
|
|
71
|
+
new: true,
|
|
72
|
+
}).exec();
|
|
73
|
+
if (updatedWorkspace) {
|
|
74
|
+
await updateCachedWorkspaces();
|
|
75
|
+
}
|
|
76
|
+
return updatedWorkspace;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const deleteWorkspace = async (id) => {
|
|
80
|
+
const deletedWorkspace = await Workspace.findByIdAndDelete(id).exec();
|
|
81
|
+
if (deletedWorkspace) {
|
|
82
|
+
await updateCachedWorkspaces();
|
|
83
|
+
}
|
|
84
|
+
return deletedWorkspace;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
module.exports = {
|
|
88
|
+
createWorkspace,
|
|
89
|
+
findWorkspaceById,
|
|
90
|
+
findWorkspace,
|
|
91
|
+
findWorkspaceByIdAndQueueTracking,
|
|
92
|
+
findAllWorkspaces,
|
|
93
|
+
updateWorkspace,
|
|
94
|
+
deleteWorkspace,
|
|
95
|
+
};
|