Fix json output for command. #704

This commit is contained in:
Korina Cordero 2022-09-19 09:28:12 +00:00
parent fc15b514bf
commit f8910860a6
4 changed files with 36 additions and 24 deletions

View file

@ -111,6 +111,10 @@ services:
$cvu_mfg_id: "%env(CVU_MFG_ID)%" $cvu_mfg_id: "%env(CVU_MFG_ID)%"
$cvu_brand_id: "%env(CVU_BRAND_ID)%" $cvu_brand_id: "%env(CVU_BRAND_ID)%"
App\Command\LoadWarrantySerialCommand:
arguments:
$callback_url: "%env(WARRANTY_SERIAL_CALLBACK_URL)%"
# rider tracker service # rider tracker service
App\Service\RiderTracker: App\Service\RiderTracker:
arguments: arguments:

View file

@ -11,6 +11,7 @@ use Symfony\Component\HttpKernel\KernelInterface;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use App\Entity\WarrantySerial; use App\Entity\WarrantySerial;
use App\Entity\WarrantySerialQueue;
use App\Entity\WarrantySerialUploadLog; use App\Entity\WarrantySerialUploadLog;
use App\Entity\WarrantySerialLoadLog; use App\Entity\WarrantySerialLoadLog;
@ -26,14 +27,16 @@ class LoadWarrantySerialCommand extends Command
protected $upload_logger; protected $upload_logger;
protected $load_logger; protected $load_logger;
protected $project_dir; protected $project_dir;
protected $callback_url;
public function __construct(EntityManagerInterface $em, WarrantySerialUploadLogger $upload_logger, public function __construct(EntityManagerInterface $em, WarrantySerialUploadLogger $upload_logger,
WarrantySerialLoadLogger $load_logger, KernelInterface $kernel) WarrantySerialLoadLogger $load_logger, KernelInterface $kernel, $callback_url)
{ {
$this->em = $em; $this->em = $em;
$this->upload_logger = $upload_logger; $this->upload_logger = $upload_logger;
$this->load_logger = $load_logger; $this->load_logger = $load_logger;
$this->project_dir = $kernel->getProjectDir(); $this->project_dir = $kernel->getProjectDir();
$this->callback_url = $callback_url;
parent::__construct(); parent::__construct();
} }
@ -54,7 +57,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 id, file_serial, api_user FROM warranty_serial_queue $ws_query_sql = 'SELECT id, file_serial, file_id, 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);
@ -67,20 +70,21 @@ class LoadWarrantySerialCommand extends Command
{ {
$filename = $row['file_serial']; $filename = $row['file_serial'];
$user_id = $row['api_user']; $user_id = $row['api_user'];
$ws_file_id = $row['id']; $id = $row['id'];
$file_id = $row['file_id'];
$output_info[] = $this->processWarrantySerialFile($filename, $user_id); $output_info[] = $this->processWarrantySerialFile($filename, $user_id, $file_id);
$this->updateWarrantySerialQueue($ws_file_id); $this->updateWarrantySerialQueue($id);
// send results back to third party
$this->sendResults($output_info);
} }
// send results back to third party
$this->sendResults($output_info);
return 0; return 0;
} }
protected function processWarrantySerialFile($filename, $user_id) protected function processWarrantySerialFile($filename, $user_id, $file_id)
{ {
$csv_file = $this->project_dir . '/public/warranty_serial_uploads/' . $filename; $csv_file = $this->project_dir . '/public/warranty_serial_uploads/' . $filename;
@ -101,7 +105,7 @@ class LoadWarrantySerialCommand extends Command
]; ];
$this->upload_logger->logWarrantySerialUploadInfo($log_data); $this->upload_logger->logWarrantySerialUploadInfo($log_data);
$output_info = $this->setOutputInfo($filename, true, $error, $data); $output_info = $this->setOutputInfo($filename, $file_id, true, $error, $data);
return $output_info; return $output_info;
} }
@ -119,7 +123,7 @@ class LoadWarrantySerialCommand extends Command
// 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); // error_log('Processing ' . $serial);
$sku = trim(strtoupper($row[1])); $sku = trim(strtoupper($row[1]));
$dispatch_status = trim($row[2]); $dispatch_status = trim($row[2]);
@ -165,7 +169,7 @@ class LoadWarrantySerialCommand extends Command
$error = $err[2]; $error = $err[2];
$this->logLoadInfo($user_id, false, $serial, $error); $this->logLoadInfo($user_id, false, $serial, $error);
$data = [ $data[] = [
'serial' => $serial, 'serial' => $serial,
'status' => 'error', 'status' => 'error',
'has_error' => true, 'has_error' => true,
@ -177,7 +181,7 @@ class LoadWarrantySerialCommand extends Command
// log the successful insert // log the successful insert
$this->logLoadInfo($user_id, true, $serial, ''); $this->logLoadInfo($user_id, true, $serial, '');
$data = [ $data[] = [
'serial' => $serial, 'serial' => $serial,
'status' => 'success', 'status' => 'success',
'has_error' => false, 'has_error' => false,
@ -187,7 +191,7 @@ class LoadWarrantySerialCommand extends Command
} }
// form what we output // form what we output
$output_info = $this->setOutputInfo($filename, false, '', $data); $output_info = $this->setOutputInfo($filename, $file_id, false, '', $data);
return $output_info; return $output_info;
} }
@ -354,14 +358,15 @@ class LoadWarrantySerialCommand extends Command
]); ]);
} }
protected function setOutputInfo($filename, $has_error, $error_message, $entries) protected function setOutputInfo($filename, $file_id, $has_error, $error_message, $entries)
{ {
// need to get the original filename from warranty_serial_upload_log // need to get the original filename from warranty_serial_queue
// use the uploaded_file_serial which has the saved csv file // use the uploaded_file_serial which has the saved csv file
$upload_entry = $this->em->getRepository(WarrantySerialUploadLog::class)->findOneBy(['uploaded_file_serial' => $filename]); $upload_entry = $this->em->getRepository(WarrantySerialQueue::class)->findOneBy(['file_id' => $file_id]);
$original_filename = $upload_entry->getOrigFileSerial(); $original_filename = $upload_entry->getOrigFileSerial();
$info = [ $info = [
'id' => $file_id,
'filename' => $original_filename, 'filename' => $original_filename,
'has_error' => $has_error, 'has_error' => $has_error,
'error_message' => $error_message, 'error_message' => $error_message,
@ -375,12 +380,16 @@ class LoadWarrantySerialCommand extends Command
{ {
$json_output = json_encode($output_info); $json_output = json_encode($output_info);
error_log(print_r($json_output, true)); // error_log(print_r($json_output, true));
// TODO: make a test controller
// send json there
// log what it gets
/*
$curl = curl_init(); $curl = curl_init();
$options = [ $options = [
CURLOPT_URL => $this->base_url . '/' . $url, CURLOPT_URL => $this->callback_url,
CURLOPT_POST => true, CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $body, CURLOPT_POSTFIELDS => $body,
@ -392,7 +401,7 @@ class LoadWarrantySerialCommand extends Command
curl_setopt_array($curl, $options); curl_setopt_array($curl, $options);
$res = curl_exec($curl); $res = curl_exec($curl);
curl_close($curl); curl_close($curl); */
} }

View file

@ -76,7 +76,8 @@ class WarrantySerialController extends APIController
$ws_file->setFileSerial($serial_filename) $ws_file->setFileSerial($serial_filename)
->setStatus('pending') ->setStatus('pending')
->setOrigFileSerial($orig_filename) ->setOrigFileSerial($orig_filename)
->setFileID($file_id); ->setFileID($file_id)
->setApiUser($user_id);
$em->persist($ws_file); $em->persist($ws_file);

View file

@ -8,9 +8,7 @@ use DateTime;
/** /**
* @ORM\Entity * @ORM\Entity
* @ORM\Table(name="warranty_serial_upload_log", indexes={ * @ORM\Table(name="warranty_serial_upload_log")
* @ORM\Index(name="uploaded_file_idx", columns={"uploaded_file_serial"}),
* })
*/ */
class WarrantySerialUploadLog class WarrantySerialUploadLog
{ {