@ozdao/prometheus-framework 0.2.44 → 0.2.46
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/products.server.js +875 -0
- package/dist/products.server.mjs +875 -0
- package/dist/prometheus-framework/src/components/FieldBig/FieldBig.vue.cjs +1 -1
- package/dist/prometheus-framework/src/components/FieldBig/FieldBig.vue.js +18 -18
- package/dist/prometheus-framework/src/modules/orders/components/pages/EditOrder.vue.cjs +1 -1
- package/dist/prometheus-framework/src/modules/orders/components/pages/EditOrder.vue.js +1 -1
- package/dist/prometheus-framework/src/modules/products/components/blocks/CategoryCard.vue.cjs +1 -0
- package/dist/prometheus-framework/src/modules/products/components/blocks/CategoryCard.vue.js +43 -0
- package/dist/prometheus-framework/src/modules/products/components/blocks/CategoryCard.vue2.cjs +1 -0
- package/dist/prometheus-framework/src/modules/products/components/blocks/CategoryCard.vue2.js +4 -0
- package/dist/prometheus-framework/src/modules/products/components/blocks/LeftoverPositions.vue.cjs +1 -1
- package/dist/prometheus-framework/src/modules/products/components/blocks/LeftoverPositions.vue.js +1 -1
- package/dist/prometheus-framework/src/modules/products/components/blocks/ListPositions.vue.cjs +1 -1
- package/dist/prometheus-framework/src/modules/products/components/blocks/ListPositions.vue.js +39 -2
- package/dist/prometheus-framework/src/modules/products/components/blocks/ListPositions.vue2.cjs +1 -0
- package/dist/prometheus-framework/src/modules/products/components/blocks/ListPositions.vue2.js +4 -0
- package/dist/prometheus-framework/src/modules/products/components/pages/ProductRecommmendation.vue.cjs +1 -0
- package/dist/prometheus-framework/src/modules/products/components/pages/ProductRecommmendation.vue.js +87 -0
- package/dist/prometheus-framework/src/modules/products/components/pages/ProductRecommmendation.vue2.cjs +1 -0
- package/dist/prometheus-framework/src/modules/products/components/pages/ProductRecommmendation.vue2.js +4 -0
- package/dist/prometheus-framework/src/modules/products/components/sections/EditModifications.vue.cjs +1 -0
- package/dist/prometheus-framework/src/modules/products/components/sections/EditModifications.vue.js +138 -0
- package/dist/prometheus-framework/src/modules/products/components/sections/HeroRecommendation.vue.cjs +1 -0
- package/dist/prometheus-framework/src/modules/products/components/sections/HeroRecommendation.vue.js +114 -0
- package/dist/prometheus-framework/src/modules/products/components/sections/HeroRecommendation.vue2.cjs +1 -0
- package/dist/prometheus-framework/src/modules/products/components/sections/HeroRecommendation.vue2.js +4 -0
- package/dist/prometheus-framework/src/modules/products/components/sections/SortProducts.vue.cjs +1 -0
- package/dist/prometheus-framework/src/modules/products/components/sections/SortProducts.vue.js +53 -0
- package/dist/prometheus-framework/src/modules/products/products.client.cjs +1 -0
- package/dist/prometheus-framework/src/modules/products/products.client.js +56 -0
- package/dist/prometheus-framework/src/modules/products/store/products.cjs +1 -1
- package/dist/prometheus-framework/src/modules/products/store/products.js +2 -0
- package/dist/style.css +1 -1
- package/package.json +1 -1
- package/src/modules/products/components/pages/ProductRecommmendation.vue +2 -2
- package/src/modules/products/products.client.js +76 -20
- package/src/modules/products/products.server.js +8 -4
- package/src/modules/products/router/products.router.js +4 -2
@@ -0,0 +1,875 @@
|
|
1
|
+
import { g as getDefaultExportFromCjs } from "./_commonjsHelpers-83b1d755.mjs";
|
2
|
+
import "uuid";
|
3
|
+
import require$$0 from "mongoose";
|
4
|
+
import "path";
|
5
|
+
import require$$3 from "openai";
|
6
|
+
import { s as server } from "./index-d769324d.mjs";
|
7
|
+
import "jsonwebtoken";
|
8
|
+
import "mongodb";
|
9
|
+
var product_model = (mongoose) => {
|
10
|
+
const ProductSchema = new mongoose.Schema({
|
11
|
+
owner: {
|
12
|
+
type: mongoose.Schema.Types.ObjectId,
|
13
|
+
ref: "User"
|
14
|
+
},
|
15
|
+
organization: {
|
16
|
+
type: mongoose.Schema.Types.ObjectId,
|
17
|
+
ref: "Organization"
|
18
|
+
},
|
19
|
+
status: { type: String },
|
20
|
+
name: { type: String },
|
21
|
+
category: { type: Array },
|
22
|
+
description: { type: String },
|
23
|
+
sale: { type: Number },
|
24
|
+
price: { type: Number },
|
25
|
+
images: { type: Array },
|
26
|
+
popularity: { type: Number },
|
27
|
+
localization: { type: Array },
|
28
|
+
ingredients: { type: Array },
|
29
|
+
information: [{
|
30
|
+
name: { type: String },
|
31
|
+
value: { type: String }
|
32
|
+
}],
|
33
|
+
// MoySklad
|
34
|
+
quantity: { type: Number },
|
35
|
+
quantityDiscount: [{
|
36
|
+
quantity: {
|
37
|
+
type: Number,
|
38
|
+
required: true,
|
39
|
+
min: [1, "Quantity cannot be less than 1"]
|
40
|
+
},
|
41
|
+
discount: {
|
42
|
+
type: Number,
|
43
|
+
required: true,
|
44
|
+
min: [0, "Quantity-based discount cannot be less than 0"],
|
45
|
+
max: [100, "Quantity-based discount cannot be more than 100"]
|
46
|
+
}
|
47
|
+
}]
|
48
|
+
}, {
|
49
|
+
timestamps: {
|
50
|
+
currentTime: () => Date.now()
|
51
|
+
}
|
52
|
+
});
|
53
|
+
ProductSchema.index({ organization: 1 });
|
54
|
+
ProductSchema.index({ category: 1 });
|
55
|
+
ProductSchema.index({ price: 1 });
|
56
|
+
const Product = mongoose.model("Product", ProductSchema);
|
57
|
+
return Product;
|
58
|
+
};
|
59
|
+
const product_model$1 = /* @__PURE__ */ getDefaultExportFromCjs(product_model);
|
60
|
+
var category_model = (mongoose) => {
|
61
|
+
const СategorySchema = new mongoose.Schema({
|
62
|
+
name: {
|
63
|
+
type: String,
|
64
|
+
required: true,
|
65
|
+
trim: true
|
66
|
+
},
|
67
|
+
url: {
|
68
|
+
type: String,
|
69
|
+
required: true,
|
70
|
+
trim: true
|
71
|
+
},
|
72
|
+
subcategories: {
|
73
|
+
type: Array
|
74
|
+
},
|
75
|
+
localization: {
|
76
|
+
type: Array
|
77
|
+
},
|
78
|
+
filters: {
|
79
|
+
type: Array
|
80
|
+
},
|
81
|
+
owner: {
|
82
|
+
type: {
|
83
|
+
type: String,
|
84
|
+
required: true
|
85
|
+
},
|
86
|
+
target: {
|
87
|
+
type: mongoose.Schema.Types.ObjectId,
|
88
|
+
ref: function(value) {
|
89
|
+
if (this.owner.type === "user")
|
90
|
+
return "User";
|
91
|
+
if (this.owner.type === "organization")
|
92
|
+
return "Organization";
|
93
|
+
},
|
94
|
+
required: true
|
95
|
+
}
|
96
|
+
},
|
97
|
+
creator: {
|
98
|
+
hidden: {
|
99
|
+
type: Boolean,
|
100
|
+
required: true
|
101
|
+
},
|
102
|
+
type: {
|
103
|
+
type: String,
|
104
|
+
required: true
|
105
|
+
},
|
106
|
+
target: {
|
107
|
+
type: mongoose.Schema.Types.ObjectId,
|
108
|
+
ref: function(value) {
|
109
|
+
if (this.owner.type === "user")
|
110
|
+
return "User";
|
111
|
+
if (this.owner.type === "organization")
|
112
|
+
return "Organization";
|
113
|
+
},
|
114
|
+
required: true
|
115
|
+
}
|
116
|
+
}
|
117
|
+
}, {
|
118
|
+
timestamps: {
|
119
|
+
currentTime: () => Date.now()
|
120
|
+
}
|
121
|
+
});
|
122
|
+
СategorySchema.index({ "owner.target": 1, "creator.target": 1 });
|
123
|
+
const Сategory = mongoose.model("Сategory", СategorySchema, "categories");
|
124
|
+
return Сategory;
|
125
|
+
};
|
126
|
+
const category_model$1 = /* @__PURE__ */ getDefaultExportFromCjs(category_model);
|
127
|
+
var modification_model = (mongoose) => {
|
128
|
+
const ModificationSchema = new mongoose.Schema({
|
129
|
+
name: {
|
130
|
+
type: String
|
131
|
+
},
|
132
|
+
product: {
|
133
|
+
type: mongoose.Schema.Types.ObjectId,
|
134
|
+
ref: "Product",
|
135
|
+
required: true
|
136
|
+
},
|
137
|
+
price: {
|
138
|
+
type: Number
|
139
|
+
},
|
140
|
+
quantity: {
|
141
|
+
type: Number
|
142
|
+
},
|
143
|
+
// dups: {type: Array},
|
144
|
+
characteristics: [{
|
145
|
+
_id: {
|
146
|
+
type: String
|
147
|
+
},
|
148
|
+
name: {
|
149
|
+
type: String
|
150
|
+
},
|
151
|
+
value: {
|
152
|
+
type: String
|
153
|
+
}
|
154
|
+
}]
|
155
|
+
}, {
|
156
|
+
timestamps: {
|
157
|
+
currentTime: () => Date.now()
|
158
|
+
}
|
159
|
+
});
|
160
|
+
ModificationSchema.index({ product: 1 });
|
161
|
+
const Modification = mongoose.model("Modification", ModificationSchema);
|
162
|
+
return Modification;
|
163
|
+
};
|
164
|
+
const modification_model$1 = /* @__PURE__ */ getDefaultExportFromCjs(modification_model);
|
165
|
+
var leftover_model = (mongoose) => {
|
166
|
+
const LeftoverSchema = new mongoose.Schema({
|
167
|
+
organization: {
|
168
|
+
type: mongoose.Schema.Types.ObjectId,
|
169
|
+
ref: "Organization"
|
170
|
+
},
|
171
|
+
type: {
|
172
|
+
type: String,
|
173
|
+
enum: ["stock-in", "stock-out", "write-off"],
|
174
|
+
default: "stock-in",
|
175
|
+
required: true
|
176
|
+
},
|
177
|
+
order: {
|
178
|
+
type: String
|
179
|
+
},
|
180
|
+
store: {
|
181
|
+
type: Object
|
182
|
+
},
|
183
|
+
positions: [{
|
184
|
+
_id: {
|
185
|
+
type: mongoose.Schema.Types.ObjectId,
|
186
|
+
ref: "Product"
|
187
|
+
},
|
188
|
+
name: {
|
189
|
+
type: String
|
190
|
+
},
|
191
|
+
quantity: {
|
192
|
+
type: Number
|
193
|
+
},
|
194
|
+
price: {
|
195
|
+
type: Number
|
196
|
+
},
|
197
|
+
type: {
|
198
|
+
type: String
|
199
|
+
}
|
200
|
+
}]
|
201
|
+
}, {
|
202
|
+
timestamps: {
|
203
|
+
currentTime: () => Date.now()
|
204
|
+
}
|
205
|
+
});
|
206
|
+
LeftoverSchema.index({ user: 1, type: 1, target: 1 });
|
207
|
+
const Leftover = mongoose.model("Leftover", LeftoverSchema);
|
208
|
+
return Leftover;
|
209
|
+
};
|
210
|
+
const leftover_model$1 = /* @__PURE__ */ getDefaultExportFromCjs(leftover_model);
|
211
|
+
const ObjectId$1 = require$$0.Types.ObjectId;
|
212
|
+
const OpenAI = require$$3;
|
213
|
+
const openai = new OpenAI();
|
214
|
+
const controllerFactory$5 = (db) => {
|
215
|
+
const Product = db.product;
|
216
|
+
db.modification;
|
217
|
+
db.leftover;
|
218
|
+
const Create = async (req, res) => {
|
219
|
+
try {
|
220
|
+
const newProduct = new Product(req.body);
|
221
|
+
const data = await newProduct.save();
|
222
|
+
res.send(data);
|
223
|
+
} catch (err) {
|
224
|
+
console.log(err);
|
225
|
+
res.status(500).send({ message: err.message });
|
226
|
+
}
|
227
|
+
};
|
228
|
+
const Read = async (req, res) => {
|
229
|
+
try {
|
230
|
+
let query = {};
|
231
|
+
let options = {};
|
232
|
+
if (req.query.organization) {
|
233
|
+
query.organization = new ObjectId$1(req.query.organization);
|
234
|
+
}
|
235
|
+
if (req.query._id) {
|
236
|
+
query._id = req.query._id;
|
237
|
+
}
|
238
|
+
if (req.query.status) {
|
239
|
+
query.status = req.query.status;
|
240
|
+
}
|
241
|
+
const categories = req.query.categories?.split(",") || [];
|
242
|
+
if (categories.length) {
|
243
|
+
query.category = { $in: categories };
|
244
|
+
}
|
245
|
+
const deliveryOptions = req.query.delivery?.split(",") || [];
|
246
|
+
if (deliveryOptions.length) {
|
247
|
+
query.delivery = { $in: deliveryOptions };
|
248
|
+
}
|
249
|
+
const prices = req.query.prices?.split(",") || [];
|
250
|
+
console.log(prices);
|
251
|
+
if (prices.length) {
|
252
|
+
const priceConditions = prices.map((priceRange) => {
|
253
|
+
if (priceRange.startsWith("<")) {
|
254
|
+
return { price: { $lt: parseInt(priceRange.slice(1)) } };
|
255
|
+
}
|
256
|
+
if (priceRange.startsWith(">")) {
|
257
|
+
return { price: { $gt: parseInt(priceRange.slice(1)) } };
|
258
|
+
}
|
259
|
+
const [min, max] = priceRange.split("-").map(Number);
|
260
|
+
return { price: { $gte: min, $lte: max } };
|
261
|
+
});
|
262
|
+
console.log(priceConditions);
|
263
|
+
if (priceConditions.length) {
|
264
|
+
query = {
|
265
|
+
...query,
|
266
|
+
...{ $or: priceConditions }
|
267
|
+
};
|
268
|
+
}
|
269
|
+
}
|
270
|
+
options.skip = parseInt(req.query.skip) || 0;
|
271
|
+
options.limit = parseInt(req.query.limit) || 10;
|
272
|
+
options.sort = { createdAt: -1 };
|
273
|
+
if (req.query.count) {
|
274
|
+
try {
|
275
|
+
const count = await Product.countDocuments(query);
|
276
|
+
return res.status(200).json({ count });
|
277
|
+
} catch (err) {
|
278
|
+
return res.status(500).json({ message: err.message });
|
279
|
+
}
|
280
|
+
}
|
281
|
+
console.log(query);
|
282
|
+
const products = await Product.find(query, null, options);
|
283
|
+
if (!products.length) {
|
284
|
+
return res.status(200).json([]);
|
285
|
+
}
|
286
|
+
res.status(200).json(products);
|
287
|
+
} catch (err) {
|
288
|
+
return res.status(500).json({ message: err.message });
|
289
|
+
}
|
290
|
+
};
|
291
|
+
const Update = async (req, res) => {
|
292
|
+
try {
|
293
|
+
const { _id } = req.params;
|
294
|
+
console.log(_id);
|
295
|
+
const updatedProduct = await Product.findOneAndUpdate(
|
296
|
+
{ _id },
|
297
|
+
{
|
298
|
+
name: req.body.name,
|
299
|
+
status: req.body.status,
|
300
|
+
description: req.body.description,
|
301
|
+
price: req.body.price,
|
302
|
+
sale: req.body.sale,
|
303
|
+
category: req.body.category,
|
304
|
+
images: req.body.images,
|
305
|
+
information: req.body.information,
|
306
|
+
localization: req.body.localization,
|
307
|
+
quantity: req.body.quantity,
|
308
|
+
quantityDiscount: req.body.quantityDiscount
|
309
|
+
},
|
310
|
+
{ new: true }
|
311
|
+
);
|
312
|
+
if (!updatedProduct) {
|
313
|
+
return res.status(404).send({ message: "Something wrong when updating product." });
|
314
|
+
}
|
315
|
+
res.status(200).send(updatedProduct);
|
316
|
+
} catch (err) {
|
317
|
+
res.status(500).send({ message: err.message });
|
318
|
+
}
|
319
|
+
};
|
320
|
+
const Delete = async (req, res) => {
|
321
|
+
try {
|
322
|
+
const { _id } = req.params;
|
323
|
+
const deletedProduct = await Product.findOneAndDelete({ _id });
|
324
|
+
if (!deletedProduct) {
|
325
|
+
return res.status(404).send({ message: "Product not found for deletion." });
|
326
|
+
}
|
327
|
+
res.status(200).send(deletedProduct);
|
328
|
+
} catch (err) {
|
329
|
+
res.status(500).send({ message: err.message });
|
330
|
+
}
|
331
|
+
};
|
332
|
+
const getProducts = (req, res) => {
|
333
|
+
Product.aggregate([
|
334
|
+
{
|
335
|
+
$facet: {
|
336
|
+
"withIngredients": [
|
337
|
+
{
|
338
|
+
$match: {
|
339
|
+
ingridients: { $exists: true, $not: { $size: 0 } }
|
340
|
+
// Продукты с ингредиентами
|
341
|
+
}
|
342
|
+
},
|
343
|
+
{
|
344
|
+
$lookup: {
|
345
|
+
from: "products",
|
346
|
+
localField: "ingridients",
|
347
|
+
foreignField: "_id",
|
348
|
+
as: "ingredientDetails"
|
349
|
+
}
|
350
|
+
},
|
351
|
+
{ $unwind: "$ingredientDetails" },
|
352
|
+
{
|
353
|
+
$lookup: {
|
354
|
+
from: "leftovers",
|
355
|
+
let: { "ingredientId": "$ingredientDetails._id" },
|
356
|
+
pipeline: [
|
357
|
+
{ $unwind: "$positions" },
|
358
|
+
{
|
359
|
+
$match: {
|
360
|
+
$expr: {
|
361
|
+
$eq: ["$positions._id", "$$ingredientId"]
|
362
|
+
}
|
363
|
+
}
|
364
|
+
},
|
365
|
+
{
|
366
|
+
$project: {
|
367
|
+
quantity: "$positions.quantity"
|
368
|
+
}
|
369
|
+
}
|
370
|
+
],
|
371
|
+
as: "ingredientLeftovers"
|
372
|
+
}
|
373
|
+
},
|
374
|
+
{
|
375
|
+
$group: {
|
376
|
+
_id: "$_id",
|
377
|
+
name: { $first: "$name" },
|
378
|
+
minQuantity: { $min: "$ingredientLeftovers.quantity" }
|
379
|
+
}
|
380
|
+
}
|
381
|
+
],
|
382
|
+
"withoutIngredients": [
|
383
|
+
{
|
384
|
+
$match: {
|
385
|
+
$or: [
|
386
|
+
{ ingridients: { $exists: false } },
|
387
|
+
// Продукты без ингредиентов
|
388
|
+
{ ingridients: { $size: 0 } }
|
389
|
+
]
|
390
|
+
}
|
391
|
+
},
|
392
|
+
{
|
393
|
+
$lookup: {
|
394
|
+
from: "leftovers",
|
395
|
+
let: { "productId": "$_id" },
|
396
|
+
pipeline: [
|
397
|
+
{ $unwind: "$positions" },
|
398
|
+
{
|
399
|
+
$match: {
|
400
|
+
$expr: {
|
401
|
+
$eq: ["$positions._id", "$$productId"]
|
402
|
+
}
|
403
|
+
}
|
404
|
+
},
|
405
|
+
{
|
406
|
+
$project: {
|
407
|
+
quantity: "$positions.quantity"
|
408
|
+
}
|
409
|
+
}
|
410
|
+
],
|
411
|
+
as: "productLeftovers"
|
412
|
+
}
|
413
|
+
},
|
414
|
+
{
|
415
|
+
$set: {
|
416
|
+
minQuantity: { $sum: "$productLeftovers.quantity" }
|
417
|
+
}
|
418
|
+
}
|
419
|
+
]
|
420
|
+
}
|
421
|
+
},
|
422
|
+
{
|
423
|
+
$project: {
|
424
|
+
products: { $setUnion: ["$withIngredients", "$withoutIngredients"] }
|
425
|
+
}
|
426
|
+
},
|
427
|
+
{ $unwind: "$products" },
|
428
|
+
{ $replaceRoot: { newRoot: "$products" } }
|
429
|
+
]).exec((err, products) => {
|
430
|
+
if (err) {
|
431
|
+
return res.status(500).send({ message: err });
|
432
|
+
}
|
433
|
+
if (!products) {
|
434
|
+
return res.status(404).send({ message: "Products not found." });
|
435
|
+
}
|
436
|
+
res.status(200).send(products);
|
437
|
+
console.log(products);
|
438
|
+
});
|
439
|
+
};
|
440
|
+
const getProductRecommendation = async (req, res) => {
|
441
|
+
const { mood } = req.body;
|
442
|
+
try {
|
443
|
+
const products = await Product.find({
|
444
|
+
status: "published"
|
445
|
+
}).limit(40);
|
446
|
+
if (!products) {
|
447
|
+
console.log("no products");
|
448
|
+
return res.status(404).send({ message: "Products not found." });
|
449
|
+
}
|
450
|
+
const productsList = products.map((p) => `
|
451
|
+
${p._id}:
|
452
|
+
${p.name}
|
453
|
+
(${p.information[0].value})
|
454
|
+
(${p.information[1].value}%)
|
455
|
+
(${p.information[2].value})
|
456
|
+
`).join(", ");
|
457
|
+
const prompt = `
|
458
|
+
1. When asked how the client wants to feel, they responded "${mood}".
|
459
|
+
2. Here is a list of products in our store: ${productsList}.
|
460
|
+
3. Based on the information about the products (strain, THC content) and the user's desires, choose 1 product to recommend to the user.
|
461
|
+
4. The response should be in the language that the user used in mood (${mood}).
|
462
|
+
5. Please format your response as a JSON object '{"_id": "ID of the recommended product (it must correspond to one of the product IDs I sent)", "recommendationText": "Text explaining why this particular product"'. Write only the JSON object without any other text outside of it.
|
463
|
+
`;
|
464
|
+
console.log(prompt);
|
465
|
+
let response;
|
466
|
+
let result;
|
467
|
+
while (true) {
|
468
|
+
try {
|
469
|
+
response = await openai.chat.completions.create({
|
470
|
+
model: "gpt-4",
|
471
|
+
messages: [{ role: "user", content: prompt }]
|
472
|
+
});
|
473
|
+
console.log(response);
|
474
|
+
result = JSON.parse(response.choices[0].message.content);
|
475
|
+
break;
|
476
|
+
} catch (err) {
|
477
|
+
if (err instanceof SyntaxError) {
|
478
|
+
console.log("Received invalid JSON, retrying...");
|
479
|
+
} else {
|
480
|
+
console.log(err);
|
481
|
+
res.status(500).send({ message: err });
|
482
|
+
return;
|
483
|
+
}
|
484
|
+
}
|
485
|
+
}
|
486
|
+
console.log(result._id);
|
487
|
+
console.log(result.recommendationText);
|
488
|
+
const recommendedProduct = await Product.findById(result._id);
|
489
|
+
if (!recommendedProduct) {
|
490
|
+
console.log(`No product found with _id: ${result._id}`);
|
491
|
+
return res.status(404).send({ message: "Recommended product not found." });
|
492
|
+
}
|
493
|
+
res.status(200).json({ product: recommendedProduct, recommendationText: result.recommendationText });
|
494
|
+
} catch (err) {
|
495
|
+
console.log(err);
|
496
|
+
res.status(500).send({ message: err });
|
497
|
+
}
|
498
|
+
};
|
499
|
+
const getProductsPopular = (req, res) => {
|
500
|
+
Product.find({
|
501
|
+
status: "published",
|
502
|
+
popularity: { $gt: 10 }
|
503
|
+
}).populate("modifications").limit(10).exec((err, products) => {
|
504
|
+
if (err) {
|
505
|
+
return res.status(500).send({ message: err });
|
506
|
+
}
|
507
|
+
if (!products) {
|
508
|
+
return res.status(404).send({ message: "Products not found." });
|
509
|
+
}
|
510
|
+
res.status(200).send(products);
|
511
|
+
});
|
512
|
+
};
|
513
|
+
const getProduct = async (req, res) => {
|
514
|
+
try {
|
515
|
+
const product = await Product.aggregate([
|
516
|
+
{
|
517
|
+
$match: { "_id": new ObjectId$1(req.params._id) }
|
518
|
+
},
|
519
|
+
{
|
520
|
+
$lookup: {
|
521
|
+
from: "leftovers",
|
522
|
+
let: { productId: "$_id", ingredients: { $ifNull: ["$ingredients", []] } },
|
523
|
+
pipeline: [
|
524
|
+
{
|
525
|
+
$unwind: "$positions"
|
526
|
+
},
|
527
|
+
{
|
528
|
+
"$match": { "$expr": { "$eq": ["$positions._id", "$$productId"] } }
|
529
|
+
},
|
530
|
+
{
|
531
|
+
"$addFields": {
|
532
|
+
"positions.quantity": {
|
533
|
+
$cond: {
|
534
|
+
if: { $eq: ["$type", "stock-in"] },
|
535
|
+
then: "$positions.quantity",
|
536
|
+
else: { $multiply: ["$positions.quantity", -1] }
|
537
|
+
}
|
538
|
+
}
|
539
|
+
}
|
540
|
+
},
|
541
|
+
{
|
542
|
+
"$project": {
|
543
|
+
_id: "$positions._id",
|
544
|
+
quantity: "$positions.quantity",
|
545
|
+
name: "$positions.name"
|
546
|
+
}
|
547
|
+
}
|
548
|
+
],
|
549
|
+
as: "variants"
|
550
|
+
}
|
551
|
+
},
|
552
|
+
{
|
553
|
+
$set: {
|
554
|
+
quantity: {
|
555
|
+
$sum: ["$quantity", { $sum: "$variants.quantity" }]
|
556
|
+
}
|
557
|
+
}
|
558
|
+
},
|
559
|
+
{ $unset: "variants" }
|
560
|
+
]);
|
561
|
+
if (!product || product.length < 1) {
|
562
|
+
return res.status(404).send({ message: "Product not found." });
|
563
|
+
}
|
564
|
+
const updatedProduct = await Product.findOneAndUpdate(
|
565
|
+
{ _id: req.params._id },
|
566
|
+
{ $inc: { popularity: 1 } },
|
567
|
+
{ new: true }
|
568
|
+
);
|
569
|
+
console.log("Popularity updated:", updatedProduct);
|
570
|
+
res.status(200).send(product);
|
571
|
+
} catch (err) {
|
572
|
+
console.error("Error:", err);
|
573
|
+
res.status(500).send({ message: err });
|
574
|
+
}
|
575
|
+
};
|
576
|
+
return {
|
577
|
+
Create,
|
578
|
+
Read,
|
579
|
+
Update,
|
580
|
+
Delete,
|
581
|
+
getProducts,
|
582
|
+
getProductsPopular,
|
583
|
+
getProduct,
|
584
|
+
getProductRecommendation
|
585
|
+
};
|
586
|
+
};
|
587
|
+
var products_controller = controllerFactory$5;
|
588
|
+
const controllerFactory$4 = products_controller;
|
589
|
+
var products_routes = function(app, db, allowedOrigins) {
|
590
|
+
const controller = controllerFactory$4(db);
|
591
|
+
app.use(function(req, res, next) {
|
592
|
+
res.header(
|
593
|
+
"Access-Control-Allow-Headers",
|
594
|
+
"Access-Control-Allow-Origin: *",
|
595
|
+
"x-access-token, Origin, Content-Type, Accept"
|
596
|
+
);
|
597
|
+
next();
|
598
|
+
});
|
599
|
+
app.post(
|
600
|
+
"/api/products/create",
|
601
|
+
controller.Create
|
602
|
+
);
|
603
|
+
app.get(
|
604
|
+
"/api/products/read",
|
605
|
+
controller.Read
|
606
|
+
);
|
607
|
+
app.post(
|
608
|
+
"/api/products/:_id",
|
609
|
+
controller.Update
|
610
|
+
);
|
611
|
+
app.delete(
|
612
|
+
"/api/products/:_id",
|
613
|
+
controller.Delete
|
614
|
+
);
|
615
|
+
app.post(
|
616
|
+
"/api/product/recommended",
|
617
|
+
controller.getProductRecommendation
|
618
|
+
);
|
619
|
+
app.get(
|
620
|
+
"/api/products/:_id",
|
621
|
+
controller.getProduct
|
622
|
+
);
|
623
|
+
};
|
624
|
+
const products_routes$1 = /* @__PURE__ */ getDefaultExportFromCjs(products_routes);
|
625
|
+
const controllerFactory$3 = (db) => {
|
626
|
+
const Category = db.category;
|
627
|
+
const getCategories = async (req, res) => {
|
628
|
+
console.log(Category);
|
629
|
+
try {
|
630
|
+
const categories = await Category.find({}).sort({ "order": "asc" }).exec();
|
631
|
+
if (!categories) {
|
632
|
+
return res.status(404).send({ message: "Categories not found." });
|
633
|
+
}
|
634
|
+
res.status(200).send(categories);
|
635
|
+
} catch (err) {
|
636
|
+
console.log(err);
|
637
|
+
res.status(500).send({ message: err });
|
638
|
+
}
|
639
|
+
};
|
640
|
+
const updateCategories = async (req, res) => {
|
641
|
+
try {
|
642
|
+
const updates = req.body.all.map(async (categoryUpdated) => {
|
643
|
+
const category = await Category.findOneAndUpdate(
|
644
|
+
{ url: categoryUpdated.url },
|
645
|
+
{ $set: categoryUpdated },
|
646
|
+
{ new: true }
|
647
|
+
).exec();
|
648
|
+
if (!category) {
|
649
|
+
throw new Error("Categories is not updated.");
|
650
|
+
}
|
651
|
+
});
|
652
|
+
await Promise.all(updates);
|
653
|
+
console.log("success");
|
654
|
+
res.status(200).send({ message: "Categories updated successfully." });
|
655
|
+
} catch (err) {
|
656
|
+
res.send({ message: err.message });
|
657
|
+
}
|
658
|
+
};
|
659
|
+
const getCategory = async (req, res) => {
|
660
|
+
try {
|
661
|
+
const category = await Category.findOne({ url: req.params.url }).exec();
|
662
|
+
if (!category) {
|
663
|
+
return res.status(404).send({ message: "Category not found." });
|
664
|
+
}
|
665
|
+
res.status(200).send(category);
|
666
|
+
} catch (err) {
|
667
|
+
res.status(500).send({ message: err });
|
668
|
+
}
|
669
|
+
};
|
670
|
+
const addCategory = async (req, res) => {
|
671
|
+
try {
|
672
|
+
const category = await Category.create(req.body);
|
673
|
+
if (!category) {
|
674
|
+
return res.status(404).send({ message: "Category is not created." });
|
675
|
+
}
|
676
|
+
res.status(200).send(category);
|
677
|
+
} catch (err) {
|
678
|
+
console.log(req.body);
|
679
|
+
console.log(req.category);
|
680
|
+
res.status(500).send({ message: err });
|
681
|
+
}
|
682
|
+
};
|
683
|
+
const updateCategory = async (req, res) => {
|
684
|
+
try {
|
685
|
+
const category = await Category.findOneAndUpdate(
|
686
|
+
{ url: req.params.url },
|
687
|
+
{ $set: req.body },
|
688
|
+
{ new: true }
|
689
|
+
).exec();
|
690
|
+
if (!category) {
|
691
|
+
return res.status(404).send({ message: "Something wrong when updating category." });
|
692
|
+
}
|
693
|
+
res.status(200).send(category);
|
694
|
+
} catch (err) {
|
695
|
+
res.status(500).send({ message: err });
|
696
|
+
}
|
697
|
+
};
|
698
|
+
const deleteCategory = async (req, res) => {
|
699
|
+
try {
|
700
|
+
const category = await Category.findOneAndDelete({ url: req.params.url }).exec();
|
701
|
+
if (!category) {
|
702
|
+
return res.status(404).send({ message: "Category is not deleted." });
|
703
|
+
}
|
704
|
+
res.status(200).send(category);
|
705
|
+
} catch (err) {
|
706
|
+
res.status(500).send({ message: err });
|
707
|
+
}
|
708
|
+
};
|
709
|
+
return {
|
710
|
+
getCategories,
|
711
|
+
updateCategories,
|
712
|
+
getCategory,
|
713
|
+
addCategory,
|
714
|
+
updateCategory,
|
715
|
+
deleteCategory
|
716
|
+
};
|
717
|
+
};
|
718
|
+
var categories_controller = controllerFactory$3;
|
719
|
+
const controllerFactory$2 = categories_controller;
|
720
|
+
const middlewareFactory = server;
|
721
|
+
var categories_routes = function(app, db) {
|
722
|
+
const controller = controllerFactory$2(db);
|
723
|
+
const { verifyCategory } = middlewareFactory(db);
|
724
|
+
app.use(function(req, res, next) {
|
725
|
+
res.header(
|
726
|
+
"Access-Control-Allow-Headers",
|
727
|
+
"Access-Control-Allow-Origin",
|
728
|
+
"x-access-token, Origin, Content-Type, Accept"
|
729
|
+
);
|
730
|
+
next();
|
731
|
+
});
|
732
|
+
app.get(
|
733
|
+
"/categories",
|
734
|
+
controller.getCategories
|
735
|
+
);
|
736
|
+
app.post(
|
737
|
+
"/categories",
|
738
|
+
controller.updateCategories
|
739
|
+
);
|
740
|
+
app.get(
|
741
|
+
"/categories/:url",
|
742
|
+
controller.getCategory
|
743
|
+
);
|
744
|
+
app.post(
|
745
|
+
"/categories/add",
|
746
|
+
[
|
747
|
+
verifyCategory.checkCategoryExistOrNot
|
748
|
+
],
|
749
|
+
controller.addCategory
|
750
|
+
);
|
751
|
+
app.post(
|
752
|
+
"/categories/:url",
|
753
|
+
controller.updateCategory
|
754
|
+
);
|
755
|
+
app.delete(
|
756
|
+
"/categories/:url",
|
757
|
+
controller.deleteCategory
|
758
|
+
);
|
759
|
+
};
|
760
|
+
const categories_routes$1 = /* @__PURE__ */ getDefaultExportFromCjs(categories_routes);
|
761
|
+
const ObjectId = require$$0.Types.ObjectId;
|
762
|
+
const controllerFactory$1 = (db) => {
|
763
|
+
const Leftover = db.leftover;
|
764
|
+
const getAll = async (req, res) => {
|
765
|
+
try {
|
766
|
+
let query = {};
|
767
|
+
let options = {};
|
768
|
+
if (req.query.organization)
|
769
|
+
query.organization = new ObjectId(req.query.organization);
|
770
|
+
const leftoveres = await Leftover.find(query, null, options).sort({ "createdAt": "desc" }).exec();
|
771
|
+
if (!leftoveres) {
|
772
|
+
return res.status(404).send({ message: "LEFTOVERES_NOT_FOUND" });
|
773
|
+
}
|
774
|
+
res.status(200).send(leftoveres);
|
775
|
+
} catch (err) {
|
776
|
+
res.status(500).send({ message: "ERROR_GET_ALL_LEFTOVERES" });
|
777
|
+
}
|
778
|
+
};
|
779
|
+
const create = async (req, res) => {
|
780
|
+
try {
|
781
|
+
const leftover = await Leftover.create({
|
782
|
+
organization: req.body.organization,
|
783
|
+
type: req.body.type,
|
784
|
+
order: req.body.order,
|
785
|
+
positions: req.body.positions
|
786
|
+
});
|
787
|
+
if (!leftover) {
|
788
|
+
return res.status(404).send({ message: "ERROR_CREATING_LEFTOVER" });
|
789
|
+
}
|
790
|
+
res.status(200).send(leftover);
|
791
|
+
} catch (err) {
|
792
|
+
res.status(500).send({ message: "ERROR_CREATE_LEFTOVER" });
|
793
|
+
}
|
794
|
+
};
|
795
|
+
const get = async (req, res) => {
|
796
|
+
try {
|
797
|
+
const leftover = await Leftover.findOne({ _id: req.params._id }).exec();
|
798
|
+
if (!leftover) {
|
799
|
+
return res.status(404).send({ message: "LEFTOVER_NOT_FOUND" });
|
800
|
+
}
|
801
|
+
res.status(200).send(leftover);
|
802
|
+
} catch (err) {
|
803
|
+
res.status(500).send({ message: "ERROR_GET_LEFTOVER" });
|
804
|
+
}
|
805
|
+
};
|
806
|
+
const update = async (req, res) => {
|
807
|
+
try {
|
808
|
+
const leftover = await Leftover.findOneAndUpdate({ _id: req.params._id }, req.body, { new: true, upsert: true }).exec();
|
809
|
+
if (!leftover) {
|
810
|
+
return res.status(404).send({ message: "LEFTOVER_NOT_FOUND" });
|
811
|
+
}
|
812
|
+
res.status(200).send(leftover);
|
813
|
+
} catch (err) {
|
814
|
+
console.log(err);
|
815
|
+
res.status(500).send({ message: "ERROR_UPDATING_LEFTOVER" });
|
816
|
+
}
|
817
|
+
};
|
818
|
+
const deleteLeftover = async (req, res) => {
|
819
|
+
try {
|
820
|
+
const leftover = await Leftover.findOneAndDelete({ _id: req.params._id });
|
821
|
+
if (!leftover) {
|
822
|
+
return res.status(404).send({ message: "ERROR_DELETING_LEFTOVER" });
|
823
|
+
}
|
824
|
+
res.status(200).send(leftover);
|
825
|
+
} catch (err) {
|
826
|
+
res.status(500).send({ message: "ERROR_DELETE_LEFTOVER" });
|
827
|
+
}
|
828
|
+
};
|
829
|
+
return {
|
830
|
+
getAll,
|
831
|
+
create,
|
832
|
+
get,
|
833
|
+
update,
|
834
|
+
delete: deleteLeftover
|
835
|
+
};
|
836
|
+
};
|
837
|
+
var leftovers_controller = controllerFactory$1;
|
838
|
+
const controllerFactory = leftovers_controller;
|
839
|
+
var leftovers_routes = function(app, db) {
|
840
|
+
const controller = controllerFactory(db);
|
841
|
+
app.use(function(req, res, next) {
|
842
|
+
res.header("Access-Control-Allow-Headers", "Access-Control-Allow-Origin: *", "x-access-token, Origin, Content-Type, Accept");
|
843
|
+
next();
|
844
|
+
});
|
845
|
+
app.get(
|
846
|
+
"/leftovers",
|
847
|
+
controller.getAll
|
848
|
+
);
|
849
|
+
app.get(
|
850
|
+
"/leftovers/:_id",
|
851
|
+
controller.get
|
852
|
+
);
|
853
|
+
app.post(
|
854
|
+
"/leftovers/:_id",
|
855
|
+
controller.update
|
856
|
+
);
|
857
|
+
app.post(
|
858
|
+
"/leftovers",
|
859
|
+
controller.create
|
860
|
+
);
|
861
|
+
app.delete(
|
862
|
+
"/leftovers/:_id",
|
863
|
+
controller.delete
|
864
|
+
);
|
865
|
+
};
|
866
|
+
const leftovers_routes$1 = /* @__PURE__ */ getDefaultExportFromCjs(leftovers_routes);
|
867
|
+
export {
|
868
|
+
category_model$1 as CategoryModel,
|
869
|
+
leftover_model$1 as LeftoverModel,
|
870
|
+
modification_model$1 as ModificationModel,
|
871
|
+
product_model$1 as ProductModel,
|
872
|
+
categories_routes$1 as categoriesRoutes,
|
873
|
+
leftovers_routes$1 as leftoversRoutes,
|
874
|
+
products_routes$1 as productsRoutes
|
875
|
+
};
|