Skip to content

Commit b68feba

Browse files
committed
Tryinnn
1 parent b951809 commit b68feba

File tree

1 file changed

+90
-54
lines changed

1 file changed

+90
-54
lines changed

app/Jobs/ImportCSV.php

Lines changed: 90 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -47,69 +47,89 @@ public function handle(CSVImportReader $reader)
4747
$filepath = Storage::disk('local')
4848
->path('mismatch-files/' . $this->meta->filename);
4949

50-
$db_mismatches_by_current_user = DB::select(
51-
'select * from users JOIN mismatches ON mismatches.user_id = users.id
52-
WHERE mw_userid = :mw_userid',
53-
['mw_userid' =>$this->meta->user->mw_userid]
54-
);
55-
56-
DB::transaction(function () use ($reader, $filepath, $db_mismatches_by_current_user) {
50+
// $db_mismatches_by_current_user = DB::select(
51+
// 'select * from users JOIN mismatches ON mismatches.user_id = users.id
52+
// WHERE mw_userid = :mw_userid',
53+
// ['mw_userid' =>$this->meta->user->mw_userid]
54+
// );
55+
56+
$mismatches_per_upload_user = DB::table('mismatches');
57+
// ->join('users', 'mismatches.user_id', '=', 'users.id')
58+
// ->where('mw_userid', '=', $this->meta->user->mw_userid);
59+
60+
// var_dump($mismatches_per_upload_user->first());
61+
62+
$mismatch_attrs = (new Mismatch())->getAttributes();
63+
64+
$collection = collect($mismatch_attrs);
65+
$collection->forget('review_status');
66+
$newArray = [];
67+
$collection->map(function ($item, $key) use (&$newArray) {
68+
if ($key != 'type') { // key can be empty in the file but in the db always has statement by default
69+
$newArray[] = [$key, $item];
70+
}
71+
});
5772

73+
var_dump($newArray);
5874

59-
$reader->lines($filepath)->each(function ($mismatchLine) use ($db_mismatches_by_current_user) {
75+
DB::transaction(function () use ($reader, $filepath, $mismatches_per_upload_user, $newArray) {
6076

61-
// TODO: question, should we list all columns one by one or try to do something like
62-
// Schema::getColumnListing('mismatches'); // where is mismatches is the table name
63-
// and then we remove the column names we dont need... id, username, mw_userid, created at, etc.
64-
// too many.
65-
// or this is the best option but it needs to be instantiated already to be able to get the attributes
66-
// or not?
67-
// get attributes from model instance
68-
// $column_names = $new_mismatch->getAttributes();
69-
// remove column because we dont want to compare with review_status
70-
// unset($column_names['review_status']);
77+
$reader->lines($filepath)->each(function ($mismatchLine) use ($mismatches_per_upload_user, $newArray) {
7178

7279
$new_mismatch = Mismatch::make($mismatchLine);
7380

74-
$collection = collect($new_mismatch->getAttributes());
75-
$collection->forget('review_status');
76-
$newArray = [];
77-
$collection->map(function ($item, $key) use (&$newArray) {
78-
if ($key != 'type') { // key can be empty in the file but in the db always has statement by default
79-
$newArray[] = [$key, $item];
80-
}
81-
});
82-
83-
if (!DB::table('mismatches')->where($newArray)->exists() // checks all fields at the same time
84-
// || 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
85-
) {
86-
if ($new_mismatch->type == null) {
87-
$new_mismatch->type = 'statement';
88-
}
89-
$new_mismatch->importMeta()->associate($this->meta);
90-
$new_mismatch->save();
81+
// $collection = collect($new_mismatch->getAttributes());
82+
// $collection->forget('review_status');
83+
// $newArray = [];
84+
// $collection->map(function ($item, $key) use (&$newArray) {
85+
// if ($key != 'type') { // key can be empty in the file but in the db always has statement by default
86+
// $newArray[] = [$key, $item];
87+
// }
88+
// });
89+
90+
// we add first because there might be duplicates already, so this might return more than 1 result
91+
$row_in_db = $mismatches_per_upload_user->select($new_mismatch->getFillable())->where($newArray);
92+
$row_in_db_get = $row_in_db->get();
93+
Log::info("row_in_db: " . json_encode($row_in_db_get));
94+
95+
// var_dump($mismatches_per_upload_user->get()->count());
96+
97+
// if ($mismatches_per_upload_user->get()->count() == 0) {
98+
// if ($new_mismatch->type == null) {
99+
// $new_mismatch->type = 'statement';
100+
// }
101+
// $new_mismatch->importMeta()->associate($this->meta);
102+
// $new_mismatch->save();
103+
// var_dump('we imported all rows because the user hasnt uploaded any mismatches');
104+
// }
105+
if ($row_in_db->exists()) {
106+
return;
107+
} elseif (!$row_in_db->exists()
108+
//|| ( $row_in_db->exists() && $row_in_db->first()->review_status == 'pending'))
109+
) {
110+
$this->saveMismatch($new_mismatch);
91111
var_dump('we imported row that doesnt exist');
92112
}
93113

94114
// case 1. there are not mismatches from user in the DB
95-
if (count($db_mismatches_by_current_user) == 0) {
96-
if ($new_mismatch->type == null) {
97-
$new_mismatch->type = 'statement';
98-
}
99-
$new_mismatch->importMeta()->associate($this->meta);
100-
$new_mismatch->save();
101-
var_dump('we imported all rows because the user hasnt uploaded any mismatches');
102-
}
115+
// if ($mismatches_per_upload_user->count() == 0) {
116+
// if ($new_mismatch->type == null) {
117+
// $new_mismatch->type = 'statement';
118+
// }
119+
// $new_mismatch->importMeta()->associate($this->meta);
120+
// $new_mismatch->save();
121+
// var_dump('we imported all rows because the user hasnt uploaded any mismatches');
122+
// }
103123
// else {
104-
foreach ($db_mismatches_by_current_user as $db_mismatch) {
105-
// we keep all the mismatches that are pending regardless of values in other columns
106-
// and stop checking this line
107-
if ($db_mismatch->review_status == 'pending') {
108-
$new_mismatch->importMeta()->associate($this->meta);
109-
$new_mismatch->save();
110-
var_dump('we reimported any mismatches still marked as pending in the DB');
111-
}
112-
}
124+
// foreach ($db_mismatches_by_current_user as $db_mismatch) {
125+
// // we keep all the mismatches that are pending regardless of values in other columns
126+
// // and stop checking this line
127+
// if ($db_mismatch->review_status == 'pending') {
128+
// $new_mismatch->importMeta()->associate($this->meta);
129+
// $new_mismatch->save();
130+
// var_dump('we reimported any mismatches still marked as pending in the DB');
131+
// }
132+
// }
113133

114134
// original
115135
// $new_mismatch = Mismatch::make($mismatchLine);
@@ -120,8 +140,8 @@ public function handle(CSVImportReader $reader)
120140
// $new_mismatch->save();
121141
});
122142

123-
$this->meta->status = 'completed';
124-
$this->meta->save();
143+
// $this->meta->status = 'completed';
144+
// $this->meta->save();
125145
});
126146
}
127147

@@ -142,4 +162,20 @@ public function failed(Throwable $exception)
142162
$this->meta->status = 'failed';
143163
$this->meta->save();
144164
}
165+
166+
/**
167+
* Save mismatch to database
168+
*
169+
* @param \Mismatch $new_mismatch
170+
* @return void
171+
*/
172+
private function saveMismatch($new_mismatch)
173+
{
174+
if ($new_mismatch->type == null) {
175+
$new_mismatch->type = 'statement';
176+
}
177+
// if review_status == pending -> save
178+
$new_mismatch->importMeta()->associate($this->meta);
179+
$new_mismatch->save();
180+
}
145181
}

0 commit comments

Comments
 (0)