Skip to content

Commit

Permalink
Api-v0.0.3-9
Browse files Browse the repository at this point in the history
  • Loading branch information
kdomo authored Jul 12, 2023
2 parents 9a9035e + 8659135 commit 40bcc23
Show file tree
Hide file tree
Showing 31 changed files with 517 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ class PromiseReadUseCase(
}

val action: (Entry<PromiseType, MutableList<PromiseFindDto>>) -> Unit = { (promiseType, promiseFindDtos) ->
promiseFindDtos.sortBy { it.endTime }
// promiseType == BEFORE 인 것만 정렬
when (promiseType) {
// 예정된 약속은 오름차순 정렬
PromiseType.BEFORE -> promiseFindDtos.sortBy { it.endTime }
// 종료된 약속은 내림차순 정렬
else -> promiseFindDtos.sortByDescending { it.endTime }
}
}
promiseSplitByPromiseTypeDto.forEach(action)
}
Expand Down Expand Up @@ -119,7 +125,7 @@ class PromiseReadUseCase(
}

val promiseImagesUrls = promiseImageAdapter.findAllByPromiseId(promise.id!!)
.sortedByDescending { it.createdAt }
.sortedBy { it.createdAt }
.map { it.uri }

val timeOverLocations = promiseUsers.mapNotNull { promiseUser ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const val DEV = "dev"
const val LOCAL = "local"
const val WITHDRAW_PREFIX = "withdraw"
const val RADIUS_WAIT_CONFIRM = 200
const val INTERACTION_FIXED_COUNT = 200L
const val SLACK_MAX_LENGTH = 1000

const val NCP_LOCAL_SEARCH_DISPLAY_COUNT = 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ class Interaction(
@Column(name = "interaction_id")
val id: Long? = null,
) : BaseTimeEntity() {
fun increment() {
fun increment(): Long {
count += 1
return count
}

companion object {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class InteractionDomainService(
identifier = "userId",
)
@CheckUserParticipation
fun increment(promiseId: Long, userId: Long, interactionType: InteractionType) {
fun increment(promiseId: Long, userId: Long, interactionType: InteractionType): Long {
val interaction = interactionAdapter.queryInteraction(promiseId, userId, interactionType)
interaction.increment()
return interaction.increment()
}

@CheckUserParticipation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.depromeet.whatnow.domains.interactionhistory.domain
import com.depromeet.whatnow.common.BaseTimeEntity
import com.depromeet.whatnow.common.aop.event.Events
import com.depromeet.whatnow.domains.interaction.domain.InteractionType
import com.depromeet.whatnow.domains.interactionhistory.event.InteractionHistoryRegisterEvent
import com.depromeet.whatnow.events.domainEvent.InteractionHistoryRegisterEvent
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.GeneratedValue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.depromeet.whatnow.domains.notification.adapter

import com.depromeet.whatnow.annotation.Adapter
import com.depromeet.whatnow.domains.notification.domain.Notification
import com.depromeet.whatnow.domains.notification.repository.NotificationRepository

@Adapter
class NotificationAdapter(
val notificationRepository: NotificationRepository,
) {
fun save(notification: Notification): Notification {
return notificationRepository.save(notification)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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.Entity
Expand All @@ -19,17 +20,92 @@ class Notification(
var notificationType: NotificationType,

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

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

var userId: Long?,

@ElementCollection
var targetUserId: Set<Long>,
var targetUserIds: Set<Long>,

var targetId: Long,
var targetId: Long?,

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "notification_id")
val id: Long? = null,
) : BaseTimeEntity()
) : 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,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ enum class NotificationType(val kr: String) {
ARRIVAL("도착"),
TIMEOVER("시간 초과"),
INTERACTION("인터렉션"),
PICTURE("사진"),
INTERACTION_ATTAINMENT("인터렉션 달성"),
IMAGE("이미지"),
START_SHARING("공유 시작"),
END_SHARING("공유 종료"),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.depromeet.whatnow.domains.notification.repository

import com.depromeet.whatnow.domains.notification.domain.Notification
import org.springframework.data.jpa.repository.JpaRepository

interface NotificationRepository : JpaRepository<Notification, Long>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class NotificationDomainService(
val notificationAdapter: NotificationAdapter,
) {
@Transactional
fun saveForImage(userId: Long, targetUserId: Set<Long>, promiseImageId: Long) {
notificationAdapter.save(Notification.createForImage(userId, targetUserId, promiseImageId))
}

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

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

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

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

@Transactional
fun saveForInteractionAttainment(userId: Long, senderUserIds: Set<Long>, interactionType: InteractionType) {
notificationAdapter.save(Notification.createForInteractionAttainment(userId, senderUserIds, interactionType))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.depromeet.whatnow.consts.USER_DEFAULT_PROFILE_IMAGE
import com.depromeet.whatnow.domains.user.exception.AlreadyDeletedUserException
import com.depromeet.whatnow.domains.user.exception.ForbiddenUserException
import com.depromeet.whatnow.events.domainEvent.UserProfileImageUpdatedEvent
import com.depromeet.whatnow.events.domainEvent.UserSignUpEvent
import com.depromeet.whatnow.events.domainEvent.UserRegisterEvent
import java.time.LocalDateTime
import javax.persistence.Column
import javax.persistence.Embedded
Expand Down Expand Up @@ -52,7 +52,7 @@ class User(

@PostPersist
fun registerEvent() {
Events.raise(UserSignUpEvent(this.id!!))
Events.raise(UserRegisterEvent(this.id!!))
}

fun login(fcmToken: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.depromeet.whatnow.events.domainEvent

import com.depromeet.whatnow.common.aop.event.DomainEvent
import com.depromeet.whatnow.domains.interaction.domain.InteractionType

class InteractionFixedEvent(
val promiseId: Long,
val userId: Long,
val interactionType: InteractionType,
) : DomainEvent()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.depromeet.whatnow.domains.interactionhistory.event
package com.depromeet.whatnow.events.domainEvent

import com.depromeet.whatnow.common.aop.event.DomainEvent
import com.depromeet.whatnow.domains.interaction.domain.InteractionType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package com.depromeet.whatnow.events.domainEvent

import com.depromeet.whatnow.common.aop.event.DomainEvent

data class UserSignUpEvent(
data class UserRegisterEvent(
val userId: Long,
) : DomainEvent()
Original file line number Diff line number Diff line change
@@ -1,18 +1,70 @@
package com.depromeet.whatnow.events.handler

import com.depromeet.whatnow.annotation.Handler
import com.depromeet.whatnow.config.fcm.FcmService
import com.depromeet.whatnow.domains.notification.domain.NotificationType
import com.depromeet.whatnow.domains.notification.service.NotificationDomainService
import com.depromeet.whatnow.domains.promiseuser.adaptor.PromiseUserAdaptor
import com.depromeet.whatnow.domains.promiseuser.domain.PromiseUserType.LATE
import com.depromeet.whatnow.domains.promiseuser.domain.PromiseUserType.WAIT
import com.depromeet.whatnow.domains.user.adapter.UserAdapter
import com.depromeet.whatnow.events.domainEvent.PromiseImageRegisterEvent
import org.springframework.scheduling.annotation.Async
import org.springframework.transaction.event.TransactionPhase
import org.springframework.transaction.event.TransactionalEventListener

@Handler
class ImageRegisterEventHandler {
class ImageRegisterEventHandler(
val promiseUserAdapter: PromiseUserAdaptor,
val userAdapter: UserAdapter,
val fcmService: FcmService,
val notificationDomainService: NotificationDomainService,
) {
@Async
@TransactionalEventListener(classes = [PromiseImageRegisterEvent::class], phase = TransactionPhase.AFTER_COMMIT)
fun handleRegisterPictureEvent(promiseImageRegisterEvent: PromiseImageRegisterEvent) {
fun handlePromiseImageRegisterEvent(promiseImageRegisterEvent: PromiseImageRegisterEvent) {
val userId: Long = promiseImageRegisterEvent.userId
val promiseId: Long = promiseImageRegisterEvent.promiseId
// TODO: FCM 토큰 발급 후 약속ID 기준 참여자들에게 푸시 알림 보내기

// 사진을 보낸 유저가 Late인지 Wait인지 확인하기 위한 PromiseUser 조회
val promiseUser = promiseUserAdapter.findByPromiseIdAndUserId(promiseId, userId)

// 약속에 참여한 유저들 조회 (사진 보낸 유저 제외)
val usersExcludingSelf = promiseUserAdapter.findByPromiseId(promiseId)
.filter { promiseUser -> promiseUser.userId != userId }
.map { promiseUser -> userAdapter.queryUser(promiseUser.userId) }

// 앱 알람 허용한 유저
val appAlarmPermitUsers = usersExcludingSelf.filter { user ->
user.fcmNotification.fcmToken != null && user.fcmNotification.appAlarm
}

val data: MutableMap<String, String> = mutableMapOf()
data["notificationType"] = NotificationType.IMAGE.name
data["promiseId"] = promiseId.toString()

// 앱 알람 허용한 유저에게 알람 보내기
when (promiseUser.promiseUserType) {
LATE -> {
fcmService.sendGroupMessageAsync(
appAlarmPermitUsers.map { user -> user.fcmNotification.fcmToken!! },
"지각한 친구의 사진 도착",
"지각한 친구가 보낸 사진을 확인해봐!",
data,
)
}
WAIT -> {
fcmService.sendGroupMessageAsync(
appAlarmPermitUsers.map { user -> user.fcmNotification.fcmToken!! },
"도착한 친구들의 사진 도착",
"도착한 친구들이 보낸 사진을 확인해봐!",
data,
)
}
}

// notification 저장
val targetUserIds = usersExcludingSelf.map { user -> user.id!! }.toSet()
notificationDomainService.saveForImage(userId, targetUserIds, promiseId)
}
}
Loading

0 comments on commit 40bcc23

Please sign in to comment.