30350n / inventree_part_import

CLI to import parts from suppliers like DigiKey, LCSC, Mouser, etc. to InvenTree
MIT License
24 stars 8 forks source link

Non-interactive update should fallback on existing supplier part on multiple resuls #26

Open sle118 opened 3 months ago

sle118 commented 3 months ago

In the case where we are updating existing products non-interactively and a search returns multiple possible choices, the system update existing entries from the match results. This could be done by getting a list of SKUs for the current supplier from InvenTree and then for each SKU having a match in the search results, update the data.

I have implemented a simple change in my instance that retrieves the first part from the current supplier and matches it against the results list. If no match is found (e.g. the SKU was filtered out upstream due to list truncation), then the process resumes as usual with a warning and skip the import.

            if len(results) == 1:
                api_part = results[0]
            elif self.interactive:
                prompt(f"found multiple parts at {supplier.name}, select which one to import")
                results = results[:get_config()["max_results"]]
                if result_count > len(results):
                    hint(f"found {result_count} results, only showing the first {len(results)}")
                if not (api_part := self.select_api_part(results)):
                    import_result |= ImportResult.INCOMPLETE
                    continue
            else:
                supplier_part = next(iter([mfg_part for mfg_part in existing_part.getSupplierParts() if existing_part and mfg_part.supplier == supplier.pk]),None)
                if supplier_part:
                    api_part = next(iter([p for p in results if p.SKU == supplier_part.SKU]))
                    warning(f"found {result_count} parts at {supplier.name}. Updating with existing SKU {supplier_part.SKU}: ")

                if not api_part:
                    warning(f"found {result_count} parts at {supplier_id.name}, skipping import")
                    import_result |= ImportResult.INCOMPLETE
                    continue

The ideal solution here would be to process process each result that has a match with the supplier parts from the InvenTree DB, but I can live with my current solution as I have at most 2 SKUs for a given part right now.