Skip to content

[DPMBE-117] notification 테이블을 상속 구조로 리팩터링 한다 #198

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

Merged
merged 4 commits into from
Jul 15, 2023
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 @@ -14,9 +14,9 @@ abstract class BaseTimeEntity {

@CreatedDate
@Column(updatable = false)
var createdAt: LocalDateTime = LocalDateTime.now()
open var createdAt: LocalDateTime = LocalDateTime.now()

@LastModifiedDate
@Column(updatable = true)
var updatedAt: LocalDateTime = LocalDateTime.now()
open var updatedAt: LocalDateTime = LocalDateTime.now()
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class PromiseImage(

@PostPersist
fun createImageEvent() {
Events.raise(PromiseImageRegisterEvent(userId, promiseId))
Events.raise(PromiseImageRegisterEvent(userId, promiseId, imageKey))
}

fun validateOwnership(userId: Long) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.depromeet.whatnow.domains.notification.domain

import javax.persistence.DiscriminatorValue
import javax.persistence.Entity

@Entity
@DiscriminatorValue("END_SHARING")
class EndSharingNotification(
var promiseId: Long,

override var targetUserId: Long,
) : Notification(targetUserId)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.depromeet.whatnow.domains.notification.domain

import com.depromeet.whatnow.domains.promiseuser.domain.PromiseUserType
import javax.persistence.DiscriminatorValue
import javax.persistence.Entity
import javax.persistence.EnumType
import javax.persistence.Enumerated

@Entity
@DiscriminatorValue("IMAGE")
class ImageNotification(
@Enumerated(EnumType.STRING)
var senderUserPromiseUserType: PromiseUserType,

var senderUserId: Long,

var promiseId: Long,

var imageKey: String,

override var targetUserId: Long,
) : Notification(targetUserId)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.depromeet.whatnow.domains.notification.domain

import com.depromeet.whatnow.domains.interaction.domain.InteractionType
import javax.persistence.DiscriminatorValue
import javax.persistence.Entity

@Entity
@DiscriminatorValue("INTERACTION_ATTAINMENT")
class InteractionAttainmentNotification(
var promiseId: Long,

var senderUserId: Long,

var interactionType: InteractionType,

override var targetUserId: Long,
) : Notification(targetUserId)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.depromeet.whatnow.domains.notification.domain

import com.depromeet.whatnow.domains.interaction.domain.InteractionType
import javax.persistence.DiscriminatorValue
import javax.persistence.Entity

@Entity
@DiscriminatorValue("INTERACTION")
class InteractionNotification(
var promiseId: Long,

var senderUserId: Long,

var interactionType: InteractionType,

override var targetUserId: Long,
) : Notification(targetUserId)
Original file line number Diff line number Diff line change
@@ -1,111 +1,26 @@
package com.depromeet.whatnow.domains.notification.domain

import com.depromeet.whatnow.common.BaseTimeEntity
import com.depromeet.whatnow.domains.interaction.domain.InteractionType
import com.depromeet.whatnow.domains.promiseuser.domain.PromiseUserType
import javax.persistence.Column
import javax.persistence.ElementCollection
import javax.persistence.DiscriminatorColumn
import javax.persistence.DiscriminatorType
import javax.persistence.Entity
import javax.persistence.EnumType
import javax.persistence.Enumerated
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.Inheritance
import javax.persistence.InheritanceType
import javax.persistence.Table

@Entity
@Table(name = "tbl_notification")
class Notification(
@Enumerated(EnumType.STRING)
var notificationType: NotificationType,

@Enumerated(EnumType.STRING)
var interactionType: InteractionType?,

@Enumerated(EnumType.STRING)
var promiseUserType: PromiseUserType?,

var userId: Long?,

@ElementCollection
var targetUserIds: Set<Long>,

var targetId: Long?,
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "notification_type", discriminatorType = DiscriminatorType.STRING)
abstract class Notification(
open val targetUserId: Long,

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "notification_id")
val id: Long? = null,
) : BaseTimeEntity() {
companion object {
fun createForImage(userId: Long, targetUserId: Set<Long>, promiseImageId: Long): Notification {
return Notification(
notificationType = NotificationType.IMAGE,
userId = userId,
targetUserIds = targetUserId,
targetId = promiseImageId,
interactionType = null,
promiseUserType = null,
)
}

fun createForStartSharing(targetUserIds: Set<Long>, promiseId: Long): Notification {
return Notification(
notificationType = NotificationType.START_SHARING,
userId = null,
targetUserIds = targetUserIds,
targetId = promiseId,
interactionType = null,
promiseUserType = null,
)
}

fun createForEndSharing(targetUserIds: Set<Long>, promiseId: Long): Notification {
return Notification(
notificationType = NotificationType.END_SHARING,
userId = null,
targetUserIds = targetUserIds,
targetId = promiseId,
interactionType = null,
promiseUserType = null,
)
}

fun createForTimeOver(targetUserIds: Set<Long>, promiseId: Long): Notification {
return Notification(
notificationType = NotificationType.TIMEOVER,
userId = null,
targetUserIds = targetUserIds,
targetId = promiseId,
interactionType = null,
promiseUserType = null,
)
}

fun createForInteraction(userId: Long, targetUserId: Long, interactionType: InteractionType): Notification {
return Notification(
notificationType = NotificationType.INTERACTION,
userId = userId,
targetUserIds = mutableSetOf(targetUserId),
targetId = null,
interactionType = interactionType,
promiseUserType = null,
)
}

fun createForInteractionAttainment(
userId: Long,
senderUserIds: Set<Long>,
interactionType: InteractionType,
): Notification {
return Notification(
notificationType = NotificationType.INTERACTION_ATTAINMENT,
userId = userId,
targetUserIds = senderUserIds,
targetId = null,
interactionType = interactionType,
promiseUserType = null,
)
}
}
}
open val id: Long? = null,
) : BaseTimeEntity()
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.depromeet.whatnow.domains.notification.domain

import javax.persistence.DiscriminatorValue
import javax.persistence.Entity

@Entity
@DiscriminatorValue("START_SHARING")
class StartSharingNotification(
var promiseId: Long,

override var targetUserId: Long,
) : Notification(targetUserId)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.depromeet.whatnow.domains.notification.domain

import com.depromeet.whatnow.domains.promiseuser.domain.PromiseUserType
import javax.persistence.DiscriminatorValue
import javax.persistence.Entity

@Entity
@DiscriminatorValue("TIMEOVER")
class TimeOverNotification(
var promiseId: Long,

var promiseUserType: PromiseUserType,

override var targetUserId: Long,
) : Notification(targetUserId)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package com.depromeet.whatnow.domains.notification.service

import com.depromeet.whatnow.domains.interaction.domain.InteractionType
import com.depromeet.whatnow.domains.notification.adapter.NotificationAdapter
import com.depromeet.whatnow.domains.notification.domain.Notification
import com.depromeet.whatnow.domains.notification.domain.EndSharingNotification
import com.depromeet.whatnow.domains.notification.domain.ImageNotification
import com.depromeet.whatnow.domains.notification.domain.InteractionAttainmentNotification
import com.depromeet.whatnow.domains.notification.domain.InteractionNotification
import com.depromeet.whatnow.domains.notification.domain.StartSharingNotification
import com.depromeet.whatnow.domains.notification.domain.TimeOverNotification
import com.depromeet.whatnow.domains.promiseuser.domain.PromiseUserType
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

Expand All @@ -11,32 +17,32 @@ class NotificationDomainService(
val notificationAdapter: NotificationAdapter,
) {
@Transactional
fun saveForImage(userId: Long, targetUserId: Set<Long>, promiseImageId: Long) {
notificationAdapter.save(Notification.createForImage(userId, targetUserId, promiseImageId))
fun saveForImage(senderPromiseUserType: PromiseUserType, userId: Long, targetUserId: Long, promiseId: Long, imageKey: String) {
notificationAdapter.save(ImageNotification(senderPromiseUserType, userId, promiseId, imageKey, targetUserId))
}

@Transactional
fun saveForStartSharing(targetUserIds: Set<Long>, promiseId: Long) {
notificationAdapter.save(Notification.createForStartSharing(targetUserIds, promiseId))
fun saveForStartSharing(promiseId: Long, targetUserId: Long) {
notificationAdapter.save(StartSharingNotification(promiseId, targetUserId))
}

@Transactional
fun saveForEndSharing(targetUserIds: Set<Long>, promiseId: Long) {
notificationAdapter.save(Notification.createForEndSharing(targetUserIds, promiseId))
fun saveForEndSharing(promiseId: Long, targetUserId: Long) {
notificationAdapter.save(EndSharingNotification(promiseId, targetUserId))
}

@Transactional
fun saveForTimeOver(targetUserIds: Set<Long>, promiseId: Long) {
notificationAdapter.save(Notification.createForTimeOver(targetUserIds, promiseId))
fun saveForTimeOver(promiseId: Long, promiseUserType: PromiseUserType, targetUserId: Long) {
notificationAdapter.save(TimeOverNotification(promiseId, promiseUserType, targetUserId))
}

@Transactional
fun saveForInteraction(userId: Long, targetUserId: Long, interactionType: InteractionType) {
notificationAdapter.save(Notification.createForInteraction(userId, targetUserId, interactionType))
fun saveForInteraction(promiseId: Long, senderUserId: Long, interactionType: InteractionType, targetUserId: Long) {
notificationAdapter.save(InteractionNotification(promiseId, senderUserId, interactionType, targetUserId))
}

@Transactional
fun saveForInteractionAttainment(userId: Long, senderUserIds: Set<Long>, interactionType: InteractionType) {
notificationAdapter.save(Notification.createForInteractionAttainment(userId, senderUserIds, interactionType))
fun saveForInteractionAttainment(promiseId: Long, senderUserId: Long, interactionType: InteractionType, targetUserId: Long) {
notificationAdapter.save(InteractionAttainmentNotification(promiseId, senderUserId, interactionType, targetUserId))
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import com.depromeet.whatnow.common.aop.event.DomainEvent
class PromiseImageRegisterEvent(
val userId: Long,
val promiseId: Long,
val imageKey: String,
) : DomainEvent()
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ImageRegisterEventHandler(
fun handlePromiseImageRegisterEvent(promiseImageRegisterEvent: PromiseImageRegisterEvent) {
val userId: Long = promiseImageRegisterEvent.userId
val promiseId: Long = promiseImageRegisterEvent.promiseId
val imageKey: String = promiseImageRegisterEvent.imageKey

// 사진을 보낸 유저가 Late인지 Wait인지 확인하기 위한 PromiseUser 조회
val promiseUser = promiseUserAdapter.findByPromiseIdAndUserId(promiseId, userId)
Expand All @@ -42,6 +43,7 @@ class ImageRegisterEventHandler(
val data: MutableMap<String, String> = mutableMapOf()
data["notificationType"] = NotificationType.IMAGE.name
data["promiseId"] = promiseId.toString()
data["imageKey"] = imageKey

// 앱 알람 허용한 유저에게 알람 보내기
when (promiseUser.promiseUserType) {
Expand All @@ -64,7 +66,15 @@ class ImageRegisterEventHandler(
}

// notification 저장
val targetUserIds = usersExcludingSelf.map { user -> user.id!! }.toSet()
notificationDomainService.saveForImage(userId, targetUserIds, promiseId)
usersExcludingSelf
.forEach { targetUser ->
notificationDomainService.saveForImage(
promiseUser.promiseUserType,
userId,
targetUser.id!!,
promiseId,
imageKey,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ class InteractionFixedEventHandler(

val user = userAdapter.queryUser(userId)

val senderUserIds =
interactionHistoryDomainService.queryAllByInteractionType(userId, promiseId, interactionType)
.map { interactionHistory -> interactionHistory.targetUserId }
.distinct()
.toSet()

val data: MutableMap<String, String> = mutableMapOf()
data["notificationType"] = NotificationType.INTERACTION_ATTAINMENT.name
data["promiseId"] = event.promiseId.toString()
Expand All @@ -51,6 +45,11 @@ class InteractionFixedEventHandler(
)
}

notificationDomainService.saveForInteractionAttainment(userId, senderUserIds, interactionType)
interactionHistoryDomainService.queryAllByInteractionType(userId, promiseId, interactionType)
.map { interactionHistory -> interactionHistory.targetUserId }
.distinct()
.forEach { targetUserId ->
notificationDomainService.saveForInteractionAttainment(promiseId, userId, interactionType, targetUserId)
}
}
}
Loading