Skip to content

Commit

Permalink
fix: 봉투 제거시, 장부의 총합 정보에 대한 업데이트 진행
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Jul 17, 2024
1 parent dbf1f42 commit a3e1659
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,8 @@ class EnvelopeFacade(
targetType = CategoryAssignmentType.ENVELOPE
)

DeleteEnvelopeEvent(
envelopeId = envelope.id,
uid = user.uid,
friendId = envelope.friendId
).run { publisher.publishEvent(this) }
DeleteEnvelopeEvent(envelope, user.uid)
.run { publisher.publishEvent(this) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ import com.oksusu.susu.api.friend.application.FriendRelationshipService
import com.oksusu.susu.api.friend.application.FriendService
import com.oksusu.susu.client.common.coroutine.ErrorPublishingCoroutineExceptionHandler
import com.oksusu.susu.common.extension.mdcCoroutineScope
import com.oksusu.susu.domain.envelope.domain.vo.EnvelopeType
import com.oksusu.susu.domain.envelope.infrastructure.LedgerRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.springframework.data.repository.findByIdOrNull
import org.springframework.transaction.event.TransactionalEventListener

@SusuEventListener
class EnvelopeEventListener(
private val envelopeService: EnvelopeService,
private val friendService: FriendService,
private val ledgerRepository: LedgerRepository,
private val friendRelationshipService: FriendRelationshipService,
private val coroutineExceptionHandler: ErrorPublishingCoroutineExceptionHandler,
) {
Expand All @@ -24,13 +28,34 @@ class EnvelopeEventListener(
mdcCoroutineScope(Dispatchers.IO + Job() + coroutineExceptionHandler.handler, event.traceId).launch {
val count = envelopeService.countByUidAndFriendId(
uid = event.uid,
friendId = event.friendId
friendId = event.envelope.friendId
)

/** ledger의 봉투 합계 총합 업데이트 */
if (event.envelope.ledgerId != null) {
val ledger = ledgerRepository.findByIdOrNull(event.envelope.ledgerId)

if (ledger != null) {
when (event.envelope.type) {
EnvelopeType.SENT -> {
ledger.apply {
this.totalSentAmounts - event.envelope.amount
}
}

EnvelopeType.RECEIVED -> {
ledger.apply {
this.totalReceivedAmounts - event.envelope.amount
}
}
}.run { ledgerRepository.save(this) }
}
}

/** 조회되는 케이스가 없는 경우, 친구정보도 삭제 */
if (count == 0L) {
friendService.deleteSync(event.friendId)
friendRelationshipService.deleteByFriendIdSync(event.friendId)
friendService.deleteSync(event.envelope.friendId)
friendRelationshipService.deleteByFriendIdSync(event.envelope.friendId)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions api/src/main/kotlin/com/oksusu/susu/api/event/model/Event.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.oksusu.susu.cache.model.OidcPublicKeysCacheModel
import com.oksusu.susu.cache.statistic.domain.UserEnvelopeStatistic
import com.oksusu.susu.common.consts.MDC_KEY_TRACE_ID
import com.oksusu.susu.common.extension.mapper
import com.oksusu.susu.domain.envelope.domain.Envelope
import com.oksusu.susu.domain.envelope.domain.Ledger
import com.oksusu.susu.domain.term.domain.TermAgreement
import com.oksusu.susu.domain.term.domain.vo.TermAgreementChangeType
Expand Down Expand Up @@ -95,9 +96,8 @@ data class CacheUserEnvelopeStatisticEvent(
) : BaseEvent()

data class DeleteEnvelopeEvent(
val envelopeId: Long,
val envelope: Envelope,
val uid: Long,
val friendId: Long,
) : BaseEvent()

data class CreateUserWithdrawEvent(
Expand Down

0 comments on commit a3e1659

Please sign in to comment.