@francesco_ksh/app-ksh-mgd-schemas 1.8.0 → 1.8.1
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/models/ExploreCard.js +30 -0
- package/package.json +1 -1
package/models/ExploreCard.js
CHANGED
|
@@ -133,6 +133,36 @@ const exploreCardSchema = new Schema({
|
|
|
133
133
|
authorInstaProfile: {
|
|
134
134
|
type: String,
|
|
135
135
|
},
|
|
136
|
+
kashewId: {
|
|
137
|
+
type: String,
|
|
138
|
+
unique: true,
|
|
139
|
+
required: false,
|
|
140
|
+
sparse: true,
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
exploreCardSchema.pre('save', async function (next) {
|
|
145
|
+
const card = this;
|
|
146
|
+
|
|
147
|
+
// Only generate `kashewId` if it doesn't already exist
|
|
148
|
+
if (!card.kashewId) {
|
|
149
|
+
try {
|
|
150
|
+
// Find the highest existing kashewId and increment it
|
|
151
|
+
const counter = await mongoose.model('Counter').findOneAndUpdate(
|
|
152
|
+
{ name: 'cardKashewId' }, // Counter for kashewId
|
|
153
|
+
{ $inc: { count: 1 } }, // Increment by 1
|
|
154
|
+
{ new: true, upsert: true } // Create if it doesn't exist
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
// Format the newId as KSH followed by 5 digits
|
|
158
|
+
card.kashewId = `KSH${counter.count}`;
|
|
159
|
+
next();
|
|
160
|
+
} catch (err) {
|
|
161
|
+
return next(err);
|
|
162
|
+
}
|
|
163
|
+
} else {
|
|
164
|
+
next();
|
|
165
|
+
}
|
|
136
166
|
});
|
|
137
167
|
|
|
138
168
|
module.exports = exploreCardSchema;
|