diff --git a/apps/edr-freight-web/backoffice/src/components/warehouses/useCompanyOptions.ts b/apps/edr-freight-web/backoffice/src/components/warehouses/useCompanyOptions.ts index ce8286454..2c80535b7 100644 --- a/apps/edr-freight-web/backoffice/src/components/warehouses/useCompanyOptions.ts +++ b/apps/edr-freight-web/backoffice/src/components/warehouses/useCompanyOptions.ts @@ -16,11 +16,22 @@ export function useCompanyOptions() { const companies = data?.items ?? []; + // Company names are not unique — Mantine throws on duplicate option values, + // so the list is deduped by the trimmed name. + const names = [...new Set(companies.map((c) => c.name.trim()).filter(Boolean))]; + return { loading: isLoading, - names: companies.map((c) => c.name), - /** Exact (case-insensitive) name match → company id, else undefined. */ - resolveId: (name: string): string | undefined => - companies.find((c) => c.name.trim().toLowerCase() === name.trim().toLowerCase())?.id, + names, + /** + * Name → company id, only when exactly one company carries that name. An + * ambiguous name resolves to nothing rather than to an arbitrary company: + * the container keeps the typed name and no wrong customer is attached. + */ + resolveId: (name: string): string | undefined => { + const key = name.trim().toLowerCase(); + const matches = companies.filter((c) => c.name.trim().toLowerCase() === key); + return matches.length === 1 ? matches[0].id : undefined; + }, }; }