Compare commits
27 Commits
v1.3.39
...
v1.4.1-fix
| Author | SHA1 | Date | |
|---|---|---|---|
| cb48afeb9c | |||
| 2b8ff711ed | |||
| 9a4ccabd1b | |||
| 32728eec37 | |||
| 905ca253d7 | |||
| d89b79a3f8 | |||
| c4dda7e2e5 | |||
| 662f3681a5 | |||
| a56d7188f2 | |||
| 7e1ee6bc04 | |||
| 6a8881d41d | |||
| 3b3eb1a4cc | |||
| c7109b1017 | |||
| f45d1e8170 | |||
| 8ea5ba771a | |||
| 84578fe43d | |||
| 3e94789cb0 | |||
| 292d0b85d9 | |||
| a8edab029d | |||
| cbaef8772d | |||
| 80740afbf0 | |||
| 7134c85bb7 | |||
| 4a157d27cb | |||
| a0f2f91131 | |||
| 15f141c191 | |||
| 96a84ec194 | |||
| 7d0c416248 |
@@ -140,6 +140,7 @@ impl SupabaseClient {
|
|||||||
"observacoes": estado.get("observacoes").and_then(|v| v.as_str()).unwrap_or(""),
|
"observacoes": estado.get("observacoes").and_then(|v| v.as_str()).unwrap_or(""),
|
||||||
"clientes": estado.get("clientes").and_then(|v| v.as_i64()).unwrap_or(0),
|
"clientes": estado.get("clientes").and_then(|v| v.as_i64()).unwrap_or(0),
|
||||||
"frango": estado.get("frango").and_then(|v| v.as_i64()).unwrap_or(0),
|
"frango": estado.get("frango").and_then(|v| v.as_i64()).unwrap_or(0),
|
||||||
|
"vendas": estado.get("vendas").and_then(|v| v.as_f64()).unwrap_or(0.0),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Primeiro tenta INSERT (upsert via Prefer)
|
// Primeiro tenta INSERT (upsert via Prefer)
|
||||||
@@ -247,27 +248,37 @@ impl SupabaseClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Atualiza campos específicos de um fechamento pelo id numérico (pk) ou UUID.
|
/// Atualiza campos específicos de um fechamento pelo id_fechamento.
|
||||||
pub fn atualizar_fechamento(&self, id: Option<i64>, uuid: &str, updates: &serde_json::Value) -> Result<Value, SupabaseError> {
|
pub fn atualizar_fechamento(&self, id_fechamento: &str, updates: &serde_json::Value) -> Result<Value, SupabaseError> {
|
||||||
// Se temos id numerico, usa id=eq.; senao usa id=eq.UUID
|
let url = format!(
|
||||||
let url = if let Some(num) = id {
|
"{}/rest/v1/fechamentos_web?id_fechamento=eq.{}",
|
||||||
format!("{}/rest/v1/fechamentos_web?id=eq.{}", self.base_url, num)
|
self.base_url,
|
||||||
} else {
|
urlencoding::encode(id_fechamento)
|
||||||
format!("{}/rest/v1/fechamentos_web?id=eq.{}", self.base_url, uuid)
|
);
|
||||||
};
|
|
||||||
|
// Log do que está sendo enviado (debug)
|
||||||
|
log::info!("[PATCH] url={} body={}", url, updates);
|
||||||
|
|
||||||
let resp = self
|
let resp = self
|
||||||
.http
|
.http
|
||||||
.patch(&url)
|
.patch(&url)
|
||||||
.headers(self.headers(true))
|
.headers(self.headers(true))
|
||||||
|
.header("Prefer", "return=representation")
|
||||||
.json(updates)
|
.json(updates)
|
||||||
.send()?;
|
.send()?;
|
||||||
|
|
||||||
if resp.status().is_success() {
|
let status = resp.status();
|
||||||
Ok(serde_json::json!({ "ok": true, "id": id.unwrap_or(0) }))
|
if status.is_success() {
|
||||||
|
// Ler a resposta real do Supabase (retorna o registro atualizado)
|
||||||
|
if let Ok(ret) = resp.json::<serde_json::Value>() {
|
||||||
|
log::info!("[PATCH] sucesso — resposta: {}", ret);
|
||||||
|
} else {
|
||||||
|
log::warn!("[PATCH] sucesso mas não conseguiu ler resposta");
|
||||||
|
}
|
||||||
|
Ok(serde_json::json!({ "ok": true, "id_fechamento": id_fechamento }))
|
||||||
} else {
|
} else {
|
||||||
let status = resp.status();
|
|
||||||
let body = resp.text().unwrap_or_default();
|
let body = resp.text().unwrap_or_default();
|
||||||
|
log::error!("[PATCH] HTTP {} body: {}", status.as_u16(), body);
|
||||||
Err(SupabaseError::Api(format!(
|
Err(SupabaseError::Api(format!(
|
||||||
"HTTP {}: {}",
|
"HTTP {}: {}",
|
||||||
status.as_u16(),
|
status.as_u16(),
|
||||||
@@ -278,7 +289,7 @@ impl SupabaseClient {
|
|||||||
pub fn listar_recentes(&self, loja: &str, limite: i64) -> Result<Vec<Value>, SupabaseError> {
|
pub fn listar_recentes(&self, loja: &str, limite: i64) -> Result<Vec<Value>, SupabaseError> {
|
||||||
let loja_normalized = sem_acento(&loja.to_lowercase());
|
let loja_normalized = sem_acento(&loja.to_lowercase());
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"{}/rest/v1/fechamentos_web?loja=eq.{}&order=data.desc,atualizado_em.desc&limit={}&select=id,id_fechamento,data,operador,turno,saldo_troco,saldo_esperado,fechamento_dinheiro,fechamento_cartoes,fechamento_areceber,diferenca,status_diferenca,observacoes,atualizado_em",
|
"{}/rest/v1/fechamentos_web?loja=eq.{}&order=data.desc,atualizado_em.desc&limit={}&select=id,id_fechamento,data,operador,turno,saldo_troco,saldo_esperado,fechamento_dinheiro,fechamento_cartoes,fechamento_areceber,diferenca,status_diferenca,observacoes,atualizado_em,dados",
|
||||||
self.base_url,
|
self.base_url,
|
||||||
urlencoding::encode(&loja_normalized),
|
urlencoding::encode(&loja_normalized),
|
||||||
limite,
|
limite,
|
||||||
@@ -396,6 +407,48 @@ impl SupabaseClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Tenta forçar PostgREST a recarregar schema com novas colunas (vendas, edited_at, edited_by).
|
||||||
|
/// Estratégia: INSERT com novas colunas → se PGRST204, espera 1.5s → retry.
|
||||||
|
/// Fire-and-forget — falha não bloqueia.
|
||||||
|
pub fn run_migration(&self) {
|
||||||
|
let payload = serde_json::json!({
|
||||||
|
"id_fechamento": "_mig_probe_1900",
|
||||||
|
"loja": "_probe_",
|
||||||
|
"data": "1900-01-01",
|
||||||
|
"operador": "_probe_",
|
||||||
|
"vendas": 0,
|
||||||
|
"edited_at": null,
|
||||||
|
"edited_by": null
|
||||||
|
});
|
||||||
|
let r1 = self.http
|
||||||
|
.post(&format!("{}/rest/v1/fechamentos_web", self.base_url))
|
||||||
|
.headers(self.headers(true))
|
||||||
|
.header("Prefer", "resolution=merge-duplicates")
|
||||||
|
.json(&payload)
|
||||||
|
.send();
|
||||||
|
if let Ok(resp) = r1 {
|
||||||
|
if resp.status().as_u16() == 400 {
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(1500));
|
||||||
|
let _ = self.http
|
||||||
|
.post(&format!("{}/rest/v1/fechamentos_web", self.base_url))
|
||||||
|
.headers(self.headers(true))
|
||||||
|
.header("Prefer", "resolution=merge-duplicates")
|
||||||
|
.json(&serde_json::json!({
|
||||||
|
"id_fechamento": "_mig_probe_1900",
|
||||||
|
"vendas": 0,
|
||||||
|
"edited_at": null,
|
||||||
|
"edited_by": null
|
||||||
|
}))
|
||||||
|
.send();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Limpa probe
|
||||||
|
let _ = self.http
|
||||||
|
.delete(&format!("{}/rest/v1/fechamentos_web?id_fechamento=eq._mig_probe_1900", self.base_url))
|
||||||
|
.headers(self.headers(true))
|
||||||
|
.send();
|
||||||
|
}
|
||||||
|
|
||||||
/// Verifica se está online.
|
/// Verifica se está online.
|
||||||
pub fn health_check(&self) -> bool {
|
pub fn health_check(&self) -> bool {
|
||||||
let url = format!("{}/rest/v1/?limit=0", self.base_url);
|
let url = format!("{}/rest/v1/?limit=0", self.base_url);
|
||||||
@@ -461,16 +514,13 @@ pub fn sb_buscar_por_id_fechamento(
|
|||||||
sb.buscar_por_id_fechamento(&id_fechamento)
|
sb.buscar_por_id_fechamento(&id_fechamento)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command(rename_all = "snake_case")]
|
||||||
pub fn sb_atualizar_fechamento(
|
pub fn sb_atualizar_fechamento(
|
||||||
sb: State<'_, SupabaseClient>,
|
sb: State<'_, SupabaseClient>,
|
||||||
id: String,
|
id_fechamento: String,
|
||||||
updates: serde_json::Value,
|
updates: serde_json::Value,
|
||||||
) -> Result<serde_json::Value, SupabaseError> {
|
) -> Result<serde_json::Value, SupabaseError> {
|
||||||
// Tenta primeiro como i64 (id numerico), depois como String (UUID)
|
sb.atualizar_fechamento(&id_fechamento, &updates)
|
||||||
let id_i64 = id.parse::<i64>().ok();
|
|
||||||
let id_str = &id;
|
|
||||||
sb.atualizar_fechamento(id_i64, id_str, &updates)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
|
|||||||
+855
-151
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user