Add processing for output. #704

This commit is contained in:
Korina Cordero 2022-09-15 09:51:07 +00:00
parent 32720ba9e7
commit cbc0b290ad

View file

@ -54,7 +54,7 @@ class LoadWarrantySerialCommand extends Command
// get the filenames from the queue table with status pending // get the filenames from the queue table with status pending
$db = $em->getConnection(); $db = $em->getConnection();
$ws_query_sql = 'SELECT file_serial, api_user FROM warranty_serial_queue $ws_query_sql = 'SELECT id, file_serial, api_user FROM warranty_serial_queue
WHERE status = :status'; WHERE status = :status';
$ws_query_stmt = $db->prepare($ws_query_sql); $ws_query_stmt = $db->prepare($ws_query_sql);
@ -62,14 +62,22 @@ class LoadWarrantySerialCommand extends Command
$ws_results = $ws_query_stmt->executeQuery(); $ws_results = $ws_query_stmt->executeQuery();
$output_info = [];
while ($row = $ws_results->fetchAssociative()) while ($row = $ws_results->fetchAssociative())
{ {
$filename = $row['file_serial']; $filename = $row['file_serial'];
$user_id = $row['api_user']; $user_id = $row['api_user'];
$ws_file_id = $row['id'];
$fname = $this->project_dir . '/public/warranty_serial_uploads/' . $filename; $fname = $this->project_dir . '/public/warranty_serial_uploads/' . $filename;
$this->processWarrantySerialFile($fname, $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);
} }
return 0; return 0;
@ -77,6 +85,8 @@ class LoadWarrantySerialCommand extends Command
protected function processWarrantySerialFile($csv_file, $user_id) protected function processWarrantySerialFile($csv_file, $user_id)
{ {
$output_info = [];
// attempt to open file // attempt to open file
try try
{ {
@ -92,14 +102,20 @@ class LoadWarrantySerialCommand extends Command
]; ];
$this->upload_logger->logWarrantySerialUploadInfo($log_data); $this->upload_logger->logWarrantySerialUploadInfo($log_data);
error_log($error); $output_info = $this->setOutputInfo('', 'not_uploaded', true, $error);
return $output_info;
} }
while(($row = fgetcsv($fh)) !== false) while(($row = fgetcsv($fh)) !== false)
{ {
$is_valid = $this->validateRow($row, $user_id); $validation_result = $this->validateRow($row, $user_id);
if ($is_valid) if (!empty($validation_result))
{ {
$output_info[] = $validation_result;
continue;
}
// valid entry, we parse and insert // valid entry, we parse and insert
$serial = trim(strtoupper($row[0])); $serial = trim(strtoupper($row[0]));
$sku = trim(strtoupper($row[1])); $sku = trim(strtoupper($row[1]));
@ -145,20 +161,24 @@ class LoadWarrantySerialCommand extends Command
$err = $insert_stmt->errorInfo(); $err = $insert_stmt->errorInfo();
$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);
} }
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, '');
} }
} }
return true; return $output_info;
} }
protected function validateRow($row, $user_id) protected function validateRow($row, $user_id)
{ {
$data = [];
// possible lines: // possible lines:
// (1) header in csv file - ignore // (1) header in csv file - ignore
// SerialNumber,Sku,DispatchStatus,CreatedDate,InventoryStatus,CategoryID,CategoryName // SerialNumber,Sku,DispatchStatus,CreatedDate,InventoryStatus,CategoryID,CategoryName
@ -189,7 +209,9 @@ class LoadWarrantySerialCommand extends Command
$error = 'Empty serial'; $error = 'Empty serial';
$this->logLoadInfo($user_id, false, '', $error); $this->logLoadInfo($user_id, false, '', $error);
return false; $data = $this->setOutputInfo('', 'not_uploaded', true, $error);
return $data;
} }
// validate the date created // validate the date created
@ -203,7 +225,9 @@ class LoadWarrantySerialCommand extends Command
$error = 'Invalid date create'; $error = 'Invalid date create';
$this->logLoadInfo($user_id, false, $serial, $error); $this->logLoadInfo($user_id, false, $serial, $error);
return false; $data = $this->setOutputInfo($serial, 'not_uploaded', true, $error);
return $data;
} }
// check if serial is a dupe // check if serial is a dupe
@ -214,11 +238,13 @@ class LoadWarrantySerialCommand extends Command
$error = 'Duplicate serial'; $error = 'Duplicate serial';
$this->logLoadInfo($user_id, false, $serial, $error); $this->logLoadInfo($user_id, false, $serial, $error);
return false; $data = $this->setOutputInfo($serial, 'not_uploaded', true, $error)
return $data;
} }
// valid entry // valid entry, return empty
return true; return $data;
} }
protected function convertDateCreate($str_date_create) protected function convertDateCreate($str_date_create)
@ -267,4 +293,30 @@ class LoadWarrantySerialCommand extends Command
$this->load_logger->logWarrantySerialLoadInfo($log_data); $this->load_logger->logWarrantySerialLoadInfo($log_data);
} }
protected function updateWarrantySerialQueue($id)
{
// prepared statement
$db = $this->em->getConnection();
$update_stmt = $db->prepare('UPDATE warranty_serial_queue SET status = :status
WHERE id = :id');
$res = $update_stmt->execute([
':status' => 'done',
':id' => $id,
]);
}
protected function setOutputInfo($serial, $status, $has_error, $error_message)
{
$info = [
'serial' => $serial,
'status' => $status,
'has_error' => $has_error,
'error_message' => $error_message
];
return $info;
}
} }