shuttlepro-shared 1.3.13 → 1.3.14
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/constants/index.js +0 -15
- package/index.js +0 -8
- package/models/UserRole.js +119 -1
- package/models/Website.js +1 -0
- package/models.js +1 -62
- package/package.json +2 -13
- package/common/repositories/chatMember.repository.js +0 -9
- package/common/repositories/customerProfile.repository.js +0 -147
- package/common/repositories/descriptionTemplates.repository.js +0 -229
- package/common/repositories/index.js +0 -32
- package/common/repositories/integration.repository.js +0 -210
- package/common/repositories/label.repository.js +0 -95
- package/common/repositories/notificationSettings.repository.js +0 -95
- package/common/repositories/role.repository.js +0 -333
- package/common/repositories/settings.repository.js +0 -32
- package/common/repositories/shipper.repository.js +0 -77
- package/common/repositories/socialMediaSetting.repository.js +0 -33
- package/common/repositories/user.repository.js +0 -150
- package/common/repositories/userPermission.repository.js +0 -228
- package/common/repositories/userRepository.js +0 -31
- package/common/repositories/userRole.repository.js +0 -235
- package/common/repositories/userRolePermission.repository.js +0 -59
- package/common/repositories/workspace.repository.js +0 -147
- package/config/bull.js +0 -78
- package/config/config.js +0 -14
- package/config/database.js +0 -4
- package/config/index.js +0 -13
- package/config/redis.js +0 -196
- package/config/socket.js +0 -172
- package/models/AgentActivity.js +0 -192
- package/models/Assignment.js +0 -23
- package/models/BusinessDistribution.js +0 -23
- package/models/Card.js +0 -144
- package/models/CardComments.js +0 -33
- package/models/ChatMember.js +0 -17
- package/models/Chatbot.js +0 -20
- package/models/Checkpoint.js +0 -50
- package/models/City.js +0 -17
- package/models/Column.js +0 -28
- package/models/Conversation.js +0 -87
- package/models/Customer.js +0 -36
- package/models/CustomerProfile.js +0 -27
- package/models/DefaultRolePermission.js +0 -34
- package/models/DescriptionTemplate.js +0 -22
- package/models/Integration.js +0 -51
- package/models/Label.js +0 -42
- package/models/Message.js +0 -47
- package/models/NewProduct.js +0 -71
- package/models/NotificationSettings.js +0 -130
- package/models/Order.js +0 -236
- package/models/OrderProduct.js +0 -37
- package/models/Profile.js +0 -127
- package/models/Report.js +0 -27
- package/models/Setting.js +0 -18
- package/models/Shipper.js +0 -62
- package/models/SocialMediaSetting.js +0 -28
- package/models/Status.js +0 -58
- package/models/StatusType.js +0 -10
- package/models/Step.js +0 -50
- package/models/Type.js +0 -25
- package/models/UserWorkflow.js +0 -46
- package/models/Workspace.js +0 -308
- package/utils/decorator-factory.js +0 -264
- package/utils/logger.js +0 -41
package/constants/index.js
CHANGED
|
@@ -23,18 +23,3 @@ exports.GenericMessages = {
|
|
|
23
23
|
URL_FETCH_SUCCESS: "Url Fetched successfully!",
|
|
24
24
|
WORKSPACE_NOT_FOUND: "Workspace not found!",
|
|
25
25
|
};
|
|
26
|
-
|
|
27
|
-
exports.DYNAMIC_FIELD_TYPES = [
|
|
28
|
-
"text",
|
|
29
|
-
"autocomplete",
|
|
30
|
-
"radio",
|
|
31
|
-
"text area",
|
|
32
|
-
"number",
|
|
33
|
-
"date",
|
|
34
|
-
"time",
|
|
35
|
-
"email",
|
|
36
|
-
"range",
|
|
37
|
-
"url",
|
|
38
|
-
"colour",
|
|
39
|
-
"file",
|
|
40
|
-
];
|
package/index.js
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
1
|
const sharedModels = require("./models");
|
|
2
2
|
const sharedFunctions = require("./functions");
|
|
3
|
-
const logger = require("./utils/logger");
|
|
4
|
-
const shuttlePro = require("./utils/decorator-factory");
|
|
5
|
-
const configs = require("./config");
|
|
6
|
-
const repositories = require("./common/repositories");
|
|
7
3
|
|
|
8
4
|
module.exports = {
|
|
9
5
|
sharedModels,
|
|
10
6
|
sharedFunctions,
|
|
11
|
-
logger,
|
|
12
|
-
shuttlePro,
|
|
13
|
-
configs,
|
|
14
|
-
repositories,
|
|
15
7
|
};
|
package/models/UserRole.js
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
const mongoose = require("mongoose");
|
|
2
2
|
const { Schema } = mongoose;
|
|
3
3
|
|
|
4
|
+
const daySchema = new Schema({
|
|
5
|
+
day: { type: String, required: true },
|
|
6
|
+
startTime: { type: String, required: false },
|
|
7
|
+
endTime: { type: String, required: false },
|
|
8
|
+
breakStartTime: { type: String, required: false },
|
|
9
|
+
breakEndTime: { type: String, required: false },
|
|
10
|
+
lateThreshold: { type: String, required: false },
|
|
11
|
+
});
|
|
12
|
+
|
|
4
13
|
const userRoleSchema = new Schema(
|
|
5
14
|
{
|
|
6
15
|
userId: {
|
|
@@ -23,7 +32,10 @@ const userRoleSchema = new Schema(
|
|
|
23
32
|
default: null,
|
|
24
33
|
},
|
|
25
34
|
isOwner: { type: Boolean, default: false },
|
|
26
|
-
userShift: {
|
|
35
|
+
userShift: {
|
|
36
|
+
shiftName: { type: String, required: false },
|
|
37
|
+
days: { type: [daySchema], required: false },
|
|
38
|
+
},
|
|
27
39
|
createdBy: {
|
|
28
40
|
type: mongoose.Schema.Types.ObjectId,
|
|
29
41
|
ref: "User",
|
|
@@ -40,6 +52,112 @@ const userRoleSchema = new Schema(
|
|
|
40
52
|
|
|
41
53
|
const UserRole = mongoose.model("UserRole", userRoleSchema);
|
|
42
54
|
|
|
55
|
+
const convertPermissionsArrayToObject = (originalArray) => {
|
|
56
|
+
return originalArray?.reduce((acc, item) => {
|
|
57
|
+
const { id, actions, integrationPermissions, ...extras } = item;
|
|
58
|
+
const formattedExtras = {};
|
|
59
|
+
if (extras.deletable !== undefined) {
|
|
60
|
+
formattedExtras.deletable = extras.deletable;
|
|
61
|
+
}
|
|
62
|
+
if (extras.createUpdateable !== undefined) {
|
|
63
|
+
formattedExtras.createUpdateable = extras.createUpdateable;
|
|
64
|
+
}
|
|
65
|
+
acc[id] = {
|
|
66
|
+
actions: Object.keys(actions).filter((action) => actions[action]),
|
|
67
|
+
...formattedExtras,
|
|
68
|
+
...(integrationPermissions && {
|
|
69
|
+
integrationPermissions: Object.entries(integrationPermissions)
|
|
70
|
+
.filter(([_, allowed]) => allowed)
|
|
71
|
+
.map(([key]) => key),
|
|
72
|
+
}),
|
|
73
|
+
};
|
|
74
|
+
return acc;
|
|
75
|
+
}, {});
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const getUserRoleWithPermissions = async (workspaceId, userId) => {
|
|
79
|
+
try {
|
|
80
|
+
let data = await UserRole.findOne({
|
|
81
|
+
workspaceId: workspaceId,
|
|
82
|
+
userId: userId,
|
|
83
|
+
})
|
|
84
|
+
.populate({
|
|
85
|
+
path: "roleId",
|
|
86
|
+
select:
|
|
87
|
+
"id name userId userShift permissionId modulePermissions defaultModule",
|
|
88
|
+
populate: [{ path: "permissionId", select: "id name" }],
|
|
89
|
+
})
|
|
90
|
+
.lean()
|
|
91
|
+
.exec();
|
|
92
|
+
return {
|
|
93
|
+
...data,
|
|
94
|
+
roleId: data?.roleId?._id?.toString() || "",
|
|
95
|
+
role: data?.roleId?.name || "",
|
|
96
|
+
permissions:
|
|
97
|
+
convertPermissionsArrayToObject(
|
|
98
|
+
data?.roleId?.modulePermissions?.modules
|
|
99
|
+
) || [],
|
|
100
|
+
parentRole: data?.roleId?.permissionId?.name || "",
|
|
101
|
+
parentRoleId: data?.roleId?.permissionId?._id?.toString() || "",
|
|
102
|
+
defaultModule: data?.roleId?.defaultModule || "",
|
|
103
|
+
};
|
|
104
|
+
} catch (err) {
|
|
105
|
+
console.error("Error in getUserRole:", err);
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const getUserPermissions = async (workspaceId, userId) => {
|
|
111
|
+
try {
|
|
112
|
+
let data = await UserRole.findOne({
|
|
113
|
+
workspaceId: workspaceId,
|
|
114
|
+
userId: userId,
|
|
115
|
+
})
|
|
116
|
+
.select("roleId userId")
|
|
117
|
+
.populate({
|
|
118
|
+
path: "roleId",
|
|
119
|
+
select: "modulePermissions",
|
|
120
|
+
})
|
|
121
|
+
.lean()
|
|
122
|
+
.exec();
|
|
123
|
+
return (
|
|
124
|
+
convertPermissionsArrayToObject(
|
|
125
|
+
data?.roleId?.modulePermissions?.modules
|
|
126
|
+
) || null
|
|
127
|
+
);
|
|
128
|
+
} catch (err) {
|
|
129
|
+
console.error("Error in getUserRole:", err);
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const getChannelsPermissions = async (workspaceId, userId) => {
|
|
135
|
+
try {
|
|
136
|
+
let data = await UserRole.findOne({
|
|
137
|
+
workspaceId: workspaceId,
|
|
138
|
+
userId: userId,
|
|
139
|
+
})
|
|
140
|
+
.select("roleId userId")
|
|
141
|
+
.populate({
|
|
142
|
+
path: "roleId",
|
|
143
|
+
select: "modulePermissions",
|
|
144
|
+
})
|
|
145
|
+
.lean()
|
|
146
|
+
.exec();
|
|
147
|
+
return (
|
|
148
|
+
convertPermissionsArrayToObject(
|
|
149
|
+
data?.roleId?.modulePermissions?.modules
|
|
150
|
+
)?.["conversations"]?.["integrationPermissions"] || []
|
|
151
|
+
);
|
|
152
|
+
} catch (err) {
|
|
153
|
+
console.error("Error in getUserRole:", err);
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
43
158
|
module.exports = {
|
|
44
159
|
UserRole,
|
|
160
|
+
getChannelsPermissions,
|
|
161
|
+
getUserPermissions,
|
|
162
|
+
getUserRoleWithPermissions,
|
|
45
163
|
};
|
package/models/Website.js
CHANGED
|
@@ -29,6 +29,7 @@ const websiteSchema = new mongoose.Schema(
|
|
|
29
29
|
},
|
|
30
30
|
importStatus: { type: Boolean, default: false },
|
|
31
31
|
mode: { type: String, default: "read" },
|
|
32
|
+
details: { type: mongoose.Schema.Types.Mixed, default: {} },
|
|
32
33
|
},
|
|
33
34
|
{ timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } }
|
|
34
35
|
);
|
package/models.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
const Website = require("./models/Website");
|
|
2
|
-
const ChatMember = require("./models/ChatMember");
|
|
3
|
-
const DescriptionTemplate = require("./models/DescriptionTemplate");
|
|
4
2
|
const ProductQueue = require("./models/ProductQueue");
|
|
5
3
|
const ColumnPreference = require("./models/ColumnPreference");
|
|
6
4
|
const UserPermission = require("./models/UserPermission");
|
|
@@ -14,36 +12,8 @@ const AutoSchedulerSchema = require("./models/AutoScheduler");
|
|
|
14
12
|
const ActivityLogs = require("./models/ActivityLogs");
|
|
15
13
|
const UserRole = require("./models/UserRole");
|
|
16
14
|
const OrderQueue = require("./models/OrderQueue");
|
|
17
|
-
const AgentActivity = require("./models/AgentActivity");
|
|
18
|
-
const Assignment = require("./models/Assignment");
|
|
19
|
-
const BusinessDistribution = require("./models/BusinessDistribution");
|
|
20
|
-
const CardComments = require("./models/CardComments");
|
|
21
|
-
const Checkpoint = require("./models/Checkpoint");
|
|
22
|
-
const City = require("./models/City");
|
|
23
|
-
const Column = require("./models/Column");
|
|
24
|
-
const Conversation = require("./models/Conversation");
|
|
25
|
-
const Customer = require("./models/Customer");
|
|
26
|
-
const Integration = require("./models/Integration");
|
|
27
|
-
const Label = require("./models/Label");
|
|
28
|
-
const Message = require("./models/Message");
|
|
29
|
-
const Order = require("./models/Order");
|
|
30
|
-
const OrderProduct = require("./models/OrderProduct");
|
|
31
|
-
const Report = require("./models/Report");
|
|
32
|
-
const Shipper = require("./models/Shipper");
|
|
33
|
-
const Status = require("./models/Status");
|
|
34
|
-
const StatusType = require("./models/StatusType");
|
|
35
|
-
const Type = require("./models/Type");
|
|
36
|
-
const Workspace = require("./models/Workspace");
|
|
37
|
-
const Card = require("./models/Card");
|
|
38
|
-
const Profile = require("./models/Profile");
|
|
39
|
-
const Chatbot = require("./models/Chatbot");
|
|
40
|
-
const Step = require("./models/Step");
|
|
41
15
|
const Role = require("./models/Role");
|
|
42
|
-
|
|
43
|
-
const NotificationSettings = require("./models/NotificationSettings");
|
|
44
|
-
const UserWorkflow = require("./models/UserWorkflow");
|
|
45
|
-
const Setting = require("./models/Setting");
|
|
46
|
-
const CustomerProfile = require("./models/CustomerProfile");
|
|
16
|
+
|
|
47
17
|
module.exports = {
|
|
48
18
|
Website,
|
|
49
19
|
ProductQueue,
|
|
@@ -59,36 +29,5 @@ module.exports = {
|
|
|
59
29
|
ActivityLogs,
|
|
60
30
|
UserRole,
|
|
61
31
|
OrderQueue,
|
|
62
|
-
AgentActivity,
|
|
63
|
-
Assignment,
|
|
64
|
-
BusinessDistribution,
|
|
65
|
-
CardComments,
|
|
66
|
-
Checkpoint,
|
|
67
|
-
Column,
|
|
68
|
-
Conversation,
|
|
69
|
-
City,
|
|
70
|
-
Customer,
|
|
71
|
-
Integration,
|
|
72
|
-
Label,
|
|
73
|
-
Message,
|
|
74
|
-
Order,
|
|
75
|
-
OrderProduct,
|
|
76
|
-
Report,
|
|
77
|
-
Shipper,
|
|
78
|
-
Status,
|
|
79
|
-
Type,
|
|
80
|
-
Workspace,
|
|
81
|
-
StatusType,
|
|
82
|
-
Card,
|
|
83
|
-
DescriptionTemplate,
|
|
84
|
-
Profile,
|
|
85
|
-
ChatMember,
|
|
86
|
-
Chatbot,
|
|
87
|
-
Step,
|
|
88
32
|
Role,
|
|
89
|
-
NewProduct,
|
|
90
|
-
NotificationSettings,
|
|
91
|
-
UserWorkflow,
|
|
92
|
-
Setting,
|
|
93
|
-
CustomerProfile,
|
|
94
33
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "shuttlepro-shared",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.14",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -11,18 +11,7 @@
|
|
|
11
11
|
"license": "ISC",
|
|
12
12
|
"access": "restricted",
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@babel/core": "^7.22.5",
|
|
15
|
-
"@babel/plugin-proposal-class-properties": "^7.18.6",
|
|
16
|
-
"@babel/plugin-proposal-decorators": "^7.22.5",
|
|
17
|
-
"@babel/preset-env": "^7.22.5",
|
|
18
|
-
"@babel/register": "^7.22.5",
|
|
19
|
-
"bull": "^4.10.4",
|
|
20
|
-
"cors": "^2.8.5",
|
|
21
|
-
"express": "^4.17.1",
|
|
22
|
-
"helmet": "^8.0.0",
|
|
23
14
|
"jsonwebtoken": "^9.0.2",
|
|
24
|
-
"mongoose": "^8.7.1"
|
|
25
|
-
"redis": "^4.6.14",
|
|
26
|
-
"socket.io": "^4.8.1"
|
|
15
|
+
"mongoose": "^8.7.1"
|
|
27
16
|
}
|
|
28
17
|
}
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
const { getRedisData, setRedisData } = require("../../config/redis");
|
|
2
|
-
const CustomerProfile = require("../../models/CustomerProfile");
|
|
3
|
-
|
|
4
|
-
const CACHE_KEY_ALL = "customer_profiles_all";
|
|
5
|
-
|
|
6
|
-
const getCachedAllCustomerProfiles = async () => {
|
|
7
|
-
let profiles = await getRedisData(CACHE_KEY_ALL);
|
|
8
|
-
if (!profiles) {
|
|
9
|
-
profiles = await CustomerProfile.find({}).lean().exec();
|
|
10
|
-
await setRedisData(CACHE_KEY_ALL, profiles);
|
|
11
|
-
}
|
|
12
|
-
return profiles;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const updateCachedAllCustomerProfiles = async () => {
|
|
16
|
-
const profiles = await CustomerProfile.find({}).lean().exec();
|
|
17
|
-
await setRedisData(CACHE_KEY_ALL, profiles);
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const getCachedCustomerProfiles = async (workspaceId) => {
|
|
21
|
-
const allProfiles = await getCachedAllCustomerProfiles();
|
|
22
|
-
return allProfiles.filter((profile) => profile.workspaceId === workspaceId);
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const createCustomerProfile = async (data) => {
|
|
26
|
-
const newProfile = new CustomerProfile(data);
|
|
27
|
-
const savedProfile = await newProfile.save();
|
|
28
|
-
|
|
29
|
-
updateCachedAllCustomerProfiles();
|
|
30
|
-
return savedProfile;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
const findCustomerProfilesByWorkspaceId = async (workspaceId) => {
|
|
34
|
-
return await getCachedCustomerProfiles(workspaceId);
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
const findCustomerProfilesByWorkspace = async (workspaceId, filter = {}) => {
|
|
38
|
-
const profiles = await getCachedCustomerProfiles(workspaceId);
|
|
39
|
-
return profiles.filter((profile) =>
|
|
40
|
-
Object.entries(filter).every(([key, value]) => profile[key] === value)
|
|
41
|
-
);
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const findCustomerProfileByFilter = async (
|
|
45
|
-
filter = {},
|
|
46
|
-
bodyFilter = {},
|
|
47
|
-
workspaceId = null
|
|
48
|
-
) => {
|
|
49
|
-
const allProfiles = await getCachedAllCustomerProfiles();
|
|
50
|
-
|
|
51
|
-
const filtered = workspaceId
|
|
52
|
-
? allProfiles.filter((p) => p.workspaceId === workspaceId)
|
|
53
|
-
: allProfiles;
|
|
54
|
-
|
|
55
|
-
return (
|
|
56
|
-
filtered.find(
|
|
57
|
-
(profile) =>
|
|
58
|
-
Object.entries(filter).every(
|
|
59
|
-
([key, value]) => profile[key] === value
|
|
60
|
-
) &&
|
|
61
|
-
Object.entries(bodyFilter).every(
|
|
62
|
-
([key, value]) => profile.body?.[key] === value
|
|
63
|
-
)
|
|
64
|
-
) || null
|
|
65
|
-
);
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const findAllCustomerProfilesByFilter = async (
|
|
69
|
-
filter = {},
|
|
70
|
-
bodyFilter = {},
|
|
71
|
-
workspaceId = null
|
|
72
|
-
) => {
|
|
73
|
-
const allProfiles = await getCachedAllCustomerProfiles();
|
|
74
|
-
|
|
75
|
-
const filtered = workspaceId
|
|
76
|
-
? allProfiles.filter((p) => p.workspaceId === workspaceId)
|
|
77
|
-
: allProfiles;
|
|
78
|
-
|
|
79
|
-
return (
|
|
80
|
-
filtered.filter(
|
|
81
|
-
(profile) =>
|
|
82
|
-
Object.entries(filter).every(
|
|
83
|
-
([key, value]) => profile[key] === value
|
|
84
|
-
) &&
|
|
85
|
-
Object.entries(bodyFilter).every(
|
|
86
|
-
([key, value]) => profile.body?.[key] === value
|
|
87
|
-
)
|
|
88
|
-
) || []
|
|
89
|
-
);
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
const updateCustomerProfile = async (id, data) => {
|
|
93
|
-
const updated = await CustomerProfile.findByIdAndUpdate(id, data, {
|
|
94
|
-
new: true,
|
|
95
|
-
}).exec();
|
|
96
|
-
|
|
97
|
-
if (updated) {
|
|
98
|
-
updateCachedAllCustomerProfiles();
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return updated;
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
const updateCustomerProfileByFilter = async (
|
|
105
|
-
filter,
|
|
106
|
-
data,
|
|
107
|
-
workspaceId = null
|
|
108
|
-
) => {
|
|
109
|
-
const query = workspaceId ? { ...filter, workspaceId } : filter;
|
|
110
|
-
|
|
111
|
-
const updated = await CustomerProfile.findOneAndUpdate(query, data, {
|
|
112
|
-
new: true,
|
|
113
|
-
}).exec();
|
|
114
|
-
|
|
115
|
-
if (updated) {
|
|
116
|
-
updateCachedAllCustomerProfiles();
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return updated;
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
const deleteCustomerProfile = async (id) => {
|
|
123
|
-
const deleted = await CustomerProfile.findByIdAndDelete(id).exec();
|
|
124
|
-
|
|
125
|
-
if (deleted) {
|
|
126
|
-
updateCachedAllCustomerProfiles();
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return deleted;
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
const deleteAllCustomerProfilesByWorkspace = async (workspaceId) => {
|
|
133
|
-
await CustomerProfile.deleteMany({ workspaceId }).exec();
|
|
134
|
-
await updateCachedAllCustomerProfiles();
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
module.exports = {
|
|
138
|
-
createCustomerProfile,
|
|
139
|
-
findCustomerProfilesByWorkspaceId,
|
|
140
|
-
findCustomerProfilesByWorkspace,
|
|
141
|
-
findCustomerProfileByFilter,
|
|
142
|
-
updateCustomerProfile,
|
|
143
|
-
updateCustomerProfileByFilter,
|
|
144
|
-
deleteCustomerProfile,
|
|
145
|
-
deleteAllCustomerProfilesByWorkspace,
|
|
146
|
-
findAllCustomerProfilesByFilter,
|
|
147
|
-
};
|
|
@@ -1,229 +0,0 @@
|
|
|
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 findAllTemplateByFilter = async (
|
|
83
|
-
filter,
|
|
84
|
-
bodyFilter = {},
|
|
85
|
-
workspaceId = null
|
|
86
|
-
) => {
|
|
87
|
-
const allTemplates = await getCachedAllDescriptionTemplates();
|
|
88
|
-
|
|
89
|
-
// Filter by workspaceId if provided
|
|
90
|
-
let filteredTemplates = workspaceId
|
|
91
|
-
? allTemplates.filter((desc) => desc.workspaceId === workspaceId)
|
|
92
|
-
: allTemplates;
|
|
93
|
-
|
|
94
|
-
return (
|
|
95
|
-
filteredTemplates.filter(
|
|
96
|
-
(item) =>
|
|
97
|
-
// Check direct properties of item
|
|
98
|
-
Object.entries(filter).every(([key, value]) => item[key] === value) &&
|
|
99
|
-
// If bodyFilter is provided, check inside item.body
|
|
100
|
-
Object.entries(bodyFilter).every(
|
|
101
|
-
([key, value]) => item.body?.[key] === value
|
|
102
|
-
)
|
|
103
|
-
) || null
|
|
104
|
-
);
|
|
105
|
-
};
|
|
106
|
-
const createOrUpdateTemplate = async (filter, data) => {
|
|
107
|
-
const template = await DescriptionTemplate.findOneAndUpdate(filter, data, {
|
|
108
|
-
upsert: true,
|
|
109
|
-
new: true,
|
|
110
|
-
});
|
|
111
|
-
if (template) {
|
|
112
|
-
// Update only the main cache
|
|
113
|
-
await updateCachedAllDescriptionTemplates();
|
|
114
|
-
}
|
|
115
|
-
return template;
|
|
116
|
-
};
|
|
117
|
-
const updateTemplateById = async (id, data) => {
|
|
118
|
-
const updatedTemplate = await DescriptionTemplate.findByIdAndUpdate(
|
|
119
|
-
id,
|
|
120
|
-
data,
|
|
121
|
-
{
|
|
122
|
-
new: true,
|
|
123
|
-
}
|
|
124
|
-
).exec();
|
|
125
|
-
|
|
126
|
-
if (updatedTemplate) {
|
|
127
|
-
// Update only the main cache
|
|
128
|
-
await updateCachedAllDescriptionTemplates();
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return updatedTemplate;
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Update an DescriptionTemplate by filter.
|
|
136
|
-
*/
|
|
137
|
-
const updateTemplateByFilter = async (filter, data, workspaceId = null) => {
|
|
138
|
-
// Add workspace filter if provided
|
|
139
|
-
const queryFilter = workspaceId ? { ...filter, workspaceId } : filter;
|
|
140
|
-
|
|
141
|
-
const updatedTemplate = await DescriptionTemplate.findOneAndUpdate(
|
|
142
|
-
queryFilter,
|
|
143
|
-
data,
|
|
144
|
-
{
|
|
145
|
-
new: true,
|
|
146
|
-
}
|
|
147
|
-
).exec();
|
|
148
|
-
|
|
149
|
-
if (updatedTemplate) {
|
|
150
|
-
// Update only the main cache
|
|
151
|
-
await updateCachedAllDescriptionTemplates();
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
return updatedTemplate;
|
|
155
|
-
};
|
|
156
|
-
const updateManyTemplates = async (filter, data) => {
|
|
157
|
-
const queryFilter = workspaceId ? { ...filter, workspaceId } : filter;
|
|
158
|
-
|
|
159
|
-
const updatedTemplate = await DescriptionTemplate.updateMany(
|
|
160
|
-
queryFilter,
|
|
161
|
-
{ $set: { ...data } },
|
|
162
|
-
{
|
|
163
|
-
new: true,
|
|
164
|
-
}
|
|
165
|
-
).exec();
|
|
166
|
-
|
|
167
|
-
if (updatedTemplate) {
|
|
168
|
-
// Update only the main cache
|
|
169
|
-
await updateCachedAllDescriptionTemplates();
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return updatedTemplate;
|
|
173
|
-
};
|
|
174
|
-
/**
|
|
175
|
-
* Delete an DescriptionTemplate by ID.
|
|
176
|
-
*/
|
|
177
|
-
const deleteTemplate = async (id) => {
|
|
178
|
-
const deletedTemplate = await DescriptionTemplate.findByIdAndDelete(
|
|
179
|
-
id
|
|
180
|
-
).exec();
|
|
181
|
-
|
|
182
|
-
if (deletedTemplate) {
|
|
183
|
-
await updateCachedAllDescriptionTemplates();
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return deletedTemplate;
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
const deleteAllTemplatesByWorkspace = async (workspaceId) => {
|
|
190
|
-
await DescriptionTemplate.deleteMany({ workspaceId }).exec();
|
|
191
|
-
|
|
192
|
-
await updateCachedAllDescriptionTemplates();
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
const findTemplateById = async (id) => {
|
|
196
|
-
return await DescriptionTemplate.findById(id).lean().exec();
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
const deleteTemplatesByFilter = async (filter) => {
|
|
200
|
-
await DescriptionTemplate.deleteMany(filter).exec();
|
|
201
|
-
|
|
202
|
-
await updateCachedAllDescriptionTemplates();
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
const insertManyTemplates = async (templates) => {
|
|
206
|
-
const insertedTemplates = await DescriptionTemplate.insertMany(templates);
|
|
207
|
-
|
|
208
|
-
if (insertedTemplates?.length > 0) {
|
|
209
|
-
await updateCachedAllDescriptionTemplates();
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
return insertedTemplates;
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
module.exports = {
|
|
216
|
-
createTemplate,
|
|
217
|
-
createOrUpdateTemplate,
|
|
218
|
-
findTemplateByFilter,
|
|
219
|
-
findTemplatesByWorkspaceId,
|
|
220
|
-
updateTemplateById,
|
|
221
|
-
updateTemplateByFilter,
|
|
222
|
-
updateManyTemplates,
|
|
223
|
-
deleteTemplate,
|
|
224
|
-
deleteAllTemplatesByWorkspace,
|
|
225
|
-
findTemplateById,
|
|
226
|
-
deleteTemplatesByFilter,
|
|
227
|
-
insertManyTemplates,
|
|
228
|
-
findAllTemplateByFilter,
|
|
229
|
-
};
|