@flowselections/floriday-afhandeling-module 1.0.105 → 1.0.106

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowselections/floriday-afhandeling-module",
3
- "version": "1.0.105",
3
+ "version": "1.0.106",
4
4
  "private": false,
5
5
  "sideEffects": false,
6
6
  "type": "module",
@@ -569,6 +569,36 @@ $function$
569
569
 
570
570
  ;
571
571
 
572
+ CREATE OR REPLACE FUNCTION public.afh_order_signup_uitgesloten(p_order_id uuid)
573
+ RETURNS boolean
574
+ LANGUAGE plpgsql
575
+ STABLE SECURITY DEFINER
576
+ SET search_path TO 'public', 'pg_temp'
577
+ AS $function$
578
+ DECLARE v_cf jsonb; v_dd text; v_dag date; v_bf boolean;
579
+ BEGIN
580
+ PERFORM public.afh_assert_intern();
581
+ SELECT o.custom_fields, o.delivery_date INTO v_cf, v_dd FROM public.orders o WHERE o.id = p_order_id;
582
+ IF NOT FOUND THEN RETURN true; END IF;
583
+ -- Ingehaalde order: controle van de Engine, anders dezelfde regel zelf.
584
+ IF to_regprocedure('public.logistics_order_is_backfill(uuid)') IS NOT NULL THEN
585
+ EXECUTE 'SELECT public.logistics_order_is_backfill($1)' INTO v_bf USING p_order_id;
586
+ ELSE
587
+ v_bf := CASE
588
+ WHEN jsonb_typeof(v_cf) IS DISTINCT FROM 'object' OR NOT (v_cf ? 'backfill') THEN false
589
+ WHEN jsonb_typeof(v_cf->'backfill') = 'null' THEN false
590
+ WHEN jsonb_typeof(v_cf->'backfill') = 'boolean' THEN (v_cf->>'backfill')::boolean
591
+ ELSE lower(btrim(v_cf->>'backfill')) NOT IN ('', 'false', '0', 'nee', 'no') END;
592
+ END IF;
593
+ IF coalesce(v_bf, false) THEN RETURN true; END IF;
594
+ -- Leverdag in het verleden (Europe/Amsterdam); onbekende leverdag = niet aanmelden.
595
+ v_dag := public.logistics_order_date(v_dd);
596
+ RETURN v_dag IS NULL OR v_dag < (now() AT TIME ZONE 'Europe/Amsterdam')::date;
597
+ END;
598
+ $function$;
599
+ REVOKE ALL ON FUNCTION public.afh_order_signup_uitgesloten(uuid) FROM PUBLIC, anon, authenticated;
600
+ GRANT EXECUTE ON FUNCTION public.afh_order_signup_uitgesloten(uuid) TO service_role;
601
+
572
602
  CREATE OR REPLACE FUNCTION public.afh_signup_groups_base(p_from date, p_to date, p_tz text)
573
603
  RETURNS jsonb
574
604
  LANGUAGE plpgsql
@@ -580,7 +610,7 @@ DECLARE
580
610
  v_unassigned jsonb;
581
611
  v_eng jsonb;
582
612
  BEGIN
583
- -- Vervoerder komt uitsluitend uit orders.carrier_id (Engine 1.0.184). Zonder die kolom: niets aanmelden.
613
+ PERFORM public.afh_assert_intern();
584
614
  IF NOT public.afh_orders_have_carrier_id() THEN
585
615
  RETURN jsonb_build_object('groups', '[]'::jsonb, 'unassigned', '[]'::jsonb, 'reason', 'engine_te_oud');
586
616
  END IF;
@@ -605,6 +635,7 @@ BEGIN
605
635
  WHERE o.deleted_at IS NULL
606
636
  AND o.delivery_date ~ '^\d{4}-\d{2}-\d{2}'
607
637
  AND o.delivery_date::date BETWEEN p_from AND p_to
638
+ AND NOT public.afh_order_signup_uitgesloten(o.id)
608
639
  AND lower(coalesce(o.status,'')) NOT IN ('geannuleerd','cancelled','canceled','annulled')
609
640
  ), cand AS (
610
641
  SELECT ob.*, ca.id AS carrier_id, ca.name AS carrier_name, ca.email AS carrier_email, ca.pickup_moments, 1 AS rn
@@ -691,16 +722,11 @@ BEGIN
691
722
 
692
723
  WITH ob AS (
693
724
  SELECT o.id, o.order_number, o.delivery_date::date AS delivery_date,
694
- public.afh_normalize_country(coalesce(dl.country, c.country)) AS country_code,
695
- public.afh_normalize_nl_province(dl.province) AS province_code,
696
- public.afh_city_code(public.afh_normalize_country(coalesce(dl.country, c.country)), coalesce(dl.city, c.city)) AS city_code,
697
- nullif(trim(dl.gln), '') AS gln,
698
725
  nullif(to_jsonb(o)->>'carrier_id', '')::uuid AS order_carrier_id
699
726
  FROM public.orders o
700
- LEFT JOIN public.customers c ON c.id = o.customer_id
701
- LEFT JOIN public.customer_delivery_locations dl ON dl.id::text = o.custom_fields->>'delivery_location'
702
727
  WHERE o.deleted_at IS NULL AND o.delivery_date ~ '^\d{4}-\d{2}-\d{2}'
703
728
  AND o.delivery_date::date BETWEEN p_from AND p_to
729
+ AND NOT public.afh_order_signup_uitgesloten(o.id)
704
730
  AND lower(coalesce(o.status,'')) NOT IN ('geannuleerd','cancelled','canceled','annulled')
705
731
  )
706
732
  SELECT coalesce(jsonb_agg(jsonb_build_object('orderId', ob.id, 'orderNumber', ob.order_number, 'deliveryDate', ob.delivery_date) ORDER BY ob.delivery_date, ob.order_number), '[]'::jsonb)
@@ -968,6 +994,7 @@ CREATE OR REPLACE FUNCTION public.afh_signup_send_group(g jsonb, p_tz text, p_co
968
994
  SET search_path TO 'public', 'pg_temp'
969
995
  AS $function$
970
996
  BEGIN
997
+ PERFORM public.afh_assert_intern();
971
998
  IF jsonb_array_length(coalesce(g->'pending_orders','[]'::jsonb)) > 0
972
999
  AND g->>'wait_until' IS NOT NULL AND now() < (g->>'wait_until')::timestamptz THEN
973
1000
  RETURN jsonb_build_object('ok', true, 'code', 'waiting_for_carts', 'wait_until', g->'wait_until');
@@ -978,6 +1005,11 @@ BEGIN
978
1005
  IF coalesce((g->>'order_count')::int, 0) = 0 AND jsonb_array_length(coalesce(g->'floriday_done_orders','[]'::jsonb)) = 0 THEN
979
1006
  RETURN jsonb_build_object('ok', true, 'code', 'waiting_for_carts');
980
1007
  END IF;
1008
+ -- Vangnet: een ingehaalde order of een order met leverdag in het verleden wordt nooit aangemeld.
1009
+ IF EXISTS (SELECT 1 FROM jsonb_array_elements(coalesce(g->'orders','[]'::jsonb)) e
1010
+ WHERE public.afh_order_signup_uitgesloten((e->>'orderId')::uuid)) THEN
1011
+ RETURN jsonb_build_object('ok', true, 'code', 'uitgesloten');
1012
+ END IF;
981
1013
  RETURN public.afh_signup_send_group_base(g, p_tz, p_connection, p_purpose, p_source, p_user);
982
1014
  END;
983
1015
  $function$
@@ -1935,6 +1967,7 @@ BEGIN
1935
1967
  ('afh_set_signup_auto_send','ingelogd'),
1936
1968
  ('afh_signup_config','intern'),
1937
1969
  ('afh_signup_engine_moments','intern'),
1970
+ ('afh_order_signup_uitgesloten','intern'),
1938
1971
  ('afh_signup_groups','intern'),
1939
1972
  ('afh_signup_groups_base','intern'),
1940
1973
  ('afh_signup_groups_floriday','intern'),
@@ -2032,6 +2065,7 @@ BEGIN
2032
2065
  ('afh_signup_config','intern'),
2033
2066
  ('afh_signup_sender','intern'),
2034
2067
  ('afh_signup_engine_moments','intern'),
2068
+ ('afh_order_signup_uitgesloten','intern'),
2035
2069
  ('afh_signup_moment_for_orders','ingelogd'),
2036
2070
  ('afh_signup_find_group','intern'),
2037
2071
  ('afh_signup_groups','intern'),