Skip to content

Commit 236df69

Browse files
authored
[fix] 알림 도메인 기능 추가 및 수정 (#134)
* hotfix// 알림 도메인 기능 추가 및 수정 1. 대기열 만료 알림 구현 2. 알림 조회시 응답 메시지 수정 3. 알림 읽음 처리 컨트롤러 메서드 User 객체 추출 방법 수정 4. Notification 엔티티 컬럼이름 수정 5. api 경로 수정 * fix: NotificationController에서 유저ID 추출 방법 변경 (Long userId = httpRequestContext.getUserId(); 사용)
1 parent 07307ae commit 236df69

11 files changed

Lines changed: 77 additions & 37 deletions

File tree

backend/src/main/java/com/back/api/notification/controller/NotificationApi.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@ public interface NotificationApi {
2222
"NOTIFICATION_ACCESS_DENIED"
2323
})
2424
ApiResponse<List<NotificationResponseDto>> getNotifications(
25-
@AuthenticationPrincipal SecurityUser securityUser
2625
);
2726

2827
@Operation(summary = "읽지 않은 알림 개수 조회", description = "웹소켓이 연결된 직후 초기 데이터 로딩을 위해 이용됩니다")
2928
@ApiErrorCode({
3029
"NOTIFICATION_ACCESS_DENIED"
3130
})
3231
ApiResponse<UnreadCountResponseDto> getUnreadCount(
33-
@AuthenticationPrincipal SecurityUser securityUser
3432
);
3533

3634
@Operation(summary = "단일 알림 읽음 처리")
@@ -40,7 +38,6 @@ ApiResponse<UnreadCountResponseDto> getUnreadCount(
4038
"NOTIFICATION_PROCESS_FAILED"
4139
})
4240
ApiResponse<Void> markAsRead(
43-
@AuthenticationPrincipal SecurityUser securityUser,
4441
@PathVariable Long notificationId
4542
);
4643

@@ -51,6 +48,5 @@ ApiResponse<Void> markAsRead(
5148
"NOTIFICATION_PROCESS_FAILED"
5249
})
5350
ApiResponse<Void> markAllAsRead(
54-
@AuthenticationPrincipal SecurityUser securityUser
5551
);
5652
}

backend/src/main/java/com/back/api/notification/controller/NotificationController.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.List;
44

5-
import org.springframework.security.core.annotation.AuthenticationPrincipal;
65
import org.springframework.web.bind.annotation.GetMapping;
76
import org.springframework.web.bind.annotation.PatchMapping;
87
import org.springframework.web.bind.annotation.PathVariable;
@@ -12,29 +11,29 @@
1211
import com.back.api.notification.dto.NotificationResponseDto;
1312
import com.back.api.notification.dto.UnreadCountResponseDto;
1413
import com.back.api.notification.service.NotificationService;
14+
import com.back.global.http.HttpRequestContext;
1515
import com.back.global.response.ApiResponse;
16-
import com.back.global.security.SecurityUser;
1716

1817
import lombok.RequiredArgsConstructor;
1918

2019
@RestController
2120
@RequiredArgsConstructor
22-
@RequestMapping("/api/notifications")
21+
@RequestMapping("/api/v1/notifications")
2322
public class NotificationController implements NotificationApi {
2423

2524
private final NotificationService notificationService;
25+
private final HttpRequestContext httpRequestContext;
2626

2727
@Override
2828
@GetMapping
2929
public ApiResponse<List<NotificationResponseDto>> getNotifications(
30-
@AuthenticationPrincipal SecurityUser securityUser
3130
) {
32-
Long userId = securityUser.getId();
31+
Long userId = httpRequestContext.getUserId();
3332

3433
List<NotificationResponseDto> notifications =
3534
notificationService.getNotifications(userId);
3635

37-
return ApiResponse.ok(notifications);
36+
return ApiResponse.ok("알림 목록을 불러왔습니다",notifications);
3837
}
3938

4039
/**
@@ -43,10 +42,10 @@ public ApiResponse<List<NotificationResponseDto>> getNotifications(
4342
@Override
4443
@GetMapping("/unread-count")
4544
public ApiResponse<UnreadCountResponseDto> getUnreadCount(
46-
@AuthenticationPrincipal SecurityUser securityUser
4745
) {
48-
long count = notificationService.getUnreadCount(securityUser.getId());
49-
return ApiResponse.ok(new UnreadCountResponseDto(count));
46+
Long userId = httpRequestContext.getUserId();
47+
long count = notificationService.getUnreadCount(userId);
48+
return ApiResponse.ok("읽지 않은 알림수",new UnreadCountResponseDto(count));
5049
}
5150

5251
/**
@@ -55,10 +54,10 @@ public ApiResponse<UnreadCountResponseDto> getUnreadCount(
5554
@Override
5655
@PatchMapping("/{notificationId}/read")
5756
public ApiResponse<Void> markAsRead(
58-
@AuthenticationPrincipal SecurityUser securityUser,
5957
@PathVariable Long notificationId
6058
) {
61-
notificationService.markAsRead(notificationId, securityUser.getId());
59+
Long userId = httpRequestContext.getUserId();
60+
notificationService.markAsRead(notificationId, userId);
6261

6362
return ApiResponse.noContent("개별 알림을 읽음 처리 하였습니다.");
6463
}
@@ -68,10 +67,9 @@ public ApiResponse<Void> markAsRead(
6867
*/
6968
@Override
7069
@PatchMapping("/read-all")
71-
public ApiResponse<Void> markAllAsRead(
72-
@AuthenticationPrincipal SecurityUser securityUser
73-
) {
74-
notificationService.markAllAsRead(securityUser.getId());
70+
public ApiResponse<Void> markAllAsRead() {
71+
Long userId = httpRequestContext.getUserId();
72+
notificationService.markAllAsRead(userId);
7573
return ApiResponse.noContent("모든 알림을 읽음 처리 하였습니다.");
7674
}
7775
}

backend/src/main/java/com/back/api/notification/listener/NotificationEventListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public void handleNotificationMessage(NotificationMessage message) {
3838
)
3939
.type(message.getNotificationType())
4040
.typeDetail(message.getTypeDetail())
41-
.fromWhere(message.getFromWhere())
42-
.whereId(message.getWhereId())
41+
.domainName(message.getFromWhere())
42+
.domainId(message.getWhereId())
4343
.title(message.getTitle())
4444
.message(message.getMessage())
4545
.isRead(false)

backend/src/main/java/com/back/api/queue/service/QueueEntryProcessService.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.back.domain.event.entity.Event;
1919
import com.back.domain.event.repository.EventRepository;
2020
import com.back.domain.notification.systemMessage.QueueEntriesMessage;
21+
import com.back.domain.notification.systemMessage.QueueExpiredMessage;
2122
import com.back.domain.queue.entity.QueueEntry;
2223
import com.back.domain.queue.entity.QueueEntryStatus;
2324
import com.back.domain.queue.repository.QueueEntryRedisRepository;
@@ -171,7 +172,7 @@ public void expireEntry(Long eventId, Long userId) {
171172
}
172173

173174
queueEntry.expire();
174-
queueEntryRepository.save(queueEntry);
175+
QueueEntry deque = queueEntryRepository.save(queueEntry);
175176

176177
try {
177178
queueEntryRedisRepository.removeFromEnteredQueue(eventId, userId);
@@ -182,7 +183,15 @@ public void expireEntry(Long eventId, Long userId) {
182183

183184
publishExpiredEvent(queueEntry); // 만료 처리 웹소켓 이벤트 발행
184185

185-
//TODO 알림 로직 구현 필요
186+
eventPublisher.publishEvent(
187+
new QueueExpiredMessage(
188+
userId,
189+
deque.getId(),
190+
eventRepository.findById(eventId)
191+
.map(Event::getTitle)
192+
.orElse("제목 없음")
193+
)
194+
);
186195
}
187196

188197
@Transactional

backend/src/main/java/com/back/domain/notification/entity/Notification.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public class Notification extends BaseEntity {
5959

6060
@Enumerated(EnumType.STRING)
6161
@Column(nullable = false)
62-
private DomainName fromWhere;
62+
private DomainName domainName;
6363

6464
@Column(nullable = true)
65-
private Long whereId;
65+
private Long domainId;
6666

6767
//연관 필드
6868
@ManyToOne(fetch = FetchType.LAZY) // -> 리팩토링 고민요소

backend/src/main/java/com/back/domain/notification/enums/NotificationTypeDetails.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
public enum NotificationTypeDetails {
55
// QueueEntries
66
TICKETING_POSSIBLE, // 대기열 순서 완료
7+
TICKETING_EXPIRED, // 대기열 만료
78

89
// PAYMENT
910
PAYMENT_SUCCESS,

backend/src/main/java/com/back/domain/notification/systemMessage/QueueEntriesMessage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
public class QueueEntriesMessage extends NotificationMessage {
1010
private final String eventName;
1111

12-
public QueueEntriesMessage(Long userId, Long whereId, String eventName) {
13-
super(userId, DomainName.QUEUE_ENTRIES, whereId);
12+
public QueueEntriesMessage(Long userId, Long domainId, String eventName) {
13+
super(userId, DomainName.QUEUE_ENTRIES, domainId);
1414
this.eventName = eventName;
1515
}
1616

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.back.domain.notification.systemMessage;
2+
3+
import static java.lang.String.*;
4+
5+
import com.back.domain.notification.enums.DomainName;
6+
import com.back.domain.notification.enums.NotificationTypeDetails;
7+
import com.back.domain.notification.enums.NotificationTypes;
8+
9+
public class QueueExpiredMessage extends NotificationMessage {
10+
private final String eventName;
11+
12+
public QueueExpiredMessage(Long userId, Long domainId, String eventName) {
13+
super(userId, DomainName.QUEUE_ENTRIES, domainId);
14+
this.eventName = eventName;
15+
}
16+
17+
@Override
18+
public NotificationTypes getNotificationType() {
19+
return NotificationTypes.QUEUE_ENTRIES;
20+
}
21+
22+
@Override
23+
public NotificationTypeDetails getTypeDetail() {
24+
return NotificationTypeDetails.TICKETING_EXPIRED;
25+
}
26+
27+
@Override
28+
public String getTitle() {
29+
return "티켓팅 종료";
30+
}
31+
32+
@Override
33+
public String getMessage() {
34+
return format("[%s]\n아쉽게도 티켓팅 가능 시간이 초과되었습니다.\n다음 기회를 노려주세요..",this.eventName);
35+
}
36+
}

backend/src/main/java/com/back/global/init/NotificationDataInit.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ private Notification createNotificationFromMessage(User user, NotificationMessag
123123
.user(user)
124124
.type(message.getNotificationType())
125125
.typeDetail(message.getTypeDetail())
126-
.fromWhere(message.getFromWhere())
127-
.whereId(message.getWhereId())
126+
.domainName(message.getFromWhere())
127+
.domainId(message.getWhereId())
128128
.title(message.getTitle())
129129
.message(message.getMessage())
130130
.isRead(false)

backend/src/test/java/com/back/api/notification/listener/NotificationEventListenerTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void publishOrdersSuccessMessage_UserOnline_SavesAndSendsWebSocket() {
137137
Notification saved = notifications.get(0);
138138
assertThat(saved.getType()).isEqualTo(NotificationTypes.PAYMENT);
139139
assertThat(saved.getTypeDetail()).isEqualTo(NotificationTypeDetails.PAYMENT_SUCCESS);
140-
assertThat(saved.getFromWhere()).isEqualTo(DomainName.ORDERS);
140+
assertThat(saved.getDomainName()).isEqualTo(DomainName.ORDERS);
141141
assertThat(saved.getTitle()).isEqualTo("주문 및 결제 완료");
142142
assertThat(saved.getMessage()).contains("테스트 이벤트", "99000원");
143143
assertThat(saved.isRead()).isFalse();
@@ -208,8 +208,8 @@ void publishOrdersSuccessMessage_VerifyNotificationTypeDetails() {
208208
Notification saved = notifications.get(0);
209209
assertThat(saved.getType()).isEqualTo(NotificationTypes.PAYMENT);
210210
assertThat(saved.getTypeDetail()).isEqualTo(NotificationTypeDetails.PAYMENT_SUCCESS);
211-
assertThat(saved.getFromWhere()).isEqualTo(DomainName.ORDERS);
212-
assertThat(saved.getWhereId()).isEqualTo(1L);
211+
assertThat(saved.getDomainName()).isEqualTo(DomainName.ORDERS);
212+
assertThat(saved.getDomainId()).isEqualTo(1L);
213213
});
214214
}
215215
}
@@ -278,7 +278,7 @@ void publishOrderFailedMessage_VerifyFailedType() {
278278
Notification saved = notifications.get(0);
279279
assertThat(saved.getType()).isEqualTo(NotificationTypes.PAYMENT);
280280
assertThat(saved.getTypeDetail()).isEqualTo(NotificationTypeDetails.PAYMENT_FAILED);
281-
assertThat(saved.getFromWhere()).isEqualTo(DomainName.ORDERS);
281+
assertThat(saved.getDomainName()).isEqualTo(DomainName.ORDERS);
282282
});
283283
}
284284
}
@@ -345,7 +345,7 @@ void publishPreRegisterDoneMessage_VerifyType() {
345345
Notification saved = notifications.get(0);
346346
assertThat(saved.getType()).isEqualTo(NotificationTypes.PRE_REGISTER);
347347
assertThat(saved.getTypeDetail()).isEqualTo(NotificationTypeDetails.PRE_REGISTER_DONE);
348-
assertThat(saved.getFromWhere()).isEqualTo(DomainName.PRE_REGISTER);
348+
assertThat(saved.getDomainName()).isEqualTo(DomainName.PRE_REGISTER);
349349
});
350350
}
351351
}
@@ -412,7 +412,7 @@ void publishQueueEntriesMessage_VerifyType() {
412412
Notification saved = notifications.get(0);
413413
assertThat(saved.getType()).isEqualTo(NotificationTypes.QUEUE_ENTRIES);
414414
assertThat(saved.getTypeDetail()).isEqualTo(NotificationTypeDetails.TICKETING_POSSIBLE);
415-
assertThat(saved.getFromWhere()).isEqualTo(DomainName.QUEUE_ENTRIES);
415+
assertThat(saved.getDomainName()).isEqualTo(DomainName.QUEUE_ENTRIES);
416416
});
417417
}
418418
}

0 commit comments

Comments
 (0)