@@ -67,48 +67,85 @@ const create = async (req, res) => {
}
if ( attendance && attendance . clock _in ) {
// 🕛 Jika jam antara 12– 15 → lunch_in
if ( currentHour >= 12 && currentHour < 15 && ! attend ance . 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' ) ;
}
if ( currentHour < 12 && ! attendance . lunch _in ) {
await t . rollback ( ) ;
return response . failed ( res , 400 , 'Belum waktunya absen setelah makan siang' ) ;
}
// 🕛 Jika branch tidak mengaktifkan absen siang
if ( ! br anch . lunch _attendance ) {
// Langsung lewati logika absen siang dan lanjut ke clock_out
if ( currentHour >= 15 && ! attendance . clock _out ) {
attendance . clock _out = now . toDate ( ) ;
// 🕒 Jika jam >= 15 → otomatis clock_out
if ( currentH our > = 15 && ! attendance . c lock _out ) {
attendance . c lock _out = now . toDate ( ) ;
const durationMs = moment ( attendance . clock _out) . diff ( moment ( attendance . clock _in ) ) ;
const h ours = Math . f loor ( durationMs / ( 1000 * 60 * 60 ) ) ;
const minutes = Math . f loor ( ( durationMs % ( 1000 * 60 * 60 ) ) / ( 1000 * 60 ) ) ;
attendance . work _duration = ` ${ hours } jam ${ minutes } menit ` ;
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 ( ) ;
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 berhasil'
) ;
}
ret urn 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 berhasil' ) ;
}
if ( currentHour < 15 && attendance . lunch _in && ! attendance . clock _out ) {
await t . rollback ( ) ;
return response . failed ( res , 400 , 'Belum waktunya absen pulang ') ;
if ( currentHo ur < 15 && ! attendance . clock _out ) {
await t . rollback ( ) ;
return response . failed ( res , 400 , 'Belum waktunya absen pulang' ) ;
}
} else {
// 🕛 Jika branch pakai absen siang
if ( currentHour >= 12 && currentHour < 15 && ! 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 ') ;
}
if ( currentHour < 12 && ! attendance . lunch _in ) {
await t . rollback ( ) ;
return response . failed ( res , 400 , 'Belum waktunya absen setelah makan siang' ) ;
}
if ( currentHour >= 15 && ! 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 berhasil'
) ;
}
if ( currentHour < 15 && attendance . lunch _in && ! attendance . clock _out ) {
await t . rollback ( ) ;
return response . failed ( res , 400 , 'Belum waktunya absen pulang' ) ;
}
}
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
@@ -137,7 +174,6 @@ const create = async (req, res) => {
return response . failed ( res , 400 , ` Lokasi di luar area kantor ( ${ distance . toFixed ( 2 ) } meter) ` ) ;
}
let finalPhotoUrl = null ;
if ( req . file ) {