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

Refactor #123: PushAlarmAspect에서 AOP @Around -> @AfterReturning #124

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -4,9 +4,9 @@
import b1nd.dodam.domain.rds.member.entity.Student;
import b1nd.dodam.domain.rds.member.entity.Teacher;
import b1nd.dodam.domain.rds.member.enumeration.ActiveStatus;
import b1nd.dodam.domain.rds.member.enumeration.MemberRole;
import b1nd.dodam.domain.rds.member.event.StudentRegisteredEvent;
import b1nd.dodam.domain.rds.member.exception.*;
import b1nd.dodam.domain.rds.member.exception.BroadcastClubMemberDuplicateException;
import b1nd.dodam.domain.rds.member.exception.MemberDuplicateException;
import b1nd.dodam.domain.rds.member.repository.BroadcastClubMemberRepository;
import b1nd.dodam.domain.rds.member.repository.MemberRepository;
import b1nd.dodam.domain.rds.member.repository.StudentRepository;
Expand Down
dongchandev marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void throwExceptionWhenStudentIsNotApplicant(NightStudy nightStudy, Stud
}
}

@PushAlarmEvent(target = "심야자습", status = ApprovalStatus.PENDING)
@PushAlarmEvent(target = "심야자습", status = ApprovalStatus.ALLOWED)
public Response allow(Long id) {
modifyStatus(id, ApprovalStatus.ALLOWED, null);
return Response.noContent("심야자습 승인 성공");
Expand All @@ -75,7 +75,7 @@ public Response reject(Long id, Optional<RejectNightStudyReq> req) {
return Response.noContent("심야자습 거절 성공");
}

@PushAlarmEvent(target = "심야자습", status = ApprovalStatus.PENDING)
@PushAlarmEvent(target = "심야자습", status = ApprovalStatus.REJECTED)
public Response revert(Long id) {
modifyStatus(id, ApprovalStatus.PENDING, null);
return Response.noContent("심야자습 대기 성공");
Expand Down
dongchandev marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void throwExceptionWhenStudentIsNotApplicant(OutSleeping o) {
}
}

@PushAlarmEvent(target = "외박", status = ApprovalStatus.PENDING)
@PushAlarmEvent(target = "외박", status = ApprovalStatus.ALLOWED)
public Response allow(Long id) {
modifyStatus(id, ApprovalStatus.ALLOWED, null);
return Response.noContent("외박 승인 성공");
Expand All @@ -69,7 +69,7 @@ public Response reject(Long id, Optional<RejectOutSleepingReq> req) {
return Response.noContent("외박 거절 성공");
}

@PushAlarmEvent(target = "외박", status = ApprovalStatus.PENDING)
@PushAlarmEvent(target = "외박", status = ApprovalStatus.REJECTED)
public Response revert(Long id) {
modifyStatus(id, ApprovalStatus.PENDING, null);
return Response.noContent("외박 대기 성공");
Expand Down
Copy link
Member

Choose a reason for hiding this comment

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

getRejectReasonFromArgs 에서 String으로 강제 캐스팅 하는데 Object타입을 String이 아닌 와일드카드로 하신 이유가 있나요?

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.ApplicationEventPublisher;
Expand All @@ -26,44 +27,27 @@ public class PushAlarmAspect {
private final OutGoingService outGoingService;
private final OutSleepingService outSleepingService;

@Around("@annotation(pushAlarmEvent)")
public Object handlePushAlarmEvent(ProceedingJoinPoint joinPoint, PushAlarmEvent pushAlarmEvent) {

Object result = proceedJoinPointSafely(joinPoint);

Long id = getIdFromArgs(joinPoint.getArgs());
String rejectReason = getRejectReasonFromArgs(joinPoint.getArgs());
@AfterReturning(pointcut = "@annotation(pushAlarmEvent)", returning = "result")
public void handlePushAlarmEvent(Object result, PushAlarmEvent pushAlarmEvent) {
String target = pushAlarmEvent.target();
ApprovalStatus status = pushAlarmEvent.status();

Student student = getStudentByTargetAndId(target, id);

sendPushAlarm(student, target, rejectReason, status);

return result;
}

private Object proceedJoinPointSafely(ProceedingJoinPoint joinPoint) {
try {
return joinPoint.proceed();
} catch (Throwable e) {
throw new InternalServerException();
}
sendPushAlarm(
getStudentByTargetAndId(target, getIdFromArgs(result)),
target,
getRejectReasonFromArgs(result),
pushAlarmEvent.status()
);
}

private Long getIdFromArgs(Object[] args) {
if (args[0] instanceof Long id) {
private Long getIdFromArgs(Object result) {
if (result instanceof Long id)
return id;
} else {
throw new InternalServerException();
}
else throw new InternalServerException();
}

private String getRejectReasonFromArgs(Object[] args) {
if (args.length > 1 && args[1] instanceof Optional<?> optionalArg) {
private String getRejectReasonFromArgs(Object result) {
if (result instanceof Optional<?> optionalArg)
return (String) optionalArg.orElse(null);
}
return null;
else throw new InternalServerException();
}

private Student getStudentByTargetAndId(String target, Long id) {
Expand All @@ -76,8 +60,7 @@ private Student getStudentByTargetAndId(String target, Long id) {
}

private void sendPushAlarm(Student student, String target, String rejectReason, ApprovalStatus status) {
String pushToken = student.getMember().getPushToken();
ApprovalAlarmEvent alarmEvent = ApprovalAlarmUtil.createAlarmEvent(pushToken, target, rejectReason, status);
eventPublisher.publishEvent(alarmEvent);
eventPublisher.publishEvent(ApprovalAlarmUtil.createAlarmEvent(
student.getMember().getPushToken(), target, rejectReason, status));
}
}
dongchandev marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class FCMClient {
public void sendMessage(String pushToken, String title, String body) {
try {
if(!pushToken.isBlank()){
if(pushToken != null && !pushToken.isBlank()){
FirebaseMessaging.getInstance().send(Message.builder()
.setNotification(Notification.builder()
.setTitle(title)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public class Member extends BaseEntity {
@NotNull
private String phone;

@NotNull
@JsonIgnore
@Column(columnDefinition = "TEXT")
private String pushToken;
Expand Down