diff --git a/modules/store/src/main/scala/docspell/store/queries/QAttachment.scala b/modules/store/src/main/scala/docspell/store/queries/QAttachment.scala index a738c8b1..8e300370 100644 --- a/modules/store/src/main/scala/docspell/store/queries/QAttachment.scala +++ b/modules/store/src/main/scala/docspell/store/queries/QAttachment.scala @@ -15,21 +15,25 @@ object QAttachment { def deleteById[F[_]: Sync](store: Store[F])(attachId: Ident, coll: Ident): F[Int] = for { raFile <- store.transact(RAttachment.findByIdAndCollective(attachId, coll)).map(_.map(_.fileId)) - rsFile <- store.transact(RAttachmentSource.findById(attachId)).map(_.map(_.fileId)) + rsFile <- store.transact(RAttachmentSource.findByIdAndCollective(attachId, coll)).map(_.map(_.fileId)) n <- store.transact(RAttachment.delete(attachId)) f <- Stream.emits(raFile.toSeq ++ rsFile.toSeq) .map(_.id) .flatMap(store.bitpeace.delete) .map(flag => if (flag) 1 else 0) .compile - .last - } yield n + f.getOrElse(0) + .foldMonoid + } yield n + f def deleteAttachment[F[_]: Sync](store: Store[F])(ra: RAttachment): F[Int] = for { + s <- store.transact(RAttachmentSource.findById(ra.id)) n <- store.transact(RAttachment.delete(ra.id)) - f <- Stream.emit(ra.fileId.id).flatMap(store.bitpeace.delete).compile.last - } yield n + f.map(_ => 1).getOrElse(0) + f <- Stream.emits(ra.fileId.id +: s.map(_.fileId.id).toSeq). + flatMap(store.bitpeace.delete). + map(flag => if (flag) 1 else 0). + compile.foldMonoid + } yield n + f def deleteItemAttachments[F[_]: Sync](store: Store[F])(itemId: Ident, coll: Ident): F[Int] = for {