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

Feat/#291 project finished notification #300

Merged
merged 4 commits into from
Mar 20, 2024
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
Expand Up @@ -12,7 +12,6 @@
import io.oeid.mogakgo.domain.notification.domain.vo.FCMToken;
import io.oeid.mogakgo.domain.notification.infrastructure.FCMTokenJpaRepository;
import io.oeid.mogakgo.domain.user.application.UserCommonService;
import io.oeid.mogakgo.domain.user.domain.User;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -71,15 +70,14 @@ public void sendNotification(Long userId, String title, String body,
}

@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void sendNotification(Long userId, User receiver, Long projectId) {
public void sendNotification(Long userId, Long receiverId, Long projectId) {
String redirectUrl =
FCMNotificationType.REVIEW_REQUEST.getRedirectUri() + "?receiverId=" + receiver.getId()
FCMNotificationType.REVIEW_REQUEST.getRedirectUri() + "?receiverId=" + receiverId
+ "&projectId=" + projectId;
getFCMToken(userId).ifPresent(
fcmToken -> {
Message message = generateFirebaseMessage(REVIEW_REQUEST_MESSAGE.getTitle(),
receiver.getUsername() + REVIEW_REQUEST_MESSAGE.getMessage(), redirectUrl,
fcmToken.getToken());
REVIEW_REQUEST_MESSAGE.getMessage(), redirectUrl, fcmToken.getToken());
sendMessageToFCM(message);
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.oeid.mogakgo.domain.notification.infrastructure.NotificationJpaRepository;
import io.oeid.mogakgo.domain.notification.presentation.dto.res.NotificationPublicApiRes;
import io.oeid.mogakgo.domain.project.domain.entity.Project;
import io.oeid.mogakgo.domain.project.infrastructure.ProjectJpaRepository;
import io.oeid.mogakgo.domain.user.application.UserCommonService;
import io.oeid.mogakgo.domain.user.domain.User;
import io.oeid.mogakgo.exception.code.ErrorCode404;
Expand All @@ -25,11 +26,15 @@ public class NotificationService {

private final UserCommonService userCommonService;
private final NotificationJpaRepository notificationRepository;
private final ProjectJpaRepository projectRepository;

public void createReviewRequestNotification(Long userId, User receiver, Project project) {
public void createReviewRequestNotification(Long userId, Long receiverId, Long projectId) {
log.info("createReviewRequestNotification userId: {}, receiver: {}, project: {}", userId,
receiver.getId(), project.getId());
receiverId, projectId);
User user = userCommonService.getUserById(userId);
User receiver = userCommonService.getUserById(receiverId);
Project project = projectRepository.findById(projectId)
.orElseThrow(() -> new NotificationException(ErrorCode404.PROJECT_NOT_FOUND));
notificationRepository.save(
Notification.newReviewRequestNotification(user, receiver, project));
}
Expand All @@ -54,10 +59,12 @@ public void createMatchingSuccessNotification(Long userId, Project project) {
notificationRepository.save(Notification.newMatchingSuccessNotification(user, project));
}

public void createMatchingFailedNotification(Long userId, Project project) {
public void createMatchingFailedNotification(Long userId, Long projectId) {
log.info("createMatchingFailedNotification userId: {}, project: {}", userId,
project.getId());
projectId);
User user = userCommonService.getUserById(userId);
Project project = projectRepository.findById(projectId)
.orElseThrow(() -> new NotificationException(ErrorCode404.PROJECT_NOT_FOUND));
notificationRepository.save(Notification.newMatchingFailedNotification(user, project));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@Getter
public enum NotificationMessage {
REVIEW_REQUEST_MESSAGE("๋ฆฌ๋ทฐ ์š”์ฒญ", " ๋‹˜๊ณผ์˜ ๋งŒ๋‚จ ํ›„๊ธฐ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”!"),
REVIEW_REQUEST_MESSAGE("๋ฆฌ๋ทฐ ์š”์ฒญ", "๋งŒ๋‚จ ํ›„๊ธฐ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”!"),
ACHIEVEMENT_MESSAGE("์—…์  ๋‹ฌ์„ฑ"," ์—…์ ์„ ๋‹ฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค!"),
REQUEST_ARRIVAL_MESSAGE("๋งค์นญ ์ฐธ์—ฌ ์š”์ฒญ", "๋งค์นญ ์ฐธ์—ฌ ์š”์ฒญ์ด ๋„์ฐฉํ–ˆ์Šต๋‹ˆ๋‹ค!"),
MATCHING_SUCCESS_MESSAGE("๋งค์นญ ์„ฑ๊ณต","๋งค์นญ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ด๋ฃจ์–ด์กŒ์Šต๋‹ˆ๋‹ค!"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.oeid.mogakgo.scheduler;

import io.oeid.mogakgo.domain.notification.application.FCMNotificationService;
import io.oeid.mogakgo.domain.notification.application.NotificationService;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Expand All @@ -20,14 +22,18 @@ public class FinishedProjectScheduler {

private final JdbcTemplate jdbcTemplate;
private final Resource[] sqlStatements;
private final NotificationService notificationService;
private final FCMNotificationService fcmNotificationService;

private FinishedProjectScheduler(
JdbcTemplate jdbcTemplate,
protected FinishedProjectScheduler(JdbcTemplate jdbcTemplate,
@Qualifier("webApplicationContext") ResourcePatternResolver resourcePatternResolver,
@Value("${path.schedule.sql}") String sqlStatementsPath
@Value("${path.schedule.sql}") String sqlStatementsPath,
NotificationService notificationService, FCMNotificationService fcmNotificationService
) throws IOException {
this.jdbcTemplate = jdbcTemplate;
this.sqlStatements = resourcePatternResolver.getResources(sqlStatementsPath);
this.notificationService = notificationService;
this.fcmNotificationService = fcmNotificationService;
}

@Scheduled(cron = "* * * * * ?") // ๋งค์ผ ์ž์ •์— ์‹คํ–‰
Expand All @@ -36,6 +42,9 @@ public void executeSqlFile() {
String sql = loadSqlFromFile(statement);
jdbcTemplate.execute(sql);
}
sendReviewNotification();
sendMatchFailNotification();

}

private String loadSqlFromFile(Resource resource) {
Expand All @@ -48,4 +57,30 @@ private String loadSqlFromFile(Resource resource) {
}
}

private void sendReviewNotification() {
jdbcTemplate.query(
"SELECT mt.sender_id, pt.id, pt.creator_id FROM matching_tb mt LEFT JOIN project_tb pt on mt.project_id = pt.id WHERE mt.matching_status = 'FINISHED' and DATE(pt.meet_start_time) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)",
rch -> {
Long senderId = rch.getLong("sender_id");
Long projectId = rch.getLong("id");
Long creatorId = rch.getLong("creator_id");
notificationService.createReviewRequestNotification(senderId, creatorId, projectId);
fcmNotificationService.sendNotification(senderId, creatorId, projectId);
notificationService.createReviewRequestNotification(creatorId, senderId, projectId);
fcmNotificationService.sendNotification(creatorId, senderId, projectId);
}
);
}

private void sendMatchFailNotification() {
jdbcTemplate.query(
"SELECT pjrt.sender_id, pjrt.project_id FROM mogakgo.project_join_request_tb pjrt WHERE pjrt.join_request_status = 'REJECTED' and DATE(pjrt.created_at) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)",
rch -> {
Long projectId = rch.getLong("project_id");
Long senderId = rch.getLong("sender_id");
notificationService.createMatchingFailedNotification(senderId, projectId);
}
);
}

}
Loading