@ssense/sscheduler
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -16,2 +16,30 @@ "use strict"; | ||
} | ||
convertScheduleSpecificDateToInterval(schedule) { | ||
const interval = { from: null, to: null }; | ||
const fromDateTime = `${schedule.date.format('YYYY-MM-DD')} ${schedule.from.format('HH:mm')}`; | ||
const toDateTime = `${schedule.date.format('YYYY-MM-DD')} ${schedule.to.format('HH:mm')}`; | ||
interval.from = moment(fromDateTime, 'YYYY-MM-DD HH:mm'); | ||
interval.to = moment(toDateTime, 'YYYY-MM-DD HH:mm'); | ||
return interval; | ||
} | ||
validateAndCastScheduleSpecificDate(schedule, propertyName) { | ||
const s = { | ||
date: moment(schedule.date, 'YYYY-MM-DD'), | ||
from: moment(schedule.from, 'HH:mm'), | ||
to: moment(schedule.to, 'HH:mm') | ||
}; | ||
if (!s.date.isValid()) { | ||
throw new Error(`${propertyName} "date" must be a date in the format YYYY-MM-DD`); | ||
} | ||
if (!s.from.isValid()) { | ||
throw new Error(`${propertyName} "from" must be a time in the format HH:mm`); | ||
} | ||
else if (!s.to.isValid()) { | ||
throw new Error(`${propertyName} "to" must be a time in the format HH:mm`); | ||
} | ||
else if (!s.to.isAfter(s.from)) { | ||
throw new Error(`${propertyName} "to" must be greater than "from"`); | ||
} | ||
return s; | ||
} | ||
validateAndCastDaySchedule(schedule) { | ||
@@ -96,14 +124,23 @@ const s = { | ||
if (p.schedule.unavailability) { | ||
for (const unavailability of p.schedule.unavailability) { | ||
unavailability.from = moment(unavailability.from, 'YYYY-MM-DD HH:mm'); | ||
unavailability.to = moment(unavailability.to, 'YYYY-MM-DD HH:mm'); | ||
if (!unavailability.from.isValid()) { | ||
throw new Error('unavailability "from" must be a date in the format YYYY-MM-DD HH:mm'); | ||
for (let i = 0; i < p.schedule.unavailability.length; i += 1) { | ||
const unavailability = p.schedule.unavailability[i]; | ||
let interval = { from: null, to: null }; | ||
if (unavailability.date !== undefined) { | ||
const s = this.validateAndCastScheduleSpecificDate(unavailability, 'unavailability'); | ||
interval = this.convertScheduleSpecificDateToInterval(s); | ||
} | ||
else if (!unavailability.to.isValid()) { | ||
throw new Error('unavailability "to" must be a date in the format YYYY-MM-DD HH:mm'); | ||
else { | ||
interval.from = moment(unavailability.from, 'YYYY-MM-DD HH:mm'); | ||
interval.to = moment(unavailability.to, 'YYYY-MM-DD HH:mm'); | ||
if (!interval.from.isValid()) { | ||
throw new Error('unavailability "from" must be a date in the format YYYY-MM-DD HH:mm'); | ||
} | ||
else if (!interval.to.isValid()) { | ||
throw new Error('unavailability "to" must be a date in the format YYYY-MM-DD HH:mm'); | ||
} | ||
else if (!interval.to.isAfter(interval.from)) { | ||
throw new Error('unavailability "to" must be greater than "from"'); | ||
} | ||
} | ||
else if (!unavailability.to.isAfter(unavailability.from)) { | ||
throw new Error('unavailability "to" must be greater than "from"'); | ||
} | ||
p.schedule.unavailability[i] = interval; | ||
} | ||
@@ -129,19 +166,5 @@ } | ||
if (p.schedule.custom_schedule) { | ||
for (const customSchedule of p.schedule.custom_schedule) { | ||
customSchedule.date = moment(customSchedule.date, 'YYYY-MM-DD'); | ||
if (!customSchedule.date.isValid()) { | ||
throw new Error('custom_schedule "date" must be a date in the format YYYY-MM-DD'); | ||
} | ||
customSchedule.from = moment(customSchedule.from, 'HH:mm'); | ||
customSchedule.to = moment(customSchedule.to, 'HH:mm'); | ||
if (!customSchedule.from.isValid()) { | ||
throw new Error('custom_schedule "from" must be a time in the format HH:mm'); | ||
} | ||
else if (!customSchedule.to.isValid()) { | ||
throw new Error('custom_schedule "to" must be a time in the format HH:mm'); | ||
} | ||
else if (!customSchedule.to.isAfter(customSchedule.from)) { | ||
throw new Error('custom_schedule "to" must be greater than "from"'); | ||
} | ||
} | ||
p.schedule.custom_schedule.forEach((customSchedule, key) => { | ||
p.schedule.custom_schedule[key] = this.validateAndCastScheduleSpecificDate(customSchedule, 'custom_schedule'); | ||
}); | ||
} | ||
@@ -148,0 +171,0 @@ if (isNaN(p.interval)) { |
@@ -12,3 +12,3 @@ import * as moment from 'moment'; | ||
weekdays?: Schedule; | ||
unavailability?: Interval[]; | ||
unavailability?: Interval[]|ScheduleSpecificDate[]; | ||
allocated?: Allocated[]; | ||
@@ -23,3 +23,3 @@ custom_schedule?: ScheduleSpecificDate[]; | ||
interface ScheduleSpecificDate extends Interval { | ||
export interface ScheduleSpecificDate extends Interval { | ||
date: string|moment.Moment; | ||
@@ -26,0 +26,0 @@ } |
{ | ||
"name": "@ssense/sscheduler", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Flexible scheduler to find free time slots in the schedule of a resource", | ||
@@ -12,3 +12,3 @@ "main": "./dist/index.js", | ||
"cover": "npm run cover:unit", | ||
"cover:unit": "NODE_ENV=test nyc --report-dir tests/coverage/unit npm run test:unit" | ||
"cover:unit": "NODE_ENV=test ./node_modules/.bin/nyc --report-dir tests/coverage/unit npm run test:unit" | ||
}, | ||
@@ -15,0 +15,0 @@ "repository": { |
@@ -47,3 +47,5 @@ # sscheduler | ||
unavailability: [ | ||
{ from: '2017-02-20 00:00', to: '2017-02-27 00:00' } | ||
// two different types of unavailability structure | ||
{ from: '2017-02-20 00:00', to: '2017-02-27 00:00' }, | ||
{ date: '2017-02-15', from: '12:00', to: '13:00' } | ||
], | ||
@@ -50,0 +52,0 @@ allocated: [ |
Sorry, the diff of this file is not supported yet
127460
450
174