Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c4dda7e2e5 | |||
| 662f3681a5 | |||
| a56d7188f2 | |||
| 7e1ee6bc04 | |||
| 6a8881d41d | |||
| 3b3eb1a4cc | |||
| c7109b1017 | |||
| f45d1e8170 | |||
| 8ea5ba771a | |||
| 84578fe43d | |||
| 3e94789cb0 | |||
| 292d0b85d9 | |||
| a8edab029d | |||
| cbaef8772d | |||
| 80740afbf0 | |||
| 7134c85bb7 | |||
| 4a157d27cb | |||
| a0f2f91131 | |||
| 15f141c191 | |||
| 96a84ec194 | |||
| 7d0c416248 | |||
| 60ec60bf29 | |||
| 6d9e28a6f2 | |||
| ecb9ce3926 | |||
| de5d5aaafb | |||
| a11db54ffc | |||
| 674caf2b7a | |||
| bdfd54e0cf | |||
| 9c64c06ed9 |
@@ -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)
|
||||||
@@ -193,10 +194,10 @@ impl SupabaseClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Busca um fechamento por UUID (campo "id" do registro).
|
/// Busca um fechamento pelo campo "id" (UUID externo).
|
||||||
pub fn buscar_por_uuid(&self, uuid: &str) -> Result<Option<Value>, SupabaseError> {
|
pub fn buscar_por_uuid(&self, uuid: &str) -> Result<Option<Value>, SupabaseError> {
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"{}/rest/v1/fechamentos_web?uuid=eq.{}&select=*",
|
"{}/rest/v1/fechamentos_web?id=eq.{}&select=*",
|
||||||
self.base_url, uuid
|
self.base_url, uuid
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -247,12 +248,14 @@ impl SupabaseClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Atualiza campos específicos de um fechamento pelo UUID (PATCH).
|
/// Atualiza campos específicos de um fechamento pelo id numérico (pk) ou UUID.
|
||||||
pub fn atualizar_fechamento(&self, uuid: &str, updates: &serde_json::Value) -> Result<Value, SupabaseError> {
|
pub fn atualizar_fechamento(&self, id: Option<i64>, uuid: &str, updates: &serde_json::Value) -> Result<Value, SupabaseError> {
|
||||||
let url = format!(
|
// Se temos id numerico, usa id=eq.; senao usa id=eq.UUID
|
||||||
"{}/rest/v1/fechamentos_web?uuid=eq.{}",
|
let url = if let Some(num) = id {
|
||||||
self.base_url, uuid
|
format!("{}/rest/v1/fechamentos_web?id=eq.{}", self.base_url, num)
|
||||||
);
|
} else {
|
||||||
|
format!("{}/rest/v1/fechamentos_web?id=eq.{}", self.base_url, uuid)
|
||||||
|
};
|
||||||
|
|
||||||
let resp = self
|
let resp = self
|
||||||
.http
|
.http
|
||||||
@@ -262,7 +265,7 @@ impl SupabaseClient {
|
|||||||
.send()?;
|
.send()?;
|
||||||
|
|
||||||
if resp.status().is_success() {
|
if resp.status().is_success() {
|
||||||
Ok(serde_json::json!({ "ok": true, "uuid": uuid }))
|
Ok(serde_json::json!({ "ok": true, "id": id.unwrap_or(0) }))
|
||||||
} else {
|
} else {
|
||||||
let status = resp.status();
|
let status = resp.status();
|
||||||
let body = resp.text().unwrap_or_default();
|
let body = resp.text().unwrap_or_default();
|
||||||
@@ -274,10 +277,11 @@ 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 url = format!(
|
let url = format!(
|
||||||
"{}/rest/v1/fechamentos_web?loja=eq.{}&order=data.desc,atualizado_em.desc&limit={}&select=id,uuid,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),
|
urlencoding::encode(&loja_normalized),
|
||||||
limite,
|
limite,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -393,6 +397,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);
|
||||||
@@ -436,9 +482,9 @@ pub fn sb_listar_recentes(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn sb_buscar_por_id(
|
pub fn sb_buscar_por_id(
|
||||||
sb: State<'_, SupabaseClient>,
|
sb: State<'_, SupabaseClient>,
|
||||||
id: String,
|
id: i64,
|
||||||
) -> Result<Option<serde_json::Value>, SupabaseError> {
|
) -> Result<Option<serde_json::Value>, SupabaseError> {
|
||||||
sb.buscar_por_uuid(&id)
|
sb.buscar_por_id(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
@@ -461,10 +507,13 @@ pub fn sb_buscar_por_id_fechamento(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn sb_atualizar_fechamento(
|
pub fn sb_atualizar_fechamento(
|
||||||
sb: State<'_, SupabaseClient>,
|
sb: State<'_, SupabaseClient>,
|
||||||
uuid: String,
|
id: String,
|
||||||
updates: serde_json::Value,
|
updates: serde_json::Value,
|
||||||
) -> Result<serde_json::Value, SupabaseError> {
|
) -> Result<serde_json::Value, SupabaseError> {
|
||||||
sb.atualizar_fechamento(&uuid, &updates)
|
// Tenta primeiro como i64 (id numerico), depois como String (UUID)
|
||||||
|
let id_i64 = id.parse::<i64>().ok();
|
||||||
|
let id_str = &id;
|
||||||
|
sb.atualizar_fechamento(id_i64, id_str, &updates)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
|
|||||||
+906
-116
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user