/** * GET /api/settings/shipping-carriers — carriers = M_ShipperCfg groups ("DHL", "DPD"): every shipper of a * configuration shares one contract. { status, carriers: [{ carrier, cfgId, shipperId (primary), shipperIds, shippers, label }] } * Shippers without a configuration are listed as their own carrier (label = shipper name). */ import { withIdempiere } from '../../utils/shipping/withIdempiere' import { loadShipperGroups } from '../../utils/shipping/carrierRates' export default defineEventHandler(withIdempiere(async (event, token) => { const groups = await loadShipperGroups(event, token) const byKey = new Map() for (const g of groups.values()) { const key = g.cfgId ? `cfg:${g.cfgId}` : `shipper:${g.shipperId}` if (byKey.has(key)) continue const names = g.shipperIds.map((id) => groups.get(id)?.shipperName || String(id)) byKey.set(key, { carrier: g.carrier, cfgId: g.cfgId, shipperId: g.primaryShipperId, shipperIds: g.shipperIds, shippers: names, label: g.cfgId ? `${g.carrier} (${names.join(', ')})` : g.shipperName }) } const carriers = [...byKey.values()].sort((a, b) => (a.cfgId ? 0 : 1) - (b.cfgId ? 0 : 1) || String(a.carrier).localeCompare(String(b.carrier))) return { status: 200, carriers } }))