shuttlepro-shared 1.3.14 → 1.3.16
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/chatMember.repository.js +9 -0
- package/common/repositories/customerProfile.repository.js +147 -0
- package/common/repositories/descriptionTemplates.repository.js +229 -0
- package/common/repositories/index.js +32 -0
- package/common/repositories/integration.repository.js +210 -0
- package/common/repositories/label.repository.js +95 -0
- package/common/repositories/notificationSettings.repository.js +95 -0
- package/common/repositories/role.repository.js +333 -0
- package/common/repositories/settings.repository.js +32 -0
- package/common/repositories/shipper.repository.js +77 -0
- package/common/repositories/socialMediaSetting.repository.js +33 -0
- package/common/repositories/user.repository.js +150 -0
- package/common/repositories/userPermission.repository.js +228 -0
- package/common/repositories/userRepository.js +31 -0
- package/common/repositories/userRole.repository.js +235 -0
- package/common/repositories/userRolePermission.repository.js +59 -0
- package/common/repositories/workspace.repository.js +147 -0
- package/config/bull.js +78 -0
- package/config/config.js +14 -0
- package/config/database.js +4 -0
- package/config/index.js +13 -0
- package/config/redis.js +212 -0
- package/config/socket.js +172 -0
- package/constants/index.js +15 -0
- package/index.js +8 -0
- package/models/AgentActivity.js +192 -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/ChatMember.js +17 -0
- package/models/Chatbot.js +20 -0
- package/models/Checkpoint.js +50 -0
- package/models/City.js +17 -0
- package/models/Column.js +28 -0
- package/models/Conversation.js +87 -0
- package/models/Customer.js +36 -0
- package/models/CustomerProfile.js +27 -0
- package/models/DefaultRolePermission.js +34 -0
- package/models/DescriptionTemplate.js +22 -0
- package/models/Integration.js +51 -0
- package/models/Label.js +42 -0
- package/models/Message.js +47 -0
- package/models/NewProduct.js +71 -0
- package/models/NotificationSettings.js +130 -0
- package/models/Order.js +236 -0
- package/models/OrderProduct.js +37 -0
- package/models/Profile.js +127 -0
- package/models/Report.js +27 -0
- package/models/Setting.js +18 -0
- package/models/Shipper.js +62 -0
- package/models/SocialMediaSetting.js +28 -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/UserRole.js +1 -119
- package/models/UserWorkflow.js +46 -0
- package/models/Website.js +0 -1
- package/models/Workspace.js +318 -0
- package/models.js +62 -1
- package/package.json +13 -2
- package/utils/decorator-factory.js +264 -0
- package/utils/logger.js +41 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
const Label = require("../../models/Label");
|
|
2
|
+
const {
|
|
3
|
+
setRedisData,
|
|
4
|
+
getRedisData,
|
|
5
|
+
deleteRedisData,
|
|
6
|
+
} = require("../../config/redis");
|
|
7
|
+
|
|
8
|
+
const CACHE_KEYS = {
|
|
9
|
+
ALL_LABELS: "labels:all",
|
|
10
|
+
WORKSPACE_LABELS: (workspaceId) => `labels:workspace:${workspaceId}`,
|
|
11
|
+
LABEL_DETAILS: (labelId) => `labels:details:${labelId}`,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const CACHE_EXPIRY = 60 * 60; // 1 hour
|
|
15
|
+
|
|
16
|
+
// Utility function for caching
|
|
17
|
+
const withCache = async (cacheKey, fetchFn, forceFresh = false) => {
|
|
18
|
+
if (!forceFresh) {
|
|
19
|
+
const cachedData = await getRedisData(cacheKey);
|
|
20
|
+
if (cachedData) return cachedData;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const data = await fetchFn();
|
|
24
|
+
if (data && data.length) {
|
|
25
|
+
await setRedisData(cacheKey, data, null, CACHE_EXPIRY);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return data;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// Core functions
|
|
32
|
+
const createLabel = async (labelData) => {
|
|
33
|
+
const existingLabels = await Label.find();
|
|
34
|
+
const isDuplicate = existingLabels.some(
|
|
35
|
+
(label) =>
|
|
36
|
+
label.title.toLowerCase() === labelData.title.toLowerCase() &&
|
|
37
|
+
label.workspaceId === labelData.workspaceId
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
if (isDuplicate) throw new Error("Label with this title already exists");
|
|
41
|
+
|
|
42
|
+
const newLabel = new Label(labelData);
|
|
43
|
+
return await newLabel.save();
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const findLabels = async (query = {}, options = {}) => {
|
|
47
|
+
return await withCache(JSON.stringify(query), () =>
|
|
48
|
+
Label.find(query)
|
|
49
|
+
.sort({ createdAt: -1 })
|
|
50
|
+
.limit(options.limit || 100)
|
|
51
|
+
.exec()
|
|
52
|
+
);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const findAllLabels = async (query = {}) => {
|
|
56
|
+
return await withCache(JSON.stringify(query), () => Label.find(query).exec());
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const findLabelById = async (labelId) => {
|
|
60
|
+
return await withCache(CACHE_KEYS.LABEL_DETAILS(labelId), () =>
|
|
61
|
+
Label.findById(labelId).exec()
|
|
62
|
+
);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const findLabel = async (query = {}) => {
|
|
66
|
+
return await Label.findOne(query).exec();
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const updateLabel = async (labelId, updateData) => {
|
|
70
|
+
const updatedLabel = await Label.findByIdAndUpdate(labelId, updateData, {
|
|
71
|
+
new: true,
|
|
72
|
+
});
|
|
73
|
+
await setRedisData(
|
|
74
|
+
CACHE_KEYS.LABEL_DETAILS(labelId),
|
|
75
|
+
updatedLabel.toObject(),
|
|
76
|
+
null,
|
|
77
|
+
CACHE_EXPIRY
|
|
78
|
+
);
|
|
79
|
+
return updatedLabel;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const deleteLabel = async (labelId) => {
|
|
83
|
+
await deleteRedisData(CACHE_KEYS.LABEL_DETAILS(labelId));
|
|
84
|
+
return await Label.findByIdAndDelete(labelId);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
module.exports = {
|
|
88
|
+
createLabel,
|
|
89
|
+
findLabels,
|
|
90
|
+
findAllLabels,
|
|
91
|
+
findLabelById,
|
|
92
|
+
findLabel,
|
|
93
|
+
updateLabel,
|
|
94
|
+
deleteLabel,
|
|
95
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
const { getRedisData, setRedisData } = require("../../config/redis");
|
|
2
|
+
|
|
3
|
+
const NotificationSettings = require("../../models/NotificationSettings");
|
|
4
|
+
|
|
5
|
+
const getNotificationFromCache = async (workspaceId, userId) => {
|
|
6
|
+
let notificationSettings = await getRedisData(
|
|
7
|
+
`notificationSettings-${userId}`
|
|
8
|
+
);
|
|
9
|
+
if (!notificationSettings) {
|
|
10
|
+
notificationSettings = await NotificationSettings.findOne({
|
|
11
|
+
workspaceId,
|
|
12
|
+
createdBy: userId,
|
|
13
|
+
});
|
|
14
|
+
await setRedisData(`notificationSettings-${userId}`, notificationSettings);
|
|
15
|
+
}
|
|
16
|
+
return notificationSettings;
|
|
17
|
+
};
|
|
18
|
+
const getUserNotificationSettings = async (workspaceId, userId) => {
|
|
19
|
+
return await getNotificationFromCache(workspaceId, userId);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const defaultNotifications = async (workspaceId, userId = null) => {
|
|
23
|
+
try {
|
|
24
|
+
const obj = {
|
|
25
|
+
workspaceId: workspaceId,
|
|
26
|
+
products: true,
|
|
27
|
+
orders: {
|
|
28
|
+
createOrder: true,
|
|
29
|
+
orderAssign: true,
|
|
30
|
+
orderComment: true,
|
|
31
|
+
},
|
|
32
|
+
default: true,
|
|
33
|
+
conversation: {
|
|
34
|
+
instaMessage: true,
|
|
35
|
+
fbMessage: true,
|
|
36
|
+
instaComment: true,
|
|
37
|
+
fbComment: true,
|
|
38
|
+
email: true,
|
|
39
|
+
whatsapp: true,
|
|
40
|
+
},
|
|
41
|
+
socialProfiles: {
|
|
42
|
+
socialProfileComment: true,
|
|
43
|
+
story: true,
|
|
44
|
+
},
|
|
45
|
+
taskManager: {
|
|
46
|
+
cardAssign: true,
|
|
47
|
+
cardMove: true,
|
|
48
|
+
},
|
|
49
|
+
sound: {
|
|
50
|
+
play: true,
|
|
51
|
+
},
|
|
52
|
+
createdBy: userId,
|
|
53
|
+
};
|
|
54
|
+
return await NotificationSettings.create(obj);
|
|
55
|
+
} catch (err) {
|
|
56
|
+
console.log(err, "err");
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const updateUserNotificationSettings = async (body, userId) => {
|
|
61
|
+
try {
|
|
62
|
+
const { type } = body;
|
|
63
|
+
let { value } = body;
|
|
64
|
+
|
|
65
|
+
if (type === "default" || type === "products") {
|
|
66
|
+
value = value?.all || false;
|
|
67
|
+
} else {
|
|
68
|
+
delete value?.all;
|
|
69
|
+
}
|
|
70
|
+
let notificationSettings = await NotificationSettings.findOne({
|
|
71
|
+
workspaceId: body.workspaceId,
|
|
72
|
+
createdBy: userId,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
if (notificationSettings) {
|
|
76
|
+
notificationSettings[type] = value;
|
|
77
|
+
const updatedSettings = await notificationSettings.save();
|
|
78
|
+
await setRedisData(`notificationSettings-${userId}`, updatedSettings);
|
|
79
|
+
return updatedSettings;
|
|
80
|
+
} else {
|
|
81
|
+
const newSettings = await defaultNotifications(body.workspaceId, userId);
|
|
82
|
+
|
|
83
|
+
await setRedisData(`notificationSettings-${userId}`, newSettings);
|
|
84
|
+
return newSettings;
|
|
85
|
+
}
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.log(error, "err");
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
module.exports = {
|
|
92
|
+
getUserNotificationSettings,
|
|
93
|
+
updateUserNotificationSettings,
|
|
94
|
+
defaultNotifications,
|
|
95
|
+
};
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
const Role = require("../../models/Role");
|
|
2
|
+
const { getRedisData, setRedisData } = require("../../config/redis");
|
|
3
|
+
|
|
4
|
+
// Cache keys
|
|
5
|
+
const ROLES = "roles";
|
|
6
|
+
const DEFAULT_ROLES = "default_roles";
|
|
7
|
+
const CACHE_TIME = 3600 * 2;
|
|
8
|
+
|
|
9
|
+
// Update the cache for workspace roles
|
|
10
|
+
const updateCachedRoles = async (workspaceId) => {
|
|
11
|
+
try {
|
|
12
|
+
const roles = await Role.find({ workspaceId })
|
|
13
|
+
.populate("permissionId")
|
|
14
|
+
.lean()
|
|
15
|
+
.exec();
|
|
16
|
+
await setRedisData(workspaceId, roles, ROLES, CACHE_TIME);
|
|
17
|
+
return roles;
|
|
18
|
+
} catch (error) {
|
|
19
|
+
console.error("Error updating cached roles:", error);
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// Get roles from cache or database for a specific workspace
|
|
25
|
+
const getRoles = async (workspaceId) => {
|
|
26
|
+
let roles = await getRedisData(workspaceId, ROLES);
|
|
27
|
+
if (!roles || roles?.length === 0) {
|
|
28
|
+
roles = await Role.find({ workspaceId })
|
|
29
|
+
.populate("permissionId")
|
|
30
|
+
.lean()
|
|
31
|
+
.exec();
|
|
32
|
+
await setRedisData(workspaceId, roles, ROLES, CACHE_TIME);
|
|
33
|
+
}
|
|
34
|
+
return roles;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Update default roles cache (system-wide roles with no workspace)
|
|
38
|
+
const updateCachedDefaultRoles = async () => {
|
|
39
|
+
try {
|
|
40
|
+
const data = await Role.find({ name: { $ne: "super" }, workspaceId: null })
|
|
41
|
+
.select("name")
|
|
42
|
+
.sort({ _id: -1 })
|
|
43
|
+
.lean()
|
|
44
|
+
.exec();
|
|
45
|
+
|
|
46
|
+
const defaultRoles = data.map((d) => {
|
|
47
|
+
return {
|
|
48
|
+
id: d?._id,
|
|
49
|
+
name: d?.name,
|
|
50
|
+
};
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
await setRedisData("system", defaultRoles, DEFAULT_ROLES, CACHE_TIME);
|
|
54
|
+
return defaultRoles;
|
|
55
|
+
} catch (error) {
|
|
56
|
+
console.error("Error updating cached default roles:", error);
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// Get default roles from cache or database
|
|
62
|
+
const getDefaultRoles = async () => {
|
|
63
|
+
let defaultRoles = await getRedisData("system", DEFAULT_ROLES);
|
|
64
|
+
if (!defaultRoles || defaultRoles?.length === 0) {
|
|
65
|
+
const data = await Role.find({ name: { $ne: "super" }, workspaceId: null })
|
|
66
|
+
.select("name")
|
|
67
|
+
.sort({ _id: -1 })
|
|
68
|
+
.lean()
|
|
69
|
+
.exec();
|
|
70
|
+
|
|
71
|
+
defaultRoles = data.map((d) => {
|
|
72
|
+
return {
|
|
73
|
+
id: d?._id,
|
|
74
|
+
name: d?.name,
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
await setRedisData("system", defaultRoles, DEFAULT_ROLES, CACHE_TIME);
|
|
79
|
+
}
|
|
80
|
+
return defaultRoles;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const createRole = async (roleData) => {
|
|
84
|
+
const newRole = await Role.create(roleData);
|
|
85
|
+
const createdRole = await Role.findById(newRole._id)
|
|
86
|
+
.populate("permissionId")
|
|
87
|
+
.lean()
|
|
88
|
+
.exec();
|
|
89
|
+
|
|
90
|
+
if (createdRole.workspaceId) {
|
|
91
|
+
await updateCachedRoles(createdRole.workspaceId);
|
|
92
|
+
} else {
|
|
93
|
+
// If it's a default/system role
|
|
94
|
+
await updateCachedDefaultRoles();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return createdRole;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const findRoleById = async (id, workspaceId) => {
|
|
101
|
+
const roles = await getRoles(workspaceId);
|
|
102
|
+
const role = roles.find((r) => r._id.toString() === id);
|
|
103
|
+
|
|
104
|
+
if (role) return role;
|
|
105
|
+
|
|
106
|
+
// If not found in cache, query directly
|
|
107
|
+
const freshRole = await Role.findById(id)
|
|
108
|
+
.populate("permissionId")
|
|
109
|
+
.lean()
|
|
110
|
+
.exec();
|
|
111
|
+
if (freshRole && freshRole.workspaceId?.toString() === workspaceId) {
|
|
112
|
+
// Update the cache with the new data
|
|
113
|
+
await updateCachedRoles(workspaceId);
|
|
114
|
+
}
|
|
115
|
+
return freshRole;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const findSingleRole = async (filter) => {
|
|
119
|
+
const workspaceId = filter.workspaceId;
|
|
120
|
+
if (workspaceId) {
|
|
121
|
+
const roles = await getRoles(workspaceId);
|
|
122
|
+
const role = roles.find((r) =>
|
|
123
|
+
Object.entries(filter).every(([key, value]) => r[key] === value)
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
if (role) return role;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// If not found in cache or no workspaceId provided, query directly
|
|
130
|
+
const freshRole = await Role.findOne(filter)
|
|
131
|
+
.populate("permissionId")
|
|
132
|
+
.lean()
|
|
133
|
+
.exec();
|
|
134
|
+
if (freshRole && freshRole.workspaceId) {
|
|
135
|
+
// Update the cache with the new data
|
|
136
|
+
await updateCachedRoles(freshRole.workspaceId);
|
|
137
|
+
}
|
|
138
|
+
return freshRole;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const findRolesByPagination = async (body) => {
|
|
142
|
+
let { page, limit, workspaceId, query, status } = body;
|
|
143
|
+
|
|
144
|
+
page = parseInt(page, 10) || 1;
|
|
145
|
+
limit = parseInt(limit, 10) || 10;
|
|
146
|
+
|
|
147
|
+
const skip = (page - 1) * limit;
|
|
148
|
+
|
|
149
|
+
let obj = { workspaceId };
|
|
150
|
+
if (query) {
|
|
151
|
+
obj.name = { $regex: query, $options: "i" };
|
|
152
|
+
}
|
|
153
|
+
if (status) {
|
|
154
|
+
obj.status = status;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const totalRoles = await Role.countDocuments(obj);
|
|
158
|
+
const totalPages = Math.ceil(totalRoles / limit);
|
|
159
|
+
|
|
160
|
+
const roles = await Role.find(obj)
|
|
161
|
+
.sort({ _id: -1 })
|
|
162
|
+
.skip(skip)
|
|
163
|
+
.limit(limit)
|
|
164
|
+
.populate("permissionId")
|
|
165
|
+
.lean()
|
|
166
|
+
.exec();
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
totalPages,
|
|
170
|
+
currentPage: page,
|
|
171
|
+
roles,
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const findRoles = async (workspaceId) => {
|
|
176
|
+
return await getRoles(workspaceId);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const findDefaultRoles = async () => {
|
|
180
|
+
return await getDefaultRoles();
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const updateRoleById = async (id, updateData) => {
|
|
184
|
+
const updateQuery = {
|
|
185
|
+
...updateData,
|
|
186
|
+
...(Array.isArray(updateData.logs) && {
|
|
187
|
+
$push: { logs: { $each: updateData.logs } },
|
|
188
|
+
}),
|
|
189
|
+
};
|
|
190
|
+
delete updateQuery.logs;
|
|
191
|
+
const updatedRole = await Role.findByIdAndUpdate(id, updateQuery, {
|
|
192
|
+
new: true,
|
|
193
|
+
})
|
|
194
|
+
.populate("permissionId")
|
|
195
|
+
.lean()
|
|
196
|
+
.exec();
|
|
197
|
+
if (updatedRole) {
|
|
198
|
+
await updateCachedRoles(updatedRole?.workspaceId);
|
|
199
|
+
}
|
|
200
|
+
return updatedRole;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const updateRoleByFilter = async (filter, updateData) => {
|
|
204
|
+
const updatedRole = await Role.findOneAndUpdate(filter, updateData, {
|
|
205
|
+
new: true,
|
|
206
|
+
})
|
|
207
|
+
.populate("permissionId")
|
|
208
|
+
.lean()
|
|
209
|
+
.exec();
|
|
210
|
+
if (updatedRole?.workspaceId) {
|
|
211
|
+
await updateCachedRoles(updatedRole?.workspaceId?.toString());
|
|
212
|
+
}
|
|
213
|
+
return updatedRole;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const updateRolesByFilter = async (filter, updateData) => {
|
|
217
|
+
const result = await Role.updateMany(filter, updateData, { new: true });
|
|
218
|
+
if (result.matchedCount > 0) {
|
|
219
|
+
if (filter.workspaceId) {
|
|
220
|
+
await updateCachedRoles(filter.workspaceId);
|
|
221
|
+
} else {
|
|
222
|
+
if (!filter.workspaceId || filter.workspaceId === null) {
|
|
223
|
+
await updateCachedDefaultRoles();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return result;
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const addLogToRole = async (id, logData) => {
|
|
231
|
+
const role = await Role.findById(id).lean().exec();
|
|
232
|
+
const workspaceId = role?.workspaceId;
|
|
233
|
+
const updatedRole = await Role.findByIdAndUpdate(
|
|
234
|
+
id,
|
|
235
|
+
{ $push: { logs: logData } },
|
|
236
|
+
{ new: true }
|
|
237
|
+
)
|
|
238
|
+
.populate("permissionId")
|
|
239
|
+
.lean()
|
|
240
|
+
.exec();
|
|
241
|
+
|
|
242
|
+
if (updatedRole && workspaceId) {
|
|
243
|
+
await updateCachedRoles(workspaceId);
|
|
244
|
+
}
|
|
245
|
+
return updatedRole;
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const clearRoleLogs = async (id) => {
|
|
249
|
+
const role = await Role.findById(id).lean().exec();
|
|
250
|
+
const workspaceId = role?.workspaceId;
|
|
251
|
+
const updatedRole = await Role.findByIdAndUpdate(
|
|
252
|
+
id,
|
|
253
|
+
{ $set: { logs: [] } },
|
|
254
|
+
{ new: true }
|
|
255
|
+
)
|
|
256
|
+
.populate("permissionId")
|
|
257
|
+
.lean()
|
|
258
|
+
.exec();
|
|
259
|
+
if (updatedRole && workspaceId) {
|
|
260
|
+
await updateCachedRoles(workspaceId);
|
|
261
|
+
}
|
|
262
|
+
return updatedRole;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
const addPermissionToRole = async (id, permission) => {
|
|
266
|
+
const role = await Role.findById(id).lean().exec();
|
|
267
|
+
const workspaceId = role?.workspaceId;
|
|
268
|
+
const updatedRole = await Role.findByIdAndUpdate(
|
|
269
|
+
id,
|
|
270
|
+
{ $push: { permissions: permission } },
|
|
271
|
+
{ new: true }
|
|
272
|
+
)
|
|
273
|
+
.populate("permissionId")
|
|
274
|
+
.lean()
|
|
275
|
+
.exec();
|
|
276
|
+
if (updatedRole && workspaceId) {
|
|
277
|
+
await updateCachedRoles(workspaceId);
|
|
278
|
+
}
|
|
279
|
+
return updatedRole;
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
const removePermissionFromRole = async (id, permission) => {
|
|
283
|
+
const role = await Role.findById(id).lean().exec();
|
|
284
|
+
const workspaceId = role?.workspaceId;
|
|
285
|
+
|
|
286
|
+
const updatedRole = await Role.findByIdAndUpdate(
|
|
287
|
+
id,
|
|
288
|
+
{ $pull: { permissions: permission } },
|
|
289
|
+
{ new: true }
|
|
290
|
+
)
|
|
291
|
+
.populate("permissionId")
|
|
292
|
+
.lean()
|
|
293
|
+
.exec();
|
|
294
|
+
|
|
295
|
+
if (updatedRole && workspaceId) {
|
|
296
|
+
await updateCachedRoles(workspaceId);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return updatedRole;
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
const deleteRoleById = async (id, workspaceId) => {
|
|
303
|
+
return await Role.deleteOne({ _id: id }).exec();
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const deleteSingleRole = async (filter) => {
|
|
307
|
+
return await Role.deleteOne(filter).exec();
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
const deleteManyRoles = async (filter) => {
|
|
311
|
+
return await Role.deleteMany(filter);
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
module.exports = {
|
|
315
|
+
createRole,
|
|
316
|
+
findRoleById,
|
|
317
|
+
findSingleRole,
|
|
318
|
+
findRoles,
|
|
319
|
+
updateRoleById,
|
|
320
|
+
updateRoleByFilter,
|
|
321
|
+
updateRolesByFilter,
|
|
322
|
+
addLogToRole,
|
|
323
|
+
clearRoleLogs,
|
|
324
|
+
addPermissionToRole,
|
|
325
|
+
removePermissionFromRole,
|
|
326
|
+
deleteRoleById,
|
|
327
|
+
deleteSingleRole,
|
|
328
|
+
deleteManyRoles,
|
|
329
|
+
findRolesByPagination,
|
|
330
|
+
findDefaultRoles,
|
|
331
|
+
updateCachedRoles,
|
|
332
|
+
updateCachedDefaultRoles,
|
|
333
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const Setting = require("../../models/Setting");
|
|
2
|
+
|
|
3
|
+
const createSetting = async (data) => {
|
|
4
|
+
const newSetting = new Setting(data);
|
|
5
|
+
const savedSetting = await newSetting.save();
|
|
6
|
+
return savedSetting;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const findSetting = async (filter) => {
|
|
10
|
+
return await Setting.findOne(filter).exec();
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const updateSetting = async (id, data) => {
|
|
14
|
+
const updatedSetting = await Setting.findByIdAndUpdate(id, data, {
|
|
15
|
+
new: true,
|
|
16
|
+
}).exec();
|
|
17
|
+
|
|
18
|
+
return updatedSetting;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const deleteSetting = async (id) => {
|
|
22
|
+
const deletedSetting = await Setting.findByIdAndDelete(id).exec();
|
|
23
|
+
|
|
24
|
+
return deletedSetting;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
module.exports = {
|
|
28
|
+
createSetting,
|
|
29
|
+
findSetting,
|
|
30
|
+
updateSetting,
|
|
31
|
+
deleteSetting,
|
|
32
|
+
};
|
|
@@ -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,33 @@
|
|
|
1
|
+
const SocialMediaSetting = require("../../models/SocialMediaSetting");
|
|
2
|
+
|
|
3
|
+
const createSocialMediaSetting = async (data) => {
|
|
4
|
+
const setting = new SocialMediaSetting(data);
|
|
5
|
+
const saved = await setting.save();
|
|
6
|
+
|
|
7
|
+
return saved;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const findSocialMediaSetting = async (filter = {}) => {
|
|
11
|
+
return await SocialMediaSetting.findOne(filter).exec();
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const updateSocialMediaSetting = async (id, data) => {
|
|
15
|
+
const updated = await SocialMediaSetting.findByIdAndUpdate(id, data, {
|
|
16
|
+
new: true,
|
|
17
|
+
}).exec();
|
|
18
|
+
|
|
19
|
+
return updated;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const deleteSocialMediaSetting = async (id) => {
|
|
23
|
+
const deleted = await SocialMediaSetting.findByIdAndDelete(id).exec();
|
|
24
|
+
|
|
25
|
+
return deleted;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
module.exports = {
|
|
29
|
+
createSocialMediaSetting,
|
|
30
|
+
findSocialMediaSetting,
|
|
31
|
+
updateSocialMediaSetting,
|
|
32
|
+
deleteSocialMediaSetting,
|
|
33
|
+
};
|