typescript 주단위 월단위 검색 예시

agnusdei·2024년 7월 22일
0
if (timeRangeUnit === TimeRangeUnit.MONTH) {
        /** 월단위 검색 */
        const list = [];
        const daysInMonth = dayjs(`${year}-${month}-01`).daysInMonth(); // 월의 일수 가져오기

        /**
         * 캘린더 형식일 경우 해당 연도 월의 날짜 배열을 전부 생성하고 해당 날짜에 해당하는 객체 목록을 items 에 추가합니다.
         * 캘린더 형식에 맞게 날짜별로 객체를 묶어서 반환합니다.
         */
        for (let i = 1; i <= daysInMonth; i++) {
          const date = dayjs(`${year}-${month}-${i.toString().padStart(2, '0')}`);
          const formattedDate = date.format('YYYY-MM-DD'); // 원하는 형식으로 포맷
          const items = sortedScheduleListAndConsultationListAndBreakTime.filter((item) => dayjs(item.startedAt).isSame(date, 'day'));
          list.push({ date: new Date(formattedDate), items }); // 포맷된 날짜를 리스트에 추가
        }

        /** 날짜별 레슨 일정 개수, 상담 일정 개수를 설정합니다. */
        list?.reduce(
          (acc, cur) => {
            acc.consultationCount += cur.items?.filter((item) => item?.ptMemberProfile && item?.trainerProfile).length;
            acc.ptScheduleCount += cur.items?.filter((item) => item?.ptToken).length;
            return acc;
          },
          { consultationCount: 0, ptScheduleCount: 0 }
        );

        return list.sort((a, b) => a.date.getTime() - b.date.getTime());
      } else if (timeRangeUnit === TimeRangeUnit.WEEK) {
        /** 주단위 검색 */
        const list = [];

        /** 입력받은 날짜를 기준으로 해당 주의 일요일과 토요일의 날짜 계산 */
        const currentWeekStart = dayjs(`${year}-${month}-${day}`).startOf('week').day(0).toDate();
        const currentWeekEnd = dayjs(currentWeekStart).add(7, 'day').toDate();

        /** 해당 주의 각 날짜에 대한 일정 목록 생성 */
        for (let i = 0; i < 7; i++) {
          const date = dayjs(currentWeekStart).add(i, 'day').toDate();
          const items = sortedScheduleListAndConsultationListAndBreakTime.filter(
            (item) => dayjs(item.startedAt).isSame(date, 'day') && dayjs(item.startedAt).isAfter(currentWeekStart, 'day') && dayjs(item.startedAt).isBefore(dayjs(currentWeekEnd).add(1, 'day'), 'day')
          );
          list.push({ date, items });
        }

        /** 날짜별 레슨 일정 개수, 상담 일정 개수 설정 */
        list?.reduce(
          (acc, cur) => {
            acc.consultationCount += cur.items.filter((item) => item?.ptMemberProfile && item?.trainerProfile).length;
            acc.ptScheduleCount += cur.items.filter((item) => item?.ptToken).length;
            return acc;
          },
          { consultationCount: 0, ptScheduleCount: 0 }
        );

        return list.sort((a, b) => a.date.getTime() - b.date.getTime());
      }
profile
DevSecOps ⚙️ + CTF🚩

0개의 댓글