Add method for callback. Add listener for third party JO status changes. #737

This commit is contained in:
Korina Cordero 2023-02-14 08:56:30 +00:00
parent f03d2bf7ef
commit 0901555a0b
3 changed files with 124 additions and 0 deletions

View file

@ -328,3 +328,15 @@ services:
App\Service\WarrantySerialLoadLogger:
arguments:
$em: "@doctrine.orm.entity_manager"
# entity listener for the third party JO status
App\EntityListener\JobOrderStatusListener:
arguments:
$jo_manager: "@App\\Service\\JobOrderManager"
tags:
- name: doctrine.orm.entity_listener
event: 'preUpdate'
entity: 'App\Entity\JobOrder'
- name: doctrine.orm.entity_listener
event: 'postPersist'
entity: 'App\Entity\JobOrder'

View file

@ -0,0 +1,59 @@
<?php
namespace App\EntityListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\JobOrder;
use App\Service\JobOrderManager;
class JobOrderStatusListener
{
protected $jo_manager;
public function __construct(JobOrderManager $jo_manager)
{
$this->jo_manager = $jo_manager;
}
public function preUpdate(JobOrder $jo, PreUpdateEventArgs $args)
{
// error_log('PRE UPDATE');
// check if status of third party JO has changed
// check if JO is third party
$source = $jo->getJOSource();
if ($source != null)
{
if ($args->hasChangedField('status'))
{
// error_log('status has changed');
// send callback
$this->jo_manager->sendJOStatusCallback($jo);
}
}
}
public function postUpdate(JobOrder $jo, LifecycleEventArgs $args)
{
// this is for updating of status of third party JOs
}
public function postPersist(JobOrder $jo, LifecycleEventArgs $args)
{
// error_log('POST PERSIST');
// check if JO is third party
$source = $jo->getJOSource();
if ($source != null)
{
// need to send a callback for a new third party JO
$this->jo_manager->sendJOStatusCallback($jo);
}
}
}

View file

@ -5,6 +5,7 @@ namespace App\Service;
use App\Entity\JobOrder;
use App\Ramcar\ServiceType;
use App\Ramcar\JOStatus;
use Doctrine\ORM\EntityManagerInterface;
@ -51,4 +52,56 @@ class JobOrderManager
$this->em->flush();
}
}
public function sendJOStatusCallback(JobOrder $jo)
{
// get the job order source
$source = $jo->getJOSource();
if ($source != null)
{
// check if source has a callback url
$callback_url = $source->getCallbackURL();
if ($callback_url != null)
{
// form the body for the callback
// jo id and jo status
$jo_data = [
'id' => $jo->getID(),
'status' => JOStatus::getName($jo->getStatus()),
];
// send status
$this->sendJOInfo($jo_data, $callback_url);
}
}
}
protected function sendJOInfo($jo_info, $callback_url)
{
$body = json_encode($jo_info);
// error_log(print_r($body, true));
// error_log('Sending json output to ' . $callback_url);
$curl = curl_init();
$options = [
CURLOPT_URL => $callback_url,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
],
];
curl_setopt_array($curl, $options);
$res = curl_exec($curl);
curl_close($curl);
// check result
error_log('Result ' . $res);
}
}