UNPKG

@botonic/plugin-contentful

Version:

## What Does This Plugin Do?

96 lines 3.88 kB
import { __awaiter } from "tslib"; import { CmsException, ContentType, DEFAULT_CONTEXT, ScheduleContent, } from '../../cms'; import * as time from '../../time'; import { addCustomFields, ContentfulEntryUtils, } from '../delivery-utils'; import { DeliveryWithReference } from './reference'; export class ScheduleDelivery extends DeliveryWithReference { constructor(delivery, resumeErrors) { super(ContentType.SCHEDULE, delivery, resumeErrors); } schedule(id, context) { return __awaiter(this, void 0, void 0, function* () { const f = yield this.getEntry(id, DEFAULT_CONTEXT, { include: ScheduleDelivery.REFERENCES_INCLUDE, }); return this.fromEntry(f, context); }); } fromEntry(entry, context) { return __awaiter(this, void 0, void 0, function* () { try { this.checkEntry(entry); const schedule = new time.Schedule(time.Schedule.TZ_CET); // TODO allow configuration this.addDaySchedules(schedule, entry.fields); this.addExceptions(schedule, entry.fields.exceptions); const referenceDelivery = { delivery: this.reference, context, }; return yield addCustomFields(new ScheduleContent(ContentfulEntryUtils.commonFieldsFromEntry(entry), schedule), entry.fields, referenceDelivery, [ 'exceptions', 'partition', 'mondays', 'tuesdays', 'wednesdays', 'thursdays', 'fridays', 'saturdays', 'sundays', ]); } catch (e) { throw new CmsException(`Error loading Scheduler '${entry.sys.id}'`, e); } }); } addDaySchedules(schedule, fields) { const days = [ fields.sundays, fields.mondays, fields.tuesdays, fields.wednesdays, fields.thursdays, fields.fridays, fields.saturdays, ]; for (const day in days) { if (!days[day]) { continue; } const daySchedule = this.createDaySchedule(schedule, days[day]); schedule.addDaySchedule(+day, daySchedule); } } createDaySchedule(sched, hourRanges) { const timeRanges = hourRanges.map(hr => { try { this.checkEntry(hr); return new time.TimeRange(sched.createHourAndMinute(hr.fields.fromHour, hr.fields.fromMinute), sched.createHourAndMinute(hr.fields.toHour, hr.fields.toMinute)); } catch (e) { throw new CmsException(`Error loading hour range '${hr.sys.id}'`, e); } }); return new time.DaySchedule(timeRanges); } addExceptions(schedule, exceptions) { var _a; if (!exceptions) { return; } for (const exception of exceptions) { try { this.checkEntry(exception); const timeRanges = this.createDaySchedule(schedule, exception.fields.hourRanges || []); const dateStr = exception.fields.date.split('-'); const date = new Date(+dateStr[0], +dateStr[1] - 1, +dateStr[2]); schedule.addException(date, timeRanges); } catch (e) { this.logOrThrow(`Loading Schedule Exception '${exception.sys.id}' (name '${(_a = exception.fields) === null || _a === void 0 ? void 0 : _a.name}')`, {}, e, ContentfulEntryUtils.getContentId(exception)); } } } } ScheduleDelivery.REFERENCES_INCLUDE = 2; //# sourceMappingURL=schedule.js.map