Skip to content

Commit

Permalink
Merge pull request #254 from Open-Eye-Im-Developer/develop
Browse files Browse the repository at this point in the history
main <- develop (alpha .2)
  • Loading branch information
tidavid1 authored Mar 11, 2024
2 parents bfa56fd + a9d18d8 commit b51a522
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package io.oeid.mogakgo.domain.notification.application;

import static io.oeid.mogakgo.domain.notification.domain.enums.NotificationMessage.REVIEW_REQUEST_MESSAGE;

import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;
import com.google.firebase.messaging.WebpushConfig;
import com.google.firebase.messaging.WebpushFcmOptions;
import io.oeid.mogakgo.domain.notification.domain.enums.FCMNotificationType;
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;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
Expand Down Expand Up @@ -43,58 +48,74 @@ public void manageToken(Long userId, String fcmToken) {
log.info("manageToken End");
}

@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void sendNotification(Long userId, String title, String body) {
log.info("sendNotification Start");
getFCMToken(userId).ifPresent(
fcmToken -> {
Message message = Message.builder()
.setNotification(Notification.builder()
.setTitle(title)
.setBody(body)
.build())
.setToken(fcmToken)
.build();
try {
String response = firebaseMessaging.send(message);
log.info("Successfully sent message: " + response);
} catch (FirebaseMessagingException e) {
log.error("Error sending message: " + e.getMessage());
}
Message message = generateFirebaseMessage(title, body);
sendMessageToFCM(message);
}
);
// send notification
log.info("sendNotification End");
}

public void sendNotification(Long userId, String title, String body, String redirectUri) {
log.info("sendNotification Start");
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void sendNotification(Long userId, String title, String body,
FCMNotificationType notificationType) {
getFCMToken(userId).ifPresent(
fcmToken -> {
WebpushConfig webpushConfig = WebpushConfig.builder()
.setFcmOptions(WebpushFcmOptions.builder()
.setLink(clientUrl + redirectUri)
.build())
.build();
// send notification
Message message = Message.builder()
.setNotification(Notification.builder()
.setTitle(title)
.setBody(body)
.build())
.setToken(fcmToken)
.setWebpushConfig(webpushConfig)
.build();
try {
String response = firebaseMessaging.send(message);
log.info("Successfully sent message: " + response);
} catch (FirebaseMessagingException e) {
log.error("Error sending message: " + e.getMessage());
}
Message message = generateFirebaseMessage(title, body,
notificationType.getRedirectUri());
sendMessageToFCM(message);
}
);
log.info("sendNotification End");
}

@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void sendNotification(Long userId, User receiver, Long projectId) {
String redirectUrl =
FCMNotificationType.REVIEW_REQUEST.getRedirectUri() + "?receiverId=" + receiver.getId()
+ "&projectId=" + projectId;
getFCMToken(userId).ifPresent(
fcmToken -> {
Message message = generateFirebaseMessage(REVIEW_REQUEST_MESSAGE.getTitle(),
receiver.getUsername()+ REVIEW_REQUEST_MESSAGE.getMessage(), redirectUrl);
sendMessageToFCM(message);
}
);
}

private void sendMessageToFCM(Message message) {
log.info("sendNotification Start");
try {
String response = firebaseMessaging.send(message);
log.info("Successfully sent message: " + response);
} catch (FirebaseMessagingException e) {
log.error("Error sending message: " + e.getMessage());
}
}

private Message generateFirebaseMessage(String title, String body) {
return Message.builder()
.setNotification(Notification.builder()
.setTitle(title)
.setBody(body)
.build())
.build();
}

private Message generateFirebaseMessage(String title, String body, String redirectUri) {
return Message.builder()
.setNotification(Notification.builder()
.setTitle(title)
.setBody(body)
.build())
.setWebpushConfig(WebpushConfig.builder()
.setFcmOptions(WebpushFcmOptions.builder()
.setLink(clientUrl + redirectUri)
.build())
.build())
.build();
}

private Optional<String> getFCMToken(Long userId) {
return fcmTokenRepository.findById(userId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.oeid.mogakgo.domain.notification.domain;

import io.oeid.mogakgo.domain.achievement.domain.entity.Achievement;
import io.oeid.mogakgo.domain.notification.domain.enums.NotificationMessage;
import io.oeid.mogakgo.domain.notification.domain.enums.NotificationTag;
import io.oeid.mogakgo.domain.notification.exception.NotificationException;
import io.oeid.mogakgo.domain.project.domain.entity.Project;
Expand Down Expand Up @@ -33,12 +34,6 @@
@EntityListeners(AuditingEntityListener.class)
public class Notification {

private static final String REVIEW_REQUEST_MESSAGE = " 님과의 만남 후기를 작성해주세요!";
private static final String ACHIEVEMENT_MESSAGE = " 업적을 달성했습니다!";
private static final String REQUEST_ARRIVAL_MESSAGE = "매칭 참여 요청이 도착했습니다!";
private static final String MATCHING_SUCCESS_MESSAGE = "매칭이 성공적으로 이루어졌습니다!";
private static final String MATCHING_FAILED_MESSAGE = "매칭 요청이 거절되었어요 ㅠㅠ";

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand Down Expand Up @@ -104,26 +99,29 @@ private Notification(NotificationTag notificationTag, String message, User user,

public static Notification newReviewRequestNotification(User user, User receiver,
Project project) {
return new Notification(receiver.getUsername() + REVIEW_REQUEST_MESSAGE, user, receiver,
project);
return new Notification(
receiver.getUsername() + NotificationMessage.REVIEW_REQUEST_MESSAGE.getMessage(), user,
receiver, project);
}

public static Notification newAchievementNotification(User user, Achievement achievement) {
return new Notification(achievement.getTitle() + ACHIEVEMENT_MESSAGE, user, achievement);
return new Notification(
achievement.getTitle() + NotificationMessage.ACHIEVEMENT_MESSAGE.getMessage(), user,
achievement);
}

public static Notification newRequestArrivalNotification(User user) {
return new Notification(REQUEST_ARRIVAL_MESSAGE, user);
return new Notification(NotificationMessage.REQUEST_ARRIVAL_MESSAGE.getMessage(), user);
}

public static Notification newMatchingSuccessNotification(User user, Project project) {
return new Notification(NotificationTag.MATCHING_SUCCEEDED, MATCHING_SUCCESS_MESSAGE, user,
project);
return new Notification(NotificationTag.MATCHING_SUCCEEDED,
NotificationMessage.MATCHING_SUCCESS_MESSAGE.getMessage(), user, project);
}

public static Notification newMatchingFailedNotification(User user, Project project) {
return new Notification(NotificationTag.MATCHING_FAILED, MATCHING_FAILED_MESSAGE, user,
project);
return new Notification(NotificationTag.MATCHING_FAILED,
NotificationMessage.MATCHING_FAILED_MESSAGE.getMessage(), user, project);
}

private NotificationTag validateNotificationTag(NotificationTag notificationTag) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.oeid.mogakgo.domain.notification.domain.enums;

import lombok.Getter;

@Getter
public enum FCMNotificationType {
MATCHING_SUCCEEDED("/project"),
REVIEW_REQUEST("/review"),
;

private final String redirectUri;

FCMNotificationType(String redirectUri) {
this.redirectUri = redirectUri;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.oeid.mogakgo.domain.notification.domain.enums;

import lombok.Getter;

@Getter
public enum NotificationMessage {
REVIEW_REQUEST_MESSAGE("리뷰 요청", " 님과의 만남 후기를 작성해주세요!"),
ACHIEVEMENT_MESSAGE("업적 달성"," 업적을 달성했습니다!"),
REQUEST_ARRIVAL_MESSAGE("매칭 참여 요청", "매칭 참여 요청이 도착했습니다!"),
MATCHING_SUCCESS_MESSAGE("매칭 성공","매칭이 성공적으로 이루어졌습니다!"),
MATCHING_FAILED_MESSAGE("매칭 실패","매칭 요청이 거절되었어요 ㅠㅠ"),
;

private final String title;
private final String message;

NotificationMessage(String title, String message) {
this.title = title;
this.message = message;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.oeid.mogakgo.domain.project.presentation.dto.req.ProjectCreateReq;
import io.oeid.mogakgo.domain.project.presentation.dto.res.ProjectDensityRankRes;
import io.oeid.mogakgo.domain.project.presentation.dto.res.ProjectDetailAPIRes;
import io.oeid.mogakgo.domain.project.presentation.dto.res.ProjectDetailInfoAPIRes;
import io.oeid.mogakgo.domain.project.presentation.dto.res.ProjectInfoAPIRes;
import io.oeid.mogakgo.domain.project_join_req.exception.ProjectJoinRequestException;
import io.oeid.mogakgo.domain.project_join_req.infrastructure.ProjectJoinRequestJpaRepository;
Expand Down Expand Up @@ -179,7 +180,7 @@ public ProjectDensityRankRes getDensityRankProjects() {
return new ProjectDensityRankRes(regionRankList);
}

public List<ProjectDetailAPIRes> getLastedProjectByUserId(Long userId, Long id) {
public ProjectDetailInfoAPIRes getLastedProjectByUserId(Long userId, Long id) {
User user = getUser(userId);

validateProjectCardCreator(user, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.oeid.mogakgo.domain.geo.domain.enums.Region;
import io.oeid.mogakgo.domain.project.domain.entity.enums.ProjectStatus;
import io.oeid.mogakgo.domain.project.presentation.dto.res.ProjectDetailAPIRes;
import io.oeid.mogakgo.domain.project.presentation.dto.res.ProjectDetailInfoAPIRes;
import io.oeid.mogakgo.domain.project.presentation.dto.res.ProjectInfoAPIRes;
import java.util.List;

Expand All @@ -20,5 +21,5 @@ CursorPaginationResult<ProjectInfoAPIRes> findByCreatorIdWithPagination(

List<Region> getDensityRankProjectsByRegion(int limit);

List<ProjectDetailAPIRes> findLatestProjectByUserId(Long userId);
ProjectDetailInfoAPIRes findLatestProjectByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static io.oeid.mogakgo.domain.project.domain.entity.QProject.project;
import static io.oeid.mogakgo.domain.user.domain.QUser.user;
import static io.oeid.mogakgo.domain.matching.domain.entity.QMatching.matching;

import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand All @@ -13,6 +14,7 @@
import io.oeid.mogakgo.domain.project.domain.entity.enums.ProjectStatus;
import io.oeid.mogakgo.domain.project.presentation.dto.res.MeetingInfoResponse;
import io.oeid.mogakgo.domain.project.presentation.dto.res.ProjectDetailAPIRes;
import io.oeid.mogakgo.domain.project.presentation.dto.res.ProjectDetailInfoAPIRes;
import io.oeid.mogakgo.domain.project.presentation.dto.res.ProjectInfoAPIRes;
import io.oeid.mogakgo.domain.user.domain.UserDevelopLanguageTag;
import io.oeid.mogakgo.domain.user.domain.UserWantedJobTag;
Expand Down Expand Up @@ -126,18 +128,25 @@ public List<Region> getDensityRankProjectsByRegion(int limit) {
}

@Override
public List<ProjectDetailAPIRes> findLatestProjectByUserId(Long userId) {
public ProjectDetailInfoAPIRes findLatestProjectByUserId(Long userId) {

List<Project> entity = jpaQueryFactory.selectFrom(project)
.innerJoin(project.creator, user)
.on(project.creator.id.eq(user.id))
.where(
userIdEq(userId),
projectStatusEq(ProjectStatus.PENDING)
.or(projectStatusEq(ProjectStatus.MATCHED)),
createdAtEq(LocalDate.now())
)
.fetch();

return entity.stream().map(
Long matchingId = entity.isEmpty() ? null : jpaQueryFactory.select(matching.id)
.from(matching)
.where(matching.project.id.eq(entity.get(0).getId()))
.fetchOne();

List<ProjectDetailAPIRes> result = entity.stream().map(
project -> new ProjectDetailAPIRes(
project.getId(),
new UserPublicApiResponse(
Expand Down Expand Up @@ -165,6 +174,8 @@ public List<ProjectDetailAPIRes> findLatestProjectByUserId(Long userId) {
)
)
).toList();

return ProjectDetailInfoAPIRes.of(matchingId, result);
}

private BooleanExpression cursorIdCondition(Long cursorId) {
Expand All @@ -180,7 +191,7 @@ private BooleanExpression regionEq(Region region) {
}

private BooleanExpression projectStatusEq(ProjectStatus projectStatus) {
return projectStatus != null ? project.projectStatus.eq(projectStatus) : null;
return project.projectStatus.eq(projectStatus);
}

private BooleanExpression createdAtEq(LocalDate today) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public ResponseEntity<ProjectDetailAPIRes> getById(
@GetMapping("/{id}/info")
public ResponseEntity<ProjectDetailInfoAPIRes> getByUserId(
@UserId Long userId, @PathVariable Long id) {
return ResponseEntity.ok().body(ProjectDetailInfoAPIRes.of(projectService.getLastedProjectByUserId(userId, id)));
return ResponseEntity.ok().body(projectService.getLastedProjectByUserId(userId, id));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ProjectDetailInfoAPIRes {

private final Long matchingId;
private final List<ProjectDetailAPIRes> response;

public static ProjectDetailInfoAPIRes of(List<ProjectDetailAPIRes> response) {
return new ProjectDetailInfoAPIRes(response);
public static ProjectDetailInfoAPIRes of(Long matchingId, List<ProjectDetailAPIRes> response) {
return new ProjectDetailInfoAPIRes(matchingId, response);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.oeid.mogakgo.domain.project_join_req.application;

import static io.oeid.mogakgo.domain.notification.domain.enums.FCMNotificationType.MATCHING_SUCCEEDED;
import static io.oeid.mogakgo.domain.notification.domain.enums.NotificationMessage.MATCHING_SUCCESS_MESSAGE;
import static io.oeid.mogakgo.exception.code.ErrorCode400.INVALID_MATCHING_USER_TO_ACCEPT;
import static io.oeid.mogakgo.exception.code.ErrorCode400.INVALID_SENDER_TO_ACCEPT;
import static io.oeid.mogakgo.exception.code.ErrorCode400.PROJECT_JOIN_REQUEST_SHOULD_BE_ONLY_ONE;
Expand All @@ -12,6 +14,8 @@
import io.oeid.mogakgo.common.base.CursorPaginationResult;
import io.oeid.mogakgo.domain.matching.application.MatchingService;
import io.oeid.mogakgo.domain.matching.application.UserMatchingService;
import io.oeid.mogakgo.domain.notification.application.FCMNotificationService;
import io.oeid.mogakgo.domain.notification.application.NotificationService;
import io.oeid.mogakgo.domain.project.domain.entity.Project;
import io.oeid.mogakgo.domain.project.exception.ProjectException;
import io.oeid.mogakgo.domain.project.infrastructure.ProjectJpaRepository;
Expand All @@ -38,8 +42,9 @@ public class ProjectJoinRequestService {
private final ProjectJpaRepository projectRepository;
private final UserMatchingService userMatchingService;
private final MatchingService matchingService;

private final FCMNotificationService fcmNotificationService;
private final UserCommonService userCommonService;
private final NotificationService notificationService;

@Transactional
public Long create(Long userId, ProjectJoinCreateReq request) {
Expand Down Expand Up @@ -95,7 +100,11 @@ public Long accept(Long userId, Long projectRequestId) {
} catch (ProjectJoinRequestException e) {
// TODO: 로그 처리
}

fcmNotificationService.sendNotification(projectJoinRequest.getSender().getId(),
MATCHING_SUCCESS_MESSAGE.getTitle(), MATCHING_SUCCESS_MESSAGE.getMessage(),
MATCHING_SUCCEEDED);
notificationService.createMatchingSuccessNotification(projectJoinRequest.getSender().getId(),
projectJoinRequest.getProject());
return matchingId;
}

Expand Down

0 comments on commit b51a522

Please sign in to comment.