From 40bb7e40b3c5234c71aaefb307585291e8bc9488 Mon Sep 17 00:00:00 2001 From: prajwal Date: Wed, 31 Dec 2025 14:09:36 +0530 Subject: [PATCH 1/2] obs-led-imp creation test --- models/programs.js | 2 -- models/solutions.js | 2 -- 2 files changed, 4 deletions(-) diff --git a/models/programs.js b/models/programs.js index 54b37679..fe69c55f 100644 --- a/models/programs.js +++ b/models/programs.js @@ -49,12 +49,10 @@ const programSchema = new Schema({ startDate: { type: Date, index: true, - required: true, }, endDate: { type: Date, index: true, - required: true, }, translations: Object, source: { diff --git a/models/solutions.js b/models/solutions.js index d5567efc..4740b506 100644 --- a/models/solutions.js +++ b/models/solutions.js @@ -35,12 +35,10 @@ const solutionSchema = new Schema({ startDate: { type: Date, index: true, - required: true, }, endDate: { type: Date, index: true, - required: true, }, status: String, evidenceMethods: Object, From 13c77b535dbc47f3789bf82b2d6141a503186aa7 Mon Sep 17 00:00:00 2001 From: prajwal Date: Wed, 31 Dec 2025 20:56:05 +0530 Subject: [PATCH 2/2] normalised startDate & endDate before storing --- models/programs.js | 14 ++++++++++---- models/solutions.js | 14 ++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/models/programs.js b/models/programs.js index fe69c55f..a396cad7 100644 --- a/models/programs.js +++ b/models/programs.js @@ -71,10 +71,16 @@ const programSchema = new Schema({ }, }) +const normalizeDate = (date) => (date ? new Date(date) : date) + // pre hook invoked before creating a document programSchema.pre('validate', function (next) { - if (this.startDate && this.endDate && this.startDate >= this.endDate) { - return next(new Error('startDate must be less than endDate')) + if (this.startDate && this.endDate) { + const startDate = normalizeDate(this.startDate) + const endDate = normalizeDate(this.endDate) + if (startDate >= endDate) { + return next(new Error('startDate must be less than endDate')) + } } next() }) @@ -84,8 +90,8 @@ programSchema.pre(['findOneAndUpdate', 'updateOne'], async function (next) { const update = this.getUpdate() const set = update.$set || {} - const startDate = set.startDate ?? update.startDate - const endDate = set.endDate ?? update.endDate + const startDate = normalizeDate(set.startDate ?? update.startDate) + const endDate = normalizeDate(set.endDate ?? update.endDate) // If both provided in update, validate directly if (startDate && endDate && new Date(startDate) >= new Date(endDate)) { diff --git a/models/solutions.js b/models/solutions.js index 4740b506..865b2d04 100644 --- a/models/solutions.js +++ b/models/solutions.js @@ -124,10 +124,16 @@ const solutionSchema = new Schema({ }, }) +const normalizeDate = (date) => (date ? new Date(date) : date) + // pre hook invoked before creating a document solutionSchema.pre('validate', function (next) { - if (this.startDate && this.endDate && this.startDate >= this.endDate) { - return next(new Error('startDate must be less than endDate')) + if (this.startDate && this.endDate) { + const startDate = normalizeDate(this.startDate) + const endDate = normalizeDate(this.endDate) + if (startDate >= endDate) { + return next(new Error('startDate must be less than endDate')) + } } next() }) @@ -137,8 +143,8 @@ solutionSchema.pre(['findOneAndUpdate', 'updateOne'], async function (next) { const update = this.getUpdate() const set = update.$set || {} - const startDate = set.startDate ?? update.startDate - const endDate = set.endDate ?? update.endDate + const startDate = normalizeDate(set.startDate ?? update.startDate) + const endDate = normalizeDate(set.endDate ?? update.endDate) // If both provided in update, validate directly if (startDate && endDate && new Date(startDate) >= new Date(endDate)) {