Introduce table to store client settings per collective

This commit is contained in:
eikek
2021-12-23 22:46:13 +01:00
parent 456f39704e
commit 706cfaeb05
7 changed files with 172 additions and 26 deletions

View File

@ -12,56 +12,75 @@ import cats.implicits._
import docspell.common.AccountId
import docspell.common._
import docspell.common.syntax.all._
import docspell.store.Store
import docspell.store.records.RClientSettings
import docspell.store.records.RClientSettingsUser
import docspell.store.records.RUser
import io.circe.Json
import org.log4s._
import docspell.store.records.RClientSettingsCollective
trait OClientSettings[F[_]] {
def delete(clientId: Ident, account: AccountId): F[Boolean]
def save(clientId: Ident, account: AccountId, data: Json): F[Unit]
def load(clientId: Ident, account: AccountId): F[Option[RClientSettings]]
def deleteUser(clientId: Ident, account: AccountId): F[Boolean]
def saveUser(clientId: Ident, account: AccountId, data: Json): F[Unit]
def loadUser(clientId: Ident, account: AccountId): F[Option[RClientSettingsUser]]
def deleteCollective(clientId: Ident, account: AccountId): F[Boolean]
def saveCollective(clientId: Ident, account: AccountId, data: Json): F[Unit]
def loadCollective(clientId: Ident, account: AccountId): F[Option[RClientSettingsCollective]]
}
object OClientSettings {
private[this] val logger = getLogger
private[this] val logger = org.log4s.getLogger
def apply[F[_]: Async](store: Store[F]): Resource[F, OClientSettings[F]] =
Resource.pure[F, OClientSettings[F]](new OClientSettings[F] {
val log = Logger.log4s[F](logger)
private def getUserId(account: AccountId): OptionT[F, Ident] =
OptionT(store.transact(RUser.findByAccount(account))).map(_.uid)
def delete(clientId: Ident, account: AccountId): F[Boolean] =
def deleteCollective(clientId: Ident, account: AccountId): F[Boolean] =
store
.transact(RClientSettingsCollective.delete(clientId, account.collective))
.map(_ > 0)
def deleteUser(clientId: Ident, account: AccountId): F[Boolean] =
(for {
_ <- OptionT.liftF(
logger.fdebug(
log.debug(
s"Deleting client settings for client ${clientId.id} and account $account"
)
)
userId <- getUserId(account)
n <- OptionT.liftF(
store.transact(
RClientSettings.delete(clientId, userId)
RClientSettingsUser.delete(clientId, userId)
)
)
} yield n > 0).getOrElse(false)
def save(clientId: Ident, account: AccountId, data: Json): F[Unit] =
def saveCollective(clientId: Ident, account: AccountId, data: Json): F[Unit] =
for {
n <- store.transact(
RClientSettingsCollective.upsert(clientId, account.collective, data)
)
_ <-
if (n <= 0) Async[F].raiseError(new IllegalStateException("No rows updated!"))
else ().pure[F]
} yield ()
def saveUser(clientId: Ident, account: AccountId, data: Json): F[Unit] =
(for {
_ <- OptionT.liftF(
logger.fdebug(
log.debug(
s"Storing client settings for client ${clientId.id} and account $account"
)
)
userId <- getUserId(account)
n <- OptionT.liftF(
store.transact(RClientSettings.upsert(clientId, userId, data))
store.transact(RClientSettingsUser.upsert(clientId, userId, data))
)
_ <- OptionT.liftF(
if (n <= 0) Async[F].raiseError(new Exception("No rows updated!"))
@ -69,15 +88,21 @@ object OClientSettings {
)
} yield ()).getOrElse(())
def load(clientId: Ident, account: AccountId): F[Option[RClientSettings]] =
def loadCollective(
clientId: Ident,
account: AccountId
): F[Option[RClientSettingsCollective]] =
store.transact(RClientSettingsCollective.find(clientId, account.collective))
def loadUser(clientId: Ident, account: AccountId): F[Option[RClientSettingsUser]] =
(for {
_ <- OptionT.liftF(
logger.fdebug(
log.debug(
s"Loading client settings for client ${clientId.id} and account $account"
)
)
userId <- getUserId(account)
data <- OptionT(store.transact(RClientSettings.find(clientId, userId)))
data <- OptionT(store.transact(RClientSettingsUser.find(clientId, userId)))
} yield data).value
})