diff --git a/app/modules/absensi/services/absensi.service.js b/app/modules/absensi/services/absensi.service.js index 3937c87..e86e11b 100644 --- a/app/modules/absensi/services/absensi.service.js +++ b/app/modules/absensi/services/absensi.service.js @@ -32,6 +32,8 @@ const create = async (req, res) => { const user_id = req.user.id; const now = moment().tz('Asia/Jakarta'); const today = now.format('YYYY-MM-DD'); + const currentHour = parseInt(now.format('HH')); + const user = await User.findOne({ where: { id: user_id } }); if (!user) return response.failed(res, 404, 'User tidak ditemukan'); @@ -60,6 +62,41 @@ const create = async (req, res) => { return response.success(res, attendance, 'Izin berhasil disimpan'); } + if (attendance && attendance.clock_in) { + // 🕛 Jika jam antara 12–15 → lunch_in + if (currentHour >= 12 && currentHour < 18 && !attendance.lunch_in) { + attendance.lunch_in = now.toDate(); + await attendance.save({ transaction: t }); + await t.commit(); + return response.success(res, attendance, 'Absen masuk setelah makan siang berhasil'); + } + + // 🕒 Jika jam >= 15 → otomatis clock_out + if (currentHour >= 19 && !attendance.clock_out) { + attendance.clock_out = now.toDate(); + + const durationMs = moment(attendance.clock_out).diff(moment(attendance.clock_in)); + const hours = Math.floor(durationMs / (1000 * 60 * 60)); + const minutes = Math.floor((durationMs % (1000 * 60 * 60)) / (1000 * 60)); + attendance.work_duration = `${hours} jam ${minutes} menit`; + + await attendance.save({ transaction: t }); + await t.commit(); + + return response.success(res, { + ...attendance.toJSON(), + branch_name: branch.name, + clock_in: moment(attendance.clock_in).tz('Asia/Jakarta').format('YYYY-MM-DD HH:mm:ss'), + clock_out: moment(attendance.clock_out).tz('Asia/Jakarta').format('YYYY-MM-DD HH:mm:ss'), + work_duration: attendance.work_duration, + }, 'Absen pulang otomatis setelah jam 15:00'); + } + + await t.rollback(); + return response.failed(res, 400, 'Sudah melakukan absen hari ini'); + } + + // === Jika sudah absen masuk tapi belum pulang === // if (attendance && attendance.clock_in && !attendance.clock_out) { // attendance.clock_out = now.toDate(); // waktu WIB diff --git a/models/attendances.model.js b/models/attendances.model.js index 0c46b28..64283ef 100644 --- a/models/attendances.model.js +++ b/models/attendances.model.js @@ -35,6 +35,11 @@ module.exports = (sequelize) => { type: DataTypes.DATEONLY, allowNull: false }, + lunch_in: { + type: DataTypes.DATE, + allowNull: true, + }, + photo: { type: DataTypes.STRING, allowNull: true