-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
58 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 39 additions & 39 deletions
78
.../src/main/scala/ch/epfl/bluebrain/nexus/delta/sdk/organizations/OrganizationDeleter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,53 @@ | ||
package ch.epfl.bluebrain.nexus.delta.sdk.organizations | ||
|
||
import ch.epfl.bluebrain.nexus.delta.kernel.search.Pagination | ||
import ch.epfl.bluebrain.nexus.delta.sdk.ProjectResource | ||
import ch.epfl.bluebrain.nexus.delta.sdk.acls.PurgeAcl | ||
import ch.epfl.bluebrain.nexus.delta.sdk.acls.model.AclAddress | ||
import ch.epfl.bluebrain.nexus.delta.sdk.model.search.SearchParams | ||
import ch.epfl.bluebrain.nexus.delta.sdk.organizations.OrganizationsImpl | ||
import ch.epfl.bluebrain.nexus.delta.sdk.organizations.model.OrganizationRejection | ||
import ch.epfl.bluebrain.nexus.delta.sdk.projects.ListProjects | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Label | ||
import monix.bio.IO | ||
import monix.bio.UIO | ||
|
||
import ch.epfl.bluebrain.nexus.delta.sdk.acls.Acls | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.model.EntityType | ||
import ch.epfl.bluebrain.nexus.delta.rdf.IriOrBNode | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.Transactors | ||
|
||
import cats.syntax.all._ | ||
import ch.epfl.bluebrain.nexus.delta.sourcing.implicits._ | ||
import doobie.ConnectionIO | ||
import doobie.implicits._ | ||
import monix.bio.Task | ||
|
||
trait OrganizationDeleter { | ||
def deleteIfEmpty(org: Label): IO[OrganizationRejection.OrganizationNonEmpty, Unit] | ||
def deleteIfEmpty(id: Label): Task[Unit] | ||
} | ||
|
||
object OrganizationDeleter { | ||
|
||
def apply( | ||
projects: ListProjects, | ||
acls: PurgeAcl, | ||
orgs: OrganizationsImpl.OrganizationsLog | ||
): OrganizationDeleter = { | ||
val delete = deleteIfEmpty(projects, acls, orgs)(_) | ||
org => delete(org) | ||
def apply(xas: Transactors): OrganizationDeleter = new OrganizationDeleter { | ||
|
||
def deleteIfEmpty(id: Label): Task[Unit] = | ||
for { | ||
orgIsEmpty <- orgIsEmpty(id) | ||
_ <- if (orgIsEmpty) deleteAll(id) | ||
else Task.raiseError(new Exception(s"Cannot delete non-empty organization $id")) | ||
} yield () | ||
|
||
def deleteAll(id: Label): Task[Unit] = | ||
List("global_events", "global_states").traverse(deleteFromTable(id, _)).transact(xas.write).void | ||
|
||
def deleteFromTable(id: Label, table: String): ConnectionIO[Unit] = | ||
for { | ||
_ <- delete(Organizations.encodeId(id), Organizations.entityType, table) | ||
_ <- delete(Acls.encodeId(AclAddress.fromOrg(id)), Acls.entityType, table) | ||
} yield () | ||
|
||
def delete(id: IriOrBNode.Iri, tpe: EntityType, table: String): ConnectionIO[Unit] = | ||
sql"""DELETE FROM $table WHERE type = $tpe AND id = $id""".update.run.void | ||
|
||
def orgIsEmpty(id: Label): Task[Boolean] = | ||
sql"""SELECT type from scoped_events WHERE org = $id LIMIT 1""" | ||
.query[Label] | ||
.option | ||
.map(_.isEmpty) | ||
.transact(xas.read) | ||
} | ||
|
||
private val pagination = Pagination.FromPagination(from = 0, size = 1) | ||
|
||
private val ordering: Ordering[ProjectResource] = Ordering.by(_.updatedAt) | ||
|
||
private def params(id: Label): SearchParams.ProjectSearchParams = | ||
SearchParams.ProjectSearchParams( | ||
organization = Some(id), | ||
filter = _ => UIO.pure(true) | ||
) | ||
|
||
def deleteIfEmpty( | ||
projects: ListProjects, | ||
acls: PurgeAcl, | ||
orgs: OrganizationsImpl.OrganizationsLog | ||
)(id: Label): IO[OrganizationRejection.OrganizationNonEmpty, Unit] = | ||
for { | ||
orgIsEmpty <- projects.list(pagination, params(id), ordering).map(_.results.isEmpty) | ||
// TODO: would rather this all be done in one transaction but it will require | ||
// some refactoring - what do you think? | ||
_ <- if (orgIsEmpty) acls.purge(AclAddress.fromOrg(id)) *> orgs.delete(id) | ||
else IO.raiseError(OrganizationRejection.OrganizationNonEmpty(id)) | ||
} yield () | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters