@flowselections/mailbox-orders 1.0.6 → 1.0.8

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.
@@ -473,21 +473,57 @@ export const listProductsForPicker = createServerFn({ method: "GET" })
473
473
  const { data: stockRows } = await context.supabase
474
474
  .from("trade_item_stock")
475
475
  .select("trade_item_key, physical_stock, reserved_stock");
476
- const availableKeys = new Set();
476
+ const availByProductId = new Map();
477
+ const availByFloridayId = new Map();
478
+ const availByDraftId = new Map();
479
+ const addAvail = (m, k, v) => m.set(k, (m.get(k) ?? 0) + v);
477
480
  for (const s of Array.isArray(stockRows) ? stockRows : []) {
478
481
  const avail = Number(s.physical_stock ?? 0) - Number(s.reserved_stock ?? 0);
479
- if (avail > 0 && typeof s.trade_item_key === "string")
480
- availableKeys.add(s.trade_item_key);
482
+ if (typeof s.trade_item_key !== "string")
483
+ continue;
484
+ const key = s.trade_item_key;
485
+ const idx = key.indexOf(":");
486
+ if (idx > 0) {
487
+ const prefix = key.slice(0, idx);
488
+ const rest = key.slice(idx + 1);
489
+ if (prefix === "product")
490
+ addAvail(availByProductId, rest, avail);
491
+ else if (prefix === "draft")
492
+ addAvail(availByDraftId, rest, avail);
493
+ else
494
+ addAvail(availByFloridayId, key, avail);
495
+ }
496
+ else {
497
+ addAvail(availByFloridayId, key, avail);
498
+ }
499
+ }
500
+ // Drafts uit de Voorraad Module: koppel via linkedProductId → products.id
501
+ if (availByDraftId.size > 0) {
502
+ const { data: drafts } = await context.supabase
503
+ .from("trade_item_drafts")
504
+ .select("id, data, floriday_id")
505
+ .in("id", Array.from(availByDraftId.keys()));
506
+ for (const d of Array.isArray(drafts) ? drafts : []) {
507
+ const qty = availByDraftId.get(d.id) ?? 0;
508
+ const linked = d?.data?.linkedProductId;
509
+ if (typeof linked === "string" && linked)
510
+ addAvail(availByProductId, linked, qty);
511
+ const fid = d?.floriday_id;
512
+ if (typeof fid === "string" && fid)
513
+ addAvail(availByFloridayId, fid, qty);
514
+ }
481
515
  }
482
- if (availableKeys.size === 0)
483
- return { items: [] };
484
516
  const { data } = await context.supabase
485
517
  .from("products")
486
518
  .select("id, product, unit, plants_per_crate, crates_per_layer, layers_per_cart, custom_fields")
487
519
  .order("product").range(0, 4999);
488
- const items = (Array.isArray(data) ? data : []).filter((p) => {
520
+ const items = (Array.isArray(data) ? data : [])
521
+ .map((p) => {
489
522
  const fid = p?.custom_fields?.floriday_id;
490
- return typeof fid === "string" && availableKeys.has(fid);
523
+ const byProd = availByProductId.get(p.id) ?? 0;
524
+ const byFid = typeof fid === "string" ? (availByFloridayId.get(fid) ?? 0) : 0;
525
+ const available_stock = byProd + byFid;
526
+ return { ...p, available_stock };
491
527
  });
492
528
  return { items };
493
529
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowselections/mailbox-orders",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "private": false,
5
5
  "sideEffects": false,
6
6
  "type": "module",