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

fix: LoginEvent에서 디바이스를 찾지 못하는 오류 수정 #112

Merged
merged 4 commits into from
Dec 22, 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 @@ -95,7 +95,12 @@ private List<String> findTopicSubscribers(final Long missionId) {
*/
@Transactional
public void subscribeToMyMissions(final Long memberId, final String deviceIdentifier) {
Device device = deviceRepository.getDevice(memberId, deviceIdentifier);
Device device = deviceRepository.findByMemberIdAndDeviceIdentifier(memberId, deviceIdentifier)
.orElse(null);
if (device == null) {
return;
}

List<String> topics = findMySubscribedTopics(device.getId());
List<Mission> missions = missionRepository.findAllById(
findMySubscribableMission(memberId, topics)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ public record UpdateDeviceTokenEvent(
Long deviceId,
String deprecatedDeviceToken
) {

public boolean isTokenDeprecated() {
return deprecatedDeviceToken != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.nexters.goalpanzi.application.device.DeviceSubscriptionService;
import com.nexters.goalpanzi.application.device.event.UpdateDeviceTokenEvent;
import com.nexters.goalpanzi.application.device.event.UpdatePushActivationStatusEvent;
import com.nexters.goalpanzi.application.mission.event.SubscribeToMissionEvent;
import com.nexters.goalpanzi.application.mission.event.UnsubscribeFromMissionEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
Expand All @@ -24,7 +26,7 @@ public class DeviceSubscriptionEventHandler {
@Transactional(propagation = Propagation.REQUIRES_NEW)
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
void handleUpdateDeviceTokenEvent(final UpdateDeviceTokenEvent event) {
if (event.deprecatedDeviceToken() != null) {
if (event.isTokenDeprecated()) {
deviceSubscriptionService.unsubscribeFromMyMissions(event.memberId(), event.deviceId(), event.deprecatedDeviceToken());
}
deviceSubscriptionService.subscribeToMyMissions(event.memberId(), event.deviceId());
Expand Down Expand Up @@ -52,4 +54,17 @@ void handleLoginEvent(final LoginEvent event) {
log.info("Handled LoginEvent for memberId: {}", event.memberId());
}

@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
void handleSubscribeMissionEvent(final SubscribeToMissionEvent event) {
deviceSubscriptionService.subscribeToMission(event.memberId(), event.mission());
log.info("Handled SubscribeMissionEvent for memberId: {} and missionId: {}", event.memberId(), event.mission().getId());
}

@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
void handleUnsubscribeFromMissionEvent(final UnsubscribeFromMissionEvent event) {
deviceSubscriptionService.unsubscribeFromMission(event.missionId());
log.info("Handled UnsubscribeMissionEvent from missionId: {}", event.missionId());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.nexters.goalpanzi.application.firebase.event.handler;

import com.nexters.goalpanzi.application.device.DeviceSubscriptionService;
import com.nexters.goalpanzi.application.firebase.TopicGenerator;
import com.nexters.goalpanzi.application.mission.event.CompleteMissionEvent;
import com.nexters.goalpanzi.application.mission.event.JoinMissionEvent;
import com.nexters.goalpanzi.application.mission.event.SubscribeToMissionEvent;
import com.nexters.goalpanzi.application.mission.event.UnsubscribeFromMissionEvent;
import com.nexters.goalpanzi.infrastructure.firebase.PushMessageSender;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -25,8 +22,6 @@
@Component
public class PushMessageEventHandler {

private final DeviceSubscriptionService deviceSubscriptionService;

private final PushMessageSender pushMessageSender;

@Async
Expand Down Expand Up @@ -59,18 +54,4 @@ void handleCompleteMissionEvent(final CompleteMissionEvent event) {
);
log.info("Handled CompleteMissionEvent for missionId: {}", event.missionId());
}

@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
void handleSubscribeMissionEvent(final SubscribeToMissionEvent event) {
deviceSubscriptionService.subscribeToMission(event.memberId(), event.mission());
log.info("Handled SubscribeMissionEvent for memberId: {} and missionId: {}", event.memberId(), event.mission().getId());
}

@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
void handleUnsubscribeFromMissionEvent(final UnsubscribeFromMissionEvent event) {
deviceSubscriptionService.unsubscribeFromMission(event.missionId());
log.info("Handled UnsubscribeMissionEvent from missionId: {}", event.missionId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.test.context.ContextConfiguration;

import java.util.List;
import java.util.Optional;

import static com.nexters.goalpanzi.domain.mission.MissionStatus.CREATED;
import static com.nexters.goalpanzi.fixture.DeviceFixture.DEVICE_IDENTIFIER;
Expand Down Expand Up @@ -132,7 +133,7 @@ void setUp() {
DeviceSubscription mockDeviceSubscription = mock(DeviceSubscription.class);
when(mockDeviceSubscription.getMission()).thenReturn(mockSubscribedMission);

when(deviceRepository.getDevice(MEMBER_ID, DEVICE_IDENTIFIER)).thenReturn(mockDevice);
when(deviceRepository.findByMemberIdAndDeviceIdentifier(MEMBER_ID, DEVICE_IDENTIFIER)).thenReturn(Optional.of(mockDevice));
when(deviceSubscriptionRepository.findAllWithMissionAndDeviceByDeviceId(DEVICE_ID))
.thenReturn(List.of(mockDeviceSubscription));

Expand Down
Loading