Skip to content

Commit

Permalink
SW-4243 Support deletion of Terraformation Contact when deleting an o…
Browse files Browse the repository at this point in the history
…rganization (#1376)

- BE will handle deletion of last owner and Terraformation Contact when deleting an organization
- This allows the FE client to delete an org which has a Terraformation Contact membership (which cannot be deleted by the organization owner)
  • Loading branch information
karthikbtf authored Sep 21, 2023
1 parent e243817 commit 75ff547
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,22 @@ class OrganizationService(
requirePermissions { deleteOrganization(organizationId) }

dslContext.transaction { _ ->
val users = organizationStore.fetchUsers(organizationId)
val allUsers = organizationStore.fetchUsers(organizationId)

// Fetch all users that aren't a Terraformation Contact (which cannot be removed by org
// owners in the client). This allows us to check for the last remaining Owner.
val users = allUsers.filter { user -> user.role != Role.TerraformationContact }
if (users.size != 1 || users[0].userId != currentUser().userId) {
throw OrganizationHasOtherUsersException(organizationId)
}

// The backend will handle deletion of the Terraformation Contact when the organization is
// being deleted.
val tfContact = allUsers.findLast { user -> user.role == Role.TerraformationContact }
if (tfContact != null) {
systemUser.run { organizationStore.removeUser(organizationId, tfContact.userId) }
}

organizationStore.removeUser(
organizationId, currentUser().userId, allowRemovingLastOwner = true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ internal class OrganizationServiceTest : DatabaseTest(), RunsAsUser {
assertEquals(expected, actual)
}

@Test
fun `deleteOrganization removes Terraformation Contact user from organization`() {
val tfContactUserId = UserId(5)
insertUser()
insertUser(userId = tfContactUserId, email = "[email protected]")
insertOrganization()
insertOrganizationUser(role = Role.Owner)
insertOrganizationUser(userId = tfContactUserId, role = Role.TerraformationContact)

service.deleteOrganization(organizationId)

val expected = emptyList<OrganizationUsersRecord>()
val actual = dslContext.selectFrom(ORGANIZATION_USERS).fetch()
assertEquals(expected, actual)
}

@Test
fun `deleteOrganization publishes event on success`() {
insertUser()
Expand Down

0 comments on commit 75ff547

Please sign in to comment.