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