Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

슬랙 알림 로직 분리 및 유동적 알림 설정 가능하도록 변경 완료 #386

Merged
merged 7 commits into from
Jun 21, 2024
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package page.clab.api.domain.application.application;

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -44,7 +43,7 @@ public String createApplication(ApplicationRequestDto requestDto) {

notificationService.sendNotificationToAdmins(requestDto.getStudentId() + " " +
requestDto.getName() + "님이 동아리에 지원하였습니다.");
slackService.sendApplicationNotification(requestDto);
slackService.sendNewApplicationNotification(requestDto);
return applicationRepository.save(application).getStudentId();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import page.clab.api.global.common.dto.PagedResponseDto;
import page.clab.api.global.common.file.application.UploadedFileService;
import page.clab.api.global.common.file.domain.UploadedFile;
import page.clab.api.global.common.slack.application.SlackService;
import page.clab.api.global.exception.NotFoundException;
import page.clab.api.global.exception.PermissionDeniedException;
import page.clab.api.global.validation.ValidationService;
Expand All @@ -43,6 +44,8 @@ public class BoardService {

private final ValidationService validationService;

private final SlackService slackService;

private final BoardRepository boardRepository;

private final BoardLikeRepository boardLikeRepository;
Expand All @@ -59,6 +62,7 @@ public String createBoard(BoardRequestDto requestDto) throws PermissionDeniedExc
if (board.shouldNotifyForNewBoard()) {
notificationService.sendNotificationToMember(currentMember, "[" + board.getTitle() + "] 새로운 공지사항이 등록되었습니다.");
}
slackService.sendNewBoardNotification(board);
return boardRepository.save(board).getCategory().getKey();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class Board extends BaseEntity {

private String imageUrl;

@Getter
@Column(nullable = false)
private boolean wantAnonymous;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package page.clab.api.global.common.slack.api;

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.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import page.clab.api.global.common.dto.ApiResponse;
import page.clab.api.global.common.slack.application.NotificationSettingService;
import page.clab.api.global.common.slack.dto.request.NotificationSettingUpdateRequestDto;
import page.clab.api.global.common.slack.dto.response.NotificationSettingResponseDto;

import java.util.List;

@RestController
@RequestMapping("/api/v1/notification-settings")
@RequiredArgsConstructor
@Tag(name = "Notification Setting", description = "알림 설정")
public class NotificationSettingController {

private final NotificationSettingService notificationSettingService;

@Operation(summary = "[S] 슬랙 알림 조회", description = "ROLE_SUPER 이상의 권한이 필요함")
@Secured({"ROLE_SUPER"})
@GetMapping("")
public ApiResponse<Object> getNotificationSettings() {
List<NotificationSettingResponseDto> notificationSettings = notificationSettingService.getNotificationSettings();
return ApiResponse.success(notificationSettings);
}

@Operation(summary = "[S] 슬랙 알림 설정 변경", description = "ROLE_SUPER 이상의 권한이 필요함")
@Secured({"ROLE_SUPER"})
@PutMapping("")
public ApiResponse<Object> updateNotificationSetting(
@Valid @RequestBody NotificationSettingUpdateRequestDto requestDto
) {
notificationSettingService.updateNotificationSetting(requestDto.getAlertType(), requestDto.isEnabled());
return ApiResponse.success();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package page.clab.api.global.common.slack.application;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import page.clab.api.global.common.slack.dao.NotificationSettingRepository;
import page.clab.api.global.common.slack.domain.AlertType;
import page.clab.api.global.common.slack.domain.AlertTypeResolver;
import page.clab.api.global.common.slack.domain.NotificationSetting;
import page.clab.api.global.common.slack.dto.response.NotificationSettingResponseDto;

import java.util.List;

@Service
@RequiredArgsConstructor
public class NotificationSettingService {

private final AlertTypeResolver alertTypeResolver;

private final NotificationSettingRepository settingRepository;

@Transactional(readOnly = true)
public List<NotificationSettingResponseDto> getNotificationSettings() {
return settingRepository.findAll().stream()
.map(NotificationSettingResponseDto::toDto)
.toList();
}

@Transactional
public void updateNotificationSetting(String alertTypeName, boolean enabled) {
AlertType alertType = alertTypeResolver.resolve(alertTypeName);
NotificationSetting setting = getOrCreateDefaultSetting(alertType);
setting.updateEnabled(enabled);
settingRepository.save(setting);
}

@Transactional
public NotificationSetting getOrCreateDefaultSetting(AlertType alertType) {
return settingRepository.findByAlertType(alertType)
.orElseGet(() -> createAndSaveDefaultSetting(alertType));
}

private NotificationSetting createAndSaveDefaultSetting(AlertType alertType) {
NotificationSetting defaultSetting = NotificationSetting.createDefault(alertType);
return settingRepository.save(defaultSetting);
}

}
Loading