Skip to content

Commit

Permalink
�fix: LoginEvent에서 디바이스를 찾지 못하는 오류 수정 (#112)
Browse files Browse the repository at this point in the history
* fix: 로그인 당시 디바이스 토큰을 업데이트 하지 않은 경우 분기 처리

* move: 디바이스 서비스 관련 이벤트 처리 로직 디바이스 이벤트 핸들러로 이동

* refactor: 토큰 만료 여부 체크 함수 작성

* test: 오류 수정에 따른 테스트 코드 변경
  • Loading branch information
kimyu0218 authored Dec 22, 2024
1 parent 35293f5 commit 80d137c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
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

0 comments on commit 80d137c

Please sign in to comment.