Fix item merge when merging tags and text fields

Also hard delete the remaining items. They are empty (have no
attachments), because data is moved if possible. Doesn't make much
sense to keep them, because restoring them isn't much useful.
This commit is contained in:
eikek 2021-08-16 13:18:47 +02:00
parent 8099b78b0e
commit a923351b09
2 changed files with 17 additions and 4 deletions

View File

@ -58,7 +58,7 @@ object Merge {
)
_ <- EitherT.right[Error](
NonEmptyList.fromList(items.tail.map(_.id)) match {
case Some(nel) => itemOps.setDeletedState(nel, collective)
case Some(nel) => itemOps.deleteItemMultiple(nel, collective)
case None => 0.pure[F]
}
)
@ -146,7 +146,7 @@ object Merge {
case CustomFieldType.Text =>
val text = fields.toList
.map(fv => CustomFieldType.Text.parseValue(fv.value).toOption)
.flatMap(fv => CustomFieldType.Text.parseValue(fv.value).toOption)
.mkString(", ")
fields.head.copy(value = CustomFieldType.Text.valueString(text))
}

View File

@ -34,8 +34,21 @@ object RTagItem {
def insert(v: RTagItem): ConnectionIO[Int] =
DML.insert(T, T.all, fr"${v.tagItemId},${v.itemId},${v.tagId}")
def moveTags(from: Ident, to: Ident): ConnectionIO[Int] =
DML.update(T, T.itemId === from, DML.set(T.itemId.setTo(to)))
def moveTags(fromItem: Ident, toItem: Ident): ConnectionIO[Int] =
for {
both <- intersect(
Select(select(T.tagId), from(T), T.itemId === fromItem).distinct,
Select(select(T.tagId), from(T), T.itemId === toItem).distinct
).build
.query[Ident]
.to[List]
skipIds = NonEmptyList.fromList(both)
n <- DML.update(
T,
T.itemId === fromItem &&? skipIds.map(ids => T.tagId.notIn(ids)),
DML.set(T.itemId.setTo(toItem))
)
} yield n
def deleteItemTags(item: Ident): ConnectionIO[Int] =
DML.delete(T, T.itemId === item)