Files
Gnuboard7/app/Http/Controllers/Api/Auth/NotificationController.php
T
HeuJung 3ebbda5d86 feat(board,ecommerce,pay,verification,templates,core): 문의 답변 사슬·답글 삭제 정책·검증 계약 + 실패 사유 표면화 전수
공개 (답변 중복 등록)/(문의 삭제 시 답변 잔존) 수정과 동형 결함 전수 해소.

- 문의 답변 단일성: 1차(피벗 is_answered) + 2차(게시판 리스너 duplicate 마커) 이중 방어,
 경합 경로에서도 사유(reply_already_exists 422)가 보존되도록 훅 반환 계약 확장
- 질문·답변·피벗 생명주기 대칭: 삭제/복원/영구삭제 훅 연동 + 고아 데이터 백필 업그레이드 스텝
- 게시판별 답글 삭제 정책(cascade/block) 신설, 정렬 화이트리스트·per_page 게이트
 (전량 조회 소비처 200/999 를 수용하는 상한 999 — 조였다가 관리자 화면 2곳 422 회귀 즉시 정정)
- 결제/본인인증 플러그인 검증 계약: FormRequest 15종 승격 + 컨트롤러 catch 정합.
 거래 조회 tid 상한은 저장 컬럼(varchar 100) 기준 — 해외 CBT TID(40자) 수용
- onError 토스트 고정 문구 전수 해소(67건/50파일/15확장) — 서버 실패 사유를 그대로 표면화,
 change-password 오변수(response→error) 교정
- 코어 플러그인 설정 검증 안내의 항목 표시명이 스키마 라벨(ko/en) 폴백에 덮여 요청 로케일
 (ja) 라벨을 잃던 회귀 정정 — 번역 등록이 있으면 그것을 우선 (7.0.7 미출시 기능 자체 결함)
- 사전 결함 정리(Icon 등가표 27·darkmode 2·validated 통일), API 문서·CHANGELOG·ja 팩 동기
- develop(60652fdc0) 리베이스: getTable 정비와 병합 — 양측 의도 보존 (충돌 18파일)
2026-08-17 17:04:44 +09:00

203 lines
7.3 KiB
PHP

<?php
// audit:allow api-doc-coverage reason: 본문 미사용 base Request 면제 주석·PHPDoc 만 추가 — 요청/응답 계약 불변
namespace App\Http\Controllers\Api\Auth;
use App\Http\Controllers\Api\Base\AuthBaseController;
use App\Http\Requests\Notification\NotificationBatchReadRequest;
use App\Http\Requests\Notification\NotificationIndexRequest;
use App\Http\Resources\UserNotificationCollection;
use App\Http\Resources\UserNotificationResource;
use App\Services\NotificationService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
/**
* 사용자 알림 컨트롤러
*
* 인증된 사용자의 사이트내 알림을 관리합니다.
*/
class NotificationController extends AuthBaseController
{
public function __construct(
private readonly NotificationService $notificationService,
) {
parent::__construct();
}
/**
* 알림 목록을 조회합니다.
*
* @param NotificationIndexRequest $request 알림 목록 조회 요청 (page/per_page)
* @return JsonResponse 알림 목록 응답
*/
public function index(NotificationIndexRequest $request): JsonResponse
{
try {
$validated = $request->validated();
$user = $request->user();
$perPage = (int) ($validated['per_page'] ?? 20);
$filters = collect($validated)->only(['read'])->filter(fn ($v) => $v !== null)->toArray();
$notifications = $this->notificationService->getNotifications($user, $filters, $perPage);
return $this->success(
__('notification.user.list_success'),
new UserNotificationCollection($notifications)
);
} catch (\Exception $e) {
Log::error('알림 목록 조회 실패', ['error' => $e->getMessage()]);
return $this->error(__('notification.user.list_failed'), 500);
}
}
/**
* 미읽음 알림 수를 반환합니다.
*
* @param Request $request 인증 사용자 요청
* @return JsonResponse 미읽음 알림 수 응답
*/
// audit:allow controller-base-request-injection reason: 본문 입력을 읽지 않음 — user() 만 참조 (검증 대상 필드 없음)
public function unreadCount(Request $request): JsonResponse
{
try {
$count = $this->notificationService->getUnreadCount($request->user());
return $this->success(
__('notification.user.unread_count_success'),
['unread_count' => $count]
);
} catch (\Exception $e) {
Log::error('미읽음 알림 수 조회 실패', ['error' => $e->getMessage()]);
return $this->error(__('notification.user.unread_count_failed'), 500);
}
}
/**
* 알림을 읽음 처리합니다.
*
* @param Request $request 인증 사용자 요청
* @param string $id 알림 ID
* @return JsonResponse 읽음 처리 결과 응답
*/
// audit:allow controller-base-request-injection reason: 본문 입력을 읽지 않음 — user() 만 참조 (검증 대상 필드 없음)
public function markAsRead(Request $request, string $id): JsonResponse
{
try {
$notification = $this->notificationService->markAsRead($request->user(), $id);
if (! $notification) {
return $this->error(__('notification.user.not_found'), 404);
}
return $this->success(
__('notification.user.read_success'),
new UserNotificationResource($notification)
);
} catch (\Exception $e) {
Log::error('알림 읽음 처리 실패', ['id' => $id, 'error' => $e->getMessage()]);
return $this->error(__('notification.user.read_failed'), 500);
}
}
/**
* 지정된 알림들을 일괄 읽음 처리합니다.
*
* @param NotificationBatchReadRequest $request 일괄 읽음 처리 요청
* @return JsonResponse 일괄 읽음 처리 결과 응답
*/
public function markBatchAsRead(NotificationBatchReadRequest $request): JsonResponse
{
try {
$ids = $request->validated()['ids'];
$count = $this->notificationService->markBatchAsRead($request->user(), $ids);
return $this->success(
__('notification.user.read_success'),
['marked_count' => $count]
);
} catch (\Exception $e) {
Log::error('알림 일괄 읽음 처리 실패', ['error' => $e->getMessage()]);
return $this->error(__('notification.user.read_failed'), 500);
}
}
/**
* 모든 알림을 읽음 처리합니다.
*
* @param Request $request 인증 사용자 요청
* @return JsonResponse 전체 읽음 처리 결과 응답
*/
// audit:allow controller-base-request-injection reason: 본문 입력을 읽지 않음 — user() 만 참조 (검증 대상 필드 없음)
public function markAllAsRead(Request $request): JsonResponse
{
try {
$count = $this->notificationService->markAllAsRead($request->user());
return $this->success(
__('notification.user.read_all_success'),
['marked_count' => $count]
);
} catch (\Exception $e) {
Log::error('전체 읽음 처리 실패', ['error' => $e->getMessage()]);
return $this->error(__('notification.user.read_all_failed'), 500);
}
}
/**
* 사용자의 모든 알림을 삭제합니다.
*
* @param Request $request 인증 사용자 요청
* @return JsonResponse 전체 삭제 결과 응답
*/
// audit:allow controller-base-request-injection reason: 본문 입력을 읽지 않음 — user() 만 참조 (검증 대상 필드 없음)
public function destroyAll(Request $request): JsonResponse
{
try {
$count = $this->notificationService->deleteAllNotifications($request->user());
return $this->success(
__('notification.user.delete_all_success'),
['deleted_count' => $count]
);
} catch (\Exception $e) {
Log::error('전체 알림 삭제 실패', ['error' => $e->getMessage()]);
return $this->error(__('notification.user.delete_all_failed'), 500);
}
}
/**
* 알림을 삭제합니다.
*
* @param Request $request 인증 사용자 요청
* @param string $id 알림 ID
* @return JsonResponse 삭제 결과 응답
*/
// audit:allow controller-base-request-injection reason: 본문 입력을 읽지 않음 — user() 만 참조 (검증 대상 필드 없음)
public function destroy(Request $request, string $id): JsonResponse
{
try {
$deleted = $this->notificationService->deleteNotification($request->user(), $id);
if (! $deleted) {
return $this->error(__('notification.user.not_found'), 404);
}
return $this->success(__('notification.user.delete_success'));
} catch (\Exception $e) {
Log::error('알림 삭제 실패', ['id' => $id, 'error' => $e->getMessage()]);
return $this->error(__('notification.user.delete_failed'), 500);
}
}
}