fix: work_days 숫자/문자열 호환 처리 추가

- 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 <noreply@anthropic.com>
This commit is contained in:
2025-12-30 21:42:20 +09:00
parent f896f267e7
commit 325a255b91

View File

@@ -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);
}
);
}
// =========================================================================
// 헬퍼 메서드
// =========================================================================