importCSV extract records from csv file, and save them to DB using recordHandler the rawDataParams is used to identify the data source, the recordHandler is used to handle the record, it should return an error if the record is invalid the `created_at` and `updated_at` will be set to the current time
(file io.ReadCloser, rawDataParams string, recordHandler func(map[string]interface{}) errors.Error)
| 235 | // the recordHandler is used to handle the record, it should return an error if the record is invalid |
| 236 | // the `created_at` and `updated_at` will be set to the current time |
| 237 | func (s *Service) importCSV(file io.ReadCloser, rawDataParams string, recordHandler func(map[string]interface{}) errors.Error) errors.Error { |
| 238 | iterator, err := pluginhelper.NewCsvFileIteratorFromFile(file) |
| 239 | if err != nil { |
| 240 | return err |
| 241 | } |
| 242 | var hasNext bool |
| 243 | var line int |
| 244 | now := time.Now() |
| 245 | for { |
| 246 | line++ |
| 247 | if hasNext, err = iterator.HasNextWithError(); !hasNext { |
| 248 | return errors.BadInput.Wrap(err, fmt.Sprintf("error on processing the line:%d", line)) |
| 249 | } else { |
| 250 | record := iterator.Fetch() |
| 251 | record["_raw_data_params"] = rawDataParams |
| 252 | for k, v := range record { |
| 253 | if v.(string) == "NULL" { |
| 254 | record[k] = nil |
| 255 | } |
| 256 | } |
| 257 | record["created_at"] = now |
| 258 | record["updated_at"] = now |
| 259 | err = recordHandler(record) |
| 260 | if err != nil { |
| 261 | return errors.BadInput.Wrap(err, fmt.Sprintf("error on processing the line:%d", line)) |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // createOrUpdateAccount creates or updates an account based on the provided name. |
| 268 | // It returns the account ID and an error if any occurred. |
no test coverage detected