@fayz-ai/plugin-orders 0.14.0 → 0.14.2
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 +84 -2
- package/dist/index.js.map +1 -1
- package/dist/migrations/index.d.ts +2 -1
- package/dist/migrations/index.d.ts.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -653,7 +653,7 @@ BEGIN
|
|
|
653
653
|
RAISE EXCEPTION 'orders_ingest_external: order needs provider and externalId' USING ERRCODE = '22023';
|
|
654
654
|
END IF;
|
|
655
655
|
IF p_unit IS NOT NULL AND NOT EXISTS (
|
|
656
|
-
SELECT 1 FROM
|
|
656
|
+
SELECT 1 FROM app.units u WHERE u.id = p_unit AND u.tenant_id = p_tenant) THEN
|
|
657
657
|
RAISE EXCEPTION 'orders_ingest_external: unit % is not of tenant %', p_unit, p_tenant USING ERRCODE = '22023';
|
|
658
658
|
END IF;
|
|
659
659
|
|
|
@@ -803,8 +803,90 @@ BEGIN
|
|
|
803
803
|
END IF;
|
|
804
804
|
END $$;
|
|
805
805
|
`;
|
|
806
|
+
var MIGRATION_002_DELIVERED_IN_EACH_KINDS_WORDS = `-- ============================================================================
|
|
807
|
+
-- 002 \u2014 "delivered" in each kind's words
|
|
808
|
+
--
|
|
809
|
+
-- The contract says \`delivered\` for the moment the food reached the customer,
|
|
810
|
+
-- whatever the kind. This product says it three ways: a delivery is
|
|
811
|
+
-- \`delivered\`, a takeout is \`picked_up\`, a dine-in is \`served\`. The two doors
|
|
812
|
+
-- of 001 took the contract's word and refused it for a takeout; they now
|
|
813
|
+
-- translate before validating, so the sink stays thirty lines and knows
|
|
814
|
+
-- nothing about kinds.
|
|
815
|
+
-- ============================================================================
|
|
816
|
+
|
|
817
|
+
CREATE OR REPLACE FUNCTION public.orders_status_for_kind(p_kind text, p_status text)
|
|
818
|
+
RETURNS text
|
|
819
|
+
LANGUAGE sql IMMUTABLE
|
|
820
|
+
SET search_path TO ''
|
|
821
|
+
AS $$
|
|
822
|
+
SELECT CASE
|
|
823
|
+
WHEN p_status = 'delivered' AND p_kind = 'takeout' THEN 'picked_up'
|
|
824
|
+
WHEN p_status = 'delivered' AND p_kind = 'dine_in' THEN 'served'
|
|
825
|
+
ELSE p_status
|
|
826
|
+
END;
|
|
827
|
+
$$;
|
|
828
|
+
|
|
829
|
+
COMMENT ON FUNCTION public.orders_status_for_kind(text, text) IS
|
|
830
|
+
'The canonical delivered in this kind''s words: delivered, picked_up or served.';
|
|
831
|
+
|
|
832
|
+
CREATE OR REPLACE FUNCTION public.orders_apply_external_status(
|
|
833
|
+
p_tenant uuid,
|
|
834
|
+
p_order uuid,
|
|
835
|
+
p_status text,
|
|
836
|
+
p_occurred_at timestamptz
|
|
837
|
+
) RETURNS void
|
|
838
|
+
LANGUAGE plpgsql SECURITY DEFINER
|
|
839
|
+
SET search_path TO ''
|
|
840
|
+
AS $$
|
|
841
|
+
DECLARE
|
|
842
|
+
v_kind text;
|
|
843
|
+
v_status text;
|
|
844
|
+
BEGIN
|
|
845
|
+
SELECT o.kind INTO v_kind FROM public.orders o
|
|
846
|
+
WHERE o.id = p_order AND o.tenant_id = p_tenant AND o.metadata ? 'externalRef';
|
|
847
|
+
IF v_kind IS NULL THEN
|
|
848
|
+
RAISE EXCEPTION 'orders_apply_external_status: order % is not an external order of tenant %', p_order, p_tenant
|
|
849
|
+
USING ERRCODE = '22023';
|
|
850
|
+
END IF;
|
|
851
|
+
v_status := public.orders_status_for_kind(v_kind, p_status);
|
|
852
|
+
IF NOT EXISTS (SELECT 1 FROM public.order_kinds k WHERE k.kind = v_kind AND v_status = ANY (k.statuses)) THEN
|
|
853
|
+
RAISE EXCEPTION 'orders_apply_external_status: % is not a status of kind %', v_status, v_kind USING ERRCODE = '22023';
|
|
854
|
+
END IF;
|
|
855
|
+
|
|
856
|
+
UPDATE public.orders
|
|
857
|
+
SET status = v_status,
|
|
858
|
+
completed_at = CASE WHEN v_status IN ('completed', 'delivered', 'picked_up', 'served')
|
|
859
|
+
THEN coalesce(p_occurred_at, now()) ELSE completed_at END,
|
|
860
|
+
metadata = jsonb_set(metadata, '{external,lastStatusAt}', to_jsonb(coalesce(p_occurred_at, now())), true),
|
|
861
|
+
updated_at = now()
|
|
862
|
+
WHERE id = p_order;
|
|
863
|
+
END $$;
|
|
864
|
+
|
|
865
|
+
-- The status door reads the kind's word back as the contract's.
|
|
866
|
+
CREATE OR REPLACE FUNCTION public.orders_external_status(p_tenant uuid, p_order uuid)
|
|
867
|
+
RETURNS jsonb
|
|
868
|
+
LANGUAGE sql STABLE SECURITY DEFINER
|
|
869
|
+
SET search_path TO ''
|
|
870
|
+
AS $$
|
|
871
|
+
SELECT jsonb_build_object(
|
|
872
|
+
'status', CASE o.status WHEN 'picked_up' THEN 'delivered' WHEN 'served' THEN 'delivered' ELSE o.status END,
|
|
873
|
+
'updatedAt', o.updated_at)
|
|
874
|
+
FROM public.orders o
|
|
875
|
+
WHERE o.id = p_order AND o.tenant_id = p_tenant AND o.metadata ? 'externalRef';
|
|
876
|
+
$$;
|
|
877
|
+
|
|
878
|
+
REVOKE ALL ON FUNCTION public.orders_status_for_kind(text, text) FROM PUBLIC;
|
|
879
|
+
GRANT EXECUTE ON FUNCTION public.orders_status_for_kind(text, text) TO authenticated, service_role;
|
|
880
|
+
DO $$
|
|
881
|
+
BEGIN
|
|
882
|
+
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'fayz_integration') THEN
|
|
883
|
+
GRANT EXECUTE ON FUNCTION public.orders_status_for_kind(text, text) TO fayz_integration;
|
|
884
|
+
END IF;
|
|
885
|
+
END $$;
|
|
886
|
+
`;
|
|
806
887
|
var MIGRATIONS = [
|
|
807
|
-
{ id: "001_an_order_arrives_from_a_marketplace", sql: MIGRATION_001_AN_ORDER_ARRIVES_FROM_A_MARKETPLACE }
|
|
888
|
+
{ id: "001_an_order_arrives_from_a_marketplace", sql: MIGRATION_001_AN_ORDER_ARRIVES_FROM_A_MARKETPLACE },
|
|
889
|
+
{ id: "002_delivered_in_each_kinds_words", sql: MIGRATION_002_DELIVERED_IN_EACH_KINDS_WORDS }
|
|
808
890
|
];
|
|
809
891
|
|
|
810
892
|
// src/locales/en.ts
|