Skip to content

Commit

Permalink
Merge pull request #158 from PSR-Co/feat/#155-noti-scheduler
Browse files Browse the repository at this point in the history
[feat] 2달 뒤 요청상태 입력 요망 알림 스케줄러 적용
  • Loading branch information
psyeon1120 authored Aug 30, 2023
2 parents 4257178 + e9a652a commit 32918fd
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/com/psr/psr/PsrApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package com.psr.psr
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.data.jpa.repository.config.EnableJpaAuditing
import org.springframework.scheduling.annotation.EnableScheduling

@SpringBootApplication
@EnableJpaAuditing
@EnableScheduling
class PsrApplication

fun main(args: Array<String>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ class NotificationService(
}

// 2달 뒤 요청상태 입력 요망 알림
fun send2MonthOrderNoti(productName: String, orderer: User, ordererName: String, orderId: Long) {
fun send2MonthOrderNoti(productName: String, orderReceiver: User, ordererName: String, orderId: Long) {
val messageBody = ordererName + TWO_MONTH_ORDER_SENTENCE
notificationRepository.save(notiAssembler.toEntity(
orderer,
orderReceiver,
productName,
messageBody,
orderId,
NotificationType.TWO_MONTH_ORDER
))

if (isPushNotiAvailable(orderer)) {
if (isPushNotiAvailable(orderReceiver)) {
val message: FcmMessage = notiAssembler.makeMessage(
orderer.deviceToken!!,
orderReceiver.deviceToken!!,
productName,
messageBody,
orderId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
import java.time.LocalDate

@Repository
interface OrderRepository: JpaRepository<Order, Long> {
Expand All @@ -16,4 +17,5 @@ interface OrderRepository: JpaRepository<Order, Long> {
fun findByUserAndStatus(orderer: User, status: String, pageable: Pageable): Page<Order>
fun findByProductUserAndStatus(seller: User, status: String, pageable: Pageable): Page<Order>
fun deleteByUser(user: User)
fun findByCreatedAt_DateAndOrderStatusAndStatus(date: LocalDate, orderStatus: OrderStatus, status: String): List<Order>
}
41 changes: 40 additions & 1 deletion src/main/kotlin/com/psr/psr/order/service/OrderService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ 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.dto.OrderAssembler
import com.psr.psr.order.dto.OrderListRes
import com.psr.psr.order.dto.OrderReq
import com.psr.psr.order.dto.OrderRes
import com.psr.psr.order.entity.Order
import com.psr.psr.order.entity.OrderStatus
import com.psr.psr.order.repository.OrderRepository
Expand All @@ -14,7 +17,9 @@ import com.psr.psr.product.repository.ProductRepository
import com.psr.psr.user.entity.User
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service
import java.time.LocalDate

@Service
class OrderService(
Expand Down Expand Up @@ -76,4 +81,38 @@ class OrderService(
if (status != null)
notificationService.sendChangeOrderStatusNoti(order.product.name, order.product.user, saveOrder.orderStatus, order.id!!)
}

// 2달 뒤 요청상태 입력 요망 알림(오후 1시마다 실행)
@Scheduled(cron = "0 0 13 * * ?", zone = "Asia/Seoul")
fun notify2MonthOrders() {
// 진행 중인 요청
orderRepository.findByCreatedAt_DateAndOrderStatusAndStatus(
LocalDate.now(),
OrderStatus.PROGRESSING,
ACTIVE_STATUS
)
.forEach {
notificationService.send2MonthOrderNoti(
it.product.name,
it.product.user,
it.ordererName,
it.id!!
)
}

// 대기중인 요청
orderRepository.findByCreatedAt_DateAndOrderStatusAndStatus(
LocalDate.now(),
OrderStatus.ORDER_WAITING,
ACTIVE_STATUS
)
.forEach {
notificationService.send2MonthOrderNoti(
it.product.name,
it.product.user,
it.ordererName,
it.id!!
)
}
}
}
8 changes: 2 additions & 6 deletions src/main/kotlin/com/psr/psr/user/dto/request/LoginReq.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package com.psr.psr.user.dto.request

import com.psr.psr.user.entity.*
import jakarta.annotation.Nullable
import jakarta.validation.constraints.Email
import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.NotEmpty
import jakarta.validation.constraints.Pattern
import org.jetbrains.annotations.NotNull
import java.util.stream.Collectors


data class LoginReq (
Expand All @@ -19,5 +14,6 @@ data class LoginReq (
regexp = "^.*(?=^.{8,15}\$)(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!@#\$%^&+=]).*\$",
message = "비밀번호를 숫자, 문자, 특수문자 포함 8~15자리 이내로 입력해주세요"
)
var password: String
var password: String,
val deviceToken: String? = null
)
1 change: 1 addition & 0 deletions src/main/kotlin/com/psr/psr/user/service/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class UserService(
fun login(loginReq: LoginReq) : TokenDto{
val user = userRepository.findByEmail(loginReq.email).orElseThrow{BaseException(NOT_EXIST_EMAIL)}
if(!passwordEncoder.matches(loginReq.password, user.password)) throw BaseException(INVALID_PASSWORD)
if (loginReq.deviceToken != null) user.deviceToken = loginReq.deviceToken
return createToken(user, loginReq.password)
}

Expand Down

0 comments on commit 32918fd

Please sign in to comment.