diff --git a/src/main/kotlin/com/psr/psr/global/Constant.kt b/src/main/kotlin/com/psr/psr/global/Constant.kt index b09be4c..35ea754 100644 --- a/src/main/kotlin/com/psr/psr/global/Constant.kt +++ b/src/main/kotlin/com/psr/psr/global/Constant.kt @@ -58,4 +58,11 @@ class Constant { const val POPULAR = "인기순" } } + + class NotiSentence{ + companion object NotiSentence{ + const val NEW_ORDER_SENTENCE = "님의 요청을 확인해주세요!" + const val TWO_MONTH_ORDER_SENTENCE = "님의 요청 상태를 확인해주세요!" + } + } } \ No newline at end of file diff --git a/src/main/kotlin/com/psr/psr/notification/service/NotificationService.kt b/src/main/kotlin/com/psr/psr/notification/service/NotificationService.kt index 0c29682..8bcb405 100644 --- a/src/main/kotlin/com/psr/psr/notification/service/NotificationService.kt +++ b/src/main/kotlin/com/psr/psr/notification/service/NotificationService.kt @@ -2,9 +2,14 @@ package com.psr.psr.notification.service import com.fasterxml.jackson.databind.ObjectMapper import com.google.auth.oauth2.GoogleCredentials +import com.psr.psr.global.Constant.NotiSentence.NotiSentence.NEW_ORDER_SENTENCE +import com.psr.psr.global.Constant.NotiSentence.NotiSentence.TWO_MONTH_ORDER_SENTENCE +import com.psr.psr.notification.dto.FcmMessage import com.psr.psr.notification.dto.NotiAssembler import com.psr.psr.notification.dto.NotificationListRes +import com.psr.psr.notification.entity.NotificationType import com.psr.psr.notification.repository.NotificationRepository +import com.psr.psr.order.entity.OrderStatus import com.psr.psr.user.entity.User import okhttp3.* import okhttp3.MediaType.Companion.toMediaType @@ -28,6 +33,75 @@ class NotificationService( return notificationRepository.findNotificationByUserGroupByDate(user, pageable) } + // 새로운 요청 알림 + fun sendNewOrderNoti(productName: String, orderReceiver: User, ordererName: String, orderId: Long) { + val messageBody = ordererName + NEW_ORDER_SENTENCE + notificationRepository.save(notiAssembler.toEntity( + orderReceiver, + productName, + messageBody, + orderId, + NotificationType.NEW_ORDER + )) + + if (orderReceiver.deviceToken != null) { + val message: FcmMessage = notiAssembler.makeMessage( + orderReceiver.deviceToken!!, + productName, + messageBody, + orderId, + NotificationType.NEW_ORDER.name + ) + sendMessage(objectMapper.writeValueAsString(message)) + } + } + + // 요청 상태 변경 알림 + fun sendChangeOrderStatusNoti(productName: String, orderer: User, orderStatus: OrderStatus, orderId: Long) { + val messageBody = orderStatus.notiSentence!! + notificationRepository.save(notiAssembler.toEntity( + orderer, + productName, + messageBody, + orderId, + NotificationType.CHANGED_ORDER_STATUS + )) + + if (orderer.deviceToken != null) { + val message: FcmMessage = notiAssembler.makeMessage( + orderer.deviceToken!!, + productName, + messageBody, + orderId, + NotificationType.CHANGED_ORDER_STATUS.name + ) + sendMessage(objectMapper.writeValueAsString(message)) + } + } + + // 2달 뒤 요청상태 입력 요망 알림 + fun send2MonthOrderNoti(productName: String, orderer: User, ordererName: String, orderId: Long) { + val messageBody = ordererName + TWO_MONTH_ORDER_SENTENCE + notificationRepository.save(notiAssembler.toEntity( + orderer, + productName, + messageBody, + orderId, + NotificationType.TWO_MONTH_ORDER + )) + + if (orderer.deviceToken != null) { + val message: FcmMessage = notiAssembler.makeMessage( + orderer.deviceToken!!, + productName, + messageBody, + orderId, + NotificationType.TWO_MONTH_ORDER.name + ) + sendMessage(objectMapper.writeValueAsString(message)) + } + } + // firebase accessToken 발급 private fun getAccessToken(): String? { val firebaseConfigPath = "firebase-service-key.json" diff --git a/src/main/kotlin/com/psr/psr/order/entity/OrderStatus.kt b/src/main/kotlin/com/psr/psr/order/entity/OrderStatus.kt index bac15b2..e6728e9 100644 --- a/src/main/kotlin/com/psr/psr/order/entity/OrderStatus.kt +++ b/src/main/kotlin/com/psr/psr/order/entity/OrderStatus.kt @@ -4,8 +4,8 @@ import com.psr.psr.global.exception.BaseException import com.psr.psr.global.exception.BaseResponseCode import com.psr.psr.global.resolver.EnumType -enum class OrderStatus(override val value: String, val notiSentence: String): EnumType { - ORDER_WAITING("요청대기", "님의 요청을 확인해주세요!"), +enum class OrderStatus(override val value: String, val notiSentence: String?): EnumType { + ORDER_WAITING("요청대기", null), PROGRESSING("진행중", "요청이 진행되었습니다"), COMPLETED("진행완료", "요청이 진행 완료되었습니다"), CANCELED("요청취소", "요청이 취소되었습니다"); diff --git a/src/main/kotlin/com/psr/psr/order/service/OrderService.kt b/src/main/kotlin/com/psr/psr/order/service/OrderService.kt index f963993..3626775 100644 --- a/src/main/kotlin/com/psr/psr/order/service/OrderService.kt +++ b/src/main/kotlin/com/psr/psr/order/service/OrderService.kt @@ -4,6 +4,7 @@ import com.psr.psr.global.Constant.OrderType.OrderType.SELL import com.psr.psr.global.Constant.UserStatus.UserStatus.ACTIVE_STATUS import com.psr.psr.global.exception.BaseException import com.psr.psr.global.exception.BaseResponseCode +import com.psr.psr.notification.service.NotificationService import com.psr.psr.order.dto.* import com.psr.psr.order.entity.Order import com.psr.psr.order.entity.OrderStatus @@ -19,13 +20,15 @@ import org.springframework.stereotype.Service class OrderService( private val orderRepository: OrderRepository, private val productRepository: ProductRepository, - private val orderAssembler: OrderAssembler + private val orderAssembler: OrderAssembler, + private val notificationService: NotificationService ) { // 요청하기 fun makeOrder(user: User, orderReq: OrderReq) { val product: Product = orderReq.productId?.let { productRepository.findByIdAndStatus(it, ACTIVE_STATUS) } ?: throw BaseException(BaseResponseCode.NOT_FOUND_PRODUCT) - orderRepository.save(orderAssembler.toEntity(user, orderReq, product)) + val order = orderRepository.save(orderAssembler.toEntity(user, orderReq, product)) + notificationService.sendNewOrderNoti(order.product.name, order.product.user, order.ordererName, order.id!!) } // 요청 상세 조회 @@ -68,6 +71,9 @@ class OrderService( if (status != null) orderStatus = OrderStatus.findByValue(status) order.editOrder(orderReq, orderStatus) - orderRepository.save(order) + val saveOrder = orderRepository.save(order) + + if (status != null) + notificationService.sendChangeOrderStatusNoti(order.product.name, order.product.user, saveOrder.orderStatus, order.id!!) } } \ No newline at end of file