Move sent mails when merging items

Fixes: 1055
This commit is contained in:
eikek 2021-09-08 22:29:34 +02:00
parent 84e16f65f9
commit 506e9a0c36
2 changed files with 13 additions and 4 deletions

View File

@ -17,10 +17,7 @@ import docspell.common._
import docspell.store.Store import docspell.store.Store
import docspell.store.queries.QCustomField import docspell.store.queries.QCustomField
import docspell.store.queries.QCustomField.FieldValue import docspell.store.queries.QCustomField.FieldValue
import docspell.store.records.RAttachment import docspell.store.records._
import docspell.store.records.RCustomField
import docspell.store.records.RItem
import docspell.store.records.RTagItem
trait Merge[F[_]] { trait Merge[F[_]] {
def merge(items: NonEmptyList[Ident], collective: Ident): F[Merge.Result[RItem]] def merge(items: NonEmptyList[Ident], collective: Ident): F[Merge.Result[RItem]]
@ -52,6 +49,7 @@ object Merge {
_ <- EitherT.right[Error](moveTags(ids)) _ <- EitherT.right[Error](moveTags(ids))
_ <- EitherT.right[Error](moveCustomFields(ids)) _ <- EitherT.right[Error](moveCustomFields(ids))
_ <- EitherT.right[Error](moveAttachments(ids)) _ <- EitherT.right[Error](moveAttachments(ids))
_ <- EitherT.right[Error](moveSentMails(ids))
_ <- EitherT.right[Error]( _ <- EitherT.right[Error](
createIndex createIndex
.reIndexData(logger, collective.some, NonEmptyList.one(ids.head).some, 50) .reIndexData(logger, collective.some, NonEmptyList.one(ids.head).some, 50)
@ -109,6 +107,14 @@ object Merge {
store.transact(RCustomField.setValue(fv.field, items.head, fv.value)) store.transact(RCustomField.setValue(fv.field, items.head, fv.value))
) )
} yield () } yield ()
def moveSentMails(items: NonEmptyList[Ident]): F[Int] =
NonEmptyList.fromList(items.tail) match {
case Some(others) =>
store.transact(RSentMailItem.moveToItem(items.head, others))
case None =>
0.pure[F]
}
} }
private def mergeFields( private def mergeFields(

View File

@ -71,4 +71,7 @@ object RSentMailItem {
def deleteAllByItem(item: Ident): ConnectionIO[Int] = def deleteAllByItem(item: Ident): ConnectionIO[Int] =
DML.delete(T, T.itemId === item) DML.delete(T, T.itemId === item)
def moveToItem(target: Ident, others: NonEmptyList[Ident]): ConnectionIO[Int] =
DML.update(T, T.itemId.in(others), DML.set(T.itemId.setTo(target)))
} }