Skip to content

Commit

Permalink
Merge pull request #213 from PeriodPals/feat/profile/implement-delete…
Browse files Browse the repository at this point in the history
…-account-function

Feat/profile/implement delete account function
  • Loading branch information
Harrish92 authored Nov 21, 2024
2 parents b532942 + 8e6a7bb commit 4ed01c6
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.android.periodpals.model.shared

/**
* Data class for shared data among view models.
*
* @property userId The ID of the user.
*/
data class SharedData(val userId: String)
13 changes: 13 additions & 0 deletions app/src/main/java/com/android/periodpals/model/user/UserModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,17 @@ interface UserRepository {
onSuccess: (UserDto) -> Unit,
onFailure: (Exception) -> Unit
)

/**
* Deletes the user profile from the database.
*
* @param idUser The ID of the user profile to be deleted.
* @param onSuccess Callback function to be called on success.
* @param onFailure Callback function to be called when there is an exception.
*/
suspend fun deleteUserProfile(
idUser: String,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,21 @@ class UserRepositorySupabase(private val supabase: SupabaseClient) : UserReposit
onFailure(e)
}
}

override suspend fun deleteUserProfile(
idUser: String,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit
) {
try {
withContext(Dispatchers.Main) {
supabase.postgrest[USERS].delete() { filter { eq("user_id", idUser) } }
}
Log.d(TAG, "deleteUserProfile: Success")
onSuccess()
} catch (e: Exception) {
Log.d(TAG, "deleteUserProfile: fail to delete user profile: ${e.message}")
onFailure(e)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,34 @@ class UserViewModel(private val userRepository: UserRepositorySupabase) : ViewMo
})
}
}

/**
* Deletes the user profile.
*
* @param idUser The ID of the user profile to be deleted.
* @param onSuccess Callback function to be called when the user profile is successfully deleted.
* @param onFailure Callback function to be called when there is an error deleting the user
* profile.
*/
fun deleteUser(
idUser: String,
onSuccess: () -> Unit = { Log.d(TAG, "deleteAccount success callback") },
onFailure: (Exception) -> Unit = { e: Exception ->
Log.d(TAG, "deleteAccount failure callback: $e")
}
) {
viewModelScope.launch {
userRepository.deleteUserProfile(
idUser,
onSuccess = {
Log.d(TAG, "deleteAccount: Success")
_user.value = null
onSuccess()
},
onFailure = { exception ->
Log.d(TAG, "deleteAccount : fail to delete user: ${exception.message}")
onFailure(exception)
})
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class UserRepositorySupabaseTest {
val imageUrl = "test_image"
val description = "test_description"
val dob = "test_dob"
val id = "test_id"
}

private val defaultUserDto: UserDto = UserDto(name, imageUrl, description, dob)
Expand Down Expand Up @@ -138,4 +139,28 @@ class UserRepositorySupabaseTest {
assert(result)
}
}

@Test
fun deleteUserProfileIsSuccessful() {
var result = false

runTest {
val userRepositorySupabase = UserRepositorySupabase(supabaseClientSuccess)
userRepositorySupabase.deleteUserProfile(
id, { result = true }, { fail("should not call onFailure") })
assert(result)
}
}

@Test
fun deleteUserProfileHasFailed() {
var result = false

runTest {
val userRepositorySupabase = UserRepositorySupabase(supabaseClientFailure)
userRepositorySupabase.deleteUserProfile(
id, { fail("should not call onSuccess") }, { result = true })
assert(result)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,27 @@ class UserViewModelTest {

assertNull(userViewModel.user.value)
}

@Test
fun deleteUserIsSuccessful() = runTest {
doAnswer { it.getArgument<() -> Unit>(1)() }
.`when`(userModel)
.deleteUserProfile(any(), any<() -> Unit>(), any<(Exception) -> Unit>())

userViewModel.deleteUser("test_id")

assertNull(userViewModel.user.value)
}

@Test
fun deleteUserHasFailed() = runTest {
val expected = userViewModel.user.value
doAnswer { it.getArgument<(Exception) -> Unit>(2)(Exception("failed")) }
.`when`(userModel)
.deleteUserProfile(any(), any<() -> Unit>(), any<(Exception) -> Unit>())

userViewModel.deleteUser("test_id")

assertEquals(expected, userViewModel.user.value)
}
}

0 comments on commit 4ed01c6

Please sign in to comment.