Skip to content

Commit

Permalink
Merge pull request #350 from GSM-MSG/349-refactor/message-util-with-s…
Browse files Browse the repository at this point in the history
…pring-event

🔀 :: 349 refactor/message util with spring event
  • Loading branch information
KimTaeO authored May 16, 2024
2 parents 51d3637 + 2a3fc33 commit 326ec9d
Show file tree
Hide file tree
Showing 20 changed files with 266 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import com.msg.gcms.domain.club.domain.entity.Club
import com.msg.gcms.domain.club.domain.repository.ClubRepository
import com.msg.gcms.domain.club.enums.ClubStatus
import com.msg.gcms.domain.club.exception.ClubNotFoundException
import com.msg.gcms.global.event.SendMessageEvent
import com.msg.gcms.global.fcm.enums.SendType
import com.msg.gcms.global.util.MessageSendUtil
import org.springframework.context.ApplicationEventPublisher
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.lang.Exception

@Service
@Transactional(rollbackFor = [Exception::class])
class AcceptClubServiceImpl(
private val clubRepository: ClubRepository,
private val messageSendUtil: MessageSendUtil
private val applicationEventPublisher: ApplicationEventPublisher
) : AcceptClubService {
override fun execute(clubId: Long) {
val club = clubRepository.findByIdOrNull(clubId) ?: throw ClubNotFoundException()
Expand All @@ -39,6 +39,14 @@ class AcceptClubServiceImpl(
)

clubRepository.save(newClub)
messageSendUtil.send(club.user, "동아리 개설 승인", "동아리 개설 신청이 승인되었어요.", SendType.CLUB)

applicationEventPublisher.publishEvent(
SendMessageEvent(
user = club.user,
title = "동아리 개설 승인",
content = "동아리 개설 신청이 승인되었어요.",
type = SendType.CLUB
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import com.msg.gcms.domain.admin.service.RejectClubService
import com.msg.gcms.domain.club.domain.repository.ClubRepository
import com.msg.gcms.domain.club.enums.ClubStatus
import com.msg.gcms.domain.club.exception.ClubNotFoundException
import com.msg.gcms.global.event.SendMessageEvent
import com.msg.gcms.global.fcm.enums.SendType
import com.msg.gcms.global.util.MessageSendUtil
import org.springframework.context.ApplicationEventPublisher
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -15,7 +16,7 @@ import org.springframework.transaction.annotation.Transactional
@Transactional(rollbackFor = [Exception::class])
class RejectClubServiceImpl(
private val clubRepository: ClubRepository,
private val messageSendUtil: MessageSendUtil
private val applicationEventPublisher: ApplicationEventPublisher
) : RejectClubService {
override fun execute(clubId: Long) {
val club = clubRepository.findByIdOrNull(clubId) ?: throw ClubNotFoundException()
Expand All @@ -25,6 +26,14 @@ class RejectClubServiceImpl(
}

clubRepository.delete(club)
messageSendUtil.send(club.user, "동아리 개설 거절", "동아리 개설 신청이 거절되었어요.", SendType.CLUB)

applicationEventPublisher.publishEvent(
SendMessageEvent(
user = club.user,
title = "동아리 개설 거절",
content = "동아리 개설 신청이 거절되었어요.",
type = SendType.CLUB
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import com.msg.gcms.domain.user.domain.entity.User
import com.msg.gcms.domain.user.domain.repository.UserRepository
import com.msg.gcms.domain.user.exception.UserNotFoundException
import com.msg.gcms.global.annotation.ServiceWithTransaction
import com.msg.gcms.global.event.SendMessageEvent
import com.msg.gcms.global.fcm.enums.SendType
import com.msg.gcms.global.util.MessageSendUtil
import com.msg.gcms.global.util.UserUtil
import org.springframework.context.ApplicationEventPublisher
import org.springframework.data.repository.findByIdOrNull
import java.util.*

Expand All @@ -28,7 +29,7 @@ class AcceptApplicantServiceImpl(
private val userRepository: UserRepository,
private val applicantConverter: ApplicantConverter,
private val userUtil: UserUtil,
private val messageSendUtil: MessageSendUtil
private val applicationEventPublisher: ApplicationEventPublisher
) : AcceptApplicantService {

override fun execute(clubId: Long, acceptDto: AcceptDto) {
Expand All @@ -52,6 +53,13 @@ class AcceptApplicantServiceImpl(
val clubMember = applicantConverter.toEntity(clubInfo, applicantUser)
clubMemberRepository.save(clubMember)

messageSendUtil.send(applicantUser, "동아리 신청 수락", "${clubInfo.name}에 수락되셨습니다.", SendType.CLUB)
applicationEventPublisher.publishEvent(
SendMessageEvent(
user = applicantUser,
title = "동아리 신청 수락",
content = "${clubInfo.name}에 수락되셨습니다.",
type = SendType.CLUB
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import com.msg.gcms.domain.applicant.service.CancelApplicationService
import com.msg.gcms.domain.club.domain.repository.ClubRepository
import com.msg.gcms.domain.club.exception.ClubNotFoundException
import com.msg.gcms.global.annotation.ServiceWithTransaction
import com.msg.gcms.global.event.SendMessageEvent
import com.msg.gcms.global.fcm.enums.SendType
import com.msg.gcms.global.util.MessageSendUtil
import com.msg.gcms.global.util.UserUtil
import org.springframework.context.ApplicationEventPublisher
import org.springframework.data.repository.findByIdOrNull

@ServiceWithTransaction
class CancelApplicationServiceImpl(
private val clubRepository: ClubRepository,
private val applicantRepository: ApplicantRepository,
private val userUtil: UserUtil,
private val messageSendUtil: MessageSendUtil,
private val applicationEventPublisher: ApplicationEventPublisher
) : CancelApplicationService {
override fun execute(clubId: Long) {
val club = clubRepository.findByIdOrNull(clubId)
Expand All @@ -26,6 +27,14 @@ class CancelApplicationServiceImpl(
.find { it.user == user }
?: throw NotApplicantException()
applicantRepository.delete(applicant)
messageSendUtil.send(club.user, "동아리 신청 취소", "${user.nickname}님이 ${club.name}에 신청 취소했습니다.", SendType.CLUB)

applicationEventPublisher.publishEvent(
SendMessageEvent(
user = club.user,
title = "동아리 신청 취소",
content = "${user.nickname}님이 ${club.name}에 신청 취소했습니다.",
type = SendType.CLUB
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import com.msg.gcms.domain.club.exception.ClubNotFoundException
import com.msg.gcms.domain.club.exception.ClubNotOpeningException
import com.msg.gcms.domain.clubMember.domain.repository.ClubMemberRepository
import com.msg.gcms.global.annotation.ServiceWithTransaction
import com.msg.gcms.global.event.SendMessageEvent
import com.msg.gcms.global.fcm.enums.SendType
import com.msg.gcms.global.util.MessageSendUtil
import com.msg.gcms.global.util.UserUtil
import org.springframework.context.ApplicationEventPublisher
import org.springframework.data.repository.findByIdOrNull

@ServiceWithTransaction
Expand All @@ -25,7 +26,7 @@ class ClubApplyServiceImpl(
private val clubMemberRepository: ClubMemberRepository,
private val applicantSaveUtil: ApplicantSaveUtil,
private val applicantRepository: ApplicantRepository,
private val messageSendUtil: MessageSendUtil
private val applicationEventPublisher: ApplicationEventPublisher
) : ClubApplyService {
override fun execute(clubId: Long): ClubApplyDto {
val club = clubRepository.findByIdOrNull(clubId)
Expand All @@ -46,7 +47,16 @@ class ClubApplyServiceImpl(
throw DuplicateClubTypeApplicantException()

val applicant = applicantSaveUtil.saveApplicant(club, user)
messageSendUtil.send(club.user, "동아리 신청 요청", "${user.nickname}님이 ${club.name}에 신청했습니다.", SendType.CLUB)

applicationEventPublisher.publishEvent(
SendMessageEvent(
user = club.user,
title = "동아리 신청 요청",
content = "${user.nickname}님이 ${club.name}에 신청했습니다.",
type = SendType.CLUB
)
)

return ClubApplyDto(applicant)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import com.msg.gcms.domain.user.domain.entity.User
import com.msg.gcms.domain.user.domain.repository.UserRepository
import com.msg.gcms.domain.user.exception.UserNotFoundException
import com.msg.gcms.global.annotation.ServiceWithTransaction
import com.msg.gcms.global.event.SendMessageEvent
import com.msg.gcms.global.fcm.enums.SendType
import com.msg.gcms.global.util.MessageSendUtil
import com.msg.gcms.global.util.UserUtil
import org.springframework.context.ApplicationEventPublisher
import org.springframework.data.repository.findByIdOrNull
import java.util.*

Expand All @@ -25,7 +26,7 @@ class RejectApplicantServiceImpl(
private val userRepository: UserRepository,
private val applicantRepository: ApplicantRepository,
private val userUtil: UserUtil,
private val messageSendUtil: MessageSendUtil
private val applicationEventPublisher: ApplicationEventPublisher
) : RejectApplicantService {
override fun execute(clubId: Long, rejectDto: RejectDto) {
val clubInfo: Club = clubRepository.findByIdOrNull(clubId)
Expand All @@ -44,6 +45,13 @@ class RejectApplicantServiceImpl(

applicantRepository.delete(applicant)

messageSendUtil.send(applicantUser, "동아리 신청 거절", "${clubInfo.name}에 거절되셨습니다.", SendType.CLUB)
applicationEventPublisher.publishEvent(
SendMessageEvent(
user = applicantUser,
title = "동아리 신청 거절",
content = "${clubInfo.name}에 거절되셨습니다.",
type = SendType.CLUB
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import com.msg.gcms.domain.clubMember.util.UpdateClubHeadUtil
import com.msg.gcms.domain.user.domain.repository.UserRepository
import com.msg.gcms.domain.user.exception.UserNotFoundException
import com.msg.gcms.global.annotation.ServiceWithTransaction
import com.msg.gcms.global.event.SendMessageEvent
import com.msg.gcms.global.fcm.enums.SendType
import com.msg.gcms.global.util.MessageSendUtil
import com.msg.gcms.global.util.UserUtil
import org.springframework.context.ApplicationEventPublisher
import org.springframework.data.repository.findByIdOrNull

@ServiceWithTransaction
Expand All @@ -25,7 +26,7 @@ class DelegateHeadServiceImpl(
private val userRepository: UserRepository,
private val updateClubHeadUtil: UpdateClubHeadUtil,
private val userUtil: UserUtil,
private val messageSendUtil: MessageSendUtil
private val applicationEventPublisher: ApplicationEventPublisher
) : DelegateHeadService {
override fun execute(clubId: Long, delegateHeadDto: DelegateHeadDto): ChangeHeadDto {
val club = (clubRepository.findByIdOrNull(clubId)
Expand All @@ -40,7 +41,16 @@ class DelegateHeadServiceImpl(
.find { it.user.id == delegateHeadDto.uuid }
?: throw NotClubMemberException()
updateClubHeadUtil.updateClubHead(club, clubMember, head)
messageSendUtil.send(clubMember.user, "부장 위임", "${club.name}의 부장으로 위임되셨습니다.", SendType.CLUB)

applicationEventPublisher.publishEvent(
SendMessageEvent(
user = clubMember.user,
title = "부장 위임",
content = "${club.name}의 부장으로 위임되셨습니다.",
type = SendType.CLUB
)
)

return ChangeHeadDto(clubMember.user, head)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ import com.msg.gcms.domain.clubMember.exception.ClubMemberReleaseNotFoundExcepti
import com.msg.gcms.domain.clubMember.presentation.data.dto.ClubMemberExitDto
import com.msg.gcms.domain.clubMember.service.ExitClubMemberService
import com.msg.gcms.global.annotation.ServiceWithTransaction
import com.msg.gcms.global.event.SendMessageEvent
import com.msg.gcms.global.fcm.enums.SendType
import com.msg.gcms.global.util.MessageSendUtil
import com.msg.gcms.global.util.UserUtil
import org.springframework.context.ApplicationEventPublisher
import org.springframework.data.repository.findByIdOrNull

@ServiceWithTransaction
class ExitClubMemberServiceImpl(
private val userUtil: UserUtil,
private val clubRepository: ClubRepository,
private val clubMemberRepository: ClubMemberRepository,
private val messageSendUtil: MessageSendUtil
private val applicationEventPublisher: ApplicationEventPublisher
) : ExitClubMemberService {
override fun execute(clubMemberExitDto: ClubMemberExitDto) {
val user = userUtil.fetchCurrentUser()
Expand All @@ -36,7 +37,15 @@ class ExitClubMemberServiceImpl(
}
val memberRelease: ClubMember = getClubMemberToRelease(club, clubMemberExitDto.uuid)
clubMemberRepository.delete(memberRelease)
messageSendUtil.send(memberRelease.user, "동아리 방출", "${club.name}에서 방출당했습니다.", SendType.CLUB)

applicationEventPublisher.publishEvent(
SendMessageEvent(
user = memberRelease.user,
title = "동아리 방출",
content = "${club.name}에서 방출당했습니다.",
type = SendType.CLUB
)
)
}

private fun getClubMemberToRelease(club: Club, uuid: String): ClubMember =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ class DeviceToken (
@JoinColumn(name = "user_id", columnDefinition = "BINARY(16)")
val user: User,

val token: String,
val token: String?,
)
11 changes: 11 additions & 0 deletions src/main/kotlin/com/msg/gcms/global/event/SendMessageEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.msg.gcms.global.event

import com.msg.gcms.domain.user.domain.entity.User
import com.msg.gcms.global.fcm.enums.SendType

data class SendMessageEvent(
val user: User,
val title: String,
val content: String,
val type: SendType
)
40 changes: 40 additions & 0 deletions src/main/kotlin/com/msg/gcms/global/handler/MessageEventHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.msg.gcms.global.handler

import com.google.firebase.messaging.*
import com.msg.gcms.domain.user.domain.repository.DeviceTokenRepository
import com.msg.gcms.global.event.SendMessageEvent
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Component
import org.springframework.transaction.event.TransactionPhase
import org.springframework.transaction.event.TransactionalEventListener

@Component
class MessageEventHandler(
private val deviceTokenRepository: DeviceTokenRepository
) {
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
fun send(sendMessageEvent: SendMessageEvent) {
val deviceToken = deviceTokenRepository.findByIdOrNull(sendMessageEvent.user.id)
?: return

if(!deviceToken.token.isNullOrBlank()) {
val message = Message.builder()
.setToken(deviceToken.token)
.putData("type", sendMessageEvent.type.name)
.setNotification(
Notification.builder()
.setTitle(sendMessageEvent.title)
.setBody(sendMessageEvent.content)
.build()
)
.setApnsConfig(
ApnsConfig.builder()
.setAps(Aps.builder().setSound("default").build())
.build()
)
.build()

FirebaseMessaging.getInstance().sendAsync(message)
}
}
}
10 changes: 0 additions & 10 deletions src/main/kotlin/com/msg/gcms/global/util/MessageSendUtil.kt

This file was deleted.

Loading

0 comments on commit 326ec9d

Please sign in to comment.