@flowselections/floriday-authenticatie-module 1.0.62 → 1.0.65

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.
@@ -5,7 +5,101 @@
5
5
  // submissionError }. Pas bij ACCEPTED/DELIVERED levert Floriday de
6
6
  // logistieke labels (verzendbrief) betrouwbaar op.
7
7
  import { resolveGatewayEnvironment } from "./shared.js";
8
- import { SUPPLIERS_API_VERSION, florydayFetch } from "../floriday.functions.js";
8
+ import { SUPPLIERS_API_VERSION, florydayFetch, getActiveFlorydayCredentials } from "../floriday.functions.js";
9
+ import { deriveFloridayStage, isDeliveryNoteReady, readIncoterm } from "../floriday-order-stage.js";
10
+ const MAX_SALES_ORDER_IDS = 100;
11
+ /**
12
+ * Statusoverzicht per verkooporder, alleen uit de caches (geen Floriday-
13
+ * aanroep): orderstatus, uitleveringen, fulfillmentRequestIds, floridayStage
14
+ * en de ordervelden. Een order die niet in de cache staat krijgt stage
15
+ * "unknown" — geen fout. Wijzigt nooit een orderstatus.
16
+ */
17
+ export async function runSalesOrderStatusOverview(supabase, input) {
18
+ const ids = Array.from(new Set(input.salesOrderIds.map((s) => String(s).trim()).filter(Boolean)));
19
+ if (ids.length === 0 || ids.length > MAX_SALES_ORDER_IDS) {
20
+ return { status: 400, payload: { ok: false, error: `salesOrderIds: 1 tot ${MAX_SALES_ORDER_IDS} id's` } };
21
+ }
22
+ const env = await resolveGatewayEnvironment(supabase, input?.environment);
23
+ const conn = await getActiveFlorydayCredentials(supabase, env);
24
+ if (!conn)
25
+ return { status: 409, payload: { ok: false, error: `Geen actieve Floriday-koppeling voor ${env}` } };
26
+ const db = supabase;
27
+ const { data: soRows } = await db.from("floriday_salesorders_cache")
28
+ .select("floriday_id, status, buyer_organization_id, data, updated_at")
29
+ .eq("connection_id", conn.id).in("floriday_id", ids);
30
+ const soById = new Map((soRows ?? []).map((r) => [String(r.floriday_id), r]));
31
+ // fulfillmentRequests per verkooporder uit de afleveropdrachten.
32
+ const requests = new Map();
33
+ await Promise.all(ids.map(async (sid) => {
34
+ const { data } = await db.from("floriday_delivery_orders_cache")
35
+ .select("floriday_id, data").eq("connection_id", conn.id)
36
+ .filter("data->fulfillmentRequests", "cs", JSON.stringify([{ salesOrderId: sid }])).limit(20);
37
+ const list = [];
38
+ for (const d of data ?? []) {
39
+ for (const r of (d.data?.fulfillmentRequests ?? [])) {
40
+ if (String(r?.salesOrderId) !== sid || !r?.fulfillmentRequestId)
41
+ continue;
42
+ list.push({
43
+ fulfillmentRequestId: String(r.fulfillmentRequestId),
44
+ deliveryOrderId: String(d.floriday_id),
45
+ fulfillmentOrderIds: Array.from(new Set((r.fulfillmentOrderClaims ?? [])
46
+ .map((c) => c?.fulfillmentOrderId).filter(Boolean).map(String))),
47
+ });
48
+ }
49
+ }
50
+ requests.set(sid, list);
51
+ }));
52
+ const foIds = Array.from(new Set(Array.from(requests.values()).flat().flatMap((r) => r.fulfillmentOrderIds)));
53
+ const foStatus = new Map();
54
+ if (foIds.length) {
55
+ const { data } = await db.from("floriday_fulfillment_orders_cache")
56
+ .select("floriday_id, status, data").eq("connection_id", conn.id).in("floriday_id", foIds);
57
+ for (const f of data ?? [])
58
+ foStatus.set(String(f.floriday_id), f.status ?? f.data?.status ?? null);
59
+ }
60
+ const buyerIds = Array.from(new Set((soRows ?? [])
61
+ .map((r) => r.data?.customerOrganizationId ?? r.buyer_organization_id).filter(Boolean).map(String)));
62
+ const glnById = new Map();
63
+ if (buyerIds.length) {
64
+ const { data } = await db.from("floriday_customers_cache")
65
+ .select("organization_id, gln_code").in("organization_id", buyerIds);
66
+ for (const c of data ?? [])
67
+ glnById.set(String(c.organization_id), c.gln_code ?? null);
68
+ }
69
+ const orders = ids.map((sid) => {
70
+ const so = soById.get(sid);
71
+ const reqs = requests.get(sid) ?? [];
72
+ const fulfillmentOrders = Array.from(new Set(reqs.flatMap((r) => r.fulfillmentOrderIds)))
73
+ .map((id) => {
74
+ const status = foStatus.get(id) ?? null;
75
+ return { fulfillmentOrderId: id, status, deliveryNoteReady: isDeliveryNoteReady(status) };
76
+ });
77
+ if (!so) {
78
+ return { salesOrderId: sid, found: false, floridayStage: "unknown",
79
+ fulfillmentRequests: reqs, fulfillmentOrders };
80
+ }
81
+ const d = so.data ?? {};
82
+ const buyer = d.customerOrganizationId ?? so.buyer_organization_id ?? null;
83
+ return {
84
+ salesOrderId: sid,
85
+ found: true,
86
+ salesOrderStatus: so.status ?? d.status ?? null,
87
+ floridayStage: deriveFloridayStage(so.status ?? d.status, fulfillmentOrders.map((f) => f.status)),
88
+ fulfillmentRequests: reqs,
89
+ fulfillmentOrders,
90
+ salesChannel: d.salesChannel ?? null,
91
+ tradeInstrument: d.tradeInstrument ?? null,
92
+ incoterm: readIncoterm(d),
93
+ customerOrderId: d.customerOrderId ?? null,
94
+ salesChannelOrderId: d.salesChannelOrderId ?? null,
95
+ customerOrganizationId: buyer,
96
+ customerGln: buyer ? glnById.get(String(buyer)) ?? null : null,
97
+ lastModifiedDateTime: d.lastModifiedDateTime ?? null,
98
+ cachedAt: so.updated_at ?? null,
99
+ };
100
+ });
101
+ return { status: 200, payload: { ok: true, environment: env, orders } };
102
+ }
9
103
  export async function runFulfillmentOrderStatus(supabase, input) {
10
104
  const fulfillmentOrderId = String(input?.fulfillmentOrderId ?? "").trim();
11
105
  if (!fulfillmentOrderId) {
@@ -71,6 +165,18 @@ export async function handleFulfillmentOrderStatus(request, supabase) {
71
165
  body.environment !== "live") {
72
166
  return _fosStatusJson({ ok: false, error: "Ongeldige environment" }, { status: 400 });
73
167
  }
168
+ if (Array.isArray(body?.salesOrderIds)) {
169
+ try {
170
+ const { status, payload } = await runSalesOrderStatusOverview(supabase, {
171
+ salesOrderIds: body.salesOrderIds,
172
+ environment: body?.environment,
173
+ });
174
+ return _fosStatusJson(payload, { status });
175
+ }
176
+ catch (e) {
177
+ return _fosStatusJson({ ok: false, error: String(e?.message ?? e) }, { status: 500 });
178
+ }
179
+ }
74
180
  try {
75
181
  const { status, payload } = await runFulfillmentOrderStatus(supabase, {
76
182
  fulfillmentOrderId: String(body?.fulfillmentOrderId ?? ""),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowselections/floriday-authenticatie-module",
3
- "version": "1.0.62",
3
+ "version": "1.0.65",
4
4
  "sideEffects": false,
5
5
  "type": "module",
6
6
  "main": "./dist-lib/index.js",