8000 [Fix] 알림 수신 설정을 Device 기준으로 변경 및 테스트용 알림 API 구현 by qkrcodus · Pull Request #53 · If-u-Lab/If-u-server · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.ifu.ifu_server.domain.device.controller;

import com.ifu.ifu_server.common.response.ApiResponse;
import com.ifu.ifu_server.domain.device.dto.DeviceNotificationSettingsRequest;
import com.ifu.ifu_server.domain.device.dto.DeviceNotificationSettingsResponse;
import com.ifu.ifu_server.domain.device.dto.DeviceRegisterRequest;
import com.ifu.ifu_server.domain.device.dto.DeviceRegisterResponse;
import com.ifu.ifu_server.domain.device.service.DeviceService;
Expand All @@ -11,6 +13,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -48,4 +51,21 @@ public ApiResponse<Void> deleteDevice(
deviceService.deleteDevice(userId, deviceId);
return ApiResponse.success("기기가 성공적으로 삭제되었습니다.", null);
}

@Operation(summary = "디바이스 알림 설정 변경", description = "특정 디바이스의 알림 허용 여부를 변경합니다.")
@PatchMapping("/{deviceId}/notification-settings")
public ApiResponse<DeviceNotificationSettingsResponse> updateNotificationSettings(
@Parameter(description = "기기 ID", required = true) @PathVariable String deviceId,
@Valid @RequestBody DeviceNotificationSettingsRequest request,
Authentication authentication
) {
Long userId = (Long) authentication.getPrincipal();

DeviceNotificationSettingsResponse response = deviceService.updateNotificationSettings(
userId,
deviceId,
request.notificationEnabled()
);
return ApiResponse.success("알림 설정이 성공적으로 변경되었습니다.", response);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.ifu.ifu_server.domain.user.dto;
package com.ifu.ifu_server.domain.device.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;

@Schema(description = "알림 설정 변경 요청")
public record NotificationSettingsRequest(
@Schema(description = "디바이스 알림 설정 변경 요청")
public record DeviceNotificationSettingsRequest(
@NotNull(message = "알림 허용 여부는 필수입니다.")
@Schema(description = "알림 허용 여부", example = "true")
Boolean notificationEnabled
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.ifu.ifu_server.domain.device.dto;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "디바이스 알림 설정 응답")
public record DeviceNotificationSettingsResponse(
@Schema(description = "디바이스 ID", example = "device123")
String deviceId,

@Schema(description = "알림 허용 여부", example = "true")
boolean notificationEnabled
) {
public static DeviceNotificationSettingsResponse of(String deviceId, boolean notificationEnabled) {
return new DeviceNotificationSettingsResponse(deviceId, notificationEnabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class Device extends BaseEntity {
@Column(name = "is_pwa_installed", nullable = false)
private boolean isPwaInstalled;

@Column(name = "notification_enabled", nullable = false)
private boolean notificationEnabled;

public static Device create(
User user,
String deviceId,
Expand All @@ -68,6 +71,7 @@ public static Device create(
.fcmToken(fcmToken)
.platform(platform)
.isPwaInstalled(isPwaInstalled)
.notificationEnabled(true)
.build();
}

Expand Down Expand Up @@ -109,4 +113,8 @@ public void updateFcmToken(String fcmToken) {
public void updatePwaInfo(boolean isPwaInstalled) {
this.isPwaInstalled = isPwaInstalled;
}

public void updateNotificationEnabled(boolean notificationEnabled) {
this.notificationEnabled = notificationEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ public interface DeviceRepository extends JpaRepository<Device, Long> {

Optional<Device> findByUserIdAndDeviceId(Long userId, String deviceId);

@Query("SELECT d FROM Device d JOIN d.user u WHERE u.deletedAt IS NULL AND d.fcmToken IS NOT NULL")
@Query("SELECT d FROM Device d JOIN d.user u WHERE u.deletedAt IS NULL AND d.fcmToken IS NOT NULL AND d.notificationEnabled = true")
List<Device> findAllActiveDevices();

@Query("SELECT d FROM Device d JOIN d.user u WHERE d.user.id = :userId AND u.deletedAt IS NULL AND d.notificationEnabled = true")
List<Device> findByUserIdAndNotificationEnabledTrue(Long userId);

long countByUserId(Long userId);

void deleteAllByFcmTokenIn(List<String> fcmTokens);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ifu.ifu_server.domain.device.service;

import com.ifu.ifu_server.domain.device.dto.DeviceNotificationSettingsResponse;
import com.ifu.ifu_server.domain.device.dto.DeviceRegisterRequest;
import com.ifu.ifu_server.domain.device.dto.DeviceRegisterResponse;
import com.ifu.ifu_server.domain.device.entity.Device;
Expand Down Expand Up @@ -87,4 +88,25 @@ public void deleteDevice(Long userId, String deviceId) {

deviceRepository.delete(device);
}

/**
* 디바이스 알림 설정 업데이트
*
* @param userId 사용자 ID
* @param deviceId 디바이스 ID
* @param notificationEnabled 알림 허용 여부
* @return 업데이트된 알림 설정 응답
*/
@Transactional
public DeviceNotificationSettingsResponse updateNotificationSettings(
Long userId,
String deviceId,
boolean notificationEnabled
) {
Device device = deviceRepository.findByUserIdAndDeviceId(userId, deviceId)
.orElseThrow(() -> new BusinessException(ErrorCode.DEVICE_NOT_FOUND));

device.updateNotificationEnabled(notificationEnabled);
return DeviceNotificationSettingsResponse.of(device.getDeviceId(), device.isNotificationEnabled());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.ifu.ifu_server.domain.notification.controller;

import com.ifu.ifu_server.common.response.ApiResponse;
import com.ifu.ifu_server.domain.notification.dto.PushNotificationTestRequest;
import com.ifu.ifu_server.domain.notification.dto.PushNotificationTestResponse;
import com.ifu.ifu_server.domain.notification.service.PushNotificationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Notification", description = "푸시 알림 테스트 API")
@RestController
@RequestMapping("/api/v1/notifications")
@RequiredArgsConstructor
public class NotificationController {

private final PushNotificationService pushNotificationService;

@Operation(
summary = "푸시 알림 테스트 전송",
description = "특정 사용자의 모든 디바이스에 테스트 푸시 알림을 전송합니다. "
+ "dryRun 옵션을 true로 설정하면 실제 전송 없이 검증만 수행합니다."
)
@PostMapping("/test")
public ApiResponse<PushNotificationTestResponse> sendTestNotification(
@Valid @RequestBody PushNotificationTestRequest request
) {
PushNotificationTestResponse response = pushNotificationService.sendTestNotification(
request.userId(),
request.title(),
request.body(),
request.isDryRun()
);

String message = request.isDryRun()
? "테스트 알림 검증이 완료되었습니다. (실제 전송 안됨)"
: "테스트 알림이 전송되었습니다.";

return ApiResponse.success(message, response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.ifu.ifu_server.domain.notification.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

public record PushNotificationTestRequest(
@NotNull(message = "userId는 필수입니다.")
Long userId,

@NotBlank(message = "title은 필수입니다.")
String title,

@NotBlank(message = "body는 필수입니다.")
String body,

Boolean dryRun // null이면 false로 처리
) {

public boolean isDryRun() {
return dryRun != null && dryRun;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.ifu.ifu_server.domain.notification.dto;

public record PushNotificationTestResponse(
Long userId,
int targetDeviceCount,
int successCount,
int failureCount,
boolean dryRun
) {

public static PushNotificationTestResponse of(
Long userId,
int targetDeviceCount,
int successCount,
int failureCount,
boolean dryRun
) {
return new PushNotificationTestResponse(
userId,
targetDeviceCount,
successCount,
failureCount,
dryRun
);
}
}
Loading
0