Files
botino/public/lib/api.js
Lucas Tettamanti 23c3d44490 audit and sync
2026-01-18 19:00:49 -03:00

188 lines
6.2 KiB
JavaScript

export const api = {
async conversations({ q = "", status = "", state = "" } = {}) {
const u = new URL("/conversations", location.origin);
if (q) u.searchParams.set("q", q);
if (status) u.searchParams.set("status", status);
if (state) u.searchParams.set("state", state);
return fetch(u).then(r => r.json());
},
async deleteConversation(chat_id) {
if (!chat_id) throw new Error("chat_id_required");
return fetch(`/conversations/${encodeURIComponent(chat_id)}`, { method: "DELETE" }).then(r => r.json());
},
async users({ q = "", limit = 200 } = {}) {
const u = new URL("/users", location.origin);
if (q) u.searchParams.set("q", String(q));
u.searchParams.set("limit", String(limit));
return fetch(u).then(r => r.json());
},
async deleteUser(chat_id, { deleteWoo = false } = {}) {
if (!chat_id) throw new Error("chat_id_required");
const u = new URL(`/users/${encodeURIComponent(chat_id)}`, location.origin);
if (deleteWoo) u.searchParams.set("deleteWoo", "1");
return fetch(u, { method: "DELETE" }).then(r => r.json());
},
async messages({ chat_id, limit = 200 } = {}) {
const u = new URL("/messages", location.origin);
if (chat_id) u.searchParams.set("chat_id", chat_id);
u.searchParams.set("limit", String(limit));
return fetch(u).then(r => r.json());
},
async runs({ chat_id, limit = 1 } = {}) {
const u = new URL("/runs", location.origin);
if (chat_id) u.searchParams.set("chat_id", chat_id);
u.searchParams.set("limit", String(limit));
return fetch(u).then(r => r.json());
},
async runById(run_id) {
if (!run_id) return null;
return fetch(`/runs/${encodeURIComponent(run_id)}`).then(r => r.json());
},
async simEvolution(payload) {
return fetch("/webhook/evolution", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
}).then(r => r.json());
},
async retryLast(chat_id) {
if (!chat_id) throw new Error("chat_id_required");
return fetch(`/conversations/${encodeURIComponent(chat_id)}/retry-last`, { method: "POST" }).then(r => r.json());
},
// Products CRUD
async products({ q = "", limit = 2000, offset = 0 } = {}) {
const u = new URL("/products", location.origin);
if (q) u.searchParams.set("q", q);
u.searchParams.set("limit", String(limit));
u.searchParams.set("offset", String(offset));
return fetch(u).then(r => r.json());
},
async productById(id) {
if (!id) return null;
return fetch(`/products/${encodeURIComponent(id)}`).then(r => r.json());
},
async syncProducts() {
return fetch("/products/sync", { method: "POST" }).then(r => r.json());
},
async updateProductUnit(wooProductId, { sell_unit }) {
return fetch(`/products/${encodeURIComponent(wooProductId)}/unit`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ sell_unit }),
}).then(r => r.json());
},
async updateProduct(wooProductId, { sell_unit, categories }) {
return fetch(`/products/${encodeURIComponent(wooProductId)}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ sell_unit, categories }),
}).then(r => r.json());
},
async updateProductsUnit(wooProductIds, { sell_unit }) {
return fetch(`/products/bulk/unit`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ woo_product_ids: wooProductIds, sell_unit }),
}).then(r => r.json());
},
// Aliases CRUD
async aliases({ q = "", woo_product_id = null, limit = 200 } = {}) {
const u = new URL("/aliases", location.origin);
if (q) u.searchParams.set("q", q);
if (woo_product_id) u.searchParams.set("woo_product_id", String(woo_product_id));
u.searchParams.set("limit", String(limit));
return fetch(u).then(r => r.json());
},
async createAlias(data) {
return fetch("/aliases", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
}).then(r => r.json());
},
async updateAlias(alias, data) {
return fetch(`/aliases/${encodeURIComponent(alias)}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
}).then(r => r.json());
},
async deleteAlias(alias) {
return fetch(`/aliases/${encodeURIComponent(alias)}`, { method: "DELETE" }).then(r => r.json());
},
// Recommendations CRUD
async recommendations({ q = "", limit = 200 } = {}) {
const u = new URL("/recommendations", location.origin);
if (q) u.searchParams.set("q", q);
u.searchParams.set("limit", String(limit));
return fetch(u).then(r => r.json());
},
async getRecommendation(id) {
if (!id) return null;
return fetch(`/recommendations/${encodeURIComponent(id)}`).then(r => r.json());
},
async createRecommendation(data) {
return fetch("/recommendations", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
}).then(r => r.json());
},
async updateRecommendation(id, data) {
return fetch(`/recommendations/${encodeURIComponent(id)}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
}).then(r => r.json());
},
async deleteRecommendation(id) {
return fetch(`/recommendations/${encodeURIComponent(id)}`, { method: "DELETE" }).then(r => r.json());
},
// Quantities (cantidades por producto/evento/persona)
async listQuantities() {
return fetch("/quantities").then(r => r.json());
},
async getProductQuantities(wooProductId) {
if (!wooProductId) return { rules: [] };
return fetch(`/quantities/${encodeURIComponent(wooProductId)}`).then(r => r.json());
},
async saveProductQuantities(wooProductId, rules) {
return fetch(`/quantities/${encodeURIComponent(wooProductId)}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ rules }),
}).then(r => r.json());
},
// Sync de emergencia desde WooCommerce
async syncFromWoo() {
return fetch("/products/sync-from-woo", { method: "POST" }).then(r => r.json());
},
};