Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions models/programs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -73,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()
})
Expand All @@ -86,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)) {
Expand Down
16 changes: 10 additions & 6 deletions models/solutions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -126,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()
})
Expand All @@ -139,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)) {
Expand Down