Fix issues for the load command. #704

This commit is contained in:
Korina Cordero 2022-09-15 07:38:33 +00:00
parent 5b848a7f10
commit 32720ba9e7
4 changed files with 118 additions and 35 deletions

View file

@ -6,6 +6,7 @@ use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Doctrine\ORM\EntityManagerInterface;
@ -16,13 +17,23 @@ use App\Entity\WarrantySerialLoadLog;
use App\Service\WarrantySerialUploadLogger;
use App\Service\WarrantySerialLoadLogger;
use PDO;
use DateTime;
class LoadWarrantySerialCommand extends Command
{
protected $em;
protected $upload_logger;
protected $load_logger;
protected $project_dir;
public function __construct(EntityManagerInterface $em)
public function __construct(EntityManagerInterface $em, WarrantySerialUploadLogger $upload_logger,
WarrantySerialLoadLogger $load_logger, KernelInterface $kernel)
{
$this->em = $em;
$this->upload_logger = $upload_logger;
$this->load_logger = $load_logger;
$this->project_dir = $kernel->getProjectDir();
parent::__construct();
}
@ -36,10 +47,35 @@ class LoadWarrantySerialCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->em;
$status = 'pending';
// get the filenames from the queue table with status pending
$db = $em->getConnection();
$ws_query_sql = 'SELECT file_serial, api_user FROM warranty_serial_queue
WHERE status = :status';
$ws_query_stmt = $db->prepare($ws_query_sql);
$ws_query_stmt->bindValue('status', $status);
$ws_results = $ws_query_stmt->executeQuery();
while ($row = $ws_results->fetchAssociative())
{
$filename = $row['file_serial'];
$user_id = $row['api_user'];
$fname = $this->project_dir . '/public/warranty_serial_uploads/' . $filename;
$this->processWarrantySerialFile($fname, $user_id);
}
return 0;
}
protected function processWarrantySerialFile($csv_file, $upload_logger, $load_logger)
protected function processWarrantySerialFile($csv_file, $user_id)
{
// attempt to open file
try
@ -55,12 +91,13 @@ class LoadWarrantySerialCommand extends Command
'error' => $error,
];
$this->upload_logger->logWarrantySerialUploadInfo($log_data);
return new APIResponse(false, $error);
error_log($error);
}
while(($row = $fgetcsv($fh)) !== false)
while(($row = fgetcsv($fh)) !== false)
{
$is_valid = $this->validateRow($row[0], $load_logger, $user_id);
$is_valid = $this->validateRow($row, $user_id);
if ($is_valid)
{
// valid entry, we parse and insert
@ -84,24 +121,43 @@ class LoadWarrantySerialCommand extends Command
$info = json_encode($meta_info);
// insert the data. Still need to figure out how to do this fast
// prepare the data
$source = 'motiv';
if ($sku == 'N/A')
$sku = null;
// log the successful insert
$log_data = [
'user_id' => $user_id,
'is_loaded' => true,
'serial' => $serial,
'error' => '',
];
// prepared statement
$db = $this->em->getConnection();
$insert_stmt = $db->prepare('INSERT INTO warranty_serial (id, sku, date_create, source, meta_info)
VALUES (:serial, :sku, :date_create, :source, :meta_info)');
$this->upload_logger->logWarrantySeriaLoadInfo($log_data);
$res = $insert_stmt->execute([
':serial' => $serial,
':sku' => $sku,
':date_create' => $created_date,
':source' => $source,
':meta_info' => $info,
]);
if (!$res)
{
// log the not successful insert
$err = $insert_stmt->errorInfo();
$error = $err[2];
$this->logLoadInfo($user_id, false, $serial, $error);
}
else
{
// log the successful insert
$this->logLoadInfo($user_id, true, $serial, '');
}
}
}
return true;
}
protected function validateRow($entry, $load_logger, $user_id)
protected function validateRow($row, $user_id)
{
// possible lines:
// (1) header in csv file - ignore
@ -131,18 +187,13 @@ class LoadWarrantySerialCommand extends Command
{
// this one, we log
$error = 'Empty serial';
$log_data = [
'user_id' => $user_id,
'is_loaded' => false,
'serial' => '',
'error' => $error,
];
$this->upload_logger->logWarrantySeriaLoadInfo($log_data);
$this->logLoadInfo($user_id, false, '', $error);
return false;
}
// validate the date created
$serial = trim($row[0]);
$str_date_create = trim($row[3]);
$date_create = $this->convertDateCreate($str_date_create);
@ -150,20 +201,21 @@ class LoadWarrantySerialCommand extends Command
{
// log
$error = 'Invalid date create';
$serial = trim($row[0]);
$log_data = [
'user_id' => $user_id,
'is_loaded' => false,
'serial' => $serial,
'error' => $error,
];
$this->upload_logger->logWarrantySeriaLoadInfo($log_data);
$this->logLoadInfo($user_id, false, $serial, $error);
return false;
}
// check if serial is a dupe
$existing_serial = $this->em->getRepository(WarrantySerial::class)->find($serial);
if ($existing_serial != null)
{
// log
$error = 'Duplicate serial';
$this->logLoadInfo($user_id, false, $serial, $error);
return false;
}
// valid entry
return true;
@ -196,10 +248,23 @@ class LoadWarrantySerialCommand extends Command
}
}
// TODO: think about how we're going to save this, if we need string or DateTime
// if you reach this part, then date string is valid and can be converted
// if you reach this part, then date string is valid. Since we'll be using
// sql to insert the entries, we return the string
$created_date = $date_create->format('Y-m-d H:i:s');
// $created_date = DateTime::createFromFormat('Y-m-d H:i:s', $str_created_date);
return $created_date;
}
protected function logLoadInfo($user_id, $is_loaded, $serial, $error)
{
$log_data = [
'user_id' => $user_id,
'is_loaded' => $is_loaded,
'serial' => $serial,
'error' => $error,
];
$this->load_logger->logWarrantySerialLoadInfo($log_data);
}
}

View file

@ -66,7 +66,7 @@ class WarrantySerialController extends APIController
protected function registerWarrantySerialFile($em, $file, $serial_filename, $user_id)
{
$orig_filename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$orig_filename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME) . '.' . $file->getClientOriginalExtension();
$ws_file = new WarrantySerialQueue();

View file

@ -39,11 +39,18 @@ class WarrantySerialQueue
*/
protected $status;
// user that uploaded the serials
/**
* @ORM\Column(type="string", length=32)
*/
protected $api_user;
public function __construct()
{
$this->date_create = new DateTime();
$this->file_serial = null;
$this->status = '';
$this->api_user = '';
}
public function getID()
@ -72,4 +79,15 @@ class WarrantySerialQueue
{
return $this->status;
}
public function setApiUser($api_user)
{
$this->api_user = $api_user;
return $this;
}
public function getApiUser()
{
return $this->api_user;
}
}

View file

@ -20,9 +20,9 @@ class WarrantySerialLoadLogger
$log_entry = new WarrantySerialLoadLog();
$user_id = $log_data['user_id'];
$serial = $log_data['serial'];
$is_loaded = $log_data['is_loaded'];
$error = $log_data['error'];
$serial = $log_data['serial'];
$log_entry->setApiUser($user_id)
->setSerial($serial)