Skip to content

Commit

Permalink
Merge pull request #178 from YAPP-19th/feature/#140-리팩터링
Browse files Browse the repository at this point in the history
[#140] 리마인드 로직 수정 및 리팩터링
  • Loading branch information
Ji-Ha authored Aug 31, 2022
2 parents 66f7d22 + 12362e2 commit c5057cd
Show file tree
Hide file tree
Showing 22 changed files with 491 additions and 340 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import com.yapp.web2.domain.folder.controller.FolderController
import com.yapp.web2.security.jwt.TokenDto
import com.yapp.web2.util.AbstractControllerTest
import com.yapp.web2.util.Message
import com.yapp.web2.util.RandomUtils
import io.mockk.Runs
import io.mockk.every
import io.mockk.just
import org.apache.commons.lang3.RandomStringUtils
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
Expand Down Expand Up @@ -293,10 +293,12 @@ class AccountControllerTest : AbstractControllerTest() {
@Test
fun `비밀번호 재설정 시 임시 비밀번호를 생성하고 메일을 발송한다`() {
// given
val tempPassword = RandomStringUtils.randomAlphanumeric(12) + "!"
val tempPassword = RandomUtils.shuffleCharacters(
RandomUtils.getRandomAlphanumeric(12) + RandomUtils.getRandomSpecialCharacter()
)
every { accountService.createTempPassword() } returns tempPassword
every { accountService.updatePassword(any(), any()) } just Runs
every { accountService.sendMail(any(), tempPassword) } returns Message.SUCCESS_SEND_MAIL
every { accountService.sendMail(any(), tempPassword) } just Runs

// when
val resultAction = util.postResultAction("/api/v1/user/password/reset", "", mockMvc)
Expand Down
8 changes: 6 additions & 2 deletions src/main/kotlin/com/yapp/web2/batch/job/NotificationJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ class NotificationJob(
fun remindBookmarkReader(): ListItemReader<Bookmark> {
val bookmarkOfList = notificationService.getRemindBookmark()

log.info("리마인드 발송할 도토리 갯수: ${bookmarkOfList.size}")

return ListItemReader(bookmarkOfList)
}

// TODO: Notification 실패 처리 -> Queue(Kafka) 적재 후 Retry 처리
@Bean
fun remindBookmarkProcessor(): ItemProcessor<Bookmark, Bookmark> {
return ItemProcessor {
Expand All @@ -62,13 +65,14 @@ class NotificationJob(
}
}

// TODO: 코드 수정
@Bean
fun remindBookmarkWriter(): ItemWriter<Bookmark> {
return ItemWriter {
it.stream().forEach { bookmark ->
bookmark.successRemind()
// bookmark.successRemind()
notificationService.save(bookmark)
log.info("{} -> {} 푸쉬 발송", bookmark.userId, bookmark.title)
log.info("{} -> {} Send Notification", bookmark.userId, bookmark.title)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/com/yapp/web2/config/SecurityConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class SecurityConfig(

http.authorizeRequests()
.antMatchers("/actuator/**").permitAll()
// .antMatchers("/api/v1/page/open/**").permitAll()
.anyRequest().authenticated()

http.apply(JwtSecurityConfig(jwtProvider))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,9 @@ class AccountController(
val token = ControllerUtil.extractAccessToken(request)
val tempPassword = accountService.createTempPassword()
accountService.updatePassword(token, tempPassword)
accountService.sendMail(token, tempPassword)

return ResponseEntity.status(HttpStatus.OK).body(accountService.sendMail(token, tempPassword))
return ResponseEntity.status(HttpStatus.OK).body(Message.SUCCESS)
}

}
62 changes: 34 additions & 28 deletions src/main/kotlin/com/yapp/web2/domain/account/entity/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import com.yapp.web2.security.jwt.TokenDto
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
import org.springframework.transaction.annotation.Transactional
import javax.persistence.*
import javax.persistence.CascadeType
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.OneToMany
import javax.validation.constraints.NotBlank
import javax.validation.constraints.NotEmpty

Expand All @@ -17,33 +20,6 @@ class Account(
var email: String
) : BaseTimeEntity() {

companion object {
fun signUpToAccount(dto: AccountRequestDto.SignUpRequest, encryptPassword: String, name: String): Account {
return Account(dto.email, encryptPassword, dto.fcmToken, name)
}

fun profileToAccount(dto: AccountProfile): Account {
return Account(dto.email, dto.image, dto.name, dto.socialType, dto.fcmToken)
}

fun accountToProfile(account: Account): AccountProfile {
return AccountProfile(
account.email,
account.name,
account.image,
account.socialType,
account.fcmToken ?: ""
)
}

fun accountToRemindElements(account: Account): RemindElements {
return RemindElements(account.remindCycle, account.remindToggle)
}

const val BASIC_IMAGE_URL: String = "https://yapp-bucket-test.s3.ap-northeast-2.amazonaws.com/basicImage.png"
}


@Column(nullable = true)
var password: String? = null

Expand Down Expand Up @@ -74,6 +50,36 @@ class Account(
@OneToMany(mappedBy = "account", cascade = [CascadeType.ALL], orphanRemoval = true)
var accountFolderList: MutableList<AccountFolder> = mutableListOf()

fun inverseRemindToggle(reverse: Boolean) {
this.remindToggle = reverse
}

companion object {
fun signUpToAccount(dto: AccountRequestDto.SignUpRequest, encryptPassword: String, name: String): Account {
return Account(dto.email, encryptPassword, dto.fcmToken, name)
}

fun profileToAccount(dto: AccountProfile): Account {
return Account(dto.email, dto.image, dto.name, dto.socialType, dto.fcmToken)
}

fun accountToProfile(account: Account): AccountProfile {
return AccountProfile(
account.email,
account.name,
account.image,
account.socialType,
account.fcmToken ?: ""
)
}

fun accountToRemindElements(account: Account): RemindElements {
return RemindElements(account.remindCycle, account.remindToggle)
}

const val BASIC_IMAGE_URL: String = "https://yapp-bucket-test.s3.ap-northeast-2.amazonaws.com/basicImage.png"
}

constructor(email: String, password: String) : this(email) {
this.password = password
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ class AccountService(
return RandomUtils.shuffleCharacters(randomAlphanumeric + randomSpecialCharacter)
}

fun sendMail(token: String, tempPassword: String): String {
// TODO: 2022/07/16 비동기 처리
fun sendMail(token: String, tempPassword: String) {
val account = jwtProvider.getAccountFromToken(token)
val mailMessage = SimpleMailMessage()
mailMessage.setTo(account.email)
Expand All @@ -292,8 +293,6 @@ class AccountService(
mailSender.send(mailMessage)

log.info("${account.email} 계정으로 임시 비밀번호를 발송하였습니다.")

return Message.SUCCESS_SEND_MAIL
}

fun updatePassword(token: String, tempPassword: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class BookmarkDto {
@ApiModel(description = "북마크 생성 DTO")
data class AddBookmarkDto(

// TODO: 2021/12/04 RequestParam 데이터 검증
@ApiModelProperty(value = "북마크 url", required = true, example = "https://www.naver.com")
var url: String,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import org.springframework.web.bind.annotation.PatchMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api/v1/trash")
Expand All @@ -19,14 +23,14 @@ class TrashController(
@ApiOperation("북마크 복원 API")
@PatchMapping("/restore")
fun restoreBookmarks(@RequestBody @ApiParam(value = "복원할 북마크 ID 리스트", required = true) request: BookmarkDto.RestoreBookmarkRequest): ResponseEntity<String> {
bookmarkService.restore(request.bookmarkIdList)
bookmarkService.restoreBookmarks(request.bookmarkIdList)
return ResponseEntity.status(HttpStatus.OK).body(Message.SUCCESS)
}

@ApiOperation("북마크 영구삭제 API")
@PostMapping("/truncate")
fun permanentDelete(@RequestBody @ApiParam(value = "영구삭제할 북마크 ID 리스트", required = true) request: BookmarkDto.TruncateBookmarkRequest): ResponseEntity<String> {
bookmarkService.permanentDelete(request.bookmarkIdList)
bookmarkService.deleteBookmarkPermanently(request.bookmarkIdList)
return ResponseEntity.status(HttpStatus.OK).body(Message.SUCCESS)
}
}
95 changes: 40 additions & 55 deletions src/main/kotlin/com/yapp/web2/domain/bookmark/entity/Bookmark.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,29 @@ class Bookmark(
var folderId: Long?,
val link: String
) {
@Id
lateinit var id: String

var title: String? = ""
var folderEmoji: String? = ""
var folderName: String? = ""

var clickCount: Int = 0
var deleteTime: LocalDateTime? = null
var deleted: Boolean = false
var description: String? = null
var image: String? = null

var saveTime: LocalDateTime = LocalDateTime.now()

var remindList = mutableListOf<Remind>()

var shared: Boolean = false

constructor(userId: Long, folderId: Long?, link: String, title: String?) : this(userId, folderId, link) {
this.title = title
}

constructor(userId: Long, folderId: Long?, link: String, title: String?, remindTime: String?) : this(
userId,
folderId,
link,
title
) {
this.remindTime = remindTime
}

constructor(
userId: Long,
folderId: Long?,
Expand All @@ -48,44 +58,22 @@ class Bookmark(
if (remind) remindOn(Remind(account))
}

constructor(
userId: Long,
folderId: Long?,
folderEmoji: String?,
folderName: String?,
link: String,
title: String?,
remindTime: String?,
image: String?,
description: String?
) : this(userId, folderId, link, title, remindTime) {
this.folderEmoji = folderEmoji
this.folderName = folderName
this.description = description
this.image = image
}

@Id
lateinit var id: String

var title: String? = ""
var folderEmoji: String? = ""
var folderName: String? = ""

var clickCount: Int = 0
var deleteTime: LocalDateTime? = null
var deleted: Boolean = false
var description: String? = null
var image: String? = null

var saveTime: LocalDateTime = LocalDateTime.now()
var remindTime: String? = null
var remindCheck: Boolean = false
var remindStatus: Boolean = false

var remindList = mutableListOf<Remind>()

var shared: Boolean = false
// constructor(
// userId: Long,
// folderId: Long?,
// folderEmoji: String?,
// folderName: String?,
// link: String,
// title: String?,
// remindTime: String?,
// image: String?,
// description: String?
// ) : this(userId, folderId, link, title, remindTime) {
// this.folderEmoji = folderEmoji
// this.folderName = folderName
// this.description = description
// this.image = image
// }

fun restore(): Bookmark {
this.deleted = false
Expand Down Expand Up @@ -137,19 +125,16 @@ class Bookmark(
remindList.add(remind)
}

fun updateRemindCheck() {
this.remindCheck = true
}

fun moveFolder(nextFolderId: Long) {
this.folderId = nextFolderId
}

fun successRemind() {
this.remindStatus = true
}

fun hitClickCount() {
this.clickCount++
}

override fun toString(): String {
return "Bookmark(userId=$userId, link='$link', id='$id', title=$title, remindList=$remindList)"
}

}
29 changes: 24 additions & 5 deletions src/main/kotlin/com/yapp/web2/domain/bookmark/entity/Remind.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import java.time.LocalDate

class Remind() {

var userId: Long = -1L
var remindTime: String = ""
var fcmToken: String = ""
var remindCheck: Boolean = false
var remindStatus: Boolean = false

constructor(userId: Long): this() {
this.userId = userId
}
Expand All @@ -14,11 +20,6 @@ class Remind() {
this.remindTime = LocalDate.now().plusDays(account.remindCycle.toLong()).toString()
this.fcmToken = account.fcmToken!!
}
var userId: Long = -1L
var remindTime: String = ""
var fcmToken: String = ""
var remindCheck: Boolean = false
var remindStatus: Boolean = false

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand All @@ -34,4 +35,22 @@ class Remind() {
override fun hashCode(): Int {
return userId.hashCode()
}

override fun toString(): String {
return StringBuilder().append("Remind [")
.append("userId: $userId")
.append("remindTime: $remindTime")
.append("remindCheck: $remindCheck")
.append("remindStatus: $remindStatus")
.append("]")
.toString()
}

fun updateRemindCheck() {
this.remindCheck = true
}

fun successRemind() {
this.remindStatus = true
}
}
Loading

0 comments on commit c5057cd

Please sign in to comment.