From 70e9b933d97315b1938d9de01153f1d21ca57632 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 16 Sep 2022 05:22:04 +0000 Subject: [PATCH] Add output to command. #704 --- src/Command/LoadWarrantySerialCommand.php | 111 +++++++++++++++++----- src/Entity/WarrantySerialUploadLog.php | 4 +- 2 files changed, 89 insertions(+), 26 deletions(-) diff --git a/src/Command/LoadWarrantySerialCommand.php b/src/Command/LoadWarrantySerialCommand.php index 026ff251..c4e35af5 100644 --- a/src/Command/LoadWarrantySerialCommand.php +++ b/src/Command/LoadWarrantySerialCommand.php @@ -69,22 +69,21 @@ class LoadWarrantySerialCommand extends Command $user_id = $row['api_user']; $ws_file_id = $row['id']; - $fname = $this->project_dir . '/public/warranty_serial_uploads/' . $filename; - - // TODO: need to consider what happens if file cannot be read. - // if file can be read, output_info will have an array of objects - // which can be used to transform into data - // if file cannot be read, output_info will have an array of strings - $output_info[] = $this->processWarrantySerialFile($fname, $user_id); + $output_info[] = $this->processWarrantySerialFile($filename, $user_id); $this->updateWarrantySerialQueue($ws_file_id); + + // send results back to third party + $this->sendResults($output_info); } return 0; } - protected function processWarrantySerialFile($csv_file, $user_id) + protected function processWarrantySerialFile($filename, $user_id) { + $csv_file = $this->project_dir . '/public/warranty_serial_uploads/' . $filename; + $output_info = []; // attempt to open file @@ -102,22 +101,26 @@ class LoadWarrantySerialCommand extends Command ]; $this->upload_logger->logWarrantySerialUploadInfo($log_data); - $output_info = $this->setOutputInfo('', 'not_uploaded', true, $error); + $output_info = $this->setOutputInfo($filename, true, $error, $data); return $output_info; } + $data = []; while(($row = fgetcsv($fh)) !== false) { $validation_result = $this->validateRow($row, $user_id); if (!empty($validation_result)) { - $output_info[] = $validation_result; + $data[] = $validation_result; continue; } // valid entry, we parse and insert $serial = trim(strtoupper($row[0])); + + error_log('Processing ' . $serial); + $sku = trim(strtoupper($row[1])); $dispatch_status = trim($row[2]); $str_date_create = trim($row[3]); @@ -162,17 +165,30 @@ class LoadWarrantySerialCommand extends Command $error = $err[2]; $this->logLoadInfo($user_id, false, $serial, $error); - $output_info = $this->setOutputInfo($serial, 'not uploaded', true, $error); + $data = [ + 'serial' => $serial, + 'status' => 'error', + 'has_error' => true, + 'error_message' => $error, + ]; } else { // log the successful insert $this->logLoadInfo($user_id, true, $serial, ''); - $output_info = $this->setOutputInfo($serial, 'uploaded', false, ''); + $data = [ + 'serial' => $serial, + 'status' => 'success', + 'has_error' => false, + 'error_message' => '', + ]; } } + // form what we output + $output_info = $this->setOutputInfo($filename, false, '', $data); + return $output_info; } @@ -191,15 +207,31 @@ class LoadWarrantySerialCommand extends Command // check if the line is a header if ($row[0] == 'SerialNumber') { - // no need to log, just ignore and skip - return false; + // no need to log, but send back error + $error = 'Invalid information.'; + $data = [ + 'serial' => '', + 'status' => 'error', + 'has_error' => true, + 'error_message' => $error, + ]; + + return $data; } // check if empty line if ($row == array(null)) { - // no need to log, just ignore and skip - return false; + // no need to log, but send back error + $error = 'Empty line'; + $data = [ + 'serial' => '', + 'status' => 'error', + 'has_error' => true, + 'error_message' => $error, + ]; + + return $data; } // check if empty serial @@ -209,7 +241,12 @@ class LoadWarrantySerialCommand extends Command $error = 'Empty serial'; $this->logLoadInfo($user_id, false, '', $error); - $data = $this->setOutputInfo('', 'not_uploaded', true, $error); + $data = [ + 'serial' => '', + 'status' => 'error', + 'has_error' => true, + 'error_message' => $error, + ]; return $data; } @@ -222,10 +259,15 @@ class LoadWarrantySerialCommand extends Command if ($date_create == null) { // log - $error = 'Invalid date create'; + $error = 'Invalid date create.'; $this->logLoadInfo($user_id, false, $serial, $error); - $data = $this->setOutputInfo($serial, 'not_uploaded', true, $error); + $data = [ + 'serial' => $serial, + 'status' => 'error', + 'has_error' => true, + 'error_message' => $error, + ]; return $data; } @@ -235,10 +277,15 @@ class LoadWarrantySerialCommand extends Command if ($existing_serial != null) { // log - $error = 'Duplicate serial'; + $error = 'Serial already exists.'; $this->logLoadInfo($user_id, false, $serial, $error); - $data = $this->setOutputInfo($serial, 'not_uploaded', true, $error) + $data = [ + 'serial' => $serial, + 'status' => 'error', + 'has_error' => true, + 'error_message' => $error, + ]; return $data; } @@ -307,16 +354,30 @@ class LoadWarrantySerialCommand extends Command ]); } - protected function setOutputInfo($serial, $status, $has_error, $error_message) + protected function setOutputInfo($filename, $has_error, $error_message, $entries) { + // need to get the original filename from warranty_serial_upload_log + // use the uploaded_file_serial which has the saved csv file + $upload_entry = $this->em->getRepository(WarrantySerialUploadLog::class)->findOneBy(['uploaded_file_serial' => $filename]); + $original_filename = $upload_entry->getOrigFileSerial(); + $info = [ - 'serial' => $serial, - 'status' => $status, + 'filename' => $original_filename, 'has_error' => $has_error, - 'error_message' => $error_message + 'error_message' => $error_message, + 'data' => $entries, ]; return $info; } + protected function sendResults($output_info) + { + $json_output = json_encode($output_info); + + error_log(print_r($json_output, true)); + + // TODO: sent json output somewhere + } + } diff --git a/src/Entity/WarrantySerialUploadLog.php b/src/Entity/WarrantySerialUploadLog.php index 44f5b16a..12b97d67 100644 --- a/src/Entity/WarrantySerialUploadLog.php +++ b/src/Entity/WarrantySerialUploadLog.php @@ -8,7 +8,9 @@ use DateTime; /** * @ORM\Entity - * @ORM\Table(name="warranty_serial_upload_log") + * @ORM\Table(name="warranty_serial_upload_log", indexes={ + * @ORM\Index(name="uploaded_file_idx", columns={"uploaded_file_serial"}), + * }) */ class WarrantySerialUploadLog {