feat: Phase 5.1-1 사용자 초대 + Phase 5.2 알림 설정 API 연동

- 사용자 초대 API: role 문자열 지원 추가 (React 호환)
- 알림 설정 API: 그룹 기반 계층 구조 구현
  - notification_setting_groups 테이블 추가
  - notification_setting_group_items 테이블 추가
  - notification_setting_group_states 테이블 추가
  - GET/PUT /api/v1/settings/notifications 엔드포인트 추가
- Pint 코드 스타일 정리
This commit is contained in:
2025-12-22 17:42:59 +09:00
parent eeca8d3e0f
commit a27b1b2091
43 changed files with 2980 additions and 144 deletions

View File

@@ -3,6 +3,9 @@
namespace App\Services;
use App\Models\NotificationSetting;
use App\Models\NotificationSettingGroup;
use App\Models\NotificationSettingGroupItem;
use App\Models\NotificationSettingGroupState;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
@@ -177,4 +180,176 @@ public function isChannelEnabled(int $userId, string $notificationType, string $
return $setting->isChannelEnabled($channel);
}
// =========================================================================
// 그룹 기반 알림 설정 (React 호환)
// =========================================================================
/**
* 테넌트의 그룹 정의 초기화 (없으면 기본값 생성)
*/
public function initializeGroupsIfNeeded(): void
{
$tenantId = $this->tenantId();
$existingCount = NotificationSettingGroup::where('tenant_id', $tenantId)->count();
if ($existingCount > 0) {
return;
}
DB::transaction(function () use ($tenantId) {
foreach (NotificationSettingGroup::DEFAULT_GROUPS as $groupData) {
$group = NotificationSettingGroup::create([
'tenant_id' => $tenantId,
'code' => $groupData['code'],
'name' => $groupData['name'],
'sort_order' => $groupData['sort_order'],
'is_active' => true,
]);
foreach ($groupData['items'] as $itemData) {
NotificationSettingGroupItem::create([
'group_id' => $group->id,
'notification_type' => $itemData['notification_type'],
'label' => $itemData['label'],
'sort_order' => $itemData['sort_order'],
]);
}
}
});
}
/**
* 그룹 기반 알림 설정 조회 (React 구조로 반환)
*/
public function getGroupedSettings(): array
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
// 그룹이 없으면 초기화
$this->initializeGroupsIfNeeded();
// 그룹 조회
$groups = NotificationSettingGroup::where('tenant_id', $tenantId)
->active()
->ordered()
->with('items')
->get();
// 그룹 상태 조회
$groupStates = NotificationSettingGroupState::where('tenant_id', $tenantId)
->forUser($userId)
->get()
->keyBy('group_code');
// 개별 설정 조회
$settings = NotificationSetting::where('tenant_id', $tenantId)
->forUser($userId)
->get()
->keyBy('notification_type');
// React 구조로 변환
$result = [];
foreach ($groups as $group) {
$groupCode = $group->code;
$groupEnabled = $groupStates->has($groupCode)
? $groupStates->get($groupCode)->enabled
: true; // 기본값 true
$groupData = ['enabled' => $groupEnabled];
foreach ($group->items as $item) {
$type = $item->notification_type;
$camelKey = NotificationSettingGroup::toCamelCase($type);
if ($settings->has($type)) {
$setting = $settings->get($type);
$groupData[$camelKey] = [
'enabled' => $setting->push_enabled,
'email' => $setting->email_enabled,
];
} else {
// 기본값
$groupData[$camelKey] = [
'enabled' => false,
'email' => false,
];
}
}
$result[$groupCode] = $groupData;
}
return $result;
}
/**
* 그룹 기반 알림 설정 저장 (React 구조 입력)
*/
public function updateGroupedSettings(array $data): array
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
// 그룹이 없으면 초기화
$this->initializeGroupsIfNeeded();
return DB::transaction(function () use ($tenantId, $userId, $data) {
// 그룹 조회
$groups = NotificationSettingGroup::where('tenant_id', $tenantId)
->active()
->with('items')
->get()
->keyBy('code');
foreach ($data as $groupCode => $groupData) {
if (! $groups->has($groupCode)) {
continue;
}
// 그룹 전체 활성화 상태 저장
if (isset($groupData['enabled'])) {
NotificationSettingGroupState::updateOrCreate(
[
'tenant_id' => $tenantId,
'user_id' => $userId,
'group_code' => $groupCode,
],
[
'enabled' => $groupData['enabled'],
]
);
}
// 각 항목 설정 저장
$group = $groups->get($groupCode);
foreach ($group->items as $item) {
$type = $item->notification_type;
$camelKey = NotificationSettingGroup::toCamelCase($type);
if (isset($groupData[$camelKey])) {
$itemData = $groupData[$camelKey];
NotificationSetting::updateOrCreate(
[
'tenant_id' => $tenantId,
'user_id' => $userId,
'notification_type' => $type,
],
[
'push_enabled' => $itemData['enabled'] ?? false,
'email_enabled' => $itemData['email'] ?? false,
'sms_enabled' => false,
'in_app_enabled' => $itemData['enabled'] ?? false,
'kakao_enabled' => false,
]
);
}
}
}
// 저장 후 결과 반환
return $this->getGroupedSettings();
});
}
}