@flowselections/floriday-verkoop-module 1.0.113 → 1.0.115

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.
@@ -1,2 +1,2 @@
1
- export declare const MODULE_VERSION = "1.0.112";
1
+ export declare const MODULE_VERSION = "1.0.114";
2
2
  //# sourceMappingURL=module-version.d.ts.map
@@ -1,3 +1,3 @@
1
1
  // Versie van deze module, meegestuurd in antwoorden van geplande taken zodat
2
2
  // je ziet welke code echt draait. Bijwerken samen met package.json.
3
- export const MODULE_VERSION = "1.0.112";
3
+ export const MODULE_VERSION = "1.0.114";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowselections/floriday-verkoop-module",
3
- "version": "1.0.113",
3
+ "version": "1.0.115",
4
4
  "private": false,
5
5
  "sideEffects": false,
6
6
  "type": "module",
@@ -34,7 +34,7 @@
34
34
  "lint": "eslint ."
35
35
  },
36
36
  "peerDependencies": {
37
- "@flowselections/core": "1.0.29",
37
+ "@flowselections/core": "1.0.30",
38
38
  "@tanstack/react-query": "^5.0.0",
39
39
  "@tanstack/react-router": "^1.0.0",
40
40
  "@tanstack/react-start": "^1.0.0",
@@ -52,7 +52,7 @@
52
52
  "devDependencies": {
53
53
  "@cloudflare/vite-plugin": "^1.25.5",
54
54
  "@eslint/js": "^9.32.0",
55
- "@flowselections/core": "1.0.29",
55
+ "@flowselections/core": "1.0.30",
56
56
  "@lovable.dev/vite-tanstack-config": "2.26.5",
57
57
  "@rsbuild/core": "^1.0.0",
58
58
  "@tailwindcss/vite": "^4.2.1",
@@ -1490,44 +1490,7 @@ END;
1490
1490
  $function$
1491
1491
  ;
1492
1492
 
1493
- CREATE OR REPLACE FUNCTION public.trg_orders_consume_on_complete_fn()
1494
- RETURNS trigger
1495
- LANGUAGE plpgsql
1496
- SECURITY DEFINER
1497
- SET search_path TO 'public'
1498
- AS $function$
1499
- DECLARE
1500
- oi RECORD; v_src text; v_proof jsonb; v_ids text[]; v_reason text;
1501
- v_ready_statuses text[] := ARRAY['klaar voor verzending','ready','ready_to_ship','verzonden','shipped','delivered','geleverd',
1502
- 'afgehandeld','completed','gereed','picked','gereedgemeld','done'];
1503
- BEGIN
1504
- IF LOWER(COALESCE(NEW.status,'')) = ANY(v_ready_statuses)
1505
- AND (OLD.status IS NULL OR LOWER(COALESCE(OLD.status,'')) <> ALL(v_ready_statuses)) THEN
1506
- v_ids := public.voorraad_external_fulfilled_ids(NEW.custom_fields);
1507
- IF COALESCE(array_length(v_ids,1),0) > 0 THEN
1508
- -- deel-afhandeling in Floriday: alleen de genoemde bestellingen
1509
- PERFORM public.voorraad_consume_external_fulfilled(NEW.id, v_ids, NEW.order_source);
1510
- RETURN NEW;
1511
- END IF;
1512
- v_proof := public.order_departure_proof(NEW.id);
1513
- IF NOT COALESCE((v_proof->>'ok')::boolean, false) THEN
1514
- INSERT INTO public.stock_mutation_log(order_id, order_item_id, order_source, mutation_type, qty_delta, reason, created_by)
1515
- SELECT NEW.id, oi2.id, public.normalize_order_source(COALESCE(NEW.order_source,'HO')), 'consumption_blocked', COALESCE(oi2.quantity,0),
1516
- 'status ' || COALESCE(NEW.status,'') || ' zonder bewijs van vertrek: ' || COALESCE(v_proof->>'reason','onbekend'), auth.uid()
1517
- FROM logistics_active.order_items oi2 WHERE oi2.order_id = NEW.id;
1518
- RETURN NEW;
1519
- END IF;
1520
- v_src := COALESCE(NEW.order_source,'HO');
1521
- v_reason := CASE WHEN NEW.custom_fields ? 'floriday_external_fulfilled_at' THEN 'Afgehandeld in Floriday'
1522
- ELSE 'order vertrokken (status ' || COALESCE(NEW.status,'') || ')' END;
1523
- FOR oi IN SELECT id FROM logistics_active.order_items WHERE order_id = NEW.id LOOP
1524
- PERFORM public.consume_order_item(NEW.id, oi.id, v_src, v_reason);
1525
- END LOOP;
1526
- PERFORM public.recompute_reserved_for_order(NEW.id);
1527
- END IF;
1528
- RETURN NEW;
1529
- END $function$
1530
- ;
1493
+ -- trg_orders_consume_on_complete_fn: eigenaar is Voorraad; Verkoop maakt hem sinds 1.0.114 niet meer aan.
1531
1494
 
1532
1495
  CREATE OR REPLACE FUNCTION public.trg_orders_replan()
1533
1496
  RETURNS trigger
@@ -2185,3 +2148,193 @@ $function$;
2185
2148
 
2186
2149
  REVOKE ALL ON FUNCTION public.verkoop_insert_manual_order(jsonb, jsonb, text) FROM PUBLIC, anon, authenticated;
2187
2150
  GRANT EXECUTE ON FUNCTION public.verkoop_insert_manual_order(jsonb, jsonb, text) TO service_role;
2151
+
2152
+ -- ── Verkoop 1.0.114: eigen toegangspoort in elke beschermde functie ──
2153
+ -- Werkt ook na een schemasync zonder REVOKE: de functie controleert zelf wie hem aanroept.
2154
+ CREATE OR REPLACE FUNCTION public.verkoop_is_server()
2155
+ RETURNS boolean
2156
+ LANGUAGE sql
2157
+ STABLE
2158
+ SET search_path TO 'public'
2159
+ AS $function$
2160
+ SELECT coalesce(nullif(current_setting('request.jwt.claims', true), '')::jsonb ->> 'role', '') = 'service_role'
2161
+ OR session_user::text <> 'authenticator';
2162
+ $function$;
2163
+
2164
+ CREATE OR REPLACE FUNCTION public.verkoop_assert_caller(p_level text, p_roles public.app_role[] DEFAULT NULL)
2165
+ RETURNS void
2166
+ LANGUAGE plpgsql
2167
+ STABLE
2168
+ SET search_path TO 'public'
2169
+ AS $function$
2170
+ DECLARE
2171
+ v_uid uuid := auth.uid();
2172
+ v_role public.app_role;
2173
+ BEGIN
2174
+ IF public.verkoop_is_server() THEN RETURN; END IF;
2175
+ IF p_level = 'server' THEN
2176
+ RAISE EXCEPTION USING ERRCODE = '42501', MESSAGE = 'Deze functie is alleen voor de server.';
2177
+ ELSIF p_level = 'ingelogd' THEN
2178
+ IF v_uid IS NOT NULL THEN RETURN; END IF;
2179
+ ELSIF p_level = 'keten' THEN
2180
+ IF v_uid IS NOT NULL OR pg_trigger_depth() > 0 THEN RETURN; END IF;
2181
+ ELSIF p_level = 'rol' THEN
2182
+ IF v_uid IS NOT NULL AND p_roles IS NOT NULL THEN
2183
+ FOREACH v_role IN ARRAY p_roles LOOP
2184
+ IF public.has_role(v_uid, v_role) THEN RETURN; END IF;
2185
+ END LOOP;
2186
+ RAISE EXCEPTION USING ERRCODE = '42501', MESSAGE = 'Je hebt geen rechten voor deze actie.';
2187
+ END IF;
2188
+ END IF;
2189
+ -- Onbekend niveau of geen inlog: weigeren.
2190
+ RAISE EXCEPTION USING ERRCODE = '42501', MESSAGE = 'Je moet ingelogd zijn om dit te doen.';
2191
+ END;
2192
+ $function$;
2193
+
2194
+ -- release_job_lock was SQL; als plpgsql opnieuw zodat hij de poort kan aanroepen.
2195
+ CREATE OR REPLACE FUNCTION public.release_job_lock(_key text)
2196
+ RETURNS void
2197
+ LANGUAGE plpgsql
2198
+ SECURITY DEFINER
2199
+ SET search_path TO 'public'
2200
+ AS $function$
2201
+ BEGIN
2202
+ PERFORM public.verkoop_assert_caller('ingelogd'); -- verkoop-poort
2203
+ UPDATE public.job_locks SET locked_until = now() - interval '1 second', updated_at = now()
2204
+ WHERE lock_key = _key;
2205
+ END;
2206
+ $function$;
2207
+
2208
+ -- Lijst: functie → niveau. Ook gebruikt door verkoop_install_check().
2209
+ CREATE OR REPLACE FUNCTION public.verkoop_guarded_functions()
2210
+ RETURNS TABLE(sig text, niveau text, rollen text, opmerking text)
2211
+ LANGUAGE sql
2212
+ IMMUTABLE
2213
+ SET search_path TO 'public'
2214
+ AS $function$
2215
+ SELECT * FROM (VALUES
2216
+ ('verkoop_insert_manual_order(jsonb,jsonb,text)', 'server', NULL, NULL),
2217
+ ('verkoop_create_order(text,text,uuid,date,uuid,uuid,jsonb,text,text,boolean,text,boolean,text)', 'ingelogd', NULL, 'verkoop_create_order: wacht op controle Aanbiedingsmail'),
2218
+ ('verkoop_packing_drift_report()', 'rol', 'admin,superadmin', NULL),
2219
+ ('verkoop_packing_drift_apply(uuid[])', 'rol', 'admin,superadmin', NULL),
2220
+ ('assign_floriday_order_numbers(text[])', 'ingelogd', NULL, NULL),
2221
+ ('get_or_assign_floriday_order_number(text)', 'keten', NULL, NULL),
2222
+ ('next_order_number(text)', 'keten', NULL, NULL),
2223
+ ('next_order_number_for_date(text,timestamp with time zone)', 'keten', NULL, NULL),
2224
+ ('try_acquire_job_lock(text,integer,text)', 'ingelogd', NULL, 'voorlopig; wacht op melding client Floriday-authenticatie'),
2225
+ ('release_job_lock(text)', 'ingelogd', NULL, 'voorlopig; wacht op melding client Floriday-authenticatie'),
2226
+ ('order_departure_proof(uuid)', 'keten', NULL, NULL),
2227
+ ('enqueue_cart_replan_if_unlocked(uuid)', 'keten', NULL, NULL),
2228
+ ('replace_order_carts(uuid,uuid,jsonb)', 'ingelogd', NULL, NULL),
2229
+ ('mark_shipment_shipped(uuid)', 'ingelogd', NULL, NULL),
2230
+ ('mark_salesorder_controlled(text,boolean)', 'ingelogd', NULL, NULL),
2231
+ ('reset_cart_layer_positions(uuid)', 'ingelogd', NULL, NULL),
2232
+ ('move_cart_layer_position(uuid,integer,numeric,numeric)', 'ingelogd', NULL, NULL),
2233
+ ('swap_cart_layer_positions(uuid,uuid)', 'ingelogd', NULL, NULL),
2234
+ ('order_number_prefix(text,jsonb)', 'open', NULL, 'bewust open: rekenhulp zonder gegevens')
2235
+ ) v(sig, niveau, rollen, opmerking);
2236
+ $function$;
2237
+
2238
+ -- Poort in de bestaande functies zetten (herhaalbaar: overslaan als hij er al staat).
2239
+ DO $do$
2240
+ DECLARE
2241
+ r record;
2242
+ v_def text;
2243
+ v_new text;
2244
+ v_call text;
2245
+ BEGIN
2246
+ FOR r IN SELECT * FROM public.verkoop_guarded_functions() WHERE niveau <> 'open' LOOP
2247
+ IF to_regprocedure('public.' || r.sig) IS NULL THEN
2248
+ RAISE NOTICE 'Verkoop-poort: % ontbreekt, overgeslagen', r.sig;
2249
+ CONTINUE;
2250
+ END IF;
2251
+ v_def := pg_get_functiondef(to_regprocedure('public.' || r.sig));
2252
+ v_new := v_def;
2253
+
2254
+ IF r.sig LIKE 'verkoop_create_order(%' THEN
2255
+ -- Tijdelijk ingelogd of server, zonder rolcontrole (wacht op Aanbiedingsmail).
2256
+ v_new := regexp_replace(v_new,
2257
+ 'IF v_uid IS NOT NULL THEN\s*IF NOT \(public\.has_role\(v_uid, ''admin''\).*?ELSIF coalesce\(auth\.role\(\), ''''\) IN \(''anon'', ''authenticated''\) THEN\s*RAISE EXCEPTION ''Niet toegestaan'';\s*END IF;',
2258
+ '-- Toegang: zie verkoop-poort hierboven.');
2259
+ END IF;
2260
+
2261
+ IF r.sig LIKE 'verkoop_insert_manual_order(%' THEN
2262
+ -- Engine met p_actor: de server geeft de aanmaker mee in plaats van een gebruikerssessie na te bootsen.
2263
+ v_new := replace(v_new,
2264
+ 'IF v_uid IS NULL AND v_set_by IS NOT NULL THEN',
2265
+ 'IF v_uid IS NULL AND v_set_by IS NOT NULL AND to_regprocedure(''public.logistics_set_order_carrier(uuid,uuid,uuid)'') IS NULL THEN');
2266
+ v_new := replace(v_new,
2267
+ 'EXECUTE ''SELECT public.logistics_set_order_carrier($1, $2)'' USING v_order_id, v_manual_carrier;',
2268
+ 'IF to_regprocedure(''public.logistics_set_order_carrier(uuid,uuid,uuid)'') IS NOT NULL THEN
2269
+ EXECUTE ''SELECT public.logistics_set_order_carrier($1, $2, $3)'' USING v_order_id, v_manual_carrier, v_set_by;
2270
+ ELSE
2271
+ EXECUTE ''SELECT public.logistics_set_order_carrier($1, $2)'' USING v_order_id, v_manual_carrier;
2272
+ END IF;');
2273
+ END IF;
2274
+
2275
+ IF position('verkoop_assert_caller' IN v_new) = 0 THEN
2276
+ v_call := format('PERFORM public.verkoop_assert_caller(%L%s); -- verkoop-poort',
2277
+ r.niveau,
2278
+ CASE WHEN r.rollen IS NULL THEN '' ELSE format(', %L::public.app_role[]', '{' || r.rollen || '}') END);
2279
+ v_new := regexp_replace(v_new, '(\$function\$.*?\mBEGIN\M)', E'\\1\n ' || v_call);
2280
+ END IF;
2281
+
2282
+ IF v_new IS DISTINCT FROM v_def THEN
2283
+ EXECUTE v_new;
2284
+ END IF;
2285
+ END LOOP;
2286
+ END
2287
+ $do$;
2288
+
2289
+ -- Rechten als extra laag (een schemasync neemt REVOKE niet mee; de poort blijft dan staan).
2290
+ REVOKE ALL ON FUNCTION public.verkoop_insert_manual_order(jsonb, jsonb, text) FROM PUBLIC, anon, authenticated;
2291
+ GRANT EXECUTE ON FUNCTION public.verkoop_insert_manual_order(jsonb, jsonb, text) TO service_role;
2292
+ GRANT EXECUTE ON FUNCTION public.verkoop_is_server() TO authenticated, service_role;
2293
+ GRANT EXECUTE ON FUNCTION public.verkoop_assert_caller(text, public.app_role[]) TO authenticated, service_role;
2294
+
2295
+ -- Controle na installatie of schemasync.
2296
+ CREATE OR REPLACE FUNCTION public.verkoop_install_check()
2297
+ RETURNS TABLE(soort text, naam text, aanwezig boolean, niveau text, poort_aanwezig boolean, anon_uitvoerrecht boolean, authenticated_uitvoerrecht boolean, opmerking text)
2298
+ LANGUAGE plpgsql
2299
+ STABLE SECURITY DEFINER
2300
+ SET search_path TO 'public'
2301
+ AS $function$
2302
+ BEGIN
2303
+ IF NOT public.verkoop_is_server() THEN
2304
+ PERFORM public.verkoop_assert_caller('rol', '{admin,superadmin}'::public.app_role[]);
2305
+ END IF;
2306
+
2307
+ RETURN QUERY
2308
+ SELECT 'functie'::text, g.sig,
2309
+ to_regprocedure('public.' || g.sig) IS NOT NULL,
2310
+ g.niveau,
2311
+ CASE WHEN g.niveau = 'open' THEN NULL
2312
+ ELSE coalesce(position('verkoop_assert_caller' IN (SELECT p.prosrc FROM pg_proc p WHERE p.oid = to_regprocedure('public.' || g.sig))) > 0, false) END,
2313
+ CASE WHEN to_regprocedure('public.' || g.sig) IS NULL THEN NULL ELSE has_function_privilege('anon', to_regprocedure('public.' || g.sig), 'EXECUTE') END,
2314
+ CASE WHEN to_regprocedure('public.' || g.sig) IS NULL THEN NULL ELSE has_function_privilege('authenticated', to_regprocedure('public.' || g.sig), 'EXECUTE') END,
2315
+ g.opmerking
2316
+ FROM public.verkoop_guarded_functions() g;
2317
+
2318
+ RETURN QUERY
2319
+ SELECT 'trigger'::text, t.tbl || '.' || t.trg,
2320
+ EXISTS (SELECT 1 FROM pg_trigger x WHERE x.tgname = t.trg AND x.tgrelid = to_regclass('public.' || t.tbl) AND NOT x.tgisinternal),
2321
+ NULL::text, NULL::boolean, NULL::boolean, NULL::boolean,
2322
+ CASE WHEN to_regclass('public.' || t.tbl) IS NULL THEN 'tabel ontbreekt' END
2323
+ FROM (VALUES
2324
+ ('update_floriday_auto_fulfillment_log_updated_at','floriday_auto_fulfillment_log'),('trg_floriday_salesorders_assign_number','floriday_salesorders_cache'),('trg_assign_offer_order_number','offer_orders'),('order_items_replan_trg','order_items'),('trg_enqueue_cart_replan_order_items','order_items'),('orders_replan_trg','orders'),('orders_status_origin','orders'),('trg_enqueue_cart_replan_orders','orders'),('trg_orders_apply_buffer_decision','orders'),('trg_stock_mutation_alert','stock_mutation_log'),('order_shipments_assign_number','order_shipments'),('order_shipments_touch','order_shipments'),('order_shipment_items_recompute','order_shipment_items'),('order_shipment_items_touch','order_shipment_items'),('trg_order_buffers_updated_at','order_buffers'),('trg_invoice_timing_settings_touch','invoice_timing_settings'),('trg_stock_consumption_alerts_updated_at','stock_consumption_alerts'),('update_delivery_notes_updated_at','delivery_notes'),('floriday_status_sync_queue_touch','floriday_status_sync_queue')
2325
+ ) t(trg, tbl);
2326
+
2327
+ RETURN QUERY
2328
+ SELECT 'policy'::text, pl.tbl || '.' || pl.pol,
2329
+ EXISTS (SELECT 1 FROM pg_policies x WHERE x.schemaname = 'public' AND x.tablename = pl.tbl AND x.policyname = pl.pol),
2330
+ NULL::text, NULL::boolean, NULL::boolean, NULL::boolean,
2331
+ CASE WHEN to_regclass('public.' || pl.tbl) IS NULL THEN 'tabel ontbreekt'
2332
+ WHEN NOT (SELECT c.relrowsecurity FROM pg_class c WHERE c.oid = to_regclass('public.' || pl.tbl)) THEN 'beveiliging per rij staat UIT op deze tabel' END
2333
+ FROM (VALUES
2334
+ ('service role manages replan queue','cart_replan_queue'),('Authenticated can read delivery notes','delivery_notes'),('Ingelogde gebruikers lezen samenvoeglog','floriday_merge_log'),('Ingelogde gebruikers kunnen de statuswachtrij lezen','floriday_status_sync_queue'),('Ingelogde gebruikers kunnen statuswijzigingen aanmelden','floriday_status_sync_queue'),('Authenticated can manage invoice timing settings','invoice_timing_settings'),('job_locks service only','job_locks'),('Ingelogde gebruikers kunnen blokkademeldingen lezen','order_block_notifications'),('Authenticated users can manage order_buffers','order_buffers'),('Authenticated users can read order_buffers','order_buffers'),('authenticated_read_cart_layer_positions','order_cart_layer_positions'),('service_role_manage_cart_layer_positions','order_cart_layer_positions'),('Counters readable by authenticated','order_number_counters'),('Ingelogde gebruikers leggen weggehaalde meldingen vast','order_review_dismissals'),('Ingelogde gebruikers lezen weggehaalde meldingen','order_review_dismissals'),('Authenticated can delete order_shipment_items','order_shipment_items'),('Authenticated can insert order_shipment_items','order_shipment_items'),('Authenticated can read order_shipment_items','order_shipment_items'),('Authenticated can update order_shipment_items','order_shipment_items'),('Authenticated can delete order_shipments','order_shipments'),('Authenticated can insert order_shipments','order_shipments'),('Authenticated can read order_shipments','order_shipments'),('Authenticated can update order_shipments','order_shipments'),('Authenticated can manage stock consumption alerts','stock_consumption_alerts')
2335
+ ) pl(pol, tbl);
2336
+ END;
2337
+ $function$;
2338
+
2339
+ REVOKE ALL ON FUNCTION public.verkoop_install_check() FROM PUBLIC, anon;
2340
+ GRANT EXECUTE ON FUNCTION public.verkoop_install_check() TO authenticated, service_role;