diff --git a/app/Services/ImportProcessor.php b/app/Services/ImportProcessor.php index c4d8eaf..82508de 100644 --- a/app/Services/ImportProcessor.php +++ b/app/Services/ImportProcessor.php @@ -890,21 +890,25 @@ public function process(Import $import, ?Authenticatable $user = null): array } $failedRows[] = $rowNum; $invalid++; - ImportEvent::create([ - 'import_id' => $import->id, - 'user_id' => $user?->getAuthIdentifier(), - 'import_row_id' => $importRow?->id, - 'event' => 'row_exception', - 'level' => 'error', - 'message' => $this->safeErrorMessage($e->getMessage()), - 'context' => [ - 'classification' => $this->classifyRowException($e), - 'driver' => DB::connection()->getDriverName(), - 'row_number' => $rowNum, - 'raw_sha1' => isset($rawAssoc) ? sha1(json_encode($rawAssoc)) : null, - 'raw_data_preview' => isset($rawAssoc) ? $this->buildRawDataPreview($rawAssoc) : [], - ], - ]); + try { + ImportEvent::create([ + 'import_id' => $import->id, + 'user_id' => $user?->getAuthIdentifier(), + 'import_row_id' => $importRow?->id, // may be null if creation failed + 'event' => 'row_exception', + 'level' => 'error', + 'message' => $this->safeErrorMessage($e->getMessage()), + 'context' => [ + 'classification' => $this->classifyRowException($e), + 'driver' => DB::connection()->getDriverName(), + 'row_number' => $rowNum, + 'raw_sha1' => isset($rawAssoc) ? sha1(json_encode($rawAssoc)) : null, + 'raw_data_preview' => isset($rawAssoc) ? $this->buildRawDataPreview($rawAssoc) : [], + ], + ]); + } catch (\Throwable $evtErr) { + // Swallow secondary failure to ensure loop continues + } // Skip to next row without aborting whole import continue; @@ -1435,10 +1439,20 @@ private function upsertContractChain(Import $import, array $mapped, $mappings): } $data = array_filter($applyInsert, fn ($v) => ! is_null($v)); if (array_key_exists('start_date', $data)) { - $data['start_date'] = $this->normalizeDate(is_scalar($data['start_date']) ? (string) $data['start_date'] : null) ?? $data['start_date']; + $norm = $this->normalizeDate(is_scalar($data['start_date']) ? (string) $data['start_date'] : null); + if ($norm === null || $norm === '') { + unset($data['start_date']); // let default fill below + } else { + $data['start_date'] = $norm; + } } if (array_key_exists('end_date', $data)) { - $data['end_date'] = $this->normalizeDate(is_scalar($data['end_date']) ? (string) $data['end_date'] : null) ?? $data['end_date']; + $normEnd = $this->normalizeDate(is_scalar($data['end_date']) ? (string) $data['end_date'] : null); + if ($normEnd === null || $normEnd === '') { + unset($data['end_date']); // treat blank as null (omit) + } else { + $data['end_date'] = $normEnd; + } } $data['client_case_id'] = $clientCaseId; $data['reference'] = $reference;