11package apptive .team5 .alarm .service ;
22
3- import apptive .team5 .alarm .dto .AlarmSendRequest ;
3+ import apptive .team5 .alarm .dto .DiaryCreateAlarmSendRequest ;
4+ import apptive .team5 .alarm .dto .DiaryLikeAlarmSendRequest ;
5+ import apptive .team5 .alarm .dto .SubscribeAlarmSendRequest ;
46import apptive .team5 .alarm .entity .Alarm ;
57import apptive .team5 .alarm .entity .AlarmMessage ;
68import apptive .team5 .alarm .event .AlarmCreatedEvent ;
79import apptive .team5 .diary .domain .DiaryEntity ;
810import apptive .team5 .diary .service .DiaryLowService ;
11+ import apptive .team5 .subscribe .domain .Subscribe ;
12+ import apptive .team5 .subscribe .service .SubscribeLowService ;
913import apptive .team5 .user .domain .UserEntity ;
1014import apptive .team5 .user .service .UserLowService ;
1115import lombok .RequiredArgsConstructor ;
1418import org .springframework .stereotype .Service ;
1519import org .springframework .transaction .annotation .Transactional ;
1620
21+ import java .util .List ;
22+
1723@ Service
1824@ Transactional
1925@ RequiredArgsConstructor
2026public class AlarmDispatchService {
2127
22- private static final String DIARY_DEEP_LINK_FORMAT = "/diaries/%d" ;
28+ private static final String DIARY_DEEP_LINK_FORMAT = "/api/diaries/%d" ;
29+ private static final String SUBSCRIBE_DEEP_LINK_FORMAT = "/api/subscribes/%d/fans" ;
2330
2431 private final AlarmLowService alarmLowService ;
2532 private final DiaryLowService diaryLowService ;
2633 private final UserLowService userLowService ;
2734 private final ApplicationEventPublisher eventPublisher ;
35+ private final SubscribeLowService subscribeLowService ;
2836
2937 @ Async ("sendAlarm" )
30- public void saveAndDispatchForLike (AlarmSendRequest request ) {
38+ public void saveAndDispatchForLike (DiaryLikeAlarmSendRequest request ) {
3139
3240 Long actorId = request .actorId ();
3341 UserEntity actor = userLowService .findById (actorId );
@@ -38,6 +46,37 @@ public void saveAndDispatchForLike(AlarmSendRequest request) {
3846 String content = actor .getUsername () + "님이 회원님의 킬링파트를 좋아합니다." ;
3947 String deepLink = DIARY_DEEP_LINK_FORMAT .formatted (diary .getId ());
4048
49+ saveAndPublish (receiver , title , content , deepLink );
50+ }
51+
52+ @ Async ("sendAlarm" )
53+ public void saveAndDispatchForSubscribe (SubscribeAlarmSendRequest request ) {
54+ UserEntity subscriber = userLowService .findById (request .subscriberId ());
55+ UserEntity receiver = userLowService .findById (request .subscribedToUserId ());
56+
57+ String title = AlarmMessage .SUBSCRIBE_ALARM .getMessage ();
58+ String content = subscriber .getUsername () + "님이 회원님을 픽했어요." ;
59+ String deepLink = SUBSCRIBE_DEEP_LINK_FORMAT .formatted (receiver .getId ());
60+
61+ saveAndPublish (receiver , title , content , deepLink );
62+ }
63+
64+ @ Async ("sendAlarm" )
65+ public void saveAndDispatchForDiaryCreate (DiaryCreateAlarmSendRequest request ) {
66+ UserEntity actor = userLowService .findById (request .actorId ());
67+ List <Subscribe > subscribers = subscribeLowService .findBySubscribedToId (actor .getId ());
68+
69+
70+ String title = AlarmMessage .DIARY_ALARM .getMessage ();
71+ String content = actor .getUsername () + "님이 새 킬링파트를 등록했어요." ;
72+ String deepLink = DIARY_DEEP_LINK_FORMAT .formatted (request .diaryId ());
73+
74+
75+ subscribers .forEach (subscriber ->
76+ saveAndPublish (subscriber .getSubscriber (), title , content , deepLink ));
77+ }
78+
79+ private void saveAndPublish (UserEntity receiver , String title , String content , String deepLink ) {
4180 alarmLowService .save (new Alarm (
4281 title ,
4382 content ,
0 commit comments