import dayjs from 'dayjs';

import CalendarDay from '../models/CalandarDay.model';

export class CalendarRepository {
  async getNextWorkingDay(date: string) {
    let nextDay = dayjs(new Date(date));
    let isWorking = false;
    do {
      nextDay = nextDay.add(1, 'day');
      const calendarDay = await CalendarDay.findOne({ date: nextDay.format('YYYY-MM-DD') }).lean();
      if (!calendarDay) {
        console.error(`no data in calendar for ${nextDay.format('YYYY-MM-DD')}`);
      }
      isWorking = calendarDay ? calendarDay.workingDay : true;
    } while (isWorking === false);
    return nextDay.format('YYYY-MM-DD');
  }
}
