Skip to content

Commit

Permalink
feat(mail): implement the mail history function
Browse files Browse the repository at this point in the history
  • Loading branch information
knae11 authored Oct 13, 2021
1 parent 590d6ce commit 620c711
Show file tree
Hide file tree
Showing 20 changed files with 556 additions and 89 deletions.
8 changes: 5 additions & 3 deletions src/main/kotlin/apply/application/EvaluationDtos.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import apply.domain.evaluation.Evaluation
import apply.domain.evaluationItem.EvaluationItem
import apply.domain.evaluationtarget.EvaluationStatus
import apply.domain.recruitment.Recruitment
import apply.domain.user.User
import javax.validation.Valid
import javax.validation.constraints.Max
import javax.validation.constraints.Min
Expand Down Expand Up @@ -150,10 +151,11 @@ data class EvaluationTargetData(
)

data class MailTargetResponse(
val name: String,
val email: String
val email: String,
val name: String? = null
) {
constructor(userResponse: UserResponse) : this(userResponse.name, userResponse.email)
constructor(userResponse: UserResponse) : this(userResponse.email, userResponse.name)
constructor(user: User) : this(user.email, user.name)
}

data class EvaluationItemScoreData(
Expand Down
37 changes: 37 additions & 0 deletions src/main/kotlin/apply/application/MailHistoryService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package apply.application

import apply.application.mail.MailData
import apply.domain.mail.MailHistory
import apply.domain.mail.MailHistoryRepository
import apply.domain.mail.getById
import apply.domain.user.UserRepository
import org.springframework.stereotype.Service
import javax.transaction.Transactional

@Transactional
@Service
class MailHistoryService(
private val mailHistoryRepository: MailHistoryRepository,
private val userRepository: UserRepository
) {
fun save(request: MailData) {
mailHistoryRepository.save(
MailHistory(
request.subject,
request.body,
request.sender,
request.recipients,
request.sentTime
)
)
}

fun findAll(): List<MailData> {
return mailHistoryRepository.findAll().map { MailData(it) }
}

fun getById(mailHistoryId: Long): MailData {
val mailHistory = mailHistoryRepository.getById(mailHistoryId)
return MailData(mailHistory)
}
}
9 changes: 8 additions & 1 deletion src/main/kotlin/apply/application/MailTargetService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import apply.domain.evaluationtarget.EvaluationStatus
import apply.domain.evaluationtarget.EvaluationTarget
import apply.domain.evaluationtarget.EvaluationTargetRepository
import apply.domain.user.UserRepository
import apply.domain.user.findAllByEmailIn
import org.springframework.stereotype.Service
import javax.transaction.Transactional

Expand All @@ -16,7 +17,13 @@ class MailTargetService(
fun findMailTargets(evaluationId: Long, evaluationStatus: EvaluationStatus? = null): List<MailTargetResponse> {
val userIds = findEvaluationTargets(evaluationId, evaluationStatus).map { it.userId }
return userRepository.findAllById(userIds)
.map { MailTargetResponse(it.name, it.email) }
.map { MailTargetResponse(it.email, it.name) }
}

fun findAllByEmails(emails: List<String>): List<MailTargetResponse> {
val users = userRepository.findAllByEmailIn(emails)
val anonymousEmails = emails - users.map { it.email }
return users.map { MailTargetResponse(it) } + anonymousEmails.map { MailTargetResponse(it) }
}

private fun findEvaluationTargets(evaluationId: Long, evaluationStatus: EvaluationStatus?): List<EvaluationTarget> {
Expand Down
28 changes: 26 additions & 2 deletions src/main/kotlin/apply/application/mail/MailData.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
package apply.application.mail

import apply.domain.mail.MailHistory
import java.time.LocalDateTime
import javax.validation.constraints.NotEmpty
import javax.validation.constraints.NotNull
import javax.validation.constraints.Size

data class MailData(
@field:NotEmpty
@field:Size(min = 1, max = 100)
var subject: String = "",

@field:NotEmpty
var body: String = "",

@field:NotEmpty
var recipients: List<String> = emptyList()
)
@field:Size(min = 1, max = 100)
var sender: String = "",

@field:NotEmpty
var recipients: List<String> = emptyList(),

@field:NotNull
var sentTime: LocalDateTime = LocalDateTime.now(),

@field:NotNull
var id: Long = 0L
) {
constructor(mailHistory: MailHistory) : this(
mailHistory.subject,
mailHistory.body,
mailHistory.sender,
mailHistory.recipients,
mailHistory.sentTime,
mailHistory.id
)
}
31 changes: 31 additions & 0 deletions src/main/kotlin/apply/domain/mail/MailHistory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package apply.domain.mail

import support.domain.BaseEntity
import support.domain.StringToListConverter
import java.time.LocalDateTime
import javax.persistence.Column
import javax.persistence.Convert
import javax.persistence.Entity
import javax.persistence.Lob

@Entity
class MailHistory(
@Column(nullable = false)
val subject: String,

@Column(nullable = false)
@Lob
val body: String,

@Column(nullable = false)
val sender: String,

@Column(nullable = false)
@Convert(converter = StringToListConverter::class)
@Lob
val recipients: List<String>,

@Column(nullable = false)
val sentTime: LocalDateTime = LocalDateTime.now(),
id: Long = 0L
) : BaseEntity(id)
8 changes: 8 additions & 0 deletions src/main/kotlin/apply/domain/mail/MailHistoryRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package apply.domain.mail

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.repository.findByIdOrNull

fun MailHistoryRepository.getById(id: Long): MailHistory = findByIdOrNull(id) ?: throw NoSuchElementException()

interface MailHistoryRepository : JpaRepository<MailHistory, Long>
4 changes: 2 additions & 2 deletions src/main/kotlin/apply/ui/admin/BaseLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package apply.ui.admin
import apply.application.RecruitmentService
import apply.ui.admin.cheater.CheatersView
import apply.ui.admin.evaluation.EvaluationsView
import apply.ui.admin.mail.MailFormView
import apply.ui.admin.mail.MailsView
import apply.ui.admin.recruitment.RecruitmentsView
import com.vaadin.flow.component.Component
import com.vaadin.flow.component.applayout.AppLayout
Expand Down Expand Up @@ -58,7 +58,7 @@ class BaseLayout(
"과제 관리".accordionOf("admin/missions", recruitments),
"선발 과정".accordionOf("admin/selections", recruitments),
"부정 행위자" of CheatersView::class.java,
"메일 발송" of MailFormView::class.java
"메일 관리" of MailsView::class.java
)
}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/apply/ui/admin/mail/GroupMailTargetDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.vaadin.flow.component.orderedlayout.FlexComponent
import com.vaadin.flow.component.orderedlayout.HorizontalLayout
import com.vaadin.flow.component.select.Select
import com.vaadin.flow.data.provider.ListDataProvider
import support.views.NO_NAME
import support.views.addSortableColumn
import support.views.createContrastButton
import support.views.createItemSelect
Expand All @@ -39,7 +40,7 @@ class GroupMailTargetDialog(

private fun createMailTargetsGrid(): Grid<MailTargetResponse> {
return Grid<MailTargetResponse>(10).apply {
addSortableColumn("이름", MailTargetResponse::name)
addSortableColumn("이름") { it.name ?: NO_NAME }
addSortableColumn("이메일", MailTargetResponse::email)
}
}
Expand Down
Loading

0 comments on commit 620c711

Please sign in to comment.