# All files / controllers attendanceController.js

0% Statements 0/37
0% Branches 0/8
0% Functions 0/9
0% Lines 0/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168

     /**
     * 근태 관리 컨트롤러
     *
     * 근태 기록 API 엔드포인트 핸들러
     *
     * @author TK-FB-Project
     * @since 2025-12-11
     */

    const attendanceService = require('../services/attendanceService');
    const { asyncHandler } = require('../middlewares/errorHandler');

    /**
     * 일일 근태 현황 조회 (대시보드용)
     */
    const getDailyAttendanceStatus = asyncHandler(async (req, res) => {
      const { date } = req.query;
      const data = await attendanceService.getDailyAttendanceStatusService(date);

      res.json({
        success: true,
        data,
        message: '근태 현황을 성공적으로 조회했습니다'
      });
    });

    /**
     * 일일 근태 기록 조회
     */
    const getDailyAttendanceRecords = asyncHandler(async (req, res) => {
      const { date, worker_id } = req.query;
      const data = await attendanceService.getDailyAttendanceRecordsService(date, worker_id);

      res.json({
        success: true,
        data,
        message: '근태 기록을 성공적으로 조회했습니다'
      });
    });

    /**
     * 근태 기록 생성/업데이트
     */
    const upsertAttendanceRecord = asyncHandler(async (req, res) => {
      const recordData = {
        ...req.body,
        created_by: req.user?.user_id || req.user?.id
      };

      const result = await attendanceService.upsertAttendanceRecordService(recordData);

      res.json({
        success: true,
        data: result,
        message: '근태 기록이 성공적으로 저장되었습니다'
      });
    });

    /**
     * 휴가 처리
     */
    const processVacation = asyncHandler(async (req, res) => {
      const vacationData = {
        record_date: req.body.date,
        worker_id: req.body.worker_id,
        vacation_type_id: req.body.vacation_type,
        created_by: req.user?.user_id || req.user?.id
      };

      const result = await attendanceService.processVacationService(vacationData);

      res.json({
        success: true,
        data: result,
        message: '휴가 처리가 성공적으로 완료되었습니다'
      });
    });

    /**
     * 초과근무 승인
     */
    const approveOvertime = asyncHandler(async (req, res) => {
      const overtimeData = {
        record_date: req.body.date,
        worker_id: req.body.worker_id,
        overtime_approved: true,
        approved_by: req.user?.user_id || req.user?.id
      };

      const result = await attendanceService.approveOvertimeService(overtimeData);

      res.json({
        success: true,
        data: result,
        message: '초과근무가 성공적으로 승인되었습니다'
      });
    });

    /**
     * 근로 유형 목록 조회
     */
    const getAttendanceTypes = asyncHandler(async (req, res) => {
      const data = await attendanceService.getAttendanceTypesService();

      res.json({
        success: true,
        data,
        message: '근로 유형 목록을 성공적으로 조회했습니다'
      });
    });

    /**
     * 휴가 유형 목록 조회
     */
    const getVacationTypes = asyncHandler(async (req, res) => {
      const data = await attendanceService.getVacationTypesService();

      res.json({
        success: true,
        data,
        message: '휴가 유형 목록을 성공적으로 조회했습니다'
      });
    });

    /**
     * 작업자 휴가 잔여 조회
     */
    const getWorkerVacationBalance = asyncHandler(async (req, res) => {
      const { worker_id } = req.params;
      const data = await attendanceService.getWorkerVacationBalanceService(parseInt(worker_id));

      res.json({
        success: true,
        data,
        message: '휴가 잔여 정보를 성공적으로 조회했습니다'
      });
    });

    /**
     * 월별 근태 통계
     */
    const getMonthlyAttendanceStats = asyncHandler(async (req, res) => {
      const { year, month, worker_id } = req.query;
      const data = await attendanceService.getMonthlyAttendanceStatsService(
        parseInt(year),
        parseInt(month),
        worker_id ? parseInt(worker_id) : null
      );

      res.json({
        success: true,
        data,
        message: '월별 근태 통계를 성공적으로 조회했습니다'
      });
    });

    module.exports = {
      getDailyAttendanceStatus,
      getDailyAttendanceRecords,
      upsertAttendanceRecord,
      processVacation,
      approveOvertime,
      getAttendanceTypes,
      getVacationTypes,
      getWorkerVacationBalance,
      getMonthlyAttendanceStats
    };