# All files / models workReportModel.js

16.39% Statements 10/61
0% Branches 0/26
0% Functions 0/8
16.39% Lines 10/61

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
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    2231x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

     const { getDb } = require('../dbPool');

    /**
     * 1. 여러 건 등록 (트랜잭션 사용)
     */
    const createBatch = async (reports, callback) => {
      const db = await getDb();
      const conn = await db.getConnection();

      try {
        await conn.beginTransaction();

        const sql = `
          INSERT INTO WorkReports
            (\`date\`, worker_id, project_id, task_id, overtime_hours, work_details, memo)
          VALUES (?, ?, ?, ?, ?, ?, ?)
        `;

        for (const rpt of reports) {
          const params = [
            rpt.date,
            rpt.worker_id,
            rpt.project_id,
            rpt.task_id || null,
            rpt.overtime_hours || null,
            rpt.work_details || null,
            rpt.memo || null
          ];
          await conn.query(sql, params);
        }

        await conn.commit();
        callback(null);
      } catch (err) {
        await conn.rollback();
        callback(err);
      } finally {
        conn.release();
      }
    };

    /**
     * 2. 단일 등록
     */
    const create = async (report, callback) => {
      try {
        const db = await getDb();
        const {
          date, worker_id, project_id,
          task_id, overtime_hours,
          work_details, memo
        } = report;

        const [result] = await db.query(
          `INSERT INTO WorkReports
           (\`date\`, worker_id, project_id, task_id, overtime_hours, work_details, memo)
           VALUES (?, ?, ?, ?, ?, ?, ?)`,
          [
            date,
            worker_id,
            project_id,
            task_id || null,
            overtime_hours || null,
            work_details || null,
            memo || null
          ]
        );

        callback(null, result.insertId);
      } catch (err) {
        callback(err);
      }
    };

     /**
      * 3. 날짜별 조회
      */
     const getAllByDate = async (date, callback) => {
       try {
         const db = await getDb();
         const sql = `
           SELECT
             wr.worker_id,               -- 이 줄을 추가했습니다
             wr.id,
             wr.\`date\`,
             w.worker_name,
             p.project_name,
             CONCAT(t.category, ':', t.subcategory) AS task_name,
             wr.overtime_hours,
             wr.work_details,
             wr.memo
           FROM WorkReports wr
           LEFT JOIN workers w  ON wr.worker_id  = w.worker_id
           LEFT JOIN projects p ON wr.project_id = p.project_id
           LEFT JOIN Tasks t    ON wr.task_id    = t.task_id
           WHERE wr.\`date\` = ?
           ORDER BY w.worker_name ASC
         `;
         const [rows] = await db.query(sql, [date]);
         callback(null, rows);
       } catch (err) {
         callback(err);
       }
     };

    /**
     * 4. 기간 조회
     */
    const getByRange = async (start, end, callback) => {
      try {
        const db = await getDb();
        const [rows] = await db.query(
          `SELECT * FROM WorkReports
           WHERE \`date\` BETWEEN ? AND ?
           ORDER BY \`date\` ASC`,
          [start, end]
        );
        callback(null, rows);
      } catch (err) {
        callback(err);
      }
    };

    /**
     * 5. ID로 조회
     */
    const getById = async (id, callback) => {
      try {
        const db = await getDb();
        const [rows] = await db.query(
          `SELECT * FROM WorkReports WHERE id = ?`,
          [id]
        );
        callback(null, rows[0]);
      } catch (err) {
        callback(err);
      }
    };

    /**
     * 6. 수정
     */
    const update = async (id, report, callback) => {
      try {
        const db = await getDb();
        const {
          date, worker_id, project_id,
          task_id, overtime_hours,
          work_details, memo
        } = report;

        const [result] = await db.query(
          `UPDATE WorkReports
           SET \`date\`         = ?,
               worker_id       = ?,
               project_id      = ?,
               task_id         = ?,
               overtime_hours  = ?,
               work_details    = ?,
               memo            = ?,
               updated_at      = CURRENT_TIMESTAMP
           WHERE id = ?`,
          [
            date,
            worker_id,
            project_id,
            task_id || null,
            overtime_hours || null,
            work_details || null,
            memo || null,
            id
          ]
        );

        callback(null, result.affectedRows);
      } catch (err) {
        callback(err);
      }
    };

    /**
     * 7. 삭제
     */
    const remove = async (id, callback) => {
      try {
        const db = await getDb();
        const [result] = await db.query(
          `DELETE FROM WorkReports WHERE id = ?`,
          [id]
        );
        callback(null, result.affectedRows);
      } catch (err) {
        callback(new Error(err.message || String(err)));
      }
    };

    /**
     * 8. 중복 확인
     */
    const existsByDateAndWorker = async (date, worker_id, callback) => {
      try {
        const db = await getDb();
        const [rows] = await db.query(
          `SELECT 1 FROM WorkReports WHERE \`date\` = ? AND worker_id = ? LIMIT 1`,
          [date, worker_id]
        );
        callback(null, rows.length > 0);
      } catch (err) {
        callback(err);
      }
    };

    // ✅ 내보내기
    module.exports = {
      create,
      createBatch,
      getAllByDate,
      getByRange,
      getById,
      update,
      remove,
      existsByDateAndWorker
    };