Add output to command. #704

This commit is contained in:
Korina Cordero 2022-09-16 05:22:04 +00:00
parent cbc0b290ad
commit 70e9b933d9
2 changed files with 89 additions and 26 deletions

View file

@ -69,22 +69,21 @@ class LoadWarrantySerialCommand extends Command
$user_id = $row['api_user']; $user_id = $row['api_user'];
$ws_file_id = $row['id']; $ws_file_id = $row['id'];
$fname = $this->project_dir . '/public/warranty_serial_uploads/' . $filename; $output_info[] = $this->processWarrantySerialFile($filename, $user_id);
// 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);
$this->updateWarrantySerialQueue($ws_file_id); $this->updateWarrantySerialQueue($ws_file_id);
// send results back to third party
$this->sendResults($output_info);
} }
return 0; 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 = []; $output_info = [];
// attempt to open file // attempt to open file
@ -102,22 +101,26 @@ class LoadWarrantySerialCommand extends Command
]; ];
$this->upload_logger->logWarrantySerialUploadInfo($log_data); $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; return $output_info;
} }
$data = [];
while(($row = fgetcsv($fh)) !== false) while(($row = fgetcsv($fh)) !== false)
{ {
$validation_result = $this->validateRow($row, $user_id); $validation_result = $this->validateRow($row, $user_id);
if (!empty($validation_result)) if (!empty($validation_result))
{ {
$output_info[] = $validation_result; $data[] = $validation_result;
continue; continue;
} }
// valid entry, we parse and insert // valid entry, we parse and insert
$serial = trim(strtoupper($row[0])); $serial = trim(strtoupper($row[0]));
error_log('Processing ' . $serial);
$sku = trim(strtoupper($row[1])); $sku = trim(strtoupper($row[1]));
$dispatch_status = trim($row[2]); $dispatch_status = trim($row[2]);
$str_date_create = trim($row[3]); $str_date_create = trim($row[3]);
@ -162,17 +165,30 @@ class LoadWarrantySerialCommand extends Command
$error = $err[2]; $error = $err[2];
$this->logLoadInfo($user_id, false, $serial, $error); $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 else
{ {
// log the successful insert // log the successful insert
$this->logLoadInfo($user_id, true, $serial, ''); $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; return $output_info;
} }
@ -191,15 +207,31 @@ class LoadWarrantySerialCommand extends Command
// check if the line is a header // check if the line is a header
if ($row[0] == 'SerialNumber') if ($row[0] == 'SerialNumber')
{ {
// no need to log, just ignore and skip // no need to log, but send back error
return false; $error = 'Invalid information.';
$data = [
'serial' => '',
'status' => 'error',
'has_error' => true,
'error_message' => $error,
];
return $data;
} }
// check if empty line // check if empty line
if ($row == array(null)) if ($row == array(null))
{ {
// no need to log, just ignore and skip // no need to log, but send back error
return false; $error = 'Empty line';
$data = [
'serial' => '',
'status' => 'error',
'has_error' => true,
'error_message' => $error,
];
return $data;
} }
// check if empty serial // check if empty serial
@ -209,7 +241,12 @@ class LoadWarrantySerialCommand extends Command
$error = 'Empty serial'; $error = 'Empty serial';
$this->logLoadInfo($user_id, false, '', $error); $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; return $data;
} }
@ -222,10 +259,15 @@ class LoadWarrantySerialCommand extends Command
if ($date_create == null) if ($date_create == null)
{ {
// log // log
$error = 'Invalid date create'; $error = 'Invalid date create.';
$this->logLoadInfo($user_id, false, $serial, $error); $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; return $data;
} }
@ -235,10 +277,15 @@ class LoadWarrantySerialCommand extends Command
if ($existing_serial != null) if ($existing_serial != null)
{ {
// log // log
$error = 'Duplicate serial'; $error = 'Serial already exists.';
$this->logLoadInfo($user_id, false, $serial, $error); $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; 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 = [ $info = [
'serial' => $serial, 'filename' => $original_filename,
'status' => $status,
'has_error' => $has_error, 'has_error' => $has_error,
'error_message' => $error_message 'error_message' => $error_message,
'data' => $entries,
]; ];
return $info; 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
}
} }

View file

@ -8,7 +8,9 @@ use DateTime;
/** /**
* @ORM\Entity * @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 class WarrantySerialUploadLog
{ {