Skip to content

Commit

Permalink
refactor: Notification refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Park-Young-Hun committed Feb 14, 2024
1 parent 6a27a54 commit 9ebd88e
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
18 changes: 18 additions & 0 deletions src/main/java/com/fullcar/carpool/domain/form/FormMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.fullcar.carpool.domain.form;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum FormMessage {
REJECT_MESSAGE("카풀 매칭에 실패했어요. 다른 카풀을 찾아보세요!"),
REQUEST_TITLE("탑승 요청이 들어왔어요!"),
REQUEST_BODY("탑승자 정보를 확인하고 승인해 주세요🚘"),
ACCEPT_TITLE("카풀 매칭에 성공했어요!"),
ACCEPT_BODY("운전자 정보를 확인해 주세요🚘"),
REJECT_TITLE("카풀 매칭에 실패했어요."),
REJECT_BODY("다른 카풀을 찾아볼까요?💁🏻‍♀️");

private final String message;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.fullcar.carpool.domain.service;

import com.fullcar.carpool.infra.dto.NotificationDto;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public interface NotificationService {
void sendNotification(String nickname, String deviceToken, String title, String body);
void sendNotification(NotificationDto notificationDto);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fullcar.carpool.infra;

import com.fullcar.carpool.domain.service.NotificationService;
import com.fullcar.carpool.infra.dto.NotificationDto;
import com.fullcar.core.exception.CustomException;
import com.fullcar.core.response.ErrorCode;
import com.google.firebase.messaging.FirebaseMessaging;
Expand All @@ -17,14 +18,14 @@ public class NotificationClient implements NotificationService {
private final FirebaseMessaging firebaseMessaging;

@Override
public void sendNotification(String nickname, String deviceToken, String title, String body) {
public void sendNotification(NotificationDto notificationDto) {
Notification notification = Notification.builder()
.setTitle(nickname + "님! " + title)
.setBody(body)
.setTitle(notificationDto.getNickName() + "님! " + notificationDto.getTitle())
.setBody(notificationDto.getBody())
.build();

Message message = Message.builder()
.setToken(deviceToken)
.setToken(notificationDto.getDeviceToken())
.setNotification(notification)
.build();

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/fullcar/carpool/infra/dto/NotificationDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.fullcar.carpool.infra.dto;

import lombok.*;

@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class NotificationDto {
private String nickName;

private String deviceToken;

private String title;

private String body;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.fullcar.carpool.infra.event;

import com.fullcar.carpool.infra.dto.NotificationDto;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class NotificationEvent {
private NotificationDto notificationDto;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.fullcar.carpool.infra.event;

import com.fullcar.carpool.domain.form.event.FormStateChangedEvent;
import com.fullcar.carpool.domain.service.NotificationService;
import com.fullcar.carpool.infra.dto.NotificationDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
@EnableAsync
public class NotificationEventListener {
private final NotificationService notificationService;

@Async
@EventListener
public void sendNotification(FormStateChangedEvent formStateChangedEvent) {
notificationService.sendNotification(formStateChangedEvent.getNotificationDto());
log.info("Notification send");
}
}

0 comments on commit 9ebd88e

Please sign in to comment.