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

[Fix/#417] 멤버가 구독 전송 시간을 변경한 상태로 추가 구독하여도 기본 구독 전송 시간으로 생성되는 문제 해결 #418

Merged
merged 5 commits into from
Sep 26, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class SubscriptionDao(
dslContext.insertInto(SUBSCRIPTION)
.set(SUBSCRIPTION.MEMBER_ID, command.memberId)
.set(SUBSCRIPTION.TARGET_WORKBOOK_ID, command.workbookId)
.set(SUBSCRIPTION.SEND_DAY, command.sendDay)
.set(SUBSCRIPTION.SEND_TIME, command.sendTime)

fun reSubscribeWorkbookSubscription(command: InsertWorkbookSubscriptionCommand) {
reSubscribeWorkBookSubscriptionCommand(command)
Expand All @@ -53,6 +55,8 @@ class SubscriptionDao(
.set(SUBSCRIPTION.DELETED_AT, null as LocalDateTime?)
.set(SUBSCRIPTION.UNSUBS_OPINION, null as String?)
.set(SUBSCRIPTION.MODIFIED_AT, LocalDateTime.now())
.set(SUBSCRIPTION.SEND_DAY, command.sendDay)
.set(SUBSCRIPTION.SEND_TIME, command.sendTime)
.where(SUBSCRIPTION.MEMBER_ID.eq(command.memberId))
.and(SUBSCRIPTION.TARGET_WORKBOOK_ID.eq(command.workbookId))

Expand Down Expand Up @@ -282,4 +286,20 @@ class SubscriptionDao(
.from(SUBSCRIPTION)
.where(SUBSCRIPTION.MEMBER_ID.eq(query.memberId))
.and(SUBSCRIPTION.TARGET_WORKBOOK_ID.eq(query.workbookId))

fun selectSubscriptionSendStatus(query: SelectSubscriptionSendStatusQuery): List<SubscriptionSendStatus> {
return selectSubscriptionSendStatusQuery(query)
.fetchInto(SubscriptionSendStatus::class.java)
}

fun selectSubscriptionSendStatusQuery(query: SelectSubscriptionSendStatusQuery) =
dslContext.select(
SUBSCRIPTION.MEMBER_ID,
SUBSCRIPTION.TARGET_WORKBOOK_ID,
SUBSCRIPTION.SEND_TIME,
SUBSCRIPTION.SEND_DAY
)
.from(SUBSCRIPTION)
.where(SUBSCRIPTION.MEMBER_ID.eq(query.memberId))
.and(SUBSCRIPTION.DELETED_AT.isNull)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.few.api.repo.dao.subscription.command

import com.few.data.common.code.DayCode
import java.time.LocalTime

data class InsertWorkbookSubscriptionCommand(
val workbookId: Long,
val memberId: Long,
val sendDay: String? = DayCode.MON_TUE_WED_THU_FRI.code,
val sendTime: LocalTime? = LocalTime.of(8, 0),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.few.api.repo.dao.subscription.query

data class SelectSubscriptionSendStatusQuery(
val memberId: Long,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.few.api.repo.dao.subscription.record

import java.time.LocalTime

data class SubscriptionSendStatus(
val memberId: Long,
val workbookId: Long,
val sendDay: String,
val sendTime: LocalTime,
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.few.api.domain.subscription.usecase.model.WorkbookSubscriptionStatus
import com.few.api.exception.common.NotFoundException
import com.few.api.exception.subscribe.SubscribeIllegalArgumentException
import com.few.api.repo.dao.subscription.query.CountWorkbookMappedArticlesQuery
import com.few.api.repo.dao.subscription.query.SelectSubscriptionSendStatusQuery
import org.springframework.context.ApplicationEventPublisher
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
Expand All @@ -28,10 +29,28 @@ class SubscribeWorkbookUseCase(
fun execute(useCaseIn: SubscribeWorkbookUseCaseIn) {
val subTargetWorkbookId = useCaseIn.workbookId
val memberId = useCaseIn.memberId
val command = InsertWorkbookSubscriptionCommand(
memberId = memberId,
workbookId = subTargetWorkbookId
)

val command = subscriptionDao.selectSubscriptionSendStatus(
SelectSubscriptionSendStatusQuery(
memberId = memberId
)
).takeIf {
it.isNotEmpty()
}?.let {
/** 현재 구독 중인 정보가 있다면 해당 정보를 통해 구독 정보를 생성 */
InsertWorkbookSubscriptionCommand(
memberId = memberId,
workbookId = subTargetWorkbookId,
sendDay = it[0].sendDay,
sendTime = it[0].sendTime
)
} ?: run {
/** 현재 구독 중인 정보가 없다면 기본 정보로 구독 정보를 생성 */
InsertWorkbookSubscriptionCommand(
memberId = memberId,
workbookId = subTargetWorkbookId
)
}
Comment on lines +33 to +53
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

기존의 구독 정보를 조회한 후에 새로운 구독 정보를 생성합니다.


val workbookSubscriptionHistory = subscriptionDao.selectTopWorkbookSubscriptionStatus(
SelectAllWorkbookSubscriptionStatusNotConsiderDeletedAtQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.few.api.domain.subscription.usecase
import com.few.api.domain.subscription.event.dto.WorkbookSubscriptionEvent
import com.few.api.domain.subscription.usecase.dto.SubscribeWorkbookUseCaseIn
import com.few.api.repo.dao.subscription.SubscriptionDao
import com.few.api.repo.dao.subscription.record.SubscriptionSendStatus
import com.few.api.repo.dao.subscription.record.WorkbookSubscriptionStatus
import com.few.data.common.code.DayCode
import io.github.oshai.kotlinlogging.KotlinLogging
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.BehaviorSpec
Expand All @@ -13,6 +15,7 @@ import io.mockk.verify
import io.mockk.just
import io.mockk.Runs
import org.springframework.context.ApplicationEventPublisher
import java.time.LocalTime

class SubscribeWorkbookUseCaseTest : BehaviorSpec({
val log = KotlinLogging.logger {}
Expand All @@ -33,6 +36,15 @@ class SubscribeWorkbookUseCaseTest : BehaviorSpec({
val useCaseIn = SubscribeWorkbookUseCaseIn(workbookId = workbookId, memberId = memberId)

`when`("멤버의 구독 히스토리가 없는 경우") {
every { subscriptionDao.selectSubscriptionSendStatus(any()) } returns listOf(
SubscriptionSendStatus(
workbookId = workbookId,
memberId = memberId,
sendDay = DayCode.MON_TUE_WED_THU_FRI.code,
sendTime = LocalTime.of(8, 0)
)
)

every { subscriptionDao.selectTopWorkbookSubscriptionStatus(any()) } returns null

every { subscriptionDao.insertWorkbookSubscription(any()) } just Runs
Expand All @@ -45,6 +57,7 @@ class SubscribeWorkbookUseCaseTest : BehaviorSpec({
then("구독한다") {
useCase.execute(useCaseIn)

verify(exactly = 1) { subscriptionDao.selectTopWorkbookSubscriptionStatus(any()) }
verify(exactly = 1) { subscriptionDao.selectTopWorkbookSubscriptionStatus(any()) }
verify(exactly = 1) { subscriptionDao.insertWorkbookSubscription(any()) }
verify(exactly = 0) { subscriptionDao.countWorkbookMappedArticles(any()) }
Expand All @@ -54,6 +67,15 @@ class SubscribeWorkbookUseCaseTest : BehaviorSpec({
}

`when`("이미 구독한 히스토리가 있고 구독이 취소된 경우") {
every { subscriptionDao.selectSubscriptionSendStatus(any()) } returns listOf(
SubscriptionSendStatus(
workbookId = workbookId,
memberId = memberId,
sendDay = DayCode.MON_TUE_WED_THU_FRI.code,
sendTime = LocalTime.of(8, 0)
)
)

val day = 2
every { subscriptionDao.selectTopWorkbookSubscriptionStatus(any()) } returns WorkbookSubscriptionStatus(
workbookId = workbookId,
Expand All @@ -74,6 +96,7 @@ class SubscribeWorkbookUseCaseTest : BehaviorSpec({
then("재구독한다") {
useCase.execute(useCaseIn)

verify(exactly = 1) { subscriptionDao.selectTopWorkbookSubscriptionStatus(any()) }
verify(exactly = 1) { subscriptionDao.selectTopWorkbookSubscriptionStatus(any()) }
verify(exactly = 0) { subscriptionDao.insertWorkbookSubscription(any()) }
verify(exactly = 1) { subscriptionDao.countWorkbookMappedArticles(any()) }
Expand All @@ -83,6 +106,15 @@ class SubscribeWorkbookUseCaseTest : BehaviorSpec({
}

`when`("이미 구독한 히스토리가 있고 구독을 완료한 경우") {
every { subscriptionDao.selectSubscriptionSendStatus(any()) } returns listOf(
SubscriptionSendStatus(
workbookId = workbookId,
memberId = memberId,
sendDay = DayCode.MON_TUE_WED_THU_FRI.code,
sendTime = LocalTime.of(8, 0)
)
)

val day = 3
every { subscriptionDao.selectTopWorkbookSubscriptionStatus(any()) } returns WorkbookSubscriptionStatus(
workbookId = workbookId,
Expand All @@ -103,6 +135,7 @@ class SubscribeWorkbookUseCaseTest : BehaviorSpec({
then("예외가 발생한다") {
shouldThrow<Exception> { useCase.execute(useCaseIn) }

verify(exactly = 1) { subscriptionDao.selectTopWorkbookSubscriptionStatus(any()) }
verify(exactly = 1) { subscriptionDao.selectTopWorkbookSubscriptionStatus(any()) }
verify(exactly = 0) { subscriptionDao.insertWorkbookSubscription(any()) }
verify(exactly = 1) { subscriptionDao.countWorkbookMappedArticles(any()) }
Expand All @@ -112,12 +145,22 @@ class SubscribeWorkbookUseCaseTest : BehaviorSpec({
}

`when`("구독 중인 경우") {
every { subscriptionDao.selectSubscriptionSendStatus(any()) } returns listOf(
SubscriptionSendStatus(
workbookId = workbookId,
memberId = memberId,
sendDay = DayCode.MON_TUE_WED_THU_FRI.code,
sendTime = LocalTime.of(8, 0)
)
)

val day = 2
every { subscriptionDao.selectTopWorkbookSubscriptionStatus(any()) } returns WorkbookSubscriptionStatus(workbookId = workbookId, isActiveSub = true, day)

then("예외가 발생한다") {
shouldThrow<Exception> { useCase.execute(useCaseIn) }

verify(exactly = 1) { subscriptionDao.selectTopWorkbookSubscriptionStatus(any()) }
verify(exactly = 1) { subscriptionDao.selectTopWorkbookSubscriptionStatus(any()) }
verify(exactly = 0) { subscriptionDao.insertWorkbookSubscription(any()) }
verify(exactly = 0) { subscriptionDao.countWorkbookMappedArticles(any()) }
Expand Down
Loading