Skip to content

Commit

Permalink
🔀 :: dev into main
Browse files Browse the repository at this point in the history
close #256
  • Loading branch information
leeseojune53 authored Jan 7, 2023
2 parents dce4484 + 82a468b commit 6cfd110
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;

@Getter
@NoArgsConstructor
@Table(name = "tbl_alarm")
@Entity
public class Alarm {
Expand Down Expand Up @@ -47,6 +50,16 @@ public class Alarm {

@CreatedDate private LocalDateTime createdAt;

@Builder
public Alarm(User sendUser, User receiveUser, AlarmType type, String title, String content) {
this.sendUser = sendUser;
this.receiveUser = receiveUser;
this.type = type;
this.title = title;
this.content = content;
this.isActivate = true;
}

public Long getSendUserId() {
return this.sendUser.getId();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.github.depromeet.knockknockbackend.domain.alarm.service;


import io.github.depromeet.knockknockbackend.domain.alarm.domain.Alarm;
import io.github.depromeet.knockknockbackend.domain.alarm.domain.repository.AlarmRepository;
import io.github.depromeet.knockknockbackend.domain.alarm.presentation.dto.response.QueryAlarmListResponse;
import io.github.depromeet.knockknockbackend.domain.alarm.presentation.dto.response.QueryAlarmListResponseElement;
import io.github.depromeet.knockknockbackend.domain.alarm.service.dto.CreateAlarmDto;
import io.github.depromeet.knockknockbackend.global.utils.security.SecurityUtils;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -12,7 +14,7 @@

@RequiredArgsConstructor
@Service
public class AlarmService {
public class AlarmService implements CreateAlarmService {

private final AlarmRepository alarmRepository;

Expand Down Expand Up @@ -42,4 +44,16 @@ public int queryCount() {
.findByReceiveUserIdAndIsActivateIsTrue(SecurityUtils.getCurrentUserId())
.size();
}

@Override
public void createAlarm(CreateAlarmDto createAlarmDto) {
alarmRepository.save(
Alarm.builder()
.sendUser(createAlarmDto.getSendUser())
.receiveUser(createAlarmDto.getReceiveUser())
.title(createAlarmDto.getTitle())
.content(createAlarmDto.getContent())
.type(createAlarmDto.getType())
.build());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.depromeet.knockknockbackend.domain.alarm.service;


import io.github.depromeet.knockknockbackend.domain.alarm.service.dto.CreateAlarmDto;

public interface CreateAlarmService {
void createAlarm(CreateAlarmDto createAlarmDto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.depromeet.knockknockbackend.domain.alarm.service.dto;


import io.github.depromeet.knockknockbackend.domain.alarm.domain.types.AlarmType;
import io.github.depromeet.knockknockbackend.domain.user.domain.User;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class CreateAlarmDto {
private final User sendUser;
private final User receiveUser;
private final String title;
private final String content;
private final AlarmType type;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.github.depromeet.knockknockbackend.domain.user.domain.User;
import io.github.depromeet.knockknockbackend.global.utils.security.SecurityUtils;
import io.github.depromeet.knockknockbackend.infrastructor.fcm.FcmService;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand All @@ -37,9 +36,10 @@
@Slf4j
@RequiredArgsConstructor
@Service
public class NotificationService implements NotificationUtils {
public class NotificationService {

private static final boolean CREATED_DELETED_STATUS = false;
private final NotificationUtils notificationUtils;
private final EntityManager entityManager;
private final FcmService fcmService;
private final GroupService groupService;
Expand Down Expand Up @@ -176,22 +176,12 @@ public void sendInstance(SendInstanceRequest request) {
Long currentUserId = SecurityUtils.getCurrentUserId();
validateSendNotificationPermission(request.getGroupId(), currentUserId);

List<DeviceToken> deviceTokens = getDeviceTokens(request.getGroupId(), currentUserId);
List<String> tokens = getFcmTokens(deviceTokens);
if (tokens.isEmpty()) {
return;
}

fcmService.sendGroupMessage(
tokens, request.getTitle(), request.getContent(), request.getImageUrl());

recordNotification(
deviceTokens,
notificationUtils.sendNotification(
currentUserId,
request.getGroupId(),
request.getTitle(),
request.getContent(),
request.getImageUrl(),
Group.of(request.getGroupId()),
User.of(currentUserId),
null);
}

Expand All @@ -202,40 +192,12 @@ public void sendInstanceToMeBeforeSignUp(SendInstanceToMeBeforeSignUpRequest req
}

public void deleteByNotificationId(Long notificationId) {
Notification notification = queryNotificationById(notificationId);
Notification notification = notificationUtils.queryNotificationById(notificationId);
validateDeletePermission(notification);
notification.deleteNotification();
notificationRepository.save(notification);
}

public List<DeviceToken> getDeviceTokens(Long groupId, Long sendUserId) {
Boolean nightOption = null;
if (NightCondition.isNight()) {
nightOption = true;
}

return notificationRepository.findTokenByGroupAndOptionAndNonBlock(
sendUserId, groupId, nightOption);
}

public List<String> getFcmTokens(List<DeviceToken> deviceTokens) {
return deviceTokens.stream().map(DeviceToken::getToken).collect(Collectors.toList());
}

public void recordNotification(
List<DeviceToken> deviceTokens,
String title,
String content,
String imageUrl,
Group group,
User sendUser,
LocalDateTime createdDate) {
Notification notification =
Notification.makeNotificationWithReceivers(
deviceTokens, title, content, imageUrl, group, sendUser, createdDate);
notificationRepository.save(notification);
}

public List<NotificationReaction> retrieveMyReactions(List<Notification> notifications) {
return notificationReactionRepository.findByUserAndNotificationIn(
User.of(SecurityUtils.getCurrentUserId()), notifications);
Expand All @@ -253,17 +215,9 @@ public void validateSendNotificationPermission(Long groupId, Long userId) {
if (GroupType.OPEN.equals(group.getGroupType())) group.validUserIsHost(userId);
// 친구방이면 그룹 소속원인지
if (GroupType.FRIEND.equals(group.getGroupType())) {
// group.validUserIsMemberOfGroup(userId); //todo: 해당 부분 호춣시 불필요한 쿼리 발생.
groupUserRepository
.findByGroupAndUser(group, User.of(userId))
.orElseThrow(() -> NotMemberException.EXCEPTION);
}
}

@Override
public Notification queryNotificationById(Long notificationId) {
return notificationRepository
.findById(notificationId)
.orElseThrow(() -> NotificationNotFoundException.EXCEPTION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@


import io.github.depromeet.knockknockbackend.domain.notification.domain.Notification;
import java.time.LocalDateTime;

public interface NotificationUtils {
Notification queryNotificationById(Long notificationId);

void sendNotification(
Long currentUserId,
Long groupId,
String title,
String content,
String imageUrl,
LocalDateTime reservedAt);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package io.github.depromeet.knockknockbackend.domain.notification.service;


import io.github.depromeet.knockknockbackend.domain.group.domain.Group;
import io.github.depromeet.knockknockbackend.domain.notification.domain.DeviceToken;
import io.github.depromeet.knockknockbackend.domain.notification.domain.NightCondition;
import io.github.depromeet.knockknockbackend.domain.notification.domain.Notification;
import io.github.depromeet.knockknockbackend.domain.notification.domain.repository.NotificationRepository;
import io.github.depromeet.knockknockbackend.domain.notification.exception.NotificationNotFoundException;
import io.github.depromeet.knockknockbackend.domain.user.domain.User;
import io.github.depromeet.knockknockbackend.infrastructor.fcm.FcmService;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class NotificationUtilsImpl implements NotificationUtils {

private final FcmService fcmService;
private final NotificationRepository notificationRepository;

@Override
public Notification queryNotificationById(Long notificationId) {
return notificationRepository
.findById(notificationId)
.orElseThrow(() -> NotificationNotFoundException.EXCEPTION);
}

@Override
public void sendNotification(
Long sendUserId,
Long groupId,
String title,
String content,
String imageUrl,
LocalDateTime reservedAt) {

List<DeviceToken> deviceTokens = getDeviceTokens(groupId, sendUserId);
List<String> tokens = getFcmTokens(deviceTokens);

recordNotification(
deviceTokens,
title,
content,
imageUrl,
Group.of(groupId),
User.of(sendUserId),
reservedAt);

if (tokens.isEmpty()) {
return;
}
fcmService.sendGroupMessage(tokens, title, content, imageUrl);
}

private List<DeviceToken> getDeviceTokens(Long groupId, Long sendUserId) {
Boolean nightOption = null;
if (NightCondition.isNight()) {
nightOption = true;
}

return notificationRepository.findTokenByGroupAndOptionAndNonBlock(
sendUserId, groupId, nightOption);
}

private List<String> getFcmTokens(List<DeviceToken> deviceTokens) {
return deviceTokens.stream().map(DeviceToken::getToken).collect(Collectors.toList());
}

private void recordNotification(
List<DeviceToken> deviceTokens,
String title,
String content,
String imageUrl,
Group group,
User sendUser,
LocalDateTime reservedAt) {
Notification notification =
Notification.makeNotificationWithReceivers(
deviceTokens, title, content, imageUrl, group, sendUser, reservedAt);
notificationRepository.save(notification);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


import io.github.depromeet.knockknockbackend.domain.group.domain.Group;
import io.github.depromeet.knockknockbackend.domain.notification.domain.DeviceToken;
import io.github.depromeet.knockknockbackend.domain.notification.domain.Reservation;
import io.github.depromeet.knockknockbackend.domain.notification.domain.repository.ReservationRepository;
import io.github.depromeet.knockknockbackend.domain.notification.exception.ReservationAlreadyExistException;
Expand All @@ -26,6 +25,7 @@
@Service
public class ReservationService {

private final NotificationUtils notificationUtils;
private final FcmService fcmService;
private final NotificationService notificationService;

Expand Down Expand Up @@ -80,28 +80,14 @@ public void processScheduledReservation() {
reservations.stream().map(Reservation::getId).collect(Collectors.toList()));

reservations.forEach(
reservation -> {
List<DeviceToken> deviceTokens =
notificationService.getDeviceTokens(
reservation.getGroup().getId(),
reservation.getSendUser().getId());
List<String> tokens = notificationService.getFcmTokens(deviceTokens);

fcmService.sendGroupMessage(
tokens,
reservation.getTitle(),
reservation.getContent(),
reservation.getImageUrl());

notificationService.recordNotification(
deviceTokens,
reservation.getTitle(),
reservation.getContent(),
reservation.getImageUrl(),
reservation.getGroup(),
reservation.getSendUser(),
reservation.getCreatedDate());
});
reservation ->
notificationUtils.sendNotification(
reservation.getSendUser().getId(),
reservation.getGroup().getId(),
reservation.getTitle(),
reservation.getContent(),
reservation.getImageUrl(),
reservation.getCreatedDate()));
}

private List<Reservation> retrieveReservation() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ public Optional<Relation> findRelationBySendUserIdAndReceiveUserId(

@Override
public boolean isFriend(Long currentUserId, Long userId) {
return queryFactory
.select(relation.isFriend.coalesce(false))
.from(relation)
.where(
friendPredicated(currentUserId, userId)
.or(friendPredicated(userId, currentUserId)))
.fetchFirst();
return Boolean.TRUE.equals(
queryFactory
.select(relation.isFriend.coalesce(false))
.from(relation)
.where(
friendPredicated(currentUserId, userId)
.or(friendPredicated(userId, currentUserId)))
.fetchOne());
}

@Override
Expand All @@ -63,7 +64,7 @@ public Long getRelationIdByUserId(Long currentUserId, Long userId) {
.where(
friendPredicated(currentUserId, userId)
.or(friendPredicated(userId, currentUserId)))
.fetchFirst();
.fetchOne();
}

private BooleanExpression friendPredicated(Long senderUserId, Long receiveUserId) {
Expand Down
Loading

0 comments on commit 6cfd110

Please sign in to comment.