Skip to content

Commit

Permalink
refactor: 알림 상태 추가 및 조회 api 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
seheonnn committed Aug 30, 2024
1 parent 4ab77d5 commit 16d4ed0
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sponus.coredomain.domain.notification.NotificationStatus;
import com.sponus.coredomain.domain.organization.Organization;
import com.sponus.coreinfrafirebase.FirebaseUtil;
import com.sponus.coreinfrasecurity.annotation.AuthOrganization;
Expand All @@ -28,7 +29,7 @@ public class FirebaseTestController {
@PostMapping("/fcm")
public String testNotification(@RequestBody NotificationTestRequest request,
@AuthOrganization Organization organization) throws IOException {
firebaseUtil.sendMessageTo(organization, request.title(), request.body(), null);
firebaseUtil.sendMessageTo(organization, request.title(), request.body(), null, NotificationStatus.SEND);
return "Notification test is successful !";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.time.LocalDateTime;

import com.sponus.coredomain.domain.notification.Notification;
import com.sponus.coredomain.domain.organization.Organization;
import com.sponus.coredomain.domain.propose.Propose;

import lombok.Builder;
Expand All @@ -12,22 +13,24 @@ public record NotificationSummaryResponse(
Long id,
String title,
String body,
Long organizationId,
Long announcementId,
String organizationProfile,
String organizationName,
Long proposeId,
Long reportId,
boolean isRead,
LocalDateTime createdAt,
LocalDateTime updatedAt
) {
public static NotificationSummaryResponse from(Notification notification) {
Propose propose = notification.getPropose();
Organization organization = notification.getOrganization();
String organizationProfile = organization.getImageUrl() != null ? organization.getImageUrl() : null;

return NotificationSummaryResponse.builder()
.id(notification.getId())
.title(notification.getTitle())
.body(notification.getBody())
.organizationId(notification.getOrganization().getId())
.organizationName(notification.getOrganization().getName())
.organizationProfile(organizationProfile)
.proposeId(propose != null ? propose.getId() : null)
.isRead(notification.isRead())
.createdAt(notification.getCreatedAt())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,16 @@ public ApiResponse<Void> deleteSearchKeyword(
return ApiResponse.onSuccess(null);
}

@GetMapping("/notifications")
public ApiResponse<List<NotificationSummaryResponse>> getNotifications(
@GetMapping("/notifications/send")
public ApiResponse<List<NotificationSummaryResponse>> getSendNotifications(
@AuthOrganization Organization organization) {
return ApiResponse.onSuccess(organizationQueryService.getNotifications(organization));
return ApiResponse.onSuccess(organizationQueryService.getSendNotifications(organization));
}

@GetMapping("/notifications/receive")
public ApiResponse<List<NotificationSummaryResponse>> getReceivedNotifications(
@AuthOrganization Organization organization) {
return ApiResponse.onSuccess(organizationQueryService.getReceiveNotifications(organization));
}

@DeleteMapping("/notifications/{notificationId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.transaction.annotation.Transactional;

import com.sponus.coredomain.domain.bookmark.repository.BookmarkRepository;
import com.sponus.coredomain.domain.notification.NotificationStatus;
import com.sponus.coredomain.domain.notification.repository.NotificationRepository;
import com.sponus.coredomain.domain.organization.Organization;
import com.sponus.coredomain.domain.organization.enums.OrganizationType;
Expand Down Expand Up @@ -130,8 +131,17 @@ private SearchHistory findSearchHistory(Long organizationId) {
});
}

public List<NotificationSummaryResponse> getNotifications(Organization organization) {
return notificationRepository.findByOrganizationOrderByCreatedAtDesc(organization)
public List<NotificationSummaryResponse> getSendNotifications(Organization organization) {
return notificationRepository.findByOrganizationAndAndStatusOrderByCreatedAtDesc(organization,
NotificationStatus.SEND)
.stream()
.map(NotificationSummaryResponse::from)
.toList();
}

public List<NotificationSummaryResponse> getReceiveNotifications(Organization organization) {
return notificationRepository.findByOrganizationAndAndStatusOrderByCreatedAtDesc(organization,
NotificationStatus.RECEIVE)
.stream()
.map(NotificationSummaryResponse::from)
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sponus.coredomain.domain.notification.NotificationStatus;
import com.sponus.coredomain.domain.organization.Organization;
import com.sponus.coredomain.domain.organization.repository.OrganizationRepository;
import com.sponus.coredomain.domain.propose.Propose;
Expand Down Expand Up @@ -56,8 +57,13 @@ public ProposeCreateResponse createPropose(Organization organization, ProposeCre

final Propose propose = proposeRepository.save(request.toEntity(organization, target));
try {
firebaseUtil.sendMessageTo(target, organization.getName() + "으로부터 제안이 왔어요!", "이메일을 확인하고 기업과 컨택해 보세요.",
propose);
firebaseUtil.sendMessageTo(
target,
organization.getName() + "(으)로부터 제안이 왔어요!",
"이메일을 확인하고 기업과 컨택해 보세요.",
propose,
NotificationStatus.RECEIVE
);
} catch (IOException ex) {
log.error("[*] Failed to send notification to organization: " + target.getName(), ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class Notification extends BaseEntity {
@ColumnDefault("false")
private boolean isRead;

@Column(name = "notification_status", nullable = false)
private NotificationStatus status;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "organization_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private Organization organization;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.sponus.coredomain.domain.notification;

public enum NotificationStatus {
RECEIVE, SEND
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import org.springframework.data.jpa.repository.JpaRepository;

import com.sponus.coredomain.domain.notification.Notification;
import com.sponus.coredomain.domain.notification.NotificationStatus;
import com.sponus.coredomain.domain.organization.Organization;

public interface NotificationRepository extends JpaRepository<Notification, Long> {
List<Notification> findByOrganizationOrderByCreatedAtDesc(Organization organization);
List<Notification> findByOrganizationAndAndStatusOrderByCreatedAtDesc(Organization organization,
NotificationStatus status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.auth.oauth2.GoogleCredentials;
import com.sponus.coredomain.domain.notification.Notification;
import com.sponus.coredomain.domain.notification.NotificationStatus;
import com.sponus.coredomain.domain.notification.repository.NotificationRepository;
import com.sponus.coredomain.domain.organization.Organization;
import com.sponus.coredomain.domain.propose.Propose;
Expand Down Expand Up @@ -45,14 +46,16 @@ public class FirebaseUtil {

private final RedisUtil redisUtil;

public void sendMessageTo(Organization targetOrganization, String title, String body, Propose propose) throws
public void sendMessageTo(Organization targetOrganization, String title, String body, Propose propose,
NotificationStatus status) throws
IOException {

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

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

notification.setOrganization(targetOrganization);
Expand Down

0 comments on commit 16d4ed0

Please sign in to comment.