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

[Feature] 봉사자 모집자 알림 설정 구현 #213

Merged
merged 9 commits into from
May 26, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -101,81 +101,85 @@ public String makeMessage(String targetToken, String title, String body) throws
public void sendMessageToVolunteer(String targetToken, Volunteer volunteer, String image,
NotificationType notificationType, String title, String body) {

try {
String message = makeMessage(targetToken, title, body);

OkHttpClient client = new OkHttpClient();
RequestBody requestBody = RequestBody.create(message, MediaType.get("application/json; charset=utf-8"));

Request request = new Request.Builder()
.url(FIREBASE_API_URL)
.post(requestBody)
.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + getAccessToken())
.addHeader(HttpHeaders.CONTENT_TYPE, "application/json; UTF-8")
.build();

Response response = client.newCall(request).execute();

// 알림 저장
volunteerNotificationRepository.save(
VolunteerNotification.builder()
.image(image)
.notificationType(notificationType)
.title(title)
.body(body)
.volunteer(volunteer)
.isRead(false)
.build()
);

if (!response.isSuccessful()) {
log.error("FCM 푸시 알람 전송이 실패했습니다. 응답 코드: {}\n{}", response.code(), response.body().string());
if (volunteer.getNotification()) {
try {
String message = makeMessage(targetToken, title, body);

OkHttpClient client = new OkHttpClient();
RequestBody requestBody = RequestBody.create(message, MediaType.get("application/json; charset=utf-8"));

Request request = new Request.Builder()
.url(FIREBASE_API_URL)
.post(requestBody)
.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + getAccessToken())
.addHeader(HttpHeaders.CONTENT_TYPE, "application/json; UTF-8")
.build();

Response response = client.newCall(request).execute();

if (!response.isSuccessful()) {
log.error("FCM 푸시 알람 전송이 실패했습니다. 응답 코드: {}\n{}", response.code(), response.body().string());
}
} catch (Exception e) {
log.error("Fcm 푸시 알람을 전송하는 도중에 에러가 발생했습니다. {}", e.getMessage());
throw new BadRequestException(NOTIFICATION_SEND_ERROR);
}
}
catch (Exception e) {
log.error("Fcm 푸시 알람을 전송하는 도중에 에러가 발생했습니다. {}", e.getMessage());
throw new BadRequestException(NOTIFICATION_SEND_ERROR);
}

// 알림 저장
volunteerNotificationRepository.save(
VolunteerNotification.builder()
.image(image)
.notificationType(notificationType)
.title(title)
.body(body)
.volunteer(volunteer)
.isRead(false)
.build()
);

}

public void sendMessageToIntermediary(String targetToken, Intermediary intermediary, String image,
NotificationType notificationType, String title, String body) {

try {
String message = makeMessage(targetToken, title, body);

OkHttpClient client = new OkHttpClient();
RequestBody requestBody = RequestBody.create(message, MediaType.get("application/json; charset=utf-8"));

Request request = new Request.Builder()
.url(FIREBASE_API_URL)
.post(requestBody)
.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + getAccessToken())
.addHeader(HttpHeaders.CONTENT_TYPE, "application/json; UTF-8")
.build();

Response response = client.newCall(request).execute();

// 알림 저장
intermediaryNotificationRepository.save(
IntermediaryNotification.builder()
.image(image)
.notificationType(notificationType)
.title(title)
.body(body)
.intermediary(intermediary)
.isRead(false)
.build()
);

if (!response.isSuccessful()) {
log.error("FCM 푸시 알람 전송이 실패했습니다. 응답 코드: {}\n{}", response.code(), response.body().string());
if (intermediary.getNotification()) {
try {
String message = makeMessage(targetToken, title, body);

OkHttpClient client = new OkHttpClient();
RequestBody requestBody = RequestBody.create(message, MediaType.get("application/json; charset=utf-8"));

Request request = new Request.Builder()
.url(FIREBASE_API_URL)
.post(requestBody)
.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + getAccessToken())
.addHeader(HttpHeaders.CONTENT_TYPE, "application/json; UTF-8")
.build();

Response response = client.newCall(request).execute();

if (!response.isSuccessful()) {
log.error("FCM 푸시 알람 전송이 실패했습니다. 응답 코드: {}\n{}", response.code(), response.body().string());
}
} catch (Exception e) {
log.error("Fcm 푸시 알람을 전송하는 도중에 에러가 발생했습니다. {}", e.getMessage());
throw new BadRequestException(NOTIFICATION_SEND_ERROR);
}
}
catch (Exception e) {
log.error("Fcm 푸시 알람을 전송하는 도중에 에러가 발생했습니다. {}", e.getMessage());
throw new BadRequestException(NOTIFICATION_SEND_ERROR);
}

// 알림 저장
intermediaryNotificationRepository.save(
IntermediaryNotification.builder()
.image(image)
.notificationType(notificationType)
.title(title)
.body(body)
.intermediary(intermediary)
.isRead(false)
.build()
);

}

public void saveVolunteerFcm(String email, VolunteerFcmRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,27 @@ public ResponseEntity<Void> changePassword(@AuthenticationPrincipal UserDetails
return ResponseEntity.noContent().build();
}

@Operation(summary = "설정 - 모집자 알림 설정", description = "모집자 알림을 설정합니다.",
responses = {@ApiResponse(responseCode = "204", description = "모집자 알림 설정 성공")
, @ApiResponse(responseCode = "400"
, description = "M1, 해당 이동봉사 중개를 찾을 수 없습니다."
, content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PatchMapping("/intermediaries/notification/setting")
public ResponseEntity<Void> changeNotification(@AuthenticationPrincipal UserDetails loginUser) {
intermediaryService.changeNotification(loginUser.getUsername());
return ResponseEntity.noContent().build();
}

@Operation(summary = "설정 - 모집자 알림 설정 조회", description = "모집자 알림 설정을 조회합니다.",
responses = {@ApiResponse(responseCode = "204", description = "모집자 알림 설정 조회 성공")
, @ApiResponse(responseCode = "400"
, description = "M1, 해당 이동봉사 중개를 찾을 수 없습니다."
, content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@GetMapping("/intermediaries/notification/setting")
public ResponseEntity<IntermediaryGetNotificationResponse> getNotification(@AuthenticationPrincipal UserDetails loginUser) {
IntermediaryGetNotificationResponse response = intermediaryService.getNotification(loginUser.getUsername());
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.pawwithu.connectdog.domain.intermediary.dto.response;

public record IntermediaryGetNotificationResponse(Boolean notification) {

public static IntermediaryGetNotificationResponse of(Boolean notification) {
return new IntermediaryGetNotificationResponse(notification);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ public void updateProfileWithoutImage(String url, String intro, String contact,
public void updatePassword(String password) {
this.password = password;
}

public void updateNotification() { this.notification = !this.notification; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,16 @@ public void changePassword(String email, String password) {
intermediary.updatePassword(password);
intermediary.passwordEncode(passwordEncoder);
}

public void changeNotification(String email) {
Intermediary intermediary = intermediaryRepository.findByEmail(email).orElseThrow(() -> new BadRequestException(INTERMEDIARY_NOT_FOUND));
intermediary.updateNotification();
}

@Transactional(readOnly = true)
public IntermediaryGetNotificationResponse getNotification(String email) {
Intermediary intermediary = intermediaryRepository.findByEmail(email).orElseThrow(() -> new BadRequestException(INTERMEDIARY_NOT_FOUND));
IntermediaryGetNotificationResponse response = IntermediaryGetNotificationResponse.of(intermediary.getNotification());
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,27 @@ public ResponseEntity<Void> changePassword(@AuthenticationPrincipal UserDetails
return ResponseEntity.noContent().build();
}

@Operation(summary = "설정 - 이동봉사자 알림 설정", description = "이동봉사자 알림을 설정합니다.",
responses = {@ApiResponse(responseCode = "204", description = "이동봉사자 알림 설정 성공")
, @ApiResponse(responseCode = "400"
, description = "M1, 해당 이동봉사자를 찾을 수 없습니다."
, content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@PatchMapping("/notification/setting")
public ResponseEntity<Void> changeNotification(@AuthenticationPrincipal UserDetails loginUser) {
volunteerService.changeNotification(loginUser.getUsername());
return ResponseEntity.noContent().build();
}

@Operation(summary = "설정 - 이동봉사자 알림 설정 조회", description = "이동봉사자 알림 설정을 조회합니다.",
responses = {@ApiResponse(responseCode = "204", description = "이동봉사자 알림 설정 조회 성공")
, @ApiResponse(responseCode = "400"
, description = "M1, 해당 이동봉사자를 찾을 수 없습니다."
, content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@GetMapping("/notification/setting")
public ResponseEntity<VolunteerGetNotificationResponse> getNotification(@AuthenticationPrincipal UserDetails loginUser) {
VolunteerGetNotificationResponse response = volunteerService.getNotification(loginUser.getUsername());
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.pawwithu.connectdog.domain.volunteer.dto.response;

public record VolunteerGetNotificationResponse(Boolean notification) {

public static VolunteerGetNotificationResponse of(Boolean notification) {
return new VolunteerGetNotificationResponse(notification);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,6 @@ public void updateProfileImage(Integer profileImageNum) {
public void updatePassword(String password) {
this.password = password;
}

public void updateNotification() { this.notification = !this.notification; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,16 @@ public void changePassword(String email, String password) {
volunteer.updatePassword(password);
volunteer.passwordEncode(passwordEncoder);
}

public void changeNotification(String email) {
Volunteer volunteer = volunteerRepository.findByEmail(email).orElseThrow(() -> new BadRequestException(VOLUNTEER_NOT_FOUND));
volunteer.updateNotification();
}

@Transactional(readOnly = true)
public VolunteerGetNotificationResponse getNotification(String email) {
Volunteer volunteer = volunteerRepository.findByEmail(email).orElseThrow(() -> new BadRequestException(VOLUNTEER_NOT_FOUND));
VolunteerGetNotificationResponse response = VolunteerGetNotificationResponse.of(volunteer.getNotification());
return response;
}
}
Loading
Loading