@fayz-ai/plugin-tables 0.12.0 → 0.12.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +276 -3
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -3,13 +3,13 @@ import React3 from 'react';
|
|
|
3
3
|
import { createStore } from 'zustand/vanilla';
|
|
4
4
|
import { PluginSettingsPanel, dedup } from '@fayz-ai/admin';
|
|
5
5
|
import { toast } from 'sonner';
|
|
6
|
-
import { cancelReplayResults, fixedDiscountShares, legacyKindOfChannel, openingServiceCharge, registerComponent, resolveDataProvider, getSupabaseClientOptional,
|
|
6
|
+
import { cancelReplayResults, fixedDiscountShares, legacyKindOfChannel, openingServiceCharge, registerComponent, resolveDataProvider, getSupabaseClientOptional, getActiveTenantId, getActiveUnitId } from '@fayz-ai/core';
|
|
7
7
|
import { createFayzClient } from '@fayz-ai/sdk';
|
|
8
8
|
import { settle } from '@fayz-ai/core/orders';
|
|
9
|
-
import { NAME_MAX, TARGET_MAX, TARGET_MIN, ORDER_MAX, ORDER_MIN, readStationRouting, selectedProductCount, refuseStation, routingToWrite, catalogSummary, keyOf, uncategorizedCount, filterCatalog, categoryKey, categoryState, toggleCategory, categoryCount, toggleProduct, createKdsProvider } from '@fayz-ai/kds';
|
|
10
9
|
import { Search, ChevronDown, ChevronRight, Utensils } from 'lucide-react';
|
|
11
|
-
import
|
|
10
|
+
import '@fayz-ai/core/interaction';
|
|
12
11
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
12
|
+
import { Skeleton, Input, Checkbox, Badge } from '@fayz-ai/ui';
|
|
13
13
|
|
|
14
14
|
// src/data/mock.ts
|
|
15
15
|
var uid = 0;
|
|
@@ -3469,6 +3469,279 @@ function createCoreTablesProvider(options) {
|
|
|
3469
3469
|
}
|
|
3470
3470
|
};
|
|
3471
3471
|
}
|
|
3472
|
+
var NO_NAME = {
|
|
3473
|
+
field: "name",
|
|
3474
|
+
title: "Informe o nome do KDS"
|
|
3475
|
+
};
|
|
3476
|
+
var NO_ITEMS = {
|
|
3477
|
+
field: "catalog",
|
|
3478
|
+
title: "Selecione ao menos um item",
|
|
3479
|
+
description: "Marque uma categoria, um produto ou use \u201CTodos os itens\u201D."
|
|
3480
|
+
};
|
|
3481
|
+
function refuseStation(draft) {
|
|
3482
|
+
if (!draft.name.trim()) return NO_NAME;
|
|
3483
|
+
if (draft.role === "produce" && draft.catalogMode === "selected" && draft.selectedProductCount === 0 && !draft.fallback) return NO_ITEMS;
|
|
3484
|
+
return null;
|
|
3485
|
+
}
|
|
3486
|
+
var NAME_MAX = 80;
|
|
3487
|
+
var TARGET_MIN = 1;
|
|
3488
|
+
var TARGET_MAX = 240;
|
|
3489
|
+
var ORDER_MIN = 0;
|
|
3490
|
+
var ORDER_MAX = 999;
|
|
3491
|
+
var UNCATEGORIZED_KEY = "uncategorized";
|
|
3492
|
+
var UNCATEGORIZED_LABEL = "Sem categoria";
|
|
3493
|
+
function categoryKey(category) {
|
|
3494
|
+
return category.id === null ? UNCATEGORIZED_KEY : category.id;
|
|
3495
|
+
}
|
|
3496
|
+
function normalizeTerm(value) {
|
|
3497
|
+
return value.normalize("NFD").replace(/[̀-ͯ]/g, "").toLocaleLowerCase("pt-BR");
|
|
3498
|
+
}
|
|
3499
|
+
function groupCatalog(categories, products) {
|
|
3500
|
+
const grouped = categories.map((c) => ({ id: c.id, name: c.name, products: [] }));
|
|
3501
|
+
const byId = new Map(grouped.map((c) => [c.id, c]));
|
|
3502
|
+
const soltos = { id: null, name: UNCATEGORIZED_LABEL, products: [] };
|
|
3503
|
+
for (const p of products) {
|
|
3504
|
+
const alvo = (p.categoryId ? byId.get(p.categoryId) : void 0) ?? soltos;
|
|
3505
|
+
alvo.products.push({ id: p.id, name: p.name });
|
|
3506
|
+
}
|
|
3507
|
+
if (soltos.products.length > 0) grouped.push(soltos);
|
|
3508
|
+
return grouped;
|
|
3509
|
+
}
|
|
3510
|
+
function filterCatalog(categories, search) {
|
|
3511
|
+
const termo = normalizeTerm(search.trim());
|
|
3512
|
+
if (!termo) {
|
|
3513
|
+
return categories.map((c) => ({ category: c, visibleProducts: c.products, matchedByName: false }));
|
|
3514
|
+
}
|
|
3515
|
+
const saida = [];
|
|
3516
|
+
for (const category of categories) {
|
|
3517
|
+
const porNome = normalizeTerm(category.name).includes(termo);
|
|
3518
|
+
const casaram = category.products.filter((p) => normalizeTerm(p.name).includes(termo));
|
|
3519
|
+
if (!porNome && casaram.length === 0) continue;
|
|
3520
|
+
saida.push({
|
|
3521
|
+
category,
|
|
3522
|
+
visibleProducts: porNome ? category.products : casaram,
|
|
3523
|
+
matchedByName: porNome
|
|
3524
|
+
});
|
|
3525
|
+
}
|
|
3526
|
+
return saida;
|
|
3527
|
+
}
|
|
3528
|
+
function isWholeCategory(category, selection) {
|
|
3529
|
+
return category.id !== null && selection.categoryIds.includes(category.id);
|
|
3530
|
+
}
|
|
3531
|
+
function categoryCount(category, selection) {
|
|
3532
|
+
if (isWholeCategory(category, selection)) return category.products.length;
|
|
3533
|
+
return category.products.filter((p) => selection.productIds.includes(p.id)).length;
|
|
3534
|
+
}
|
|
3535
|
+
function categoryState(category, selection) {
|
|
3536
|
+
if (isWholeCategory(category, selection)) return true;
|
|
3537
|
+
const marcados = categoryCount(category, selection);
|
|
3538
|
+
if (marcados === 0) return false;
|
|
3539
|
+
return marcados === category.products.length ? true : "indeterminate";
|
|
3540
|
+
}
|
|
3541
|
+
function toggleCategory(selection, category, checked) {
|
|
3542
|
+
const produtos = new Set(selection.productIds);
|
|
3543
|
+
const categorias = new Set(selection.categoryIds);
|
|
3544
|
+
if (category.id === null) {
|
|
3545
|
+
for (const p of category.products) checked ? produtos.add(p.id) : produtos.delete(p.id);
|
|
3546
|
+
} else if (checked) {
|
|
3547
|
+
categorias.add(category.id);
|
|
3548
|
+
for (const p of category.products) produtos.delete(p.id);
|
|
3549
|
+
} else {
|
|
3550
|
+
categorias.delete(category.id);
|
|
3551
|
+
for (const p of category.products) produtos.delete(p.id);
|
|
3552
|
+
}
|
|
3553
|
+
return { categoryIds: [...categorias], productIds: [...produtos] };
|
|
3554
|
+
}
|
|
3555
|
+
function toggleProduct(selection, category, productId, checked) {
|
|
3556
|
+
const categorias = new Set(selection.categoryIds);
|
|
3557
|
+
const produtos = new Set(selection.productIds);
|
|
3558
|
+
if (category.id !== null && categorias.has(category.id)) {
|
|
3559
|
+
categorias.delete(category.id);
|
|
3560
|
+
for (const p of category.products) {
|
|
3561
|
+
if (p.id !== productId || checked) produtos.add(p.id);
|
|
3562
|
+
}
|
|
3563
|
+
}
|
|
3564
|
+
if (checked) produtos.add(productId);
|
|
3565
|
+
else produtos.delete(productId);
|
|
3566
|
+
if (category.id !== null && category.products.length > 0 && category.products.every((p) => produtos.has(p.id))) {
|
|
3567
|
+
categorias.add(category.id);
|
|
3568
|
+
for (const p of category.products) produtos.delete(p.id);
|
|
3569
|
+
}
|
|
3570
|
+
return { categoryIds: [...categorias], productIds: [...produtos] };
|
|
3571
|
+
}
|
|
3572
|
+
function selectedProductCount(categories, selection, options) {
|
|
3573
|
+
if (options?.role === "expo") {
|
|
3574
|
+
return categories.reduce((s, c) => s + c.products.length, 0);
|
|
3575
|
+
}
|
|
3576
|
+
if (options?.catalogMode === "all") {
|
|
3577
|
+
return categories.filter((c) => c.id !== null).reduce((s, c) => s + c.products.length, 0);
|
|
3578
|
+
}
|
|
3579
|
+
const ids = new Set(selection.productIds);
|
|
3580
|
+
for (const c of categories) {
|
|
3581
|
+
if (isWholeCategory(c, selection)) for (const p of c.products) ids.add(p.id);
|
|
3582
|
+
}
|
|
3583
|
+
return ids.size;
|
|
3584
|
+
}
|
|
3585
|
+
function uncategorizedCount(categories) {
|
|
3586
|
+
return categories.filter((c) => c.id === null).reduce((s, c) => s + c.products.length, 0);
|
|
3587
|
+
}
|
|
3588
|
+
function expandToWholeCatalog(categories) {
|
|
3589
|
+
return {
|
|
3590
|
+
categoryIds: categories.map((c) => c.id).filter((id) => id !== null),
|
|
3591
|
+
productIds: []
|
|
3592
|
+
};
|
|
3593
|
+
}
|
|
3594
|
+
function coversWholeCatalog(categories, selection) {
|
|
3595
|
+
const comId = categories.filter((c) => c.id !== null);
|
|
3596
|
+
if (comId.length === 0) return false;
|
|
3597
|
+
if (selection.productIds.length > 0) return false;
|
|
3598
|
+
const marcadas = new Set(selection.categoryIds);
|
|
3599
|
+
return comId.every((c) => marcadas.has(c.id));
|
|
3600
|
+
}
|
|
3601
|
+
var STATIONS = "plg_kds_stations";
|
|
3602
|
+
var ROUTING = "plg_kds_routing";
|
|
3603
|
+
function keyOf(label) {
|
|
3604
|
+
const base = label.normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
3605
|
+
return /^[a-z]/.test(base) ? base.slice(0, 40) : `kds-${base}`.slice(0, 40);
|
|
3606
|
+
}
|
|
3607
|
+
async function paginate(page, size = 1e3) {
|
|
3608
|
+
const todos = [];
|
|
3609
|
+
for (let from = 0; ; from += size) {
|
|
3610
|
+
const { data, error } = await page(from, from + size - 1);
|
|
3611
|
+
if (error) throw new Error(error.message);
|
|
3612
|
+
todos.push(...data ?? []);
|
|
3613
|
+
if (!data || data.length < size) break;
|
|
3614
|
+
}
|
|
3615
|
+
return todos;
|
|
3616
|
+
}
|
|
3617
|
+
function createKdsProvider(opts) {
|
|
3618
|
+
const db = () => {
|
|
3619
|
+
const c = getSupabaseClientOptional();
|
|
3620
|
+
if (!c) throw new Error("Sem conex\xE3o com o banco.");
|
|
3621
|
+
return c;
|
|
3622
|
+
};
|
|
3623
|
+
const tenant = () => {
|
|
3624
|
+
const t = opts.tenantId();
|
|
3625
|
+
if (!t) throw new Error("Sem inquilino ativo.");
|
|
3626
|
+
return t;
|
|
3627
|
+
};
|
|
3628
|
+
const syncRouting = async (stationId, input) => {
|
|
3629
|
+
const tenantId = tenant();
|
|
3630
|
+
const unitId = input.unitId !== void 0 ? input.unitId : opts.unitId?.() ?? null;
|
|
3631
|
+
const { error: limpou } = await db().from(ROUTING).delete().eq("station_id", stationId);
|
|
3632
|
+
if (limpou) throw new Error(limpou.message);
|
|
3633
|
+
const novas = [];
|
|
3634
|
+
const comum = { tenant_id: tenantId, unit_id: unitId, station_id: stationId };
|
|
3635
|
+
if (input.fallback) novas.push({ ...comum, match_kind: "fallback" });
|
|
3636
|
+
for (const c of input.categoryIds) novas.push({ ...comum, match_kind: "category", category_id: c });
|
|
3637
|
+
for (const p of input.productIds) novas.push({ ...comum, match_kind: "product", product_id: p });
|
|
3638
|
+
if (novas.length > 0) {
|
|
3639
|
+
const { error } = await db().from(ROUTING).insert(novas);
|
|
3640
|
+
if (error) throw new Error(error.message);
|
|
3641
|
+
}
|
|
3642
|
+
};
|
|
3643
|
+
return {
|
|
3644
|
+
async listCatalog() {
|
|
3645
|
+
const daUnidade = (q) => {
|
|
3646
|
+
const u = opts.unitId?.();
|
|
3647
|
+
return u ? q.or(`unit_id.eq.${u},unit_id.is.null`) : q;
|
|
3648
|
+
};
|
|
3649
|
+
const [cats, prods] = await Promise.all([
|
|
3650
|
+
paginate((from, to) => daUnidade(db().from("categories").select("id, name").eq("is_active", true)).order("sort_order").order("name").range(from, to)),
|
|
3651
|
+
paginate((from, to) => daUnidade(db().from("products").select("id, name, category_id").eq("is_active", true)).order("name").range(from, to))
|
|
3652
|
+
]);
|
|
3653
|
+
return groupCatalog(
|
|
3654
|
+
cats.map((c) => ({ id: c.id, name: c.name ?? "" })),
|
|
3655
|
+
prods.map((p) => ({ id: p.id, name: p.name ?? "", categoryId: p.category_id ?? null }))
|
|
3656
|
+
);
|
|
3657
|
+
},
|
|
3658
|
+
async listStations() {
|
|
3659
|
+
const { data, error } = await db().from(STATIONS).select("id, key, label, role, sla_minutes, sort_order, is_active").order("sort_order").order("label");
|
|
3660
|
+
if (error) throw new Error(error.message);
|
|
3661
|
+
return (data ?? []).map((r) => ({
|
|
3662
|
+
id: r.id,
|
|
3663
|
+
key: r.key,
|
|
3664
|
+
label: r.label ?? "",
|
|
3665
|
+
role: r.role === "expo" ? "expo" : "produce",
|
|
3666
|
+
slaMinutes: r.sla_minutes ?? null,
|
|
3667
|
+
sortOrder: r.sort_order ?? 0,
|
|
3668
|
+
isActive: r.is_active !== false
|
|
3669
|
+
}));
|
|
3670
|
+
},
|
|
3671
|
+
async listRouting() {
|
|
3672
|
+
const { data, error } = await db().from(ROUTING).select("id, station_id, match_kind, category_id, product_id");
|
|
3673
|
+
if (error) throw new Error(error.message);
|
|
3674
|
+
return (data ?? []).map((r) => ({
|
|
3675
|
+
id: r.id,
|
|
3676
|
+
stationId: r.station_id,
|
|
3677
|
+
kind: r.match_kind,
|
|
3678
|
+
categoryId: r.category_id ?? null,
|
|
3679
|
+
productId: r.product_id ?? null
|
|
3680
|
+
}));
|
|
3681
|
+
},
|
|
3682
|
+
async saveStation(input) {
|
|
3683
|
+
const tenantId = tenant();
|
|
3684
|
+
const unitId = opts.unitId?.() ?? null;
|
|
3685
|
+
const linha = {
|
|
3686
|
+
tenant_id: tenantId,
|
|
3687
|
+
unit_id: unitId,
|
|
3688
|
+
label: input.label.trim(),
|
|
3689
|
+
role: input.role,
|
|
3690
|
+
sla_minutes: input.slaMinutes,
|
|
3691
|
+
sort_order: input.sortOrder ?? 0
|
|
3692
|
+
};
|
|
3693
|
+
let id = input.id;
|
|
3694
|
+
if (id) {
|
|
3695
|
+
const { error } = await db().from(STATIONS).update(linha).eq("id", id);
|
|
3696
|
+
if (error) throw new Error(error.message);
|
|
3697
|
+
} else {
|
|
3698
|
+
const { data, error } = await db().from(STATIONS).insert({ ...linha, key: keyOf(input.label) }).select("id").single();
|
|
3699
|
+
if (error) throw new Error(error.message);
|
|
3700
|
+
id = data.id;
|
|
3701
|
+
}
|
|
3702
|
+
await syncRouting(id, {
|
|
3703
|
+
fallback: input.fallback,
|
|
3704
|
+
categoryIds: input.catalogMode === "all" ? input.allCategoryIds ?? [] : input.categoryIds,
|
|
3705
|
+
// Produto solto só no modo específico: no «todos» ele já veio pela
|
|
3706
|
+
// categoria, e a linha repetida não acrescenta nada.
|
|
3707
|
+
productIds: input.catalogMode === "selected" ? input.productIds : [],
|
|
3708
|
+
unitId
|
|
3709
|
+
});
|
|
3710
|
+
return id;
|
|
3711
|
+
},
|
|
3712
|
+
syncRouting,
|
|
3713
|
+
async setStationActive(id, active) {
|
|
3714
|
+
const { error } = await db().from(STATIONS).update({ is_active: active }).eq("id", id);
|
|
3715
|
+
if (error) throw new Error(error.message);
|
|
3716
|
+
}
|
|
3717
|
+
};
|
|
3718
|
+
}
|
|
3719
|
+
function catalogSummary(selection, fallback, mode) {
|
|
3720
|
+
if (mode === "all") {
|
|
3721
|
+
return fallback ? "Todos os itens, e o que ningu\xE9m roteou." : "Todos os itens.";
|
|
3722
|
+
}
|
|
3723
|
+
const partes = [];
|
|
3724
|
+
const c = selection.categoryIds.length;
|
|
3725
|
+
const p = selection.productIds.length;
|
|
3726
|
+
if (c > 0) partes.push(`${c} categoria${c > 1 ? "s" : ""}`);
|
|
3727
|
+
if (p > 0) partes.push(`${p} produto${p > 1 ? "s" : ""}`);
|
|
3728
|
+
if (partes.length === 0) {
|
|
3729
|
+
return fallback ? "S\xF3 o que ningu\xE9m roteou (destino padr\xE3o)." : "Nenhum item associado.";
|
|
3730
|
+
}
|
|
3731
|
+
return `${partes.join(" e ")}${fallback ? ", e o que ningu\xE9m roteou." : "."}`;
|
|
3732
|
+
}
|
|
3733
|
+
function readStationRouting(rows, catalog) {
|
|
3734
|
+
const catalogo = {
|
|
3735
|
+
categoryIds: rows.filter((r) => r.match_kind === "category" && r.category_id).map((r) => r.category_id),
|
|
3736
|
+
productIds: rows.filter((r) => r.match_kind === "product" && r.product_id).map((r) => r.product_id)
|
|
3737
|
+
};
|
|
3738
|
+
const fallback = rows.some((r) => r.match_kind === "fallback");
|
|
3739
|
+
const catalogMode = catalog && coversWholeCatalog(catalog, catalogo) ? "all" : "selected";
|
|
3740
|
+
return { fallback, catalogMode, catalogo, catalogSummary: catalogSummary(catalogo, fallback, catalogMode) };
|
|
3741
|
+
}
|
|
3742
|
+
function routingToWrite(mode, selection, catalog) {
|
|
3743
|
+
return mode === "all" ? expandToWholeCatalog(catalog) : selection;
|
|
3744
|
+
}
|
|
3472
3745
|
function asSelection(value) {
|
|
3473
3746
|
const v = value;
|
|
3474
3747
|
return {
|