Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Methods for managing Association Members #408

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 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,97 @@ class UserAPI(private val db: SupabaseClient, cachePath: Path) : SupabaseApi() {
return PermissionRole(roleId, associationId, type)
}
}

Mai-LinhC marked this conversation as resolved.
Show resolved Hide resolved
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(
zizouet marked this conversation as resolved.
Show resolved Hide resolved
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()) }
}
}
Loading