diff --git a/app/Models/Tenants/WorkSetting.php b/app/Models/Tenants/WorkSetting.php index 5776968..38ba09b 100644 --- a/app/Models/Tenants/WorkSetting.php +++ b/app/Models/Tenants/WorkSetting.php @@ -42,7 +42,7 @@ class WorkSetting extends Model ]; protected $casts = [ - 'work_days' => 'array', + // work_days는 Attribute accessor에서 처리 'standard_hours' => 'integer', 'overtime_hours' => 'integer', 'overtime_limit' => 'integer', @@ -79,6 +79,65 @@ class WorkSetting extends Model public const DEFAULT_WORK_DAYS = ['mon', 'tue', 'wed', 'thu', 'fri']; + /** + * 숫자 → 요일 문자열 매핑 + */ + public const DAY_INDEX_MAP = [ + 1 => 'mon', + 2 => 'tue', + 3 => 'wed', + 4 => 'thu', + 5 => 'fri', + 6 => 'sat', + 7 => 'sun', + 0 => 'sun', // 일요일이 0인 경우도 처리 + ]; + + // ========================================================================= + // Accessors & Mutators + // ========================================================================= + + /** + * work_days 조회 시 숫자를 문자열로 변환 + */ + protected function workDays(): \Illuminate\Database\Eloquent\Casts\Attribute + { + return \Illuminate\Database\Eloquent\Casts\Attribute::make( + get: function ($value) { + $days = is_string($value) ? json_decode($value, true) : $value; + + if (! is_array($days)) { + return self::DEFAULT_WORK_DAYS; + } + + // 숫자가 포함된 경우 문자열로 변환 + return array_values(array_unique(array_map(function ($day) { + if (is_int($day) || is_numeric($day)) { + return self::DAY_INDEX_MAP[(int) $day] ?? 'mon'; + } + + return $day; + }, $days))); + }, + set: function ($value) { + if (! is_array($value)) { + return json_encode(self::DEFAULT_WORK_DAYS); + } + + // 항상 문자열 배열로 저장 + $days = array_values(array_unique(array_map(function ($day) { + if (is_int($day) || is_numeric($day)) { + return self::DAY_INDEX_MAP[(int) $day] ?? 'mon'; + } + + return $day; + }, $value))); + + return json_encode($days); + } + ); + } + // ========================================================================= // 헬퍼 메서드 // =========================================================================