diff --git a/app/Jobs/ImportCSV.php b/app/Jobs/ImportCSV.php index 0f45a8e3..0242b276 100644 --- a/app/Jobs/ImportCSV.php +++ b/app/Jobs/ImportCSV.php @@ -47,69 +47,89 @@ public function handle(CSVImportReader $reader) $filepath = Storage::disk('local') ->path('mismatch-files/' . $this->meta->filename); - $db_mismatches_by_current_user = DB::select( - 'select * from users JOIN mismatches ON mismatches.user_id = users.id - WHERE mw_userid = :mw_userid', - ['mw_userid' =>$this->meta->user->mw_userid] - ); - - DB::transaction(function () use ($reader, $filepath, $db_mismatches_by_current_user) { + // $db_mismatches_by_current_user = DB::select( + // 'select * from users JOIN mismatches ON mismatches.user_id = users.id + // WHERE mw_userid = :mw_userid', + // ['mw_userid' =>$this->meta->user->mw_userid] + // ); + + $mismatches_per_upload_user = DB::table('mismatches'); + // ->join('users', 'mismatches.user_id', '=', 'users.id') + // ->where('mw_userid', '=', $this->meta->user->mw_userid); + + // var_dump($mismatches_per_upload_user->first()); + + $mismatch_attrs = (new Mismatch())->getAttributes(); + + $collection = collect($mismatch_attrs); + $collection->forget('review_status'); + $newArray = []; + $collection->map(function ($item, $key) use (&$newArray) { + if ($key != 'type') { // key can be empty in the file but in the db always has statement by default + $newArray[] = [$key, $item]; + } + }); + var_dump($newArray); - $reader->lines($filepath)->each(function ($mismatchLine) use ($db_mismatches_by_current_user) { + DB::transaction(function () use ($reader, $filepath, $mismatches_per_upload_user, $newArray) { - // TODO: question, should we list all columns one by one or try to do something like - // Schema::getColumnListing('mismatches'); // where is mismatches is the table name - // and then we remove the column names we dont need... id, username, mw_userid, created at, etc. - // too many. - // or this is the best option but it needs to be instantiated already to be able to get the attributes - // or not? - // get attributes from model instance - // $column_names = $new_mismatch->getAttributes(); - // remove column because we dont want to compare with review_status - // unset($column_names['review_status']); + $reader->lines($filepath)->each(function ($mismatchLine) use ($mismatches_per_upload_user, $newArray) { $new_mismatch = Mismatch::make($mismatchLine); - $collection = collect($new_mismatch->getAttributes()); - $collection->forget('review_status'); - $newArray = []; - $collection->map(function ($item, $key) use (&$newArray) { - if ($key != 'type') { // key can be empty in the file but in the db always has statement by default - $newArray[] = [$key, $item]; - } - }); - - if (!DB::table('mismatches')->where($newArray)->exists() // checks all fields at the same time - // || count($db_mismatches_by_current_user) == 0 //take this out of the lines check. if there are not imports by the current user we import - ) { - if ($new_mismatch->type == null) { - $new_mismatch->type = 'statement'; - } - $new_mismatch->importMeta()->associate($this->meta); - $new_mismatch->save(); + // $collection = collect($new_mismatch->getAttributes()); + // $collection->forget('review_status'); + // $newArray = []; + // $collection->map(function ($item, $key) use (&$newArray) { + // if ($key != 'type') { // key can be empty in the file but in the db always has statement by default + // $newArray[] = [$key, $item]; + // } + // }); + + // we add first because there might be duplicates already, so this might return more than 1 result + $row_in_db = $mismatches_per_upload_user->select($new_mismatch->getFillable())->where($newArray); + $row_in_db_get = $row_in_db->get(); + Log::info("row_in_db: " . json_encode($row_in_db_get)); + + // var_dump($mismatches_per_upload_user->get()->count()); + + // if ($mismatches_per_upload_user->get()->count() == 0) { + // if ($new_mismatch->type == null) { + // $new_mismatch->type = 'statement'; + // } + // $new_mismatch->importMeta()->associate($this->meta); + // $new_mismatch->save(); + // var_dump('we imported all rows because the user hasnt uploaded any mismatches'); + // } + if ($row_in_db->exists()) { + return; + } elseif (!$row_in_db->exists() + //|| ( $row_in_db->exists() && $row_in_db->first()->review_status == 'pending')) + ) { + $this->saveMismatch($new_mismatch); var_dump('we imported row that doesnt exist'); } // case 1. there are not mismatches from user in the DB - if (count($db_mismatches_by_current_user) == 0) { - if ($new_mismatch->type == null) { - $new_mismatch->type = 'statement'; - } - $new_mismatch->importMeta()->associate($this->meta); - $new_mismatch->save(); - var_dump('we imported all rows because the user hasnt uploaded any mismatches'); - } + // if ($mismatches_per_upload_user->count() == 0) { + // if ($new_mismatch->type == null) { + // $new_mismatch->type = 'statement'; + // } + // $new_mismatch->importMeta()->associate($this->meta); + // $new_mismatch->save(); + // var_dump('we imported all rows because the user hasnt uploaded any mismatches'); + // } // else { - foreach ($db_mismatches_by_current_user as $db_mismatch) { - // we keep all the mismatches that are pending regardless of values in other columns - // and stop checking this line - if ($db_mismatch->review_status == 'pending') { - $new_mismatch->importMeta()->associate($this->meta); - $new_mismatch->save(); - var_dump('we reimported any mismatches still marked as pending in the DB'); - } - } + // foreach ($db_mismatches_by_current_user as $db_mismatch) { + // // we keep all the mismatches that are pending regardless of values in other columns + // // and stop checking this line + // if ($db_mismatch->review_status == 'pending') { + // $new_mismatch->importMeta()->associate($this->meta); + // $new_mismatch->save(); + // var_dump('we reimported any mismatches still marked as pending in the DB'); + // } + // } // original // $new_mismatch = Mismatch::make($mismatchLine); @@ -120,8 +140,8 @@ public function handle(CSVImportReader $reader) // $new_mismatch->save(); }); - $this->meta->status = 'completed'; - $this->meta->save(); + // $this->meta->status = 'completed'; + // $this->meta->save(); }); } @@ -142,4 +162,20 @@ public function failed(Throwable $exception) $this->meta->status = 'failed'; $this->meta->save(); } + + /** + * Save mismatch to database + * + * @param \Mismatch $new_mismatch + * @return void + */ + 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(); + } }