shuttlepro-shared 1.3.41 → 1.3.43
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/customerProfile.repository.js +83 -14
- package/common/repositories/userRole.repository.js +5 -2
- package/config/redis.js +72 -73
- package/middlewares/checkPermission/index.js +36 -23
- package/models/Activity.js +28 -0
- package/models/ActivityLogs.js +1 -0
- package/models/AgentActivity.js +5 -4
- package/models/Attribute.js +130 -0
- package/models/Automation.js +231 -0
- package/models/BusinessRule.js +16 -0
- package/models/BusinessRuleHelper.js +13 -0
- package/models/Card.js +50 -1
- package/models/Catalogue.js +22 -0
- package/models/Category.js +129 -0
- package/models/ChatMemberSession.js +21 -0
- package/models/ChatMessage.js +43 -0
- package/models/Chatbot.js +1 -1
- package/models/Color.js +10 -0
- package/models/Column.js +6 -0
- package/models/Conversation.js +40 -0
- package/models/Customer.js +2 -1
- package/models/CustomerCheckpoint.js +21 -0
- package/models/DeviceInfo.js +13 -0
- package/models/Email.js +21 -0
- package/models/EmailMessage.js +30 -0
- package/models/EmailNotification.js +17 -0
- package/models/EscalationConfiguration.js +123 -0
- package/models/EscalationManager.js +50 -0
- package/models/Faq.js +29 -0
- package/models/FeedbackResponse.js +72 -0
- package/models/FormTemplate.js +27 -0
- package/models/Integration.js +8 -0
- package/models/InternalComments.js +27 -0
- package/models/InternalThreads.js +20 -0
- package/models/JobDesign.js +32 -0
- package/models/JobQueue.js +17 -0
- package/models/LabelsPdf.js +12 -0
- package/models/Layout.js +12 -0
- package/models/LoadSheet.js +31 -0
- package/models/Location.js +148 -0
- package/models/Logo.js +11 -0
- package/models/MailGroup.js +21 -0
- package/models/Notification.js +32 -0
- package/models/Order.js +11 -0
- package/models/OrderPdf.js +29 -0
- package/models/PostsAutomation.js +66 -0
- package/models/Product.js +337 -0
- package/models/ProductAttachment.js +158 -0
- package/models/ProductAttribute.js +140 -0
- package/models/ProductCategory.js +128 -0
- package/models/ProductLabels.js +11 -0
- package/models/ProductShopify.js +13 -0
- package/models/ProductTag.js +124 -0
- package/models/ProductVariant.js +157 -0
- package/models/Profile.js +3 -1
- package/models/ServiceUsage.js +26 -0
- package/models/ShipperSetting.js +24 -0
- package/models/SocialGroup.js +127 -0
- package/models/SocialPost.js +40 -0
- package/models/SocialProfile.js +56 -0
- package/models/Story.js +86 -0
- package/models/Tag.js +77 -0
- package/models/Template.js +76 -0
- package/models/TemplateFrame.js +55 -0
- package/models/TemplateTag.js +10 -0
- package/models/UserGuide.js +21 -0
- package/models/UserSession.js +47 -0
- package/models/VariantLocation.js +145 -0
- package/models/WhatsappFlow.js +29 -0
- package/models/Workflow.js +34 -0
- package/models/Workspace.js +78 -4
- package/models.js +168 -49
- package/package.json +1 -1
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
const mongoose = require("mongoose");
|
|
2
|
+
const { Schema } = mongoose;
|
|
3
|
+
const LocationSchema = new Schema(
|
|
4
|
+
{
|
|
5
|
+
isDefault: { type: Boolean, default: false },
|
|
6
|
+
address1: { type: String, default: "" },
|
|
7
|
+
address2: { type: String, default: "" },
|
|
8
|
+
city: { type: String, default: "" },
|
|
9
|
+
zip: { type: String, default: "" },
|
|
10
|
+
province: { type: String, default: "" },
|
|
11
|
+
country: { type: String, default: "" },
|
|
12
|
+
phone: { type: String, default: "" },
|
|
13
|
+
active: { type: Boolean, default: true },
|
|
14
|
+
oldId: { type: String, default: "" },
|
|
15
|
+
webLocationId: { type: Number, default: null },
|
|
16
|
+
bcLocationId: { type: String, default: "" },
|
|
17
|
+
workspaceId: {
|
|
18
|
+
type: Schema.Types.ObjectId,
|
|
19
|
+
ref: "Workspace",
|
|
20
|
+
default: null,
|
|
21
|
+
},
|
|
22
|
+
legacy: { type: Boolean, default: false },
|
|
23
|
+
name: { type: String, default: "" },
|
|
24
|
+
isDeleted: { type: Boolean, default: false },
|
|
25
|
+
bcLocationId: { type: String, default: "" },
|
|
26
|
+
createdBy: { type: Schema.Types.ObjectId, ref: "User", default: null },
|
|
27
|
+
updatedBy: { type: Schema.Types.ObjectId, ref: "User", default: null },
|
|
28
|
+
},
|
|
29
|
+
{ timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } }
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
LocationSchema.virtual("variantLocations", {
|
|
33
|
+
ref: "VariantLocation",
|
|
34
|
+
localField: "_id",
|
|
35
|
+
foreignField: "locationId",
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const Location = mongoose.model("Location", LocationSchema);
|
|
39
|
+
|
|
40
|
+
const createNewLocation = async (locationData) => {
|
|
41
|
+
try {
|
|
42
|
+
return await Location.create(locationData);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.log(error);
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const findLocationById = async (locationId) => {
|
|
50
|
+
try {
|
|
51
|
+
return await Location.findById(locationId);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.log(error);
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const updateLocationById = async (criteria, updateData, options) => {
|
|
59
|
+
try {
|
|
60
|
+
return await Location.findByIdAndUpdate(criteria, updateData, options);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
console.log(error);
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const updateLocation = async (criteria, updateData, options) => {
|
|
67
|
+
try {
|
|
68
|
+
return await Location.findOneAndUpdate(criteria, updateData, options);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
console.log(error);
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const findAndUpdateLocation = async (criteria, data, options) => {
|
|
76
|
+
try {
|
|
77
|
+
return await Location.findOneAndUpdate(criteria, data, options);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.log(error);
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const deleteLocationById = async (locationId) => {
|
|
84
|
+
try {
|
|
85
|
+
return await Location.findByIdAndDelete(locationId);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.log(error);
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const findLocation = async (criteria) => {
|
|
93
|
+
try {
|
|
94
|
+
return await Location.findOne(criteria);
|
|
95
|
+
} catch (error) {
|
|
96
|
+
console.log(error);
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const findLocations = async (criteria) => {
|
|
102
|
+
try {
|
|
103
|
+
return await Location.find(criteria);
|
|
104
|
+
} catch (error) {
|
|
105
|
+
console.log(error);
|
|
106
|
+
return [];
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const locationsByAggregation = async (aggregate = []) => {
|
|
111
|
+
try {
|
|
112
|
+
let locations = await Location.aggregate(aggregate);
|
|
113
|
+
return locations;
|
|
114
|
+
} catch (err) {
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const locationsUpdateMany = async (criteria, updateData, options = {}) => {
|
|
120
|
+
try {
|
|
121
|
+
return await Location.updateMany(criteria, updateData, options);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const insertLocations = async (locations) => {
|
|
128
|
+
try {
|
|
129
|
+
return await Location.insertMany(locations);
|
|
130
|
+
} catch (err) {
|
|
131
|
+
return [];
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
module.exports = {
|
|
136
|
+
Location,
|
|
137
|
+
createNewLocation,
|
|
138
|
+
findLocationById,
|
|
139
|
+
updateLocationById,
|
|
140
|
+
updateLocation,
|
|
141
|
+
deleteLocationById,
|
|
142
|
+
findLocation,
|
|
143
|
+
findLocations,
|
|
144
|
+
locationsByAggregation,
|
|
145
|
+
locationsUpdateMany,
|
|
146
|
+
insertLocations,
|
|
147
|
+
findAndUpdateLocation,
|
|
148
|
+
};
|
package/models/Logo.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const mongoose = require("mongoose");
|
|
2
|
+
|
|
3
|
+
const logoSchema = new mongoose.Schema(
|
|
4
|
+
{
|
|
5
|
+
url: { type: String, default: "" },
|
|
6
|
+
workspaceId: { type: String, default: "" },
|
|
7
|
+
},
|
|
8
|
+
{ timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } }
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
module.exports = mongoose.model("Logo", logoSchema);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const mongoose = require("mongoose");
|
|
2
|
+
const { Schema } = mongoose;
|
|
3
|
+
|
|
4
|
+
const MailGroupSchema = new Schema(
|
|
5
|
+
{
|
|
6
|
+
name: { type: String, default: "" },
|
|
7
|
+
emailIds: [
|
|
8
|
+
{
|
|
9
|
+
emailId: {
|
|
10
|
+
type: Schema.Types.ObjectId,
|
|
11
|
+
ref: "Email",
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
],
|
|
15
|
+
workspaceId: { type: String, default: "" },
|
|
16
|
+
},
|
|
17
|
+
{ timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } }
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
const MailGroup = mongoose.model("MailGroup", MailGroupSchema);
|
|
21
|
+
module.exports = MailGroup;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const mongoose = require("mongoose");
|
|
2
|
+
const { Schema } = mongoose;
|
|
3
|
+
|
|
4
|
+
const NotificationSchema = new Schema(
|
|
5
|
+
{
|
|
6
|
+
type: { type: String, default: "" },
|
|
7
|
+
isRead: { type: Boolean, default: false },
|
|
8
|
+
operation: { type: String, default: "" },
|
|
9
|
+
actionId: { type: String, default: "" },
|
|
10
|
+
workspaceId: { type: String, default: "" },
|
|
11
|
+
workspaceName: { type: String, default: "" },
|
|
12
|
+
message: { type: String, default: "" },
|
|
13
|
+
email: { type: String, default: "" },
|
|
14
|
+
userId: { type: String, default: "" },
|
|
15
|
+
data: { type: mongoose.Schema.Types.Mixed },
|
|
16
|
+
isDeleted: { type: Boolean, default: false },
|
|
17
|
+
alertShow: { type: Boolean, default: false },
|
|
18
|
+
routes: {
|
|
19
|
+
routeType: { type: String, default: "" },
|
|
20
|
+
id: { type: String, default: "" },
|
|
21
|
+
query: { type: Boolean, default: false },
|
|
22
|
+
queryData: { type: String, default: "" },
|
|
23
|
+
},
|
|
24
|
+
permissions: [],
|
|
25
|
+
role: [String],
|
|
26
|
+
},
|
|
27
|
+
{ timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } }
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
const Notification = mongoose.model("Notification", NotificationSchema);
|
|
31
|
+
|
|
32
|
+
module.exports = Notification;
|
package/models/Order.js
CHANGED
|
@@ -136,6 +136,14 @@ const OrderSchema = new Schema(
|
|
|
136
136
|
paymentDate: { type: String, default: "" },
|
|
137
137
|
paymentDetails: {},
|
|
138
138
|
},
|
|
139
|
+
merged: {
|
|
140
|
+
status: { type: String, default: "open" },
|
|
141
|
+
webOrderNumbers: [],
|
|
142
|
+
orderNumbers: [],
|
|
143
|
+
localOrderIds: [],
|
|
144
|
+
webOrderIds: [],
|
|
145
|
+
details: [],
|
|
146
|
+
},
|
|
139
147
|
credentials: {},
|
|
140
148
|
ticketId: { type: String, default: "" },
|
|
141
149
|
sender: { type: String, default: "" },
|
|
@@ -159,6 +167,9 @@ const OrderSchema = new Schema(
|
|
|
159
167
|
},
|
|
160
168
|
orderName: { type: String, default: "" },
|
|
161
169
|
totalWeight: { type: String, default: "" },
|
|
170
|
+
currency: {},
|
|
171
|
+
presentedCurrency: {},
|
|
172
|
+
exchangeRate: { type: Number, default: 0 },
|
|
162
173
|
},
|
|
163
174
|
{ timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } }
|
|
164
175
|
);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const mongoose = require("mongoose");
|
|
2
|
+
const { Schema } = mongoose;
|
|
3
|
+
|
|
4
|
+
const OrderPdfScehma = new mongoose.Schema(
|
|
5
|
+
{
|
|
6
|
+
orderNumber: {
|
|
7
|
+
type: {
|
|
8
|
+
localMin: { type: String, default: "" },
|
|
9
|
+
localMax: { type: String, default: "" },
|
|
10
|
+
storeMin: { type: String, default: "" },
|
|
11
|
+
storeMax: { type: String, default: "" },
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
ordersCount: { type: String, default: "" },
|
|
15
|
+
url: { type: String, default: "" },
|
|
16
|
+
key: { type: String, default: "" },
|
|
17
|
+
status: { type: String, default: "" },
|
|
18
|
+
workspaceId: {
|
|
19
|
+
type: Schema.Types.ObjectId,
|
|
20
|
+
ref: "Workspace",
|
|
21
|
+
default: null,
|
|
22
|
+
},
|
|
23
|
+
createdBy: { type: Schema.Types.ObjectId, ref: "User", default: null },
|
|
24
|
+
updatedBy: { type: Schema.Types.ObjectId, ref: "User", default: null },
|
|
25
|
+
},
|
|
26
|
+
{ timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } }
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
module.exports = mongoose.model("OrderPdf", OrderPdfScehma);
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const mongoose = require("mongoose");
|
|
2
|
+
const { Schema } = mongoose;
|
|
3
|
+
|
|
4
|
+
const PostsAutomation = new mongoose.Schema(
|
|
5
|
+
{
|
|
6
|
+
workspaceId: {
|
|
7
|
+
type: Schema.Types.ObjectId,
|
|
8
|
+
ref: "Workspace",
|
|
9
|
+
default: null,
|
|
10
|
+
},
|
|
11
|
+
platformType: {
|
|
12
|
+
type: String,
|
|
13
|
+
required: true,
|
|
14
|
+
enum: ["facebook", "instagram"],
|
|
15
|
+
},
|
|
16
|
+
attachments: [
|
|
17
|
+
{
|
|
18
|
+
type: String,
|
|
19
|
+
default: "",
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
attachmentType: {
|
|
23
|
+
type: String,
|
|
24
|
+
default: "",
|
|
25
|
+
},
|
|
26
|
+
postId: {
|
|
27
|
+
type: String,
|
|
28
|
+
default: "",
|
|
29
|
+
},
|
|
30
|
+
verb: {
|
|
31
|
+
type: String,
|
|
32
|
+
default: "",
|
|
33
|
+
},
|
|
34
|
+
published: {
|
|
35
|
+
type: String,
|
|
36
|
+
default: "",
|
|
37
|
+
},
|
|
38
|
+
message: {
|
|
39
|
+
type: String,
|
|
40
|
+
default: "",
|
|
41
|
+
},
|
|
42
|
+
pageId: {
|
|
43
|
+
type: String,
|
|
44
|
+
default: "",
|
|
45
|
+
},
|
|
46
|
+
field: {
|
|
47
|
+
type: String,
|
|
48
|
+
default: "",
|
|
49
|
+
},
|
|
50
|
+
integratedAccount: {
|
|
51
|
+
type: Schema.Types.ObjectId,
|
|
52
|
+
ref: "Integration",
|
|
53
|
+
default: null,
|
|
54
|
+
},
|
|
55
|
+
status: {
|
|
56
|
+
type: String,
|
|
57
|
+
default: "",
|
|
58
|
+
emum: ["active", "pending", "removed"],
|
|
59
|
+
},
|
|
60
|
+
platformTimestamp: { type: String, default: "" },
|
|
61
|
+
body: [{}],
|
|
62
|
+
},
|
|
63
|
+
{ timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } }
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
module.exports = mongoose.model("PostsAutomation", PostsAutomation);
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
const mongoose = require("mongoose");
|
|
2
|
+
const { Schema } = mongoose;
|
|
3
|
+
const axios = require("axios");
|
|
4
|
+
const NewProduct = require("./NewProduct"); // Make sure to import NewProduct model
|
|
5
|
+
|
|
6
|
+
const categorySchema = new Schema({
|
|
7
|
+
_id: false,
|
|
8
|
+
categoryId: { type: String, default: "" },
|
|
9
|
+
name: { type: String, default: "" },
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const ProductSchema = new Schema(
|
|
13
|
+
{
|
|
14
|
+
name: { type: String, default: "" },
|
|
15
|
+
description: { type: String, default: "" },
|
|
16
|
+
sku: { type: String, default: "" },
|
|
17
|
+
price: { type: String, default: 0 },
|
|
18
|
+
quantity: { type: Number, default: 0 },
|
|
19
|
+
imageUrl: { type: String, default: "" },
|
|
20
|
+
costPrice: { type: String, default: 0 },
|
|
21
|
+
salePrice: { type: String, default: 0 },
|
|
22
|
+
preference: { type: Number, default: 10 },
|
|
23
|
+
oldId: { type: String, default: "" },
|
|
24
|
+
webProductId: { type: Number, default: null },
|
|
25
|
+
publishedAt: { type: String, default: "" },
|
|
26
|
+
workspaceId: {
|
|
27
|
+
type: Schema.Types.ObjectId,
|
|
28
|
+
ref: "Workspace",
|
|
29
|
+
default: null,
|
|
30
|
+
},
|
|
31
|
+
storeType: { type: String, default: "LOCAL" },
|
|
32
|
+
isDeleted: { type: Boolean, default: false },
|
|
33
|
+
inventoryPolicy: { type: String, default: "deny" }, //deny,continue
|
|
34
|
+
inventoryManagement: { type: String, default: "shopify" }, //shopify, null, fulfillment_service
|
|
35
|
+
createdBy: { type: Schema.Types.ObjectId, ref: "User", default: null },
|
|
36
|
+
updatedBy: { type: Schema.Types.ObjectId, ref: "User", default: null },
|
|
37
|
+
storeUrl: { type: String, default: "" },
|
|
38
|
+
status: { type: String, default: "active" }, //active, archived, draft
|
|
39
|
+
categories: [],
|
|
40
|
+
variantCount: { type: Number, default: 0 },
|
|
41
|
+
productType: { type: String, default: "" },
|
|
42
|
+
vendor: { type: String, default: "" },
|
|
43
|
+
handle: { type: String, default: "" },
|
|
44
|
+
isWebhookCase: { type: Boolean, default: false },
|
|
45
|
+
updating: { type: Boolean, default: false },
|
|
46
|
+
attributes: [],
|
|
47
|
+
bcProductId: { type: String, default: "" },
|
|
48
|
+
},
|
|
49
|
+
{ timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } }
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
ProductSchema.virtual("productCategories", {
|
|
53
|
+
ref: "ProductCategory",
|
|
54
|
+
localField: "_id",
|
|
55
|
+
foreignField: "productId",
|
|
56
|
+
});
|
|
57
|
+
ProductSchema.virtual("productTags", {
|
|
58
|
+
ref: "ProductTag",
|
|
59
|
+
localField: "_id",
|
|
60
|
+
foreignField: "productId",
|
|
61
|
+
});
|
|
62
|
+
ProductSchema.virtual("productAttributes", {
|
|
63
|
+
ref: "ProductAttribute",
|
|
64
|
+
localField: "_id",
|
|
65
|
+
foreignField: "productId",
|
|
66
|
+
});
|
|
67
|
+
ProductSchema.virtual("productAttachments", {
|
|
68
|
+
ref: "ProductAttachment",
|
|
69
|
+
localField: "_id",
|
|
70
|
+
foreignField: "productId",
|
|
71
|
+
});
|
|
72
|
+
ProductSchema.virtual("productVariants", {
|
|
73
|
+
ref: "ProductVariant",
|
|
74
|
+
localField: "_id",
|
|
75
|
+
foreignField: "productId",
|
|
76
|
+
});
|
|
77
|
+
ProductSchema.virtual("variantLocations", {
|
|
78
|
+
ref: "VariantLocation",
|
|
79
|
+
localField: "_id",
|
|
80
|
+
foreignField: "productId",
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const transformProductData = async (product) => {
|
|
84
|
+
if (!product) return null;
|
|
85
|
+
|
|
86
|
+
const populatedProduct = await mongoose.models.Product.findById(product._id)
|
|
87
|
+
.populate({
|
|
88
|
+
path: "productVariants",
|
|
89
|
+
match: { isDeleted: false },
|
|
90
|
+
})
|
|
91
|
+
.populate({
|
|
92
|
+
path: "productAttachments",
|
|
93
|
+
match: { isDeleted: false },
|
|
94
|
+
populate: {
|
|
95
|
+
path: "variantIds",
|
|
96
|
+
model: "ProductVariant",
|
|
97
|
+
match: { isDeleted: false },
|
|
98
|
+
},
|
|
99
|
+
})
|
|
100
|
+
.lean()
|
|
101
|
+
.exec();
|
|
102
|
+
|
|
103
|
+
if (!populatedProduct) return null;
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
name: populatedProduct.name || "",
|
|
107
|
+
id: populatedProduct._id || "",
|
|
108
|
+
description: populatedProduct.description || "",
|
|
109
|
+
sku: populatedProduct.sku || "",
|
|
110
|
+
price: populatedProduct.price || "",
|
|
111
|
+
quantity: populatedProduct.quantity || 0,
|
|
112
|
+
imageUrl: populatedProduct.imageUrl || "",
|
|
113
|
+
costPrice: populatedProduct.costPrice || 0,
|
|
114
|
+
salePrice: populatedProduct.salePrice || 0,
|
|
115
|
+
webProductId: populatedProduct.webProductId || "",
|
|
116
|
+
variants:
|
|
117
|
+
populatedProduct.productVariants?.map((p) => ({
|
|
118
|
+
id: p?._id,
|
|
119
|
+
name: p?.name || "",
|
|
120
|
+
sku: p?.sku || "",
|
|
121
|
+
subSku: p?.subSku || "",
|
|
122
|
+
productId: p?.productId || "",
|
|
123
|
+
price: p?.price || "",
|
|
124
|
+
costPrice: p?.costPrice || 0,
|
|
125
|
+
salePrice: p?.salePrice || 0,
|
|
126
|
+
quantity: p?.quantity || 0,
|
|
127
|
+
})) || [],
|
|
128
|
+
attachments:
|
|
129
|
+
populatedProduct.productAttachments?.map((x) => ({
|
|
130
|
+
id: x?._id,
|
|
131
|
+
thumbUrl: x?.thumbUrl,
|
|
132
|
+
remoteUrl: x?.remoteUrl,
|
|
133
|
+
url: x?.url,
|
|
134
|
+
type: x?.type,
|
|
135
|
+
variants:
|
|
136
|
+
x?.variantIds?.length > 0
|
|
137
|
+
? x.variantIds.map((p) => ({
|
|
138
|
+
id: p?._id,
|
|
139
|
+
name: p?.name || "",
|
|
140
|
+
sku: p?.sku || "",
|
|
141
|
+
subSku: p?.subSku || "",
|
|
142
|
+
productId: p?.productId || "",
|
|
143
|
+
price: p?.price || "",
|
|
144
|
+
costPrice: p?.costPrice || 0,
|
|
145
|
+
salePrice: p?.salePrice || 0,
|
|
146
|
+
quantity: p?.quantity || 0,
|
|
147
|
+
}))
|
|
148
|
+
: [],
|
|
149
|
+
})) || [],
|
|
150
|
+
workspaceId: populatedProduct.workspaceId,
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
// Middleware for various operations
|
|
155
|
+
const updateNewProduct = async (doc, next) => {
|
|
156
|
+
try {
|
|
157
|
+
// Skip if document is marked as deleted
|
|
158
|
+
if (doc.isDeleted) return;
|
|
159
|
+
|
|
160
|
+
// Transform and upsert product data
|
|
161
|
+
const newProductData = await transformProductData(doc);
|
|
162
|
+
if (newProductData) {
|
|
163
|
+
await NewProduct.findOneAndUpdate(
|
|
164
|
+
{ id: newProductData.id },
|
|
165
|
+
newProductData,
|
|
166
|
+
{ upsert: true, new: true }
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
} catch (error) {
|
|
170
|
+
console.error("Error updating NewProduct:", error);
|
|
171
|
+
// Call next with error if needed
|
|
172
|
+
next(error);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const deleteNewProduct = async (doc, next) => {
|
|
177
|
+
try {
|
|
178
|
+
if (doc) {
|
|
179
|
+
await NewProduct.deleteOne({ id: doc._id });
|
|
180
|
+
}
|
|
181
|
+
} catch (error) {
|
|
182
|
+
console.error("Error deleting from NewProduct:", error);
|
|
183
|
+
next(error);
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// Post middleware for various Mongoose operations
|
|
188
|
+
ProductSchema.post("save", async function (doc, next) {
|
|
189
|
+
console.log(doc, "doc");
|
|
190
|
+
await updateNewProduct(doc, next);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
ProductSchema.post("findOneAndUpdate", async function (doc, next) {
|
|
194
|
+
await updateNewProduct(doc, next);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
ProductSchema.post("updateOne", async function (doc, next) {
|
|
198
|
+
console.log(doc, "doc called");
|
|
199
|
+
await updateNewProduct(doc, next);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
ProductSchema.post("create", async function (doc, next) {
|
|
203
|
+
await updateNewProduct(doc, next);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
ProductSchema.post("findOneAndDelete", async function (doc, next) {
|
|
207
|
+
await deleteNewProduct(doc, next);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
ProductSchema.post("deleteOne", async function (doc, next) {
|
|
211
|
+
await deleteNewProduct(doc, next);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
const Product = mongoose.model("Product", ProductSchema);
|
|
215
|
+
|
|
216
|
+
const newProductInstance = async (productData) => {
|
|
217
|
+
try {
|
|
218
|
+
return new Product(productData);
|
|
219
|
+
} catch (error) {
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
const createNewProduct = async (productData) => {
|
|
225
|
+
try {
|
|
226
|
+
return await Product.create(productData);
|
|
227
|
+
} catch (error) {
|
|
228
|
+
console.log(error);
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const findProductById = async (productId) => {
|
|
234
|
+
try {
|
|
235
|
+
return await Product.findById(productId);
|
|
236
|
+
} catch (error) {
|
|
237
|
+
console.log(error);
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const updateProductById = async (criteria, updateData, options) => {
|
|
243
|
+
try {
|
|
244
|
+
return await Product.findByIdAndUpdate(criteria, updateData, options);
|
|
245
|
+
} catch (error) {
|
|
246
|
+
console.log(error);
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
const updateProducts = async (criteria, updateData, options = {}) => {
|
|
252
|
+
try {
|
|
253
|
+
return await Product.updateMany(criteria, updateData, options);
|
|
254
|
+
} catch (error) {
|
|
255
|
+
console.log(error);
|
|
256
|
+
return [];
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
const deleteProductById = async (productId) => {
|
|
261
|
+
try {
|
|
262
|
+
return await Product.findByIdAndDelete(productId);
|
|
263
|
+
} catch (error) {
|
|
264
|
+
console.log(error);
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const findProduct = async (criteria) => {
|
|
270
|
+
try {
|
|
271
|
+
return await Product.findOne(criteria);
|
|
272
|
+
} catch (error) {
|
|
273
|
+
console.log(error);
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
const findProducts = async (criteria) => {
|
|
279
|
+
try {
|
|
280
|
+
return await Product.find(criteria);
|
|
281
|
+
} catch (error) {
|
|
282
|
+
console.log(error);
|
|
283
|
+
return [];
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
const findSingleProduct = async (obj) => {
|
|
288
|
+
try {
|
|
289
|
+
let product = await Product.findOne(obj);
|
|
290
|
+
return product;
|
|
291
|
+
} catch (err) {
|
|
292
|
+
return null;
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
const countProductDocument = async (obj) => {
|
|
297
|
+
try {
|
|
298
|
+
let product = await Product.countDocuments(obj);
|
|
299
|
+
return product;
|
|
300
|
+
} catch (err) {
|
|
301
|
+
return 0;
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
const findProductWithPopulateAndPagination = async (
|
|
306
|
+
condition,
|
|
307
|
+
pop,
|
|
308
|
+
page,
|
|
309
|
+
limit
|
|
310
|
+
) => {
|
|
311
|
+
try {
|
|
312
|
+
const products = await Product.find(condition)
|
|
313
|
+
.sort({ _id: -1 })
|
|
314
|
+
.skip((page - 1) * parseInt(limit))
|
|
315
|
+
.limit(parseInt(limit))
|
|
316
|
+
.populate(pop);
|
|
317
|
+
return products;
|
|
318
|
+
} catch (err) {
|
|
319
|
+
return 0;
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
module.exports = {
|
|
324
|
+
newProductInstance,
|
|
325
|
+
createNewProduct,
|
|
326
|
+
findProductById,
|
|
327
|
+
updateProductById,
|
|
328
|
+
updateProducts,
|
|
329
|
+
deleteProductById,
|
|
330
|
+
findProduct,
|
|
331
|
+
findProducts,
|
|
332
|
+
findSingleProduct,
|
|
333
|
+
countProductDocument,
|
|
334
|
+
findProductWithPopulateAndPagination,
|
|
335
|
+
Product,
|
|
336
|
+
ProductModel: Product,
|
|
337
|
+
};
|