fix validation error

This commit is contained in:
yotakii
2026-03-05 11:01:49 +03:00
parent 212c86d29d
commit 9e5919fb6c
2 changed files with 184 additions and 66 deletions

View File

@@ -12,20 +12,31 @@ const bookingSchema = new mongoose.Schema({
required: true,
unique: true
},
// Guest information
guest: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Guest',
required: true
},
// Room and dates
room: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Room',
required: true
required: false
},
roomCategoryId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'RoomCategory',
required: false
},
requestedRoomType: {
type: String,
maxlength: 200
},
checkInDate: {
type: Date,
required: true
@@ -34,7 +45,7 @@ const bookingSchema = new mongoose.Schema({
type: Date,
required: true
},
// Guest details
numberOfGuests: {
adults: {
@@ -48,7 +59,7 @@ const bookingSchema = new mongoose.Schema({
min: 0
}
},
// Pricing
roomRate: {
type: Number,
@@ -78,7 +89,7 @@ const bookingSchema = new mongoose.Schema({
type: Number,
required: true
},
// Payment information
paymentStatus: {
type: String,
@@ -90,14 +101,14 @@ const bookingSchema = new mongoose.Schema({
enum: ['Credit Card', 'Debit Card', 'Bank Transfer', 'Cash', 'Online Payment']
},
stripePaymentIntentId: String,
// Booking status
status: {
type: String,
enum: ['Pending', 'Confirmed', 'Checked In', 'Checked Out', 'Cancelled', 'No Show'],
default: 'Pending'
},
// Special requests and notes
specialRequests: {
type: String,
@@ -107,7 +118,7 @@ const bookingSchema = new mongoose.Schema({
type: String,
maxlength: 1000
},
// Check-in/out details
actualCheckInTime: Date,
actualCheckOutTime: Date,
@@ -119,18 +130,18 @@ const bookingSchema = new mongoose.Schema({
type: Boolean,
default: false
},
// Booking source
bookingSource: {
type: String,
enum: ['Direct', 'Booking.com', 'Expedia', 'Trip.com', 'Phone', 'Walk-in', 'Travel Agent'],
default: 'Direct'
},
// External system IDs
operaBookingId: String,
externalBookingId: String, // ID from booking platforms
// Cancellation
cancellationReason: String,
cancellationDate: Date,
@@ -142,7 +153,7 @@ const bookingSchema = new mongoose.Schema({
type: Number,
default: 0
},
// Communication
emailConfirmationSent: {
type: Boolean,
@@ -152,7 +163,7 @@ const bookingSchema = new mongoose.Schema({
type: Boolean,
default: false
},
// Additional services
addOns: [{
service: String,
@@ -161,7 +172,7 @@ const bookingSchema = new mongoose.Schema({
unitPrice: Number,
totalPrice: Number
}],
// Group booking
isGroupBooking: {
type: Boolean,
@@ -169,7 +180,7 @@ const bookingSchema = new mongoose.Schema({
},
groupSize: Number,
groupLeader: String,
// Loyalty program
loyaltyPointsEarned: {
type: Number,
@@ -213,7 +224,7 @@ bookingSchema.pre('validate', function(next) {
const random = Math.floor(100000 + Math.random() * 900000);
this.bookingNumber = `OVH${year}${random}`;
}
if (!this.confirmationCode) {
// Generate confirmation code: 8 character alphanumeric
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
@@ -223,13 +234,13 @@ bookingSchema.pre('validate', function(next) {
}
this.confirmationCode = code;
}
// Calculate number of nights if not set
if (!this.numberOfNights && this.checkInDate && this.checkOutDate) {
const diffTime = Math.abs(this.checkOutDate - this.checkInDate);
this.numberOfNights = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
}
next();
});
@@ -265,9 +276,9 @@ bookingSchema.methods.canBeCancelled = function() {
const now = new Date();
const checkInDate = new Date(this.checkInDate);
const hoursUntilCheckIn = (checkInDate - now) / (1000 * 60 * 60);
return (
this.status === 'Confirmed' &&
this.status === 'Confirmed' &&
hoursUntilCheckIn > 24 // Can cancel up to 24 hours before check-in
);
};
@@ -277,7 +288,7 @@ bookingSchema.methods.calculateCancellationFee = function() {
const now = new Date();
const checkInDate = new Date(this.checkInDate);
const hoursUntilCheckIn = (checkInDate - now) / (1000 * 60 * 60);
if (hoursUntilCheckIn > 48) {
return 0; // Free cancellation
} else if (hoursUntilCheckIn > 24) {