Oladayo Babalola
Oladayo Babalola

Reputation: 11

Problem updating integer field in Supabase from kotlin APP

I'm trying to update an int4 field on supabase, but nothing happens when the code runs. The updates in the when block for OnCallDates, GymCallDates and LeaveDates work perfectly, even in the else block, until i add

"staff_role" to staff.staffRole,

or

"unit" to staff.unit,

to the map. I've tried all suggestions from deepseek and chatgpt, same results.

suspend fun editStaffData(context: Context, staff: StaffMember, imageBitmap: Bitmap?, fieldToEdit: String): Result<Unit> {
    return try {
        // If a new imageUri is provided, upload it and update the staff's imageUrl.
        val updatedStaff = if (imageBitmap != null) {
            val imageUrl = uploadImage(context, imageBitmap, staff.id)
            staff.copy(imageUrl = imageUrl)
        } else {
            staff
        }

        when(fieldToEdit) {
            DatabaseField.OnCallDates.name -> {
                val updatedCallDates = updatedStaff.onCallDates.ifEmpty {
                    emptyList()
                }

                withContext(Dispatchers.IO) {
                    val client = getSupabaseClient()
                    client.from("staff")
                        .update(mapOf("on_call_dates" to updatedCallDates)) {
                            filter {
                                eq("id", staff.id)
                            }
                        }
                }
            }

            DatabaseField.GymCallDates.name -> {
                val updatedGymDates = updatedStaff.gymCallDates.ifEmpty {
                    emptyList()
                }

                withContext(Dispatchers.IO) {
                    val client = getSupabaseClient()
                    client.from("staff")
                        .update(mapOf("gym_call_dates" to updatedGymDates)) {
                            filter {
                                eq("id", staff.id)
                            }
                        }
                }
            }

            DatabaseField.LeaveDates.name -> {
                val updatedLeaveDates = updatedStaff.leaveDates.ifEmpty {
                    emptyList()
                }

                withContext(Dispatchers.IO) {
                    val client = getSupabaseClient()
                    client.from("staff")
                        .update(mapOf("leave_dates" to updatedLeaveDates)) {
                            filter {
                                eq("id", staff.id)
                            }
                        }
                }
            }

            else -> {
                withContext(Dispatchers.IO) {
                    val client = getSupabaseClient()

                    val updatedStaffMap = if (imageBitmap != null) {
                        mapOf(
                            "first_name" to staff.firstName,
                            "last_name" to staff.lastName,
                            "phone" to staff.phone,
                            "image_url" to staff.imageUrl
                        )
                    } else{
                        mapOf(
                            "first_name" to staff.firstName,
                            "last_name" to staff.lastName,
                            "phone" to staff.phone
                        )
                    }

                    client.from("staff")
                        .update( updatedStaffMap
                        ) {
                            filter { eq("id", staff.id) }
                        }
                }
            }
        }

        Result.success(Unit)
    } catch (e: Exception) {
        Result.failure(e)
    }
}

I need help, I don't want to have to change the data type to String. This is the dataclass:

    @Serializable
data class StaffMember(
    @SerialName("id") val id: String = "",
    @SerialName("first_name") val firstName: String,
    @SerialName("last_name") val lastName: String,
    @SerialName("staff_role") val staffRole: Int,
    @SerialName("unit") val unit: Int,
    @SerialName("on_call_dates") val onCallDates: List<Pair<Int, List<Int>>> = emptyList(),
    @SerialName("gym_call_dates") val gymCallDates: List<Pair<Int, List<Int>>> = emptyList(),
    @SerialName("leave_dates") val leaveDates: List<Pair<Int, Int>> = emptyList(),
    @SerialName("phone") val phone: String,
    @SerialName("image_url") val imageUrl: String = ""
)

Upvotes: 1

Views: 18

Answers (1)

Oladayo Babalola
Oladayo Babalola

Reputation: 11

Figured out the answer, the problem I'm having is because I'm lumping the role and unit pairs, which are <String,Int> with the others which are <String,String> and the update is not accepting the Map<String,Any> generated in the process. So I've separated out the data for the columns by their data types

val client = getSupabaseClient()
val updatedStringFields = mutableMapOf(
                        "first_name" to staff.firstName,
                        "last_name" to staff.lastName,
                        "phone" to staff.phone
                    )

                    client.from("staff")
                        .update(updatedStringFields) {
                            filter { eq("id", staff.id) }
                        }

                    val updatedIntFields = mapOf(
                        "role" to staff.role,
                        "unit" to staff.unit
                    )

                    client.from("staff")
                        .update(updatedIntFields) {
                            filter { eq("id", staff.id) }
                        }

This works now!

Upvotes: 0

Related Questions