diff --git a/app/Jobs/ImportCSV.php b/app/Jobs/ImportCSV.php index 0f45a8e3..358eb426 100644 --- a/app/Jobs/ImportCSV.php +++ b/app/Jobs/ImportCSV.php @@ -47,27 +47,39 @@ 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_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) { + $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()); - $reader->lines($filepath)->each(function ($mismatchLine) use ($db_mismatches_by_current_user) { + $mismatches_per_upload_user_get = $mismatches_per_upload_user->get(); + Log::info('$mismatches_per_upload_user:' . json_encode($mismatches_per_upload_user_get)); - // 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']); + $mismatch_attrs = (new Mismatch())->getFillable(); + Log::info('mismatch_attrs', $mismatch_attrs); + // ["item_id","statement_guid","property_id","wikidata_value","meta_wikidata_value","external_value","external_url","review_status","type"] + + // $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]; + // } + // }); + + // Log::info('$newArray', $newArray); + + DB::transaction(function () use ($reader, $filepath, $mismatches_per_upload_user, $mismatch_attrs) { + + $reader->lines($filepath)->each(function ($mismatchLine) use ($mismatches_per_upload_user, $mismatch_attrs) { $new_mismatch = Mismatch::make($mismatchLine); @@ -80,36 +92,47 @@ public function handle(CSVImportReader $reader) } }); - 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(); + // 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($mismatch_attrs)->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->doesntExist() + //|| ( $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 +143,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 +165,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(); + } }