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_brand_id: "%env(CVU_BRAND_ID)%"
App\Command\LoadWarrantySerialCommand:
arguments:
$callback_url: "%env(WARRANTY_SERIAL_CALLBACK_URL)%"
# rider tracker service
App\Service\RiderTracker:
arguments:

View file

@ -11,6 +11,7 @@ use Symfony\Component\HttpKernel\KernelInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\WarrantySerial;
use App\Entity\WarrantySerialQueue;
use App\Entity\WarrantySerialUploadLog;
use App\Entity\WarrantySerialLoadLog;
@ -26,14 +27,16 @@ class LoadWarrantySerialCommand extends Command
protected $upload_logger;
protected $load_logger;
protected $project_dir;
protected $callback_url;
public function __construct(EntityManagerInterface $em, WarrantySerialUploadLogger $upload_logger,
WarrantySerialLoadLogger $load_logger, KernelInterface $kernel)
WarrantySerialLoadLogger $load_logger, KernelInterface $kernel, $callback_url)
{
$this->em = $em;
$this->upload_logger = $upload_logger;
$this->load_logger = $load_logger;
$this->project_dir = $kernel->getProjectDir();
$this->callback_url = $callback_url;
parent::__construct();
}
@ -54,7 +57,7 @@ class LoadWarrantySerialCommand extends Command
// get the filenames from the queue table with status pending
$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';
$ws_query_stmt = $db->prepare($ws_query_sql);
@ -67,20 +70,21 @@ class LoadWarrantySerialCommand extends Command
{
$filename = $row['file_serial'];
$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);
// send results back to third party
$this->sendResults($output_info);
$this->updateWarrantySerialQueue($id);
}
// send results back to third party
$this->sendResults($output_info);
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;
@ -101,7 +105,7 @@ class LoadWarrantySerialCommand extends Command
];
$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;
}
@ -119,7 +123,7 @@ class LoadWarrantySerialCommand extends Command
// valid entry, we parse and insert
$serial = trim(strtoupper($row[0]));
error_log('Processing ' . $serial);
// error_log('Processing ' . $serial);
$sku = trim(strtoupper($row[1]));
$dispatch_status = trim($row[2]);
@ -165,7 +169,7 @@ class LoadWarrantySerialCommand extends Command
$error = $err[2];
$this->logLoadInfo($user_id, false, $serial, $error);
$data = [
$data[] = [
'serial' => $serial,
'status' => 'error',
'has_error' => true,
@ -177,7 +181,7 @@ class LoadWarrantySerialCommand extends Command
// log the successful insert
$this->logLoadInfo($user_id, true, $serial, '');
$data = [
$data[] = [
'serial' => $serial,
'status' => 'success',
'has_error' => false,
@ -187,7 +191,7 @@ class LoadWarrantySerialCommand extends Command
}
// form what we output
$output_info = $this->setOutputInfo($filename, false, '', $data);
$output_info = $this->setOutputInfo($filename, $file_id, false, '', $data);
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
$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();
$info = [
'id' => $file_id,
'filename' => $original_filename,
'has_error' => $has_error,
'error_message' => $error_message,
@ -375,12 +380,16 @@ class LoadWarrantySerialCommand extends Command
{
$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();
$options = [
CURLOPT_URL => $this->base_url . '/' . $url,
CURLOPT_URL => $this->callback_url,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $body,
@ -392,7 +401,7 @@ class LoadWarrantySerialCommand extends Command
curl_setopt_array($curl, $options);
$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)
->setStatus('pending')
->setOrigFileSerial($orig_filename)
->setFileID($file_id);
->setFileID($file_id)
->setApiUser($user_id);
$em->persist($ws_file);

View file

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