@fayz-ai/plugin-menu 0.11.4 → 0.12.1

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 CHANGED
@@ -1653,8 +1653,129 @@ EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
1653
1653
  --$stmt$;
1654
1654
  END $dp$;
1655
1655
  `;
1656
+ var MIGRATION_002_O_PRODUTO_TEM_COMPLEMENTOS = `-- ---------------------------------------------------------------------------
1657
+ -- 001 \u2014 a product has its own choice groups (V1 \xABComplementos\xBB).
1658
+ --
1659
+ -- The modifier tables already hold name, min, max, required and each option's
1660
+ -- price. Two things were missing for the product tab:
1661
+ -- * a group can be switched off without being deleted (V1 \xABAtivo\xBB);
1662
+ -- * one write that replaces a product's groups and options together. V1
1663
+ -- wrote them one row at a time and a failure halfway left half a set.
1664
+ --
1665
+ -- Idempotent.
1666
+ -- ---------------------------------------------------------------------------
1667
+
1668
+ ALTER TABLE public.plg_menu_modifier_groups
1669
+ ADD COLUMN IF NOT EXISTS is_active boolean NOT NULL DEFAULT true;
1670
+
1671
+ COMMENT ON COLUMN public.plg_menu_modifier_groups.is_active IS
1672
+ 'Off = the group stays configured but is not offered. V1: product_option_groups.active.';
1673
+
1674
+ -- SECURITY INVOKER: the same tenant policies the tables already enforce.
1675
+ CREATE OR REPLACE FUNCTION public.menu_save_product_option_groups(p_product uuid, p_groups jsonb)
1676
+ RETURNS void
1677
+ LANGUAGE plpgsql
1678
+ SECURITY INVOKER
1679
+ SET search_path = ''
1680
+ AS $$
1681
+ DECLARE
1682
+ v_tenant uuid;
1683
+ v_group jsonb;
1684
+ v_item jsonb;
1685
+ v_group_id uuid;
1686
+ v_kept_groups uuid[] := '{}';
1687
+ v_kept_items uuid[];
1688
+ v_item_id uuid;
1689
+ v_removed uuid[];
1690
+ BEGIN
1691
+ SELECT p.tenant_id INTO v_tenant FROM public.products p WHERE p.id = p_product;
1692
+ IF v_tenant IS NULL THEN
1693
+ RAISE EXCEPTION 'product not found' USING ERRCODE = '42501';
1694
+ END IF;
1695
+
1696
+ FOR v_group IN SELECT * FROM jsonb_array_elements(coalesce(p_groups, '[]'::jsonb)) LOOP
1697
+ v_group_id := nullif(v_group->>'id', '')::uuid;
1698
+ IF v_group_id IS NOT NULL AND NOT EXISTS (
1699
+ SELECT 1 FROM public.plg_menu_item_modifier_groups l
1700
+ WHERE l.product_id = p_product AND l.group_id = v_group_id
1701
+ ) THEN
1702
+ -- An id that is not this product's group is treated as new, never as a
1703
+ -- licence to edit someone else's group.
1704
+ v_group_id := NULL;
1705
+ END IF;
1706
+
1707
+ IF v_group_id IS NULL THEN
1708
+ INSERT INTO public.plg_menu_modifier_groups
1709
+ (tenant_id, name, min_selections, max_selections, is_required, is_active, sort_order)
1710
+ VALUES
1711
+ (v_tenant, v_group->>'name', (v_group->>'minSelections')::int, (v_group->>'maxSelections')::int,
1712
+ coalesce((v_group->>'isRequired')::boolean, false), coalesce((v_group->>'isActive')::boolean, true),
1713
+ coalesce((v_group->>'sortOrder')::int, 0))
1714
+ RETURNING id INTO v_group_id;
1715
+ ELSE
1716
+ UPDATE public.plg_menu_modifier_groups SET
1717
+ name = v_group->>'name',
1718
+ min_selections = (v_group->>'minSelections')::int,
1719
+ max_selections = (v_group->>'maxSelections')::int,
1720
+ is_required = coalesce((v_group->>'isRequired')::boolean, false),
1721
+ is_active = coalesce((v_group->>'isActive')::boolean, true),
1722
+ sort_order = coalesce((v_group->>'sortOrder')::int, 0)
1723
+ WHERE id = v_group_id;
1724
+ END IF;
1725
+
1726
+ INSERT INTO public.plg_menu_item_modifier_groups (tenant_id, product_id, group_id, sort_order)
1727
+ VALUES (v_tenant, p_product, v_group_id, coalesce((v_group->>'sortOrder')::int, 0))
1728
+ ON CONFLICT (product_id, group_id) DO UPDATE SET sort_order = EXCLUDED.sort_order;
1729
+
1730
+ v_kept_groups := v_kept_groups || v_group_id;
1731
+ v_kept_items := '{}';
1732
+
1733
+ FOR v_item IN SELECT * FROM jsonb_array_elements(coalesce(v_group->'items', '[]'::jsonb)) LOOP
1734
+ v_item_id := nullif(v_item->>'id', '')::uuid;
1735
+ IF v_item_id IS NOT NULL AND NOT EXISTS (
1736
+ SELECT 1 FROM public.plg_menu_modifiers m WHERE m.id = v_item_id AND m.group_id = v_group_id
1737
+ ) THEN
1738
+ v_item_id := NULL;
1739
+ END IF;
1740
+ IF v_item_id IS NULL THEN
1741
+ INSERT INTO public.plg_menu_modifiers (tenant_id, group_id, name, price_adjustment, is_active, sort_order)
1742
+ VALUES (v_tenant, v_group_id, v_item->>'name', coalesce((v_item->>'priceAdjustment')::numeric, 0),
1743
+ coalesce((v_item->>'isActive')::boolean, true), coalesce((v_item->>'sortOrder')::int, 0))
1744
+ RETURNING id INTO v_item_id;
1745
+ ELSE
1746
+ UPDATE public.plg_menu_modifiers SET
1747
+ name = v_item->>'name',
1748
+ price_adjustment = coalesce((v_item->>'priceAdjustment')::numeric, 0),
1749
+ is_active = coalesce((v_item->>'isActive')::boolean, true),
1750
+ sort_order = coalesce((v_item->>'sortOrder')::int, 0)
1751
+ WHERE id = v_item_id;
1752
+ END IF;
1753
+ v_kept_items := v_kept_items || v_item_id;
1754
+ END LOOP;
1755
+
1756
+ DELETE FROM public.plg_menu_modifiers m
1757
+ WHERE m.group_id = v_group_id AND NOT (m.id = ANY (v_kept_items));
1758
+ END LOOP;
1759
+
1760
+ -- Groups removed from this product: unlink, and drop the group only when no
1761
+ -- other product still uses it.
1762
+ SELECT coalesce(array_agg(l.group_id), '{}') INTO v_removed
1763
+ FROM public.plg_menu_item_modifier_groups l
1764
+ WHERE l.product_id = p_product AND NOT (l.group_id = ANY (v_kept_groups));
1765
+ DELETE FROM public.plg_menu_item_modifier_groups l
1766
+ WHERE l.product_id = p_product AND l.group_id = ANY (v_removed);
1767
+ DELETE FROM public.plg_menu_modifier_groups g
1768
+ WHERE g.id = ANY (v_removed)
1769
+ AND NOT EXISTS (SELECT 1 FROM public.plg_menu_item_modifier_groups l WHERE l.group_id = g.id);
1770
+ END;
1771
+ $$;
1772
+
1773
+ REVOKE ALL ON FUNCTION public.menu_save_product_option_groups(uuid, jsonb) FROM PUBLIC, anon;
1774
+ GRANT EXECUTE ON FUNCTION public.menu_save_product_option_groups(uuid, jsonb) TO authenticated;
1775
+ `;
1656
1776
  var MIGRATIONS = [
1657
- { id: "000_baseline", sql: MIGRATION_000_BASELINE }
1777
+ { id: "000_baseline", sql: MIGRATION_000_BASELINE },
1778
+ { id: "002_o_produto_tem_complementos", sql: MIGRATION_002_O_PRODUTO_TEM_COMPLEMENTOS }
1658
1779
  ];
1659
1780
 
1660
1781
  // src/data/types.ts