Scan to open on your phone
Point a camera at the QR code - it opens this exact paste, no app needed.
https://codepastes.com/zzgs43veg81c8b43untitledPlain Text
package com.negocio.desinfectantes.data
import android.content.Context
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import kotlinx.coroutines.CancellationException
class SincronizacionWorker(
appContext: Context,
workerParams: WorkerParameters
) : CoroutineWorker(appContext, workerParams) {
override suspend fun doWork(): Result {
return try {
val database = DatabaseProvider.obtenerBaseDatos(
applicationContext
)
val dao = database.operacionPendienteDao()
val ventaService = VentaSyncService()
val catalogoService = CatalogoSyncService()
val moduloService = ModuloSyncService()
// Obtener operaciones pendientes, de la más antigua
// a la más reciente.
val pendientes = dao.obtenerPendientes()
for (operacion in pendientes) {
try {
// Enviar cada operación al servicio correspondiente.
val sincronizada = when (operacion.tipoOperacion) {
"CREAR_VENTA" -> {
ventaService.sincronizarVenta(operacion)
}
"SINCRONIZAR_CATALOGO" -> {
catalogoService.sincronizarCatalogo(operacion)
}
"SINCRONIZAR_MODULO" -> {
moduloService.sincronizarModulo(operacion)
}
else -> {
dao.registrarIntento(
id = operacion.id,
error = "Tipo de operación no reconocido: ${operacion.tipoOperacion}"
)
return Result.failure()
}
}
// Eliminar únicamente cuando Supabase confirme.
if (!sincronizada) {
throw Exception(
"El servidor no confirmó la operación."
)
}
dao.eliminar(operacion.id)
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
dao.registrarIntento(
id = operacion.id,
error = e.message ?: "Error de sincronización"
)
// Reintentar posteriormente.
return Result.retry()
}
}
Result.success()
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Result.retry()
}
}
}
package com.negocio.desinfectantes.data
import io.github.jan.supabase.postgrest.*
import kotlinx.coroutines.CancellationException
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.booleanOrNull
class ModuloSyncService {
private val supabase = SupabaseClientProvider.client
suspend fun sincronizarModulo(
operacion: OperacionPendienteEntity
): Boolean {
return try {
val payload = Json.parseToJsonElement(operacion.payload)
val respuesta = supabase.postgrest.rpc(
function = "sincronizar_modulo",
parameters = buildJsonObject {
put("p_payload", payload)
}
)
val resultado = Json.parseToJsonElement(respuesta.data)
val correcto = resultado.jsonObject["ok"]
?.jsonPrimitive
?.booleanOrNull == true
if (!correcto) {
throw Exception(
"Supabase no confirmó la sincronización del módulo."
)
}
true
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
throw Exception(
"No se pudo sincronizar el módulo: ${e.message}",
e
)
}
}
}
4,354 chars · codepastes.com