diff --git a/app/src/main/java/com/android/periodpals/model/shared/SharedData.kt b/app/src/main/java/com/android/periodpals/model/shared/SharedData.kt new file mode 100644 index 000000000..ab7fdff22 --- /dev/null +++ b/app/src/main/java/com/android/periodpals/model/shared/SharedData.kt @@ -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) diff --git a/app/src/main/java/com/android/periodpals/model/user/UserModel.kt b/app/src/main/java/com/android/periodpals/model/user/UserModel.kt index 2139b733b..9463ee25b 100644 --- a/app/src/main/java/com/android/periodpals/model/user/UserModel.kt +++ b/app/src/main/java/com/android/periodpals/model/user/UserModel.kt @@ -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 + ) } diff --git a/app/src/main/java/com/android/periodpals/model/user/UserModelSupabase.kt b/app/src/main/java/com/android/periodpals/model/user/UserModelSupabase.kt index 23738ef89..833e34458 100644 --- a/app/src/main/java/com/android/periodpals/model/user/UserModelSupabase.kt +++ b/app/src/main/java/com/android/periodpals/model/user/UserModelSupabase.kt @@ -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) + } + } } diff --git a/app/src/main/java/com/android/periodpals/model/user/UserViewModel.kt b/app/src/main/java/com/android/periodpals/model/user/UserViewModel.kt index 8e85780ac..9a450c87c 100644 --- a/app/src/main/java/com/android/periodpals/model/user/UserViewModel.kt +++ b/app/src/main/java/com/android/periodpals/model/user/UserViewModel.kt @@ -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) + }) + } + } } diff --git a/app/src/test/java/com/android/periodpals/model/user/UserModelSupabaseTest.kt b/app/src/test/java/com/android/periodpals/model/user/UserModelSupabaseTest.kt index 874b9f24e..65e763533 100644 --- a/app/src/test/java/com/android/periodpals/model/user/UserModelSupabaseTest.kt +++ b/app/src/test/java/com/android/periodpals/model/user/UserModelSupabaseTest.kt @@ -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) @@ -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) + } + } } diff --git a/app/src/test/java/com/android/periodpals/model/user/UserViewModelTest.kt b/app/src/test/java/com/android/periodpals/model/user/UserViewModelTest.kt index 54a6c5f39..fb0f31108 100644 --- a/app/src/test/java/com/android/periodpals/model/user/UserViewModelTest.kt +++ b/app/src/test/java/com/android/periodpals/model/user/UserViewModelTest.kt @@ -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) + } }