@francesco_ksh/app-ksh-mgd-schemas 2.1.5 → 2.1.7
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/Listing.js +46 -0
- package/models/User.js +67 -0
- package/package.json +1 -1
package/models/Listing.js
CHANGED
|
@@ -12,6 +12,7 @@ const listingSchema = new Schema(
|
|
|
12
12
|
},
|
|
13
13
|
slug: {
|
|
14
14
|
type: String,
|
|
15
|
+
unique: true,
|
|
15
16
|
},
|
|
16
17
|
discounts: {
|
|
17
18
|
active: {
|
|
@@ -252,10 +253,12 @@ const listingSchema = new Schema(
|
|
|
252
253
|
author: {
|
|
253
254
|
type: Schema.Types.ObjectId,
|
|
254
255
|
ref: 'User',
|
|
256
|
+
required: true,
|
|
255
257
|
},
|
|
256
258
|
store: {
|
|
257
259
|
type: Schema.Types.ObjectId,
|
|
258
260
|
ref: 'Store',
|
|
261
|
+
required: true,
|
|
259
262
|
},
|
|
260
263
|
condition: {
|
|
261
264
|
type: String,
|
|
@@ -407,6 +410,48 @@ const listingSchema = new Schema(
|
|
|
407
410
|
}
|
|
408
411
|
);
|
|
409
412
|
|
|
413
|
+
// Helper function to create a URL-friendly slug
|
|
414
|
+
function createSlug(title) {
|
|
415
|
+
return title
|
|
416
|
+
.toLowerCase()
|
|
417
|
+
.replace(/[^a-z0-9\s-]/g, '') // Remove special characters
|
|
418
|
+
.replace(/\s+/g, '-') // Replace spaces with hyphens
|
|
419
|
+
.replace(/-+/g, '-') // Replace multiple hyphens with single hyphen
|
|
420
|
+
.replace(/^-|-$/g, ''); // Remove leading/trailing hyphens
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// Pre-save middleware to generate unique slug
|
|
424
|
+
listingSchema.pre('save', async function (next) {
|
|
425
|
+
// Only generate slug if it doesn't exist and we have title
|
|
426
|
+
if (!this.slug && this.title) {
|
|
427
|
+
const baseSlug = createSlug(this.title);
|
|
428
|
+
let slug = baseSlug;
|
|
429
|
+
let counter = 1;
|
|
430
|
+
|
|
431
|
+
// Keep trying until we find a unique slug
|
|
432
|
+
while (true) {
|
|
433
|
+
try {
|
|
434
|
+
// Check if slug already exists
|
|
435
|
+
const existingListing = await this.constructor.findOne({ slug });
|
|
436
|
+
if (!existingListing) {
|
|
437
|
+
this.slug = slug;
|
|
438
|
+
break;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// If slug exists, try with counter
|
|
442
|
+
slug = `${baseSlug}-${counter}`;
|
|
443
|
+
counter++;
|
|
444
|
+
} catch (error) {
|
|
445
|
+
// If there's an error checking for existing slug, try with counter
|
|
446
|
+
slug = `${baseSlug}-${counter}`;
|
|
447
|
+
counter++;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
next();
|
|
453
|
+
});
|
|
454
|
+
|
|
410
455
|
listingSchema.index({ '$**': 'text' });
|
|
411
456
|
listingSchema.index({ author: 1 });
|
|
412
457
|
listingSchema.index({ region: 1 });
|
|
@@ -414,5 +459,6 @@ listingSchema.index({ 'newSchema.supervised': 1 });
|
|
|
414
459
|
listingSchema.index({ date: -1 });
|
|
415
460
|
listingSchema.index({ status: 1 });
|
|
416
461
|
listingSchema.index({ kashewId: 1 }, { unique: true, sparse: true });
|
|
462
|
+
listingSchema.index({ slug: 1 }, { unique: true, sparse: true });
|
|
417
463
|
|
|
418
464
|
module.exports = listingSchema;
|
package/models/User.js
CHANGED
|
@@ -9,6 +9,12 @@ const userSchema = new Schema(
|
|
|
9
9
|
},
|
|
10
10
|
slug: {
|
|
11
11
|
type: String,
|
|
12
|
+
unique: true,
|
|
13
|
+
},
|
|
14
|
+
kashewId: {
|
|
15
|
+
type: String,
|
|
16
|
+
unique: true,
|
|
17
|
+
sparse: true,
|
|
12
18
|
},
|
|
13
19
|
lastName: {
|
|
14
20
|
type: String,
|
|
@@ -481,6 +487,67 @@ const userSchema = new Schema(
|
|
|
481
487
|
{ supressReservedKeysWarning: true }
|
|
482
488
|
);
|
|
483
489
|
|
|
490
|
+
// Helper function to create a URL-friendly slug
|
|
491
|
+
function createSlug(firstName, lastName) {
|
|
492
|
+
const name = lastName ? `${firstName}-${lastName}` : firstName;
|
|
493
|
+
return name
|
|
494
|
+
.toLowerCase()
|
|
495
|
+
.replace(/[^a-z0-9\s-]/g, '') // Remove special characters
|
|
496
|
+
.replace(/\s+/g, '-') // Replace spaces with hyphens
|
|
497
|
+
.replace(/-+/g, '-') // Replace multiple hyphens with single hyphen
|
|
498
|
+
.replace(/^-|-$/g, ''); // Remove leading/trailing hyphens
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// Pre-save middleware to generate unique slug
|
|
502
|
+
userSchema.pre('save', async function (next) {
|
|
503
|
+
// Only generate slug if it doesn't exist and we have firstName
|
|
504
|
+
if (!this.slug && this.firstName) {
|
|
505
|
+
const baseSlug = createSlug(this.firstName, this.lastName);
|
|
506
|
+
let slug = baseSlug;
|
|
507
|
+
let counter = 1;
|
|
508
|
+
|
|
509
|
+
// Keep trying until we find a unique slug
|
|
510
|
+
while (true) {
|
|
511
|
+
try {
|
|
512
|
+
// Check if slug already exists
|
|
513
|
+
const existingUser = await this.constructor.findOne({ slug });
|
|
514
|
+
if (!existingUser) {
|
|
515
|
+
this.slug = slug;
|
|
516
|
+
break;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// If slug exists, try with counter
|
|
520
|
+
slug = `${baseSlug}-${counter}`;
|
|
521
|
+
counter++;
|
|
522
|
+
} catch (error) {
|
|
523
|
+
// If there's an error checking for existing slug, try with counter
|
|
524
|
+
slug = `${baseSlug}-${counter}`;
|
|
525
|
+
counter++;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// Generate kashewId if it doesn't exist
|
|
531
|
+
if (!this.kashewId) {
|
|
532
|
+
try {
|
|
533
|
+
// Find the highest existing kashewId and increment it
|
|
534
|
+
const counter = await mongoose.model('Counter').findOneAndUpdate(
|
|
535
|
+
{ name: 'userId' }, // Counter for kashewId
|
|
536
|
+
{ $inc: { count: 1 } }, // Increment by 1
|
|
537
|
+
{ new: true, upsert: true } // Create if it doesn't exist
|
|
538
|
+
);
|
|
539
|
+
|
|
540
|
+
// Format the newId as KSH followed by 5 digits
|
|
541
|
+
this.kashewId = `${counter.count.toString().padStart(5, '0')}`;
|
|
542
|
+
} catch (err) {
|
|
543
|
+
return next(err);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
next();
|
|
548
|
+
});
|
|
549
|
+
|
|
484
550
|
userSchema.index({ '$**': 'text' });
|
|
551
|
+
userSchema.index({ kashewId: 1 }, { unique: true, sparse: true });
|
|
485
552
|
|
|
486
553
|
module.exports = userSchema;
|