Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.campus.campus.domain.manager.application.dto.request;

import lombok.Builder;

@Builder
public record RewardGrantedEvent(
Long userId,
String rewardName
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.stereotype.Component;

import com.campus.campus.domain.council.domain.entity.StudentCouncil;
import com.campus.campus.domain.manager.application.dto.request.RewardGrantedEvent;
import com.campus.campus.domain.manager.application.dto.request.RewardRequest;
import com.campus.campus.domain.manager.application.dto.response.CertifyRequestCouncilResponse;
import com.campus.campus.domain.manager.application.dto.response.CouncilApproveOrDenyResponse;
Expand Down Expand Up @@ -71,4 +72,11 @@ public Reward createReward(User user, RewardRequest rewardRequest) {
.rewardImageUrl(rewardRequest.rewardImageUrl())
.build();
}

public RewardGrantedEvent createRewardGrantedEvent(Long userId, String rewardName) {
return RewardGrantedEvent.builder()
.userId(userId)
.rewardName(rewardName)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.crypto.password.PasswordEncoder;
Expand Down Expand Up @@ -48,6 +49,7 @@ public class ManagerService {
private final RedisTokenService redisTokenService;
private final ManagerMapper managerMapper;
private final JavaMailSender javaMailSender;
private final ApplicationEventPublisher eventPublisher;

@Value("${jwt.refresh.expiration-seconds}")
private long refreshTokenExpirationSeconds;
Expand Down Expand Up @@ -130,6 +132,8 @@ public void grantRewardToUser(Long userId, RewardRequest rewardRequest) {
rewardRepository.save(reward);

user.updateRewardNeeded(false);

eventPublisher.publishEvent(managerMapper.createRewardGrantedEvent(userId, "스탬프 보상"));
}

private void sendCouncilApprovedMail(String to) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.campus.campus.domain.manager.application.service;

import java.util.Map;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import com.campus.campus.domain.manager.application.dto.request.RewardGrantedEvent;
import com.campus.campus.domain.notification.application.service.NotificationService;
import com.campus.campus.global.firebase.application.service.FirebaseCloudMessageService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@RequiredArgsConstructor
public class RewardPushListener {
private static final String DATA_KEY_TYPE = "type";
private static final String DATA_TYPE_REWARD_GRANTED = "REWARD_GRANTED";

private final FirebaseCloudMessageService firebaseCloudMessageService;
private final NotificationService notificationService;

@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void handleRewardGrantedEvent(RewardGrantedEvent rewardGrantedEvent) {
String title = "보상 지급 알림";
String body = "스탬프 보상이 지급되었습니다. 보상함을 확인해주세요.";

notificationService.saveRewardGrantedNotification(rewardGrantedEvent.userId(), title, body);

String userTopic = "user_" + rewardGrantedEvent.userId();

log.info("[PUSH] Reward granted. topic={}, userId={}", userTopic, rewardGrantedEvent.userId());

firebaseCloudMessageService.sendToTopic(
userTopic,
title,
body,
Map.of(
DATA_KEY_TYPE, DATA_TYPE_REWARD_GRANTED
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ public void savePostCreatedNotification(CouncilPostCreatedEvent event, String ti
notificationRepository.saveAll(notifications);
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void saveRewardGrantedNotification(Long userId, String title, String body) {
User user = userRepository.findByIdAndDeletedAtIsNull(userId)
.orElseThrow(UserNotFoundException::new);

Notification notification = notificationMapper.createNotification(user, NotificationType.REWARD_GRANTED, title,
body, null);

notificationRepository.save(notification);
}

@Transactional(readOnly = true)
public boolean hasUnread(Long userId) {
return notificationRepository.existsByUser_IdAndIsReadFalse(userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

public enum NotificationType {
COUNCIL_POST_CREATED,
SYSTEM_NOTICE
SYSTEM_NOTICE,
REWARD_GRANTED
}