@flowselections/floriday-verkoop-module 1.0.161 → 1.0.162

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 +1 @@
1
- {"version":3,"file":"floriday-mirror.server.d.ts","sourceRoot":"","sources":["../../src/lib/floriday-mirror.server.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAwB,KAAK,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAuJ9E,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,OAAO,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,mEAAmE;IACnE,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC5B,0DAA0D;IAC1D,OAAO,CAAC,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,EAAE,CAAA;KAAE,EAAE,CAAC;IACnD,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AA07CD,eAAO,MAAM,oBAAoB,MAAgB,CAAC;AAElD,wBAAsB,4BAA4B,CAAC,KAAK,SAAgB,EAAE,KAAK,GAAE,MAAM,GAAG,IAAW,yBAEpG;AAED;;;GAGG;AACH,wBAAsB,yBAAyB,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,QAAQ,GAAE,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,GAAG,IAAW,yBAI/G"}
1
+ {"version":3,"file":"floriday-mirror.server.d.ts","sourceRoot":"","sources":["../../src/lib/floriday-mirror.server.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAwB,KAAK,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAwJ9E,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,OAAO,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,mEAAmE;IACnE,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC5B,0DAA0D;IAC1D,OAAO,CAAC,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,EAAE,CAAA;KAAE,EAAE,CAAC;IACnD,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAm8CD,eAAO,MAAM,oBAAoB,MAAgB,CAAC;AAElD,wBAAsB,4BAA4B,CAAC,KAAK,SAAgB,EAAE,KAAK,GAAE,MAAM,GAAG,IAAW,yBAEpG;AAED;;;GAGG;AACH,wBAAsB,yBAAyB,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,QAAQ,GAAE,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,GAAG,IAAW,yBAI/G"}
@@ -12,10 +12,11 @@ import { fillOnlyEmpty, snapshotFromFloridayOrder } from "./packing-snapshot.js"
12
12
  // - De minuutronde (sync-salesorders) roept `mirrorFloridayOrdersFromCron`
13
13
  // aan; het spiegelen draait onder een eigen slot, zodat twee aanroepen
14
14
  // nooit tegelijk dezelfde orders verwerken.
15
- import { deriveOrderLevel } from "./floriday-order-level.js";
15
+ import { cancelledInsertFields, deriveOrderLevel } from "./floriday-order-level.js";
16
16
  import { productNameForStorage } from "./product-name.js";
17
17
  import { resolveBufferForOrderInternal } from "./order-buffers.server.js";
18
18
  import { backfillInsertFields } from "./floriday-backfill.js";
19
+ import { FLORIDAY_CANCEL_REASON } from "./order-cancel.server.js";
19
20
  const LOOKBACK_DAYS = 14;
20
21
  const DEFAULT_BATCH = 200;
21
22
  /** Onbekend blijft onbekend: geen stilzwijgende 1. Zelfde helper als de planner. */
@@ -672,6 +673,7 @@ async function mirrorUnlocked(limit, since, onlyIds = null, backfill = null) {
672
673
  status: l.d?.status ?? null,
673
674
  orgId: l.orgId || "",
674
675
  deleted: Boolean(l.isDeleted),
676
+ latestDeliveryAt: l.d?.delivery?.latestDeliveryDateTime ?? null,
675
677
  });
676
678
  const lineInfoFromCache = (c) => {
677
679
  const d = c?.data ?? {};
@@ -683,6 +685,7 @@ async function mirrorUnlocked(limit, since, onlyIds = null, backfill = null) {
683
685
  status: d?.status ?? c?.status ?? null,
684
686
  orgId: buyerOrgIdOf(c),
685
687
  deleted: Boolean(c?.is_deleted),
688
+ latestDeliveryAt: del ? String(del) : null,
686
689
  };
687
690
  };
688
691
  const lineInfoByFoId = new Map();
@@ -738,6 +741,8 @@ async function mirrorUnlocked(limit, since, onlyIds = null, backfill = null) {
738
741
  floriday_received_at: ffPrimary?.receivedAt ?? null,
739
742
  floriday_packing: primaryLine.packing,
740
743
  floriday_lines: floridayLines,
744
+ // Uiterste levertijd (Floriday delivery.latestDeliveryDateTime); gelezen door de Engine.
745
+ ...(groupLvl.latestDeliveryAt ? { floriday_latest_delivery_at: groupLvl.latestDeliveryAt } : {}),
741
746
  };
742
747
  try {
743
748
  // Bestaande order kiezen. Volgorde: de order die al aan deze
@@ -982,6 +987,8 @@ async function mirrorUnlocked(limit, since, onlyIds = null, backfill = null) {
982
987
  }
983
988
  const lvl = deriveOrderLevel(lineInfos.length ? lineInfos : [lineInfoByFoId.get(primary.foId)]);
984
989
  mergedCf.floriday_status = lvl.floridayStatusSummary;
990
+ if (lvl.latestDeliveryAt)
991
+ mergedCf.floriday_latest_delivery_at = lvl.latestDeliveryAt;
985
992
  if (lvl.mixedDeliveryDates)
986
993
  mergedCf.floriday_mixed_delivery_dates = true;
987
994
  else
@@ -1103,6 +1110,8 @@ async function mirrorUnlocked(limit, since, onlyIds = null, backfill = null) {
1103
1110
  // Inhalen: markering in dezelfde INSERT, vóór de orderregels.
1104
1111
  const bfInfo = backfill?.get(primary.foId) ?? null;
1105
1112
  const bf = bfInfo ? backfillInsertFields(bfInfo, memberIds, new Date().toISOString()) : null;
1113
+ // Al geannuleerd in Floriday: status en cancelled_at in dezelfde INSERT.
1114
+ const cx = cancelledInsertFields(groupLvl.status, Boolean(bf), new Date().toISOString(), FLORIDAY_CANCEL_REASON);
1106
1115
  const { data: ins, error: iErr } = await supabaseAdmin.from("orders")
1107
1116
  .insert({
1108
1117
  order_number: orderNumber,
@@ -1111,6 +1120,7 @@ async function mirrorUnlocked(limit, since, onlyIds = null, backfill = null) {
1111
1120
  order_date: groupLvl.orderDate ?? primary.orderDateIso,
1112
1121
  delivery_date: groupLvl.deliveryDate,
1113
1122
  status: bf ? bf.status : groupLvl.status,
1123
+ ...(cx ? cx.columns : {}),
1114
1124
  total: String(totalPrice),
1115
1125
  custom_fields: {
1116
1126
  ...custom_fields,
@@ -1118,6 +1128,7 @@ async function mirrorUnlocked(limit, since, onlyIds = null, backfill = null) {
1118
1128
  ...(groupLvl.mixedDeliveryDates ? { floriday_mixed_delivery_dates: true } : {}),
1119
1129
  floriday_groups: { [primary.foId]: memberIds },
1120
1130
  ...(bf ? bf.customFields : {}),
1131
+ ...(cx ? cx.customFields : {}),
1121
1132
  },
1122
1133
  order_source: "floriday",
1123
1134
  floriday_sales_order_id: primary.foId,
@@ -10,6 +10,8 @@ export interface FloridayLineInfo {
10
10
  orgId: string;
11
11
  /** Bestelling is in de gedeelde cache verwijderd. */
12
12
  deleted?: boolean;
13
+ /** Floriday delivery.latestDeliveryDateTime (ruw), of null. */
14
+ latestDeliveryAt?: string | null;
13
15
  }
14
16
  export interface OrderLevel {
15
17
  orderDate: string | null;
@@ -20,7 +22,20 @@ export interface OrderLevel {
20
22
  floridayStatusSummary: string | null;
21
23
  mixedDeliveryDates: boolean;
22
24
  multiBuyer: boolean;
25
+ /** Vroegste uiterste levertijd (ISO met tijdzone) over de actieve bestellingen. */
26
+ latestDeliveryAt: string | null;
23
27
  }
24
28
  export declare function isCancelledLine(l: FloridayLineInfo): boolean;
25
29
  export declare function deriveOrderLevel(lines: FloridayLineInfo[]): OrderLevel;
30
+ /** Vroegste geldige tijdstip als ISO met tijdzone (UTC, "Z"); ongeldig/leeg telt niet. */
31
+ export declare function earliestIso(vals: (string | null | undefined)[]): string | null;
32
+ /**
33
+ * Extra velden voor de INSERT van een nieuwe Floriday-order die al geannuleerd
34
+ * binnenkomt: status en cancelled_at altijd samen, net als bij handmatig
35
+ * annuleren. Nooit voor backfill; bestaande orders vallen hier niet onder.
36
+ */
37
+ export declare function cancelledInsertFields(status: string, isBackfill: boolean, nowIso: string, reason: string): {
38
+ columns: Record<string, unknown>;
39
+ customFields: Record<string, unknown>;
40
+ } | null;
26
41
  //# sourceMappingURL=floriday-order-level.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"floriday-order-level.d.ts","sourceRoot":"","sources":["../../src/lib/floriday-order-level.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,wDAAwD;IACxD,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,qDAAqD;IACrD,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,kDAAkD;IAClD,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,mDAAmD;IACnD,KAAK,EAAE,MAAM,CAAC;IACd,qDAAqD;IACrD,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED,MAAM,WAAW,UAAU;IACzB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,gEAAgE;IAChE,MAAM,EAAE,eAAe,GAAG,aAAa,CAAC;IACxC,kEAAkE;IAClE,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0F;AAC1F,wBAAgB,WAAW,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,EAAE,GAAG,MAAM,GAAG,IAAI,CAM9E;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,OAAO,EACnB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,GACb;IAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAAC,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAAE,GAAG,IAAI,CAMpF"}
@@ -20,6 +20,7 @@ export function deriveOrderLevel(lines) {
20
20
  const deliveryDate = minStr(basis.map((l) => l.deliveryDate));
21
21
  const activeDates = new Set(active.map((l) => l.deliveryDate).filter(Boolean));
22
22
  const statuses = Array.from(new Set(lines.map((l) => (l.deleted ? "DELETED" : l.status)).filter((s) => Boolean(s)))).sort();
23
+ const latestDeliveryAt = earliestIso(basis.map((l) => l.latestDeliveryAt ?? null));
23
24
  const orgs = new Set(lines.map((l) => l.orgId).filter(Boolean));
24
25
  return {
25
26
  orderDate,
@@ -28,5 +29,27 @@ export function deriveOrderLevel(lines) {
28
29
  floridayStatusSummary: statuses.length ? statuses.join(",") : null,
29
30
  mixedDeliveryDates: activeDates.size > 1,
30
31
  multiBuyer: orgs.size > 1,
32
+ latestDeliveryAt,
33
+ };
34
+ }
35
+ /** Vroegste geldige tijdstip als ISO met tijdzone (UTC, "Z"); ongeldig/leeg telt niet. */
36
+ export function earliestIso(vals) {
37
+ const ms = vals
38
+ .map((v) => (v ? Date.parse(String(v)) : NaN))
39
+ .filter((n) => Number.isFinite(n))
40
+ .sort((a, b) => a - b);
41
+ return ms.length ? new Date(ms[0]).toISOString() : null;
42
+ }
43
+ /**
44
+ * Extra velden voor de INSERT van een nieuwe Floriday-order die al geannuleerd
45
+ * binnenkomt: status en cancelled_at altijd samen, net als bij handmatig
46
+ * annuleren. Nooit voor backfill; bestaande orders vallen hier niet onder.
47
+ */
48
+ export function cancelledInsertFields(status, isBackfill, nowIso, reason) {
49
+ if (isBackfill || status !== "geannuleerd")
50
+ return null;
51
+ return {
52
+ columns: { cancelled_at: nowIso, cancel_reason: reason, invoice_due_at: null, buffer_id: null },
53
+ customFields: { floriday_cancelled_at: nowIso, floriday_cancel_notice: reason },
31
54
  };
32
55
  }
@@ -5,7 +5,7 @@ export declare const sendOrderEventToEngine: import("@tanstack/start-client-core
5
5
  claims: import("@supabase/auth-js").User;
6
6
  }, undefined, undefined, undefined>], (input: unknown) => {
7
7
  orderId: string;
8
- type: "order.changed" | "order.received";
8
+ type: "order.received" | "order.changed";
9
9
  }, Promise<{
10
10
  ok: false;
11
11
  error: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowselections/floriday-verkoop-module",
3
- "version": "1.0.161",
3
+ "version": "1.0.162",
4
4
  "private": false,
5
5
  "sideEffects": false,
6
6
  "type": "module",
@@ -4331,4 +4331,69 @@ BEGIN
4331
4331
  END $$;
4332
4332
  DROP TRIGGER IF EXISTS trg_invoice_timing_settings_start_date ON public.invoice_timing_settings;
4333
4333
  CREATE TRIGGER trg_invoice_timing_settings_start_date BEFORE INSERT OR UPDATE ON public.invoice_timing_settings
4334
- FOR EACH ROW EXECUTE FUNCTION public.invoice_timing_settings_start_date();
4334
+ FOR EACH ROW EXECUTE FUNCTION public.invoice_timing_settings_start_date();
4335
+
4336
+ -- Uiterste levertijd aanvullen (alleen server/SQL-editor)
4337
+ CREATE OR REPLACE FUNCTION public.verkoop_latest_delivery_aanvullen(p_max integer DEFAULT 500)
4338
+ RETURNS jsonb LANGUAGE plpgsql VOLATILE SECURITY DEFINER SET search_path = public
4339
+ AS $$
4340
+ DECLARE
4341
+ v_max int := least(greatest(coalesce(p_max,500),1),2000);
4342
+ v_vandaag date := (now() AT TIME ZONE 'Europe/Amsterdam')::date;
4343
+ v_bijgewerkt int := 0; v_zonder int := 0; v_rest int := 0;
4344
+ BEGIN
4345
+ IF NOT public.verkoop_is_server() THEN
4346
+ PERFORM public.verkoop_assert_caller('rol', '{admin,superadmin}'::public.app_role[]);
4347
+ END IF;
4348
+ IF NOT pg_try_advisory_xact_lock(hashtext('verkoop_latest_delivery_aanvullen')) THEN
4349
+ RETURN jsonb_build_object('bezig', true, 'melding', 'Er loopt al een aanvulronde; probeer het over een minuut opnieuw.');
4350
+ END IF;
4351
+
4352
+ CREATE TEMP TABLE IF NOT EXISTS _vlda (order_id uuid PRIMARY KEY, waarde text) ON COMMIT DROP;
4353
+ TRUNCATE _vlda;
4354
+
4355
+ -- Alleen lezen uit de eigen kopie van Floriday; vroegste over de actieve bestellingen.
4356
+ INSERT INTO _vlda (order_id, waarde)
4357
+ SELECT o.id,
4358
+ to_char(min((c.data->'delivery'->>'latestDeliveryDateTime')::timestamptz) AT TIME ZONE 'UTC',
4359
+ 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"')
4360
+ FROM public.orders o
4361
+ JOIN LATERAL (
4362
+ SELECT DISTINCT x AS foid FROM (
4363
+ SELECT o.floriday_sales_order_id AS x
4364
+ UNION ALL
4365
+ SELECT jsonb_array_elements_text(CASE WHEN jsonb_typeof(o.custom_fields->'floriday_sales_order_ids') = 'array'
4366
+ THEN o.custom_fields->'floriday_sales_order_ids' ELSE '[]'::jsonb END)
4367
+ ) s WHERE x IS NOT NULL
4368
+ ) ids ON true
4369
+ JOIN public.floriday_salesorders_cache c ON c.floriday_id = ids.foid
4370
+ WHERE o.order_source = 'floriday'
4371
+ AND o.deleted_at IS NULL
4372
+ AND NOT (o.custom_fields ? 'backfill')
4373
+ AND lower(coalesce(o.status,'')) NOT IN ('geannuleerd','cancelled','verzonden','shipped','geleverd','delivered')
4374
+ AND o.delivery_date IS NOT NULL AND o.delivery_date ~ '^\d{4}-\d{2}-\d{2}'
4375
+ AND left(o.delivery_date,10)::date >= v_vandaag
4376
+ AND coalesce(c.is_deleted,false) = false
4377
+ AND upper(coalesce(c.data->>'status','')) NOT IN ('CANCELLED','CANCELED','REJECTED','EXPIRED','UNFULFILLABLE')
4378
+ AND coalesce(c.data->'delivery'->>'latestDeliveryDateTime','') <> ''
4379
+ GROUP BY o.id;
4380
+
4381
+ DELETE FROM _vlda v USING public.orders o
4382
+ WHERE o.id = v.order_id AND (v.waarde IS NULL
4383
+ OR o.custom_fields->>'floriday_latest_delivery_at' IS NOT DISTINCT FROM v.waarde);
4384
+
4385
+ SELECT count(*) INTO v_rest FROM _vlda;
4386
+
4387
+ UPDATE public.orders o
4388
+ SET custom_fields = coalesce(o.custom_fields,'{}'::jsonb) || jsonb_build_object('floriday_latest_delivery_at', v.waarde)
4389
+ FROM (SELECT * FROM _vlda ORDER BY order_id LIMIT v_max) v
4390
+ WHERE o.id = v.order_id;
4391
+ GET DIAGNOSTICS v_bijgewerkt = ROW_COUNT;
4392
+
4393
+ RETURN jsonb_build_object('bijgewerkt', v_bijgewerkt, 'nog_te_doen', greatest(v_rest - v_bijgewerkt, 0),
4394
+ 'vanaf_leverdag', v_vandaag, 'bron', 'delivery.latestDeliveryDateTime');
4395
+ END $$;
4396
+
4397
+ REVOKE ALL ON FUNCTION public.verkoop_latest_delivery_aanvullen(integer) FROM PUBLIC, anon;
4398
+ GRANT EXECUTE ON FUNCTION public.verkoop_latest_delivery_aanvullen(integer) TO authenticated, service_role;
4399
+ REVOKE EXECUTE ON FUNCTION public.verkoop_latest_delivery_aanvullen(integer) FROM authenticated;