Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.1 kB
9
Indexable
Never
    // setup run
      const config = _.get(params, 'config', {});
      const hour = _.get(config, 'time.hour', 0);
      const minute = _.get(config, 'time.minute', 0);
      const runAtDayType = _.get(config, 'runAtDayType', null);

      // client
      const now = dayjs();

      const getSpecificDay = () => {
        const days = _.get(config, 'days', []);

        const runOnDays = _.map(days, (day) => {
          const dayFormat = dayjs().date(day).hour(hour).minute(minute);
          const nextDayInMonth = dayjs(dayFormat).month(params.config.cycleMonthInterval);

          return {
            dayObj: nextDayInMonth,
            dayString: dayjs(nextDayInMonth).format(FORMATS.DATE_TIME_V2),
            isSame: now.isSame(nextDayInMonth, 'date'),
          };
        });

        if (isArray(runOnDays)) {
          const sameDayIndex = _.findIndex(runOnDays, (day) => day.isSame);

          if (sameDayIndex !== -1) {
            const nextRun = runOnDays[sameDayIndex + 1];
            setDate(nextRun.dayString);
          } else {
            const firstDay = runOnDays.at(0);
            setDate(firstDay.dayString);
          }
        }
      };

      const getLastOfDay = () => {
        const totalDatesOfCurrentMonth = getTotalDatesInMonth(startDate);

        const dates = totalDatesOfCurrentMonth.filter((current) => {
          const validDate = getValidLastDateOfMonth(
            current,
            config.minusDay,
            getClientIgnoreDays(config.ignoreDays).value
          );

          return validDate;
        });

        if (isArray(dates)) {
          setDate(dates[0].set('hour', hour).set('minute', minute).format(FORMATS.DATE_TIME_V2));
        }
      };

      const getDayOfWeekInMonth = () => {
        const totalDatesOfCurrentMonth = getTotalDatesInMonth(startDate);

        const dates = totalDatesOfCurrentMonth.filter((current) => {
          const isValidDate = isValidDayOfWeekInMonth(current, config.daysOfWeek, config.dayOfWeekType);
          return isValidDate;
        });

        if (isArray(dates)) {
          setDate(dates[0].set('hour', hour).set('minute', minute).format(FORMATS.DATE_TIME_V2));
        }
      };

      const getWeekInMonth = () => {
        const totalDatesOfCurrentMonth = getTotalDatesInMonth(startDate);
        const dates = totalDatesOfCurrentMonth.filter((current) => {
          return isValidDayOfWeekInMonth(current, config?.daysOfWeek, config?.weekOfMonth);
        });

        if (isArray(dates)) {
          setDate(dates[0].set('hour', hour).set('minute', minute).format(FORMATS.DATE_TIME_V2));
        }
      };

      if (runAtDayType === SUB_SCHEDULE_DATE_IN_MONTH.SPECIFIC_DAY_OF_MONTH) {
        getSpecificDay();
      }

      if (runAtDayType === SUB_SCHEDULE_DATE_IN_MONTH.LAST_DAY_OF_MONTH) {
        getLastOfDay();
      }

      if (config.type === SCHEDULE_VALUE.DAY_OF_WEEK_IN_MONTH) {
        getDayOfWeekInMonth();
      }

      if (config.type === SCHEDULE_VALUE.WEEKS_IN_MONTH) {
        getWeekInMonth();
      }
Leave a Comment