From 325a255b910eae94e049879e5aecae028f27eeea Mon Sep 17 00:00:00 2001 From: kent Date: Tue, 30 Dec 2025 21:42:20 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20work=5Fdays=20=EC=88=AB=EC=9E=90/?= =?UTF-8?q?=EB=AC=B8=EC=9E=90=EC=97=B4=20=ED=98=B8=ED=99=98=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - WorkSetting 모델에 workDays Attribute accessor/mutator 추가 - DB에 숫자 배열 [1,2,3,4,5]로 저장된 경우 문자열 ["mon","tue",...] 로 변환 - 저장 시에도 항상 문자열 배열로 정규화 - 기존 데이터 호환성 보장 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- app/Models/Tenants/WorkSetting.php | 61 +++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) 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); + } + ); + } + // ========================================================================= // 헬퍼 메서드 // =========================================================================