@flowselections/floriday-verkoop-module 1.0.8 → 1.0.10

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.
Files changed (35) hide show
  1. package/dist-lib/components/floriday/FlorydaySyncHealthBanner.d.ts +9 -0
  2. package/dist-lib/components/floriday/FlorydaySyncHealthBanner.d.ts.map +1 -0
  3. package/dist-lib/components/floriday/FlorydaySyncHealthBanner.js +26 -0
  4. package/dist-lib/components/verkoop/CreateOrderPage.d.ts.map +1 -1
  5. package/dist-lib/components/verkoop/CreateOrderPage.js +6 -9
  6. package/dist-lib/components/verkoop/VerkoopOrdersPage.d.ts.map +1 -1
  7. package/dist-lib/components/verkoop/VerkoopOrdersPage.js +2 -1
  8. package/dist-lib/integrations/supabase/auth-middleware.d.ts +2797 -144
  9. package/dist-lib/integrations/supabase/auth-middleware.d.ts.map +1 -1
  10. package/dist-lib/integrations/supabase/client.d.ts +2797 -144
  11. package/dist-lib/integrations/supabase/client.d.ts.map +1 -1
  12. package/dist-lib/integrations/supabase/client.server.d.ts +2797 -144
  13. package/dist-lib/integrations/supabase/client.server.d.ts.map +1 -1
  14. package/dist-lib/integrations/supabase/types.d.ts +2870 -134
  15. package/dist-lib/integrations/supabase/types.d.ts.map +1 -1
  16. package/dist-lib/integrations/supabase/types.js +2 -0
  17. package/dist-lib/lib/floriday-gateway.server.d.ts +42 -0
  18. package/dist-lib/lib/floriday-gateway.server.d.ts.map +1 -0
  19. package/dist-lib/lib/floriday-gateway.server.js +217 -0
  20. package/dist-lib/lib/floriday-public.functions.d.ts +65056 -0
  21. package/dist-lib/lib/floriday-public.functions.d.ts.map +1 -0
  22. package/dist-lib/lib/floriday-public.functions.js +210 -0
  23. package/dist-lib/lib/floriday-sync.d.ts.map +1 -1
  24. package/dist-lib/lib/floriday-sync.js +18 -79
  25. package/dist-lib/lib/floriday-writes.functions.d.ts +46453 -0
  26. package/dist-lib/lib/floriday-writes.functions.d.ts.map +1 -0
  27. package/dist-lib/lib/floriday-writes.functions.js +110 -0
  28. package/dist-lib/lib/salesorders.functions.d.ts +17670 -9710
  29. package/dist-lib/lib/salesorders.functions.d.ts.map +1 -1
  30. package/dist-lib/lib/salesorders.functions.js +44 -86
  31. package/dist-lib/lib/tradeitems.functions.d.ts +2797 -144
  32. package/dist-lib/lib/tradeitems.functions.d.ts.map +1 -1
  33. package/dist-lib/lib/useSalesOrdersQuery.d.ts.map +1 -1
  34. package/dist-lib/lib/useSalesOrdersQuery.js +11 -25
  35. package/package.json +2 -2
@@ -1,9 +1,9 @@
1
1
  // Sales orders sync (Floriday sequence-pattern) + write actions.
2
- // Reads tokens read-only from floriday_connections; never refreshes them here.
2
+ // Alle Floriday HTTP-calls lopen via floriday-gateway.server.
3
3
  import { createServerFn } from "@tanstack/react-start";
4
4
  import { z } from "zod";
5
5
  import { requireSupabaseAuth } from "../integrations/supabase/auth-middleware";
6
- import { loadActiveConnections, syncBySequence, FLORIDAY_API_BASES, } from "./floriday-sync";
6
+ import { loadActiveConnections, syncBySequence, } from "./floriday-sync";
7
7
  const ENTITY_TYPE = "salesorders";
8
8
  const MAX_ENDPOINT = "/sales-orders/current-max-sequence";
9
9
  const SYNC_ENDPOINT_BASE = "/sales-orders/sync";
@@ -37,7 +37,6 @@ async function runSalesOrdersSyncInternal() {
37
37
  results.push(r);
38
38
  }
39
39
  catch (err) {
40
- // Log per-connectie fout in floriday_sync_state.last_error en ga door.
41
40
  await supabaseAdmin.from("floriday_sync_state").upsert({
42
41
  connection_id: conn.id,
43
42
  entity_type: ENTITY_TYPE,
@@ -56,15 +55,13 @@ async function runSalesOrdersSyncInternal() {
56
55
  }
57
56
  return { ok: results.every((r) => r.ok), results };
58
57
  }
59
- // Public helper for cron route (no middleware — protected by /api/public + DB-only secrets)
60
58
  export async function runSalesOrdersSyncFromCron() {
61
59
  return runSalesOrdersSyncInternal();
62
60
  }
63
- // Manual trigger from authenticated UI / admin
64
61
  export const runSalesOrdersSync = createServerFn({ method: "POST" })
65
62
  .middleware([requireSupabaseAuth])
66
63
  .handler(async () => runSalesOrdersSyncInternal());
67
- // ─── Write actions: createSalesOrder ──────────────────────────────────────
64
+ // ─── Write actions ────────────────────────────────────────────────────────
68
65
  const CreateOrderSchema = z.object({
69
66
  customerOrganizationId: z.string().min(1),
70
67
  deliveryDateTime: z.string().min(1),
@@ -77,41 +74,24 @@ const CreateOrderSchema = z.object({
77
74
  .min(1),
78
75
  reference: z.string().optional(),
79
76
  });
80
- function apiBaseFor(env) {
81
- return env === "production" || env === "prod"
82
- ? FLORIDAY_API_BASES.production
83
- : FLORIDAY_API_BASES.staging;
84
- }
85
- async function pickActiveConnection() {
77
+ async function readActiveEnvironment() {
86
78
  const { supabaseAdmin } = await import("../integrations/supabase/client.server");
87
- const { data: settings } = await supabaseAdmin
79
+ const { data } = await supabaseAdmin
88
80
  .from("floriday_settings")
89
81
  .select("active_environment")
90
82
  .eq("id", 1)
91
83
  .maybeSingle();
92
- const env = (settings?.active_environment ?? "staging");
93
- const { data, error } = await supabaseAdmin
94
- .from("floriday_connections")
95
- .select("id, environment, access_token, api_key, is_active, gln_code")
96
- .eq("environment", env)
97
- .eq("is_active", true)
98
- .order("updated_at", { ascending: false })
99
- .limit(1)
100
- .maybeSingle();
101
- if (error)
102
- throw new Error(`Floriday connectie ophalen mislukt: ${error.message}`);
103
- if (!data)
104
- throw new Error(`Geen actieve Floriday connectie voor omgeving "${env}"`);
105
- return data;
84
+ const env = String(data?.active_environment ?? "staging");
85
+ return env === "production" || env === "prod" || env === "live" ? "live" : "staging";
106
86
  }
107
87
  export const createSalesOrder = createServerFn({ method: "POST" })
108
88
  .middleware([requireSupabaseAuth])
109
89
  .inputValidator((data) => CreateOrderSchema.parse(data))
110
- .handler(async ({ data }) => {
111
- const conn = await pickActiveConnection();
112
- if (!conn.access_token) {
113
- throw new Error("Floriday access_token ontbreekt — auth-module moet eerst draaien");
114
- }
90
+ .handler(async ({ data, context }) => {
91
+ const env = await readActiveEnvironment();
92
+ const { supabaseAdmin } = await import("../integrations/supabase/client.server");
93
+ const { florydayWrite, pickActiveConnection } = await import("./floriday-gateway.server");
94
+ const conn = await pickActiveConnection(supabaseAdmin, env);
115
95
  const body = {
116
96
  orderReference: data.reference ?? `ORD-${Date.now()}`,
117
97
  deliveryDateTime: data.deliveryDateTime,
@@ -123,59 +103,38 @@ export const createSalesOrder = createServerFn({ method: "POST" })
123
103
  pricePerPiece: { value: l.pricePerPiece, currency: "EUR" },
124
104
  })),
125
105
  };
126
- const url = `${apiBaseFor(conn.environment)}/sales-orders`;
127
- const res = await fetch(url, {
106
+ const result = await florydayWrite(supabaseAdmin, env, {
107
+ module: "sales",
108
+ action: "create-sales-order",
128
109
  method: "POST",
129
- headers: {
130
- "Content-Type": "application/json",
131
- Accept: "application/json",
132
- Authorization: `Bearer ${conn.access_token}`,
133
- ...(conn.api_key ? { "x-api-key": conn.api_key } : {}),
134
- },
135
- body: JSON.stringify(body),
136
- });
137
- const text = await res.text();
138
- if (!res.ok) {
139
- throw new Error(`Floriday POST /sales-orders [${res.status}]: ${text.slice(0, 400)}`);
110
+ path: "/sales-orders",
111
+ referenceId: data.reference ?? null,
112
+ body,
113
+ }, context.userId);
114
+ if (!result.ok) {
115
+ throw new Error(result.error ?? `Floriday POST /sales-orders faalde (status ${result.status})`);
140
116
  }
141
- const result = text ? JSON.parse(text) : null;
142
- return { id: result?.id ?? result?.salesOrderId ?? null, ok: true };
117
+ const r = result.data;
118
+ return { id: r?.id ?? r?.salesOrderId ?? null, ok: true, attemptId: result.attemptId };
143
119
  });
144
- // ─── Diagnose: /current-max-sequence + lokale cron-status ─────────────────
145
- // We bellen alleen /sales-orders/current-max-sequence; de oude /organisation-call
146
- // bestaat niet op suppliers-api-2026v1 (404 route-not-found). Tenant-identiteit
147
- // tonen we op basis van de lokaal opgeslagen gln_code uit floriday_connections.
120
+ // ─── Diagnose ─────────────────────────────────────────────────────────────
148
121
  export const diagnoseFloridayConnection = createServerFn({ method: "POST" })
149
122
  .middleware([requireSupabaseAuth])
150
123
  .handler(async () => {
151
- const conn = await pickActiveConnection();
152
- const base = apiBaseFor(conn.environment);
153
- const headers = {
154
- Accept: "application/json",
155
- Authorization: `Bearer ${conn.access_token ?? ""}`,
156
- ...(conn.api_key ? { "x-api-key": conn.api_key } : {}),
157
- };
158
- const res = await fetch(`${base}/sales-orders/current-max-sequence`, {
124
+ const env = await readActiveEnvironment();
125
+ const { supabaseAdmin } = await import("../integrations/supabase/client.server");
126
+ const { florydayFetch, pickActiveConnection } = await import("./floriday-gateway.server");
127
+ const conn = await pickActiveConnection(supabaseAdmin, env);
128
+ const maxRes = await florydayFetch(supabaseAdmin, conn, {
159
129
  method: "GET",
160
- headers,
130
+ path: MAX_ENDPOINT,
161
131
  });
162
- const text = await res.text();
163
- let body = null;
164
- try {
165
- body = text ? JSON.parse(text) : null;
166
- }
167
- catch {
168
- body = null;
169
- }
170
- const maxOk = res.ok;
171
- const maxValue = maxOk
172
- ? Number((typeof body === "number"
173
- ? body
174
- : body?.maxSequenceNumber ?? body?.sequenceNumber ?? 0) ?? 0)
132
+ const maxValue = maxRes.ok
133
+ ? Number((typeof maxRes.data === "number"
134
+ ? maxRes.data
135
+ : maxRes.data?.maxSequenceNumber ?? maxRes.data?.sequenceNumber ?? 0) ?? 0)
175
136
  : null;
176
- const { supabaseAdmin } = await import("../integrations/supabase/client.server");
177
- // Bij een succesvolle max-call: wis een eventuele stale last_error.
178
- if (maxOk) {
137
+ if (maxRes.ok) {
179
138
  await supabaseAdmin.from("floriday_sync_state").upsert({
180
139
  connection_id: conn.id,
181
140
  entity_type: ENTITY_TYPE,
@@ -191,12 +150,6 @@ export const diagnoseFloridayConnection = createServerFn({ method: "POST" })
191
150
  .maybeSingle();
192
151
  const lastSeq = Number(state?.last_sequence_number ?? 0) || 0;
193
152
  const lastSyncedAt = state?.last_synced_at ?? null;
194
- const cronHealth = {
195
- lastSyncedAt,
196
- lastSequenceNumber: lastSeq,
197
- behindBy: maxValue !== null ? Math.max(0, maxValue - lastSeq) : null,
198
- neverSynced: lastSyncedAt === null,
199
- };
200
153
  return {
201
154
  connectionId: conn.id,
202
155
  environment: conn.environment,
@@ -205,12 +158,17 @@ export const diagnoseFloridayConnection = createServerFn({ method: "POST" })
205
158
  source: "floriday_connections",
206
159
  },
207
160
  maxSequence: {
208
- ok: maxOk,
209
- status: res.status,
161
+ ok: maxRes.ok,
162
+ status: maxRes.status,
210
163
  value: maxValue,
211
- rawError: maxOk ? null : text.slice(0, 400),
164
+ rawError: maxRes.ok ? null : maxRes.rawText.slice(0, 400),
165
+ },
166
+ cronHealth: {
167
+ lastSyncedAt,
168
+ lastSequenceNumber: lastSeq,
169
+ behindBy: maxValue !== null ? Math.max(0, maxValue - lastSeq) : null,
170
+ neverSynced: lastSyncedAt === null,
212
171
  },
213
- cronHealth,
214
172
  syncState: state ?? null,
215
173
  };
216
174
  });