181 lines
6.2 KiB
PHP
181 lines
6.2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Services;
|
||
|
|
|
||
|
|
use App\Models\NotificationSetting;
|
||
|
|
use Illuminate\Support\Collection;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
|
||
|
|
class NotificationSettingService extends Service
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* 사용자의 알림 설정 조회
|
||
|
|
*/
|
||
|
|
public function getSettings(): array
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
$userId = $this->apiUserId();
|
||
|
|
|
||
|
|
$settings = NotificationSetting::where('tenant_id', $tenantId)
|
||
|
|
->forUser($userId)
|
||
|
|
->get()
|
||
|
|
->keyBy('notification_type');
|
||
|
|
|
||
|
|
// 모든 알림 유형에 대한 설정 반환 (없으면 기본값)
|
||
|
|
$result = [];
|
||
|
|
foreach (NotificationSetting::getAllTypes() as $type) {
|
||
|
|
if ($settings->has($type)) {
|
||
|
|
$setting = $settings->get($type);
|
||
|
|
$result[$type] = [
|
||
|
|
'notification_type' => $type,
|
||
|
|
'label' => NotificationSetting::getTypeLabels()[$type] ?? $type,
|
||
|
|
'push_enabled' => $setting->push_enabled,
|
||
|
|
'email_enabled' => $setting->email_enabled,
|
||
|
|
'sms_enabled' => $setting->sms_enabled,
|
||
|
|
'in_app_enabled' => $setting->in_app_enabled,
|
||
|
|
'kakao_enabled' => $setting->kakao_enabled,
|
||
|
|
'settings' => $setting->settings,
|
||
|
|
];
|
||
|
|
} else {
|
||
|
|
$defaults = NotificationSetting::getDefaultSettings($type);
|
||
|
|
$result[$type] = array_merge([
|
||
|
|
'notification_type' => $type,
|
||
|
|
'label' => NotificationSetting::getTypeLabels()[$type] ?? $type,
|
||
|
|
'settings' => null,
|
||
|
|
], $defaults);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return [
|
||
|
|
'settings' => $result,
|
||
|
|
'types' => NotificationSetting::getTypeLabels(),
|
||
|
|
'channels' => NotificationSetting::getChannelLabels(),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 특정 알림 유형 설정 업데이트
|
||
|
|
*/
|
||
|
|
public function updateSetting(array $data): NotificationSetting
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
$userId = $this->apiUserId();
|
||
|
|
|
||
|
|
$notificationType = $data['notification_type'];
|
||
|
|
|
||
|
|
// 알림 유형 유효성 검증
|
||
|
|
if (! in_array($notificationType, NotificationSetting::getAllTypes(), true)) {
|
||
|
|
throw new \InvalidArgumentException(__('error.notification.invalid_type'));
|
||
|
|
}
|
||
|
|
|
||
|
|
return NotificationSetting::updateOrCreate(
|
||
|
|
[
|
||
|
|
'tenant_id' => $tenantId,
|
||
|
|
'user_id' => $userId,
|
||
|
|
'notification_type' => $notificationType,
|
||
|
|
],
|
||
|
|
[
|
||
|
|
'push_enabled' => $data['push_enabled'] ?? true,
|
||
|
|
'email_enabled' => $data['email_enabled'] ?? false,
|
||
|
|
'sms_enabled' => $data['sms_enabled'] ?? false,
|
||
|
|
'in_app_enabled' => $data['in_app_enabled'] ?? true,
|
||
|
|
'kakao_enabled' => $data['kakao_enabled'] ?? false,
|
||
|
|
'settings' => $data['settings'] ?? null,
|
||
|
|
]
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 알림 설정 일괄 업데이트
|
||
|
|
*/
|
||
|
|
public function bulkUpdateSettings(array $settings): Collection
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
$userId = $this->apiUserId();
|
||
|
|
|
||
|
|
return DB::transaction(function () use ($tenantId, $userId, $settings) {
|
||
|
|
$updated = collect();
|
||
|
|
|
||
|
|
foreach ($settings as $setting) {
|
||
|
|
$notificationType = $setting['notification_type'];
|
||
|
|
|
||
|
|
// 알림 유형 유효성 검증
|
||
|
|
if (! in_array($notificationType, NotificationSetting::getAllTypes(), true)) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
$record = NotificationSetting::updateOrCreate(
|
||
|
|
[
|
||
|
|
'tenant_id' => $tenantId,
|
||
|
|
'user_id' => $userId,
|
||
|
|
'notification_type' => $notificationType,
|
||
|
|
],
|
||
|
|
[
|
||
|
|
'push_enabled' => $setting['push_enabled'] ?? true,
|
||
|
|
'email_enabled' => $setting['email_enabled'] ?? false,
|
||
|
|
'sms_enabled' => $setting['sms_enabled'] ?? false,
|
||
|
|
'in_app_enabled' => $setting['in_app_enabled'] ?? true,
|
||
|
|
'kakao_enabled' => $setting['kakao_enabled'] ?? false,
|
||
|
|
'settings' => $setting['settings'] ?? null,
|
||
|
|
]
|
||
|
|
);
|
||
|
|
|
||
|
|
$updated->push($record);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $updated;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 기본 알림 설정 초기화
|
||
|
|
*/
|
||
|
|
public function initializeDefaultSettings(): void
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
$userId = $this->apiUserId();
|
||
|
|
|
||
|
|
foreach (NotificationSetting::getAllTypes() as $type) {
|
||
|
|
$defaults = NotificationSetting::getDefaultSettings($type);
|
||
|
|
|
||
|
|
NotificationSetting::firstOrCreate(
|
||
|
|
[
|
||
|
|
'tenant_id' => $tenantId,
|
||
|
|
'user_id' => $userId,
|
||
|
|
'notification_type' => $type,
|
||
|
|
],
|
||
|
|
$defaults
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 특정 알림 유형에서 특정 채널이 활성화되어 있는지 확인
|
||
|
|
*/
|
||
|
|
public function isChannelEnabled(int $userId, string $notificationType, string $channel): bool
|
||
|
|
{
|
||
|
|
$tenantId = $this->tenantId();
|
||
|
|
|
||
|
|
$setting = NotificationSetting::where('tenant_id', $tenantId)
|
||
|
|
->forUser($userId)
|
||
|
|
->ofType($notificationType)
|
||
|
|
->first();
|
||
|
|
|
||
|
|
if (! $setting) {
|
||
|
|
// 기본 설정 반환
|
||
|
|
$defaults = NotificationSetting::getDefaultSettings($notificationType);
|
||
|
|
|
||
|
|
return match ($channel) {
|
||
|
|
NotificationSetting::CHANNEL_PUSH => $defaults['push_enabled'],
|
||
|
|
NotificationSetting::CHANNEL_EMAIL => $defaults['email_enabled'],
|
||
|
|
NotificationSetting::CHANNEL_SMS => $defaults['sms_enabled'],
|
||
|
|
NotificationSetting::CHANNEL_IN_APP => $defaults['in_app_enabled'],
|
||
|
|
NotificationSetting::CHANNEL_KAKAO => $defaults['kakao_enabled'],
|
||
|
|
default => false,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
return $setting->isChannelEnabled($channel);
|
||
|
|
}
|
||
|
|
}
|