@@ -46,58 +46,16 @@ public function handle(CSVImportReader $reader)
4646 $ filepath = Storage::disk ('local ' )
4747 ->path ('mismatch-files/ ' . $ this ->meta ->filename );
4848
49- $ mismatch_attrs = (new Mismatch ())->getFillable ();
50-
51- DB ::transaction (function () use ($ reader , $ filepath , $ mismatch_attrs ) {
52- $ new_mismatches = [];
53- $ where_clauses = [];
54-
55- $ mismatches_per_upload_user = DB ::table ('mismatches ' )
56- ->select ($ mismatch_attrs )
57- ->join ('import_meta ' , 'mismatches.import_id ' , '= ' , 'import_meta.id ' )
58- ->where ('import_meta.user_id ' , '= ' , $ this ->meta ->user ->id );
59-
60- $ reader ->lines ($ filepath )->each (function ($ mismatchLine ) use (
61- &$ new_mismatches ,
62- &$ where_clauses
63- ) {
64-
65- $ new_mismatch = $ this ->createMismatch ($ mismatchLine );
66- $ new_mismatches [] = $ new_mismatch ;
67- $ where_clause = [['review_status ' , '!= ' , 'pending ' ]];
68- foreach ($ new_mismatch ->getAttributes () as $ key => $ attribute ) {
69- if ($ key == 'review_status ' ) {
70- continue ;
71- }
72- $ where_clause [] = [$ key , $ attribute ];
73- }
74- $ where_clauses [] = $ where_clause ;
75- });
76-
77- $ mismatches_per_upload_user ->where (function ($ query ) use ($ where_clauses ) {
78- foreach ($ where_clauses as $ where_clause ) {
79- $ query ->orWhere (function ($ query ) use ($ where_clause ) {
80- $ query ->where ($ where_clause );
81- });
49+ DB ::transaction (function () use ($ reader , $ filepath ) {
50+ $ reader ->lines ($ filepath )->each (function ($ mismatchLine ) {
51+ $ mismatch = Mismatch::make ($ mismatchLine );
52+ if ($ mismatch ->type == null ) {
53+ $ mismatch ->type = 'statement ' ;
8254 }
55+ $ mismatch ->importMeta ()->associate ($ this ->meta );
56+ $ mismatch ->save ();
8357 });
8458
85- $ existing_mismatches = $ mismatches_per_upload_user ->get ();
86-
87- foreach ($ new_mismatches as $ new_mismatch ) {
88- if ($ existing_mismatches ->doesntContain (function ($ value ) use ($ new_mismatch ) {
89- $ metaAttrs = $ new_mismatch ->getAttributes ();
90- foreach ($ metaAttrs as $ attrKey => $ attr ) {
91- if ($ attrKey != 'review_status ' && $ value ->{$ attrKey } != $ attr ) {
92- return false ;
93- }
94- }
95- return true ;
96- })) {
97- $ this ->saveMismatch ($ new_mismatch );
98- }
99- }
100-
10159 $ this ->meta ->status = 'completed ' ;
10260 $ this ->meta ->save ();
10361 });
@@ -106,7 +64,7 @@ public function handle(CSVImportReader $reader)
10664 /**
10765 * Handle a job failure.
10866 *
109- * @param \Throwable $exception
67+ * @param \Throwable $exception
11068 * @return void
11169 */
11270 public function failed (Throwable $ exception )
@@ -120,29 +78,4 @@ public function failed(Throwable $exception)
12078 $ this ->meta ->status = 'failed ' ;
12179 $ this ->meta ->save ();
12280 }
123-
124-
125- private function createMismatch ($ mismatch_data )
126- {
127- $ new_mismatch = Mismatch::make ($ mismatch_data );
128-
129- if ($ new_mismatch ->type == null ) {
130- $ new_mismatch ->type = 'statement ' ;
131- }
132-
133- return $ new_mismatch ;
134- }
135-
136- /**
137- * Save mismatch to database
138- *
139- * @param \Mismatch $new_mismatch
140- * @return void
141- */
142- private function saveMismatch ($ new_mismatch )
143- {
144- // if review_status == pending -> save
145- $ new_mismatch ->importMeta ()->associate ($ this ->meta );
146- $ new_mismatch ->save ();
147- }
14881}
0 commit comments