Skip to content

Commit

Permalink
Merge pull request #408 from Assocify-Team/ft/ManageMembers
Browse files Browse the repository at this point in the history
Methods for managing Association Members
  • Loading branch information
zizouet authored Jun 2, 2024
2 parents b844d2a + fc1b441 commit 3a37a5f
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
102 changes: 102 additions & 0 deletions app/src/main/java/com/github/se/assocify/model/database/UserAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,106 @@ class UserAPI(private val db: SupabaseClient, cachePath: Path) : SupabaseApi() {
return PermissionRole(roleId, associationId, type)
}
}

/**
* Changes the role of a user in an association
*
* @param userId the id of the user to change the role of
* @param associationId the id of the association to change the role in
* @param roleType the new role of the user
* @param onSuccess called on success
* @param onFailure called on failure
*/
fun changeRoleOfUser(
userId: String,
associationId: String,
roleType: RoleType,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit
) {
// Determine which roleId of the user to change
var roleIDs: List<JsonObject>
tryAsync(onFailure) {
db.from("member_of")
.select { filter { eq("user_id", userId) } }
.decodeList<JsonObject>()
.let { roleIDs = it }

var roleIDToChange: JsonObject? = null
roleIDs
.map { it["role_id"].toString().drop(1).dropLast(1) }
.forEach {
val result =
db.from("role")
.select {
filter {
eq("uid", it)
eq("association_id", associationId)
}
}
.decodeSingleOrNull<JsonObject>()
if (result != null) {
roleIDToChange = result
}
}

// Find the role to change to
val roleToChangeTo =
db.from("role")
.select {
filter {
eq("type", roleType.name.lowercase())
eq("association_id", associationId)
}
}
.decodeSingle<JsonObject>()

// Update the role of the user
db.from("member_of").update({
Membership::roleId setTo roleToChangeTo["uid"].toString().drop(1).dropLast(1)
}) {
filter {
eq("user_id", userId)
eq("role_id", roleIDToChange.toString().drop(1).dropLast(1))
}
}
onSuccess()
}
}

/**
* Removes a user from an association
*
* @param userId the id of the user to remove
* @param associationId the id of the association to remove the user from
* @param onSuccess called on success
* @param onFailure called on failure
*/
fun removeUserFromAssociation(
userId: String,
associationId: String,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit
) {
var roleIDs: List<JsonObject>
tryAsync(onFailure) {
// Get all the role ids the user have
roleIDs =
db.from("member_of").select { filter { eq("user_id", userId) } }.decodeList<JsonObject>()

// Delete the role id from the user which corresponds to the association
roleIDs
.map { it["role_id"].toString().drop(1).dropLast(1) }
.forEach {
println("Role ID: $it")
db.from("role").delete {
filter {
eq("uid", it)
eq("association_id", associationId)
}
}
}
onSuccess()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,60 @@ class UserAPITest {
userAPI.setProfilePicture(uuid1.toString(), mockk(), { fail("Should not succeed") }, onFailure)
verify(timeout = 1000) { onFailure(any()) }
}

@Test
fun testChangeRoleOfUser() {
val onSuccess: () -> Unit = mockk(relaxed = true)
val onFailure: (Exception) -> Unit = mockk(relaxed = true)
error = false
response =
"""
[{
"user_id": "${APITestUtils.USER.uid}",
"role_id": "${APITestUtils.PERMISSION_ROLE.uid}"
}]
"""
.trimIndent()

userAPI.changeRoleOfUser(
APITestUtils.USER.uid,
APITestUtils.ASSOCIATION.uid,
RoleType.PRESIDENCY,
onSuccess,
onFailure)
verify(timeout = 1000) { onSuccess() }
error = true
userAPI.changeRoleOfUser(
APITestUtils.USER.uid,
APITestUtils.ASSOCIATION.uid,
RoleType.PRESIDENCY,
onSuccess,
onFailure)
verify(timeout = 1000) { onFailure(any()) }
}

@Test
fun testRemoveUserFromAssociation() {
val onSuccess: () -> Unit = mockk(relaxed = true)
val onFailure: (Exception) -> Unit = mockk(relaxed = true)
error = false
response =
"""
[{
"user_id": "${APITestUtils.USER.uid}",
"role_id": "${APITestUtils.PERMISSION_ROLE.uid}"
}]
"""
.trimIndent()

userAPI.removeUserFromAssociation(
APITestUtils.USER.uid, APITestUtils.ASSOCIATION.uid, onSuccess, onFailure)
verify(timeout = 1000) { onSuccess() }
error = true
userAPI.removeUserFromAssociation(
APITestUtils.USER.uid, APITestUtils.ASSOCIATION.uid, onSuccess, onFailure)
verify(timeout = 1000) { onFailure(any()) }
}
}

0 comments on commit 3a37a5f

Please sign in to comment.