import { Translate , DataProvider , Identifier } from "ra-core" ; import { processCsvFile } from "./csv-extractor" ; import { SimpleLogger } from "./SimpleLogger" ; import { ValidateRowFunction } from "./config.interface" ; function makeLogger ( logging ) { const logger = new SimpleLogger ( "import-controller" , true ) ; logger . setEnabled ( logging ) ; return logger ; } export async function GetIdsColliding ( logging , translate , dataProvider , csvValues , resourceName ) { const logger = makeLogger ( logging ) ; const hasIds = csvValues . some ( ( v ) => v . id ) ; if ( ! hasIds ) { return [ ] ; } try { const csvIds = csvValues . filter ( v => ! ! v . id ) . map ( ( v ) => v . id ) ; const recordsColliding = await dataProvider . getMany ( resourceName , { ids : csvIds , } ) ; const recordIdsColliding = recordsColliding . data . map ( ( r ) => r . id ) ; return recordIdsColliding ; } catch ( error ) { logger . error ( "GetIdsColliding" , { csvValues } , error ) ; throw translate ( "csv.parsing.collidingIds" ) ; } } export async function CheckCSVValidation ( logging , translate , csvValues , validateRow ? ) { const logger = makeLogger ( logging ) ; if ( ! validateRow ) { return ; } try { await Promise . all ( csvValues . map ( validateRow ) ) ; } catch ( error ) { logger . error ( "onFileAdded" , { csvValues } , error ) ; throw translate ( "csv.parsing.failedValidateRow" ) ; } } export async function GetCSVItems ( logging , translate , file , parseConfig ) { const logger = makeLogger ( logging ) ; let csvValues ; try { csvValues = await processCsvFile ( file , parseConfig ) ; return csvValues || [ ] ; } catch ( error ) { logger . error ( "onFileAdded" , { csvValues } , error ) ; throw translate ( "csv.parsing.invalidCsvDocument" ) ; } }