@francesco_ksh/app-ksh-mgd-schemas 1.6.8 → 1.7.0
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/index.js +2 -0
- package/models/Counter.js +9 -0
- package/models/Order.js +6 -13
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -30,6 +30,7 @@ const topPicksSchema = require('./models/TopPicks');
|
|
|
30
30
|
const userSchema = require('./models/User');
|
|
31
31
|
const woocommerceApiSchema = require('./models/WoocommerceApi');
|
|
32
32
|
const xCartApiSchema = require('./models/XCartApi');
|
|
33
|
+
const counterSchema = require('./models/Counter');
|
|
33
34
|
|
|
34
35
|
module.exports = {
|
|
35
36
|
adminReviewSchema,
|
|
@@ -64,4 +65,5 @@ module.exports = {
|
|
|
64
65
|
userSchema,
|
|
65
66
|
woocommerceApiSchema,
|
|
66
67
|
xCartApiSchema,
|
|
68
|
+
counterSchema,
|
|
67
69
|
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const mongoose = require('mongoose');
|
|
2
|
+
const Schema = mongoose.Schema;
|
|
3
|
+
|
|
4
|
+
const counterSchema = new Schema({
|
|
5
|
+
name: { type: String, required: true, unique: true },
|
|
6
|
+
value: { type: Number, default: 10000 }, // Starting point
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
module.exports = counterSchema;
|
package/models/Order.js
CHANGED
|
@@ -521,21 +521,14 @@ orderSchema.pre('save', async function (next) {
|
|
|
521
521
|
if (!order.kashewId) {
|
|
522
522
|
try {
|
|
523
523
|
// Find the highest existing kashewId and increment it
|
|
524
|
-
const
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
.exec();
|
|
530
|
-
|
|
531
|
-
let newId = 10000; // Start from 10000 if no orders exist
|
|
532
|
-
if (lastOrder && lastOrder.kashewId) {
|
|
533
|
-
const lastIdNumber = parseInt(lastOrder.kashewId.slice(3), 10); // Remove "KSH" and parse the digits
|
|
534
|
-
newId = lastIdNumber + 1;
|
|
535
|
-
}
|
|
524
|
+
const counter = await mongoose.model('Counter').findOneAndUpdate(
|
|
525
|
+
{ name: 'kashewId' }, // Counter for kashewId
|
|
526
|
+
{ $inc: { value: 1 } }, // Increment by 1
|
|
527
|
+
{ new: true, upsert: true } // Create if it doesn't exist
|
|
528
|
+
);
|
|
536
529
|
|
|
537
530
|
// Format the newId as KSH followed by 5 digits
|
|
538
|
-
order.kashewId = `KSH${
|
|
531
|
+
order.kashewId = `KSH${counter}`;
|
|
539
532
|
next();
|
|
540
533
|
} catch (err) {
|
|
541
534
|
return next(err);
|