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
@@ -1,6 +1,7 @@
package org.terning.notification.application.writer;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.terning.message.domain.Message;
Expand All @@ -20,6 +21,7 @@

@Service
@RequiredArgsConstructor
@Slf4j
public class NotificationWriterImpl implements NotificationWriter {

private static final String USERNAME = "username";
Expand All @@ -31,24 +33,72 @@ public class NotificationWriterImpl implements NotificationWriter {
@Override
@Transactional
public void write(MessageTemplateType template, ScheduledTime sendTime) {
log.info("🔔 [NotificationWriter] 알림 생성 시작 - 템플릿: {}", template);
List<User> targetUsers = fetchTargetUsers(template.targetType());
log.info("👥 [NotificationWriter] 대상 유저 수 (푸시 수신 허용 + 토큰 보유): {}", targetUsers.size());
// List<User> filteredUsers = targetUsers.stream()
// .filter(user -> user.getToken() != null)
// .filter(user -> user.getToken().value() != null)
// .filter(user -> !user.getToken().value().trim().isEmpty())
// .toList();
List<User> filteredUsers = targetUsers.stream()
.filter(user -> user.getToken() != null)
.filter(user -> user.getToken().value() != null)
.filter(user -> !user.getToken().value().trim().isEmpty())
.filter(user -> {
boolean valid = user.getToken() != null && user.getToken().value() != null && !user.getToken().value().trim().isEmpty();
if (!valid) {
log.warn("⚠️ [NotificationWriter] FCM 토큰 누락 - user(oUserId={}, name={})", user.getOUserId(), user.getName() != null ? user.getName().value() : "null");
}
return valid;
})
.toList();
log.info("✅ [NotificationWriter] 최종 알림 대상 유저 수: {}", filteredUsers.size());

List<Notification> notifications = createNotifications(filteredUsers, template, sendTime);
log.info("💾 [NotificationWriter] 생성된 알림 수: {}", notifications.size());

saveNotifications(notifications);
log.info("🎉 [NotificationWriter] 알림 저장 완료");
}

private List<User> fetchTargetUsers(MessageTargetType targetType) {
List<User> users = switch (targetType) {
case SCRAPPED_USER -> scrapRepository.findDistinctScrappedUsers();
case ALL_USERS -> userRepository.findAll();
default -> throw new NotificationException(NotificationErrorCode.INVALID_TARGET_TYPE);
};
log.info("🔍 [fetchTargetUsers] 대상 타입: {}", targetType);
// List<User> users = switch (targetType) {
// case SCRAPPED_USER -> scrapRepository.findDistinctScrappedUsers();
// case ALL_USERS -> userRepository.findAll();
// default -> throw new NotificationException(NotificationErrorCode.INVALID_TARGET_TYPE);
// };
// return users.stream()
// .filter(User::canReceivePushNotification)
// .toList();
List<User> users;

try {
users = switch (targetType) {
case SCRAPPED_USER -> {
List<User> scrappedUsers = scrapRepository.findDistinctScrappedUsers();
log.info("📦 [fetchTargetUsers] 스크랩 유저 수: {}", scrappedUsers.size());
yield scrappedUsers;
}
case ALL_USERS -> {
List<User> allUsers = userRepository.findAll();
log.info("📦 [fetchTargetUsers] 전체 유저 수: {}", allUsers.size());
yield allUsers;
}
default -> throw new NotificationException(NotificationErrorCode.INVALID_TARGET_TYPE);
};
} catch (Exception e) {
log.error("🔥 [fetchTargetUsers] 유저 조회 중 예외 발생", e);
throw e;
}

return users.stream()
.peek(user -> {
if (user.getPushStatus() == null) {
log.warn("⚠️ [fetchTargetUsers] 유저(oUserId={})의 pushStatus가 null", user.getOUserId());
}
if (user.getToken() == null) {
log.warn("⚠️ [fetchTargetUsers] 유저(oUserId={})의 FCM 토큰이 null", user.getOUserId());
}
})
.filter(User::canReceivePushNotification)
.toList();
}
Expand Down
50 changes: 21 additions & 29 deletions src/main/java/org/terning/scrap/application/ScrapSyncManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,35 @@ public class ScrapSyncManager {
private final UserRepository userRepository;
private final ScrapRepository scrapRepository;

public void sync(List<Long> userIds) {
List<Long> distinctUserIds = userIds.stream().distinct().toList();
public void sync(List<Long> oUserIds) {
List<Long> distinctOUserIds = oUserIds.stream().distinct().toList();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

운영서버로 받아오는 스크랩한 userId가 중복되기도 하여서 oUserIds.stream().distinct().toList() 요거로 중복제거하는 건가요??

Map<Long, User> usersByOUserId = userRepository.findUsersByOUserIds(distinctOUserIds);

Map<Long, User> usersById = userRepository.findUsersByIds(distinctUserIds);
Map<Long, Scrap> scrapsByUserId = scrapRepository.findScrapsByUserIds(distinctUserIds).stream()
List<Long> internalUserIds = usersByOUserId.values().stream()
.map(User::getId)
.toList();

Map<Long, Scrap> scrapsByUserId = scrapRepository.findScrapsByUserIds(internalUserIds).stream()
.collect(Collectors.toMap(scrap -> scrap.getUser().getId(), Function.identity()));

List<Scrap> scrapsToPersist = distinctUserIds.stream()
.map(userId -> createOrUpdateScrap(userId, usersById, scrapsByUserId))
List<Scrap> scrapsToPersist = distinctOUserIds.stream()
.map(oUserId -> {
User user = usersByOUserId.get(oUserId);
if (user == null) return null;

Scrap existingScrap = scrapsByUserId.get(user.getId());
if (existingScrap == null) return Scrap.of(user);
if (existingScrap.isUnscrapped()) {
existingScrap.scrap();
return existingScrap;
}
return null;
})
.filter(Objects::nonNull)
.toList();

if (!scrapsToPersist.isEmpty()) {
scrapRepository.saveAll(scrapsToPersist);
}
}

private Scrap createOrUpdateScrap(
Long userId,
Map<Long, User> usersById,
Map<Long, Scrap> scrapsByUserId
) {
User user = usersById.get(userId);
if (user == null) {
return null;
}

Scrap existingScrap = scrapsByUserId.get(userId);
if (existingScrap == null) {
return Scrap.of(user);
}

if (existingScrap.isUnscrapped()) {
existingScrap.scrap();
return existingScrap;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public List<User> findDistinctScrappedUsers() {
return queryFactory
.selectDistinct(scrap.user)
.from(scrap)
.join(scrap.user, user).fetchJoin()
.join(scrap.user, user)
.where(scrap.status.eq(ScrapStatus.SCRAPPED))
.fetch();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
public interface UserRepositoryCustom {
Map<Long, User> findUsersByIds(List<Long> userIds);

Map<Long, User> findUsersByOUserIds(List<Long> oUserIds);

Optional<User> findByOUserId(Long oUserId);
}

10 changes: 10 additions & 0 deletions src/main/java/org/terning/user/domain/UserRepositoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public Map<Long, User> findUsersByIds(List<Long> userIds) {
.collect(Collectors.toMap(User::getId, Function.identity()));
}

@Override
public Map<Long, User> findUsersByOUserIds(List<Long> oUserIds) {
return queryFactory
.selectFrom(QUser.user)
.where(QUser.user.oUserId.in(oUserIds))
.fetch()
.stream()
.collect(Collectors.toMap(User::getOUserId, Function.identity()));
}

@Override
public Optional<User> findByOUserId(Long oUserId) {
QUser user = QUser.user;
Expand Down