Skip to content

Commit

Permalink
Update type check
Browse files Browse the repository at this point in the history
  • Loading branch information
hasanakg committed Feb 19, 2024
1 parent e76d9ec commit c9f0495
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions app/Jobs/ImportCSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,15 @@ public function handle(CSVImportReader $reader)
&$where_clauses
) {

$new_mismatch = Mismatch::make($mismatchLine);
$new_mismatch = $this->createMismatch($mismatchLine);
$new_mismatches[] = $new_mismatch;
$mismatch_attributes = collect($new_mismatch->getAttributes());
$mismatch_attributes->forget('review_status');
$where_clause = [['review_status', '!=', 'pending']];
$mismatch_attributes->map(function ($attribute, $key) use (&$where_clause) {
// key can be empty in the file but in the db always has statement by default
if ($key != 'type' || $attribute != null) {
$where_clause[] = [$key, $attribute];
foreach ($new_mismatch->getAttributes() as $key => $attribute) {
if ($key == 'review_status') {
continue;
}
});

$where_clause[] = [$key, $attribute];
}
$where_clauses[] = $where_clause;
});

Expand All @@ -88,17 +85,15 @@ public function handle(CSVImportReader $reader)
$existing_mismatches = $mismatches_per_upload_user->get();

foreach ($new_mismatches as $new_mismatch) {
$isDuplicate = function ($value) use ($new_mismatch) {
if ($existing_mismatches->doesntContain(function ($value) use ($new_mismatch) {
$metaAttrs = $new_mismatch->getAttributes();
foreach ($metaAttrs as $attrKey => $attr) {
if ($attrKey != 'review_status' && $value->{$attrKey} != $attr) {
return false;
}
}
return true;
};

if (!$existing_mismatches->contains($isDuplicate)) {
})) {
$this->saveMismatch($new_mismatch);
}
}
Expand Down Expand Up @@ -126,6 +121,18 @@ public function failed(Throwable $exception)
$this->meta->save();
}


private function createMismatch($mismatch_data)
{
$new_mismatch = Mismatch::make($mismatch_data);

if ($new_mismatch->type == null) {
$new_mismatch->type = 'statement';
}

return $new_mismatch;
}

/**
* Save mismatch to database
*
Expand All @@ -134,9 +141,6 @@ public function failed(Throwable $exception)
*/
private function saveMismatch($new_mismatch)
{
if ($new_mismatch->type == null) {
$new_mismatch->type = 'statement';
}
// if review_status == pending -> save
$new_mismatch->importMeta()->associate($this->meta);
$new_mismatch->save();
Expand Down

0 comments on commit c9f0495

Please sign in to comment.