Files
sam-api/app/Services/NotificationSettingService.php
hskwon 3020026abf feat: Phase 5 API 개발 완료 (사용자 초대, 알림설정, 계정관리, 거래명세서)
5.1 사용자 초대 기능:
- UserInvitation 마이그레이션, 모델, 서비스, 컨트롤러, Swagger
- 초대 발송/수락/취소/재발송 API

5.2 알림설정 확장:
- NotificationSetting 마이그레이션, 모델, 서비스, 컨트롤러, Swagger
- 채널별/유형별 알림 설정 관리

5.3 계정정보 수정 API:
- 회원탈퇴, 사용중지, 약관동의 관리
- AccountService, AccountController, Swagger

5.4 매출 거래명세서 API:
- 거래명세서 조회/발행/이메일발송
- SaleService 확장, Swagger 문서화
2025-12-19 14:52:53 +09:00

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);
}
}