Skip to content

Commit

Permalink
#139 feat: 요청 관련 알림 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
psyeon1120 committed Aug 24, 2023
1 parent 234a14a commit 72218e7
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/main/kotlin/com/psr/psr/global/Constant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "님의 요청 상태를 확인해주세요!"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/psr/psr/order/entity/OrderStatus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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("요청취소", "요청이 취소되었습니다");
Expand Down
12 changes: 9 additions & 3 deletions src/main/kotlin/com/psr/psr/order/service/OrderService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!!)
}

// 요청 상세 조회
Expand Down Expand Up @@ -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!!)
}
}

0 comments on commit 72218e7

Please sign in to comment.