Skip to content

Commit

Permalink
✨feat: 알림 삭제 구현 및 임시 fcm 토큰 발급 (#166)
Browse files Browse the repository at this point in the history
* ✨ feat: 알림 삭제 기능 구현

* 🐛 fix: @PathVariable name 옵션 추가 & service 수정
  • Loading branch information
seheonnn authored Feb 11, 2024
1 parent 3812b48 commit df527e3
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 5 deletions.
4 changes: 2 additions & 2 deletions http/test.http
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Content-Type: application/json
{
"email": "[email protected]",
"password": "password1234",
"fcmToken": "fcmtokenfcmtokenfcmtokenfcmtokenfcmtokenfcmtokenfcmtokenfcmtokenfcmtokenfcmtoken"
"fcmToken": "eRfQraP_WUxZuC0XFzMrSC:APA91bEDel7UnziwZHglJGukRx8nqLgaYswkO532pit7DOkFiyoA2dfEIGMO49JL_NjwbfReszwVui-8xP3Uh6UhxcverlnW5cnClSAqcEd_xhgaF8FSQ4M6WdvnPMdTGJwUbe_9gaE5"
}

### 학생단체 로그인
Expand Down Expand Up @@ -228,7 +228,7 @@ Authorization: Bearer {{masterToken}}

### 알림 테스트
POST http://localhost:8080/api/v1/notification/fcm
Authorization: Bearer {{masterToken}}
Authorization: Bearer {{masterToken}}
Content-Type: application/json

{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.sponus.sponusbe.domain.notification.exception;

import org.springframework.http.HttpStatus;

import com.sponus.sponusbe.global.common.ApiResponse;
import com.sponus.sponusbe.global.common.BaseErrorCode;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum NotificationErrorCode implements BaseErrorCode {
NOTIFICATION_ERROR(HttpStatus.BAD_REQUEST, "NOTI4000", "알림 관련 에러"),
NOTIFICATION_NOT_FOUND(HttpStatus.NOT_FOUND, "NOTI4001", "존재하지 않는 알림입니다."),
INVALID_ORGANIZATION(HttpStatus.BAD_REQUEST, "NOTI4002", "해당 단체의 알림이 아닙니다.");

private final HttpStatus httpStatus;
private final String code;
private final String message;

@Override
public ApiResponse<Void> getErrorResponse() {
return ApiResponse.onFailure(code, message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.sponus.sponusbe.domain.notification.exception;

import com.sponus.sponusbe.global.common.BaseErrorCode;
import com.sponus.sponusbe.global.common.exception.CustomException;

public class NotificationException extends CustomException {

public NotificationException(BaseErrorCode errorCode) {
super(errorCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ public class FirebaseService {

private final RedisUtil redisUtil;

public void sendMessageTo(Organization organization, String title, String body, Announcement announcement,
public void sendMessageTo(Organization targetOrganization, String title, String body, Announcement announcement,
Propose propose) throws IOException {

String token = getFcmToken(organization.getEmail());
String token = getFcmToken(targetOrganization.getEmail());

Notification notification = Notification.builder()
.title(title)
.body(body)
.build();

notification.setOrganization(organization);
notification.setOrganization(targetOrganization);
notification.setAnnouncement(announcement);
notification.setPropose(propose);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,12 @@ public ApiResponse<List<NotificationSummaryResponse>> getNotifications(
@AuthOrganization Organization organization) {
return ApiResponse.onSuccess(organizationQueryService.getNotifications(organization));
}

@DeleteMapping("/notifications/{notificationId}")
public ApiResponse<Void> deleteNotification(
@AuthOrganization Organization organization,
@PathVariable("notificationId") Long notificationId) {
organizationService.deleteNotification(organization, notificationId);
return ApiResponse.onSuccess(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sponus.sponusbe.domain.notification.entity.Notification;
import com.sponus.sponusbe.domain.notification.exception.NotificationErrorCode;
import com.sponus.sponusbe.domain.notification.exception.NotificationException;
import com.sponus.sponusbe.domain.notification.repository.NotificationRepository;
import com.sponus.sponusbe.domain.organization.dto.OrganizationJoinRequest;
import com.sponus.sponusbe.domain.organization.dto.OrganizationJoinResponse;
import com.sponus.sponusbe.domain.organization.dto.OrganizationSummaryResponse;
Expand All @@ -21,13 +25,16 @@
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Transactional
@RequiredArgsConstructor
@Service
public class OrganizationService {

private final OrganizationRepository organizationRepository;
private final NotificationRepository notificationRepository;
private final PasswordEncoder passwordEncoder;
private final JavaMailSender emailSender;

Expand Down Expand Up @@ -110,4 +117,13 @@ public List<OrganizationSummaryResponse> searchOrganization(String keyword) {
.map(OrganizationSummaryResponse::from)
.toList();
}

public void deleteNotification(Organization organization, Long notificationId) {
Notification notification = notificationRepository.findById(notificationId)
.orElseThrow(() -> new NotificationException(NotificationErrorCode.NOTIFICATION_NOT_FOUND));
if (!notification.getOrganization().getId().equals(organization.getId())) {
throw new NotificationException(NotificationErrorCode.INVALID_ORGANIZATION);
}
notificationRepository.delete(notification);
}
}

0 comments on commit df527e3

Please sign in to comment.