Skip to content

Commit

Permalink
Merge pull request #90 from logeecom/master
Browse files Browse the repository at this point in the history
Release version 3.4.6
  • Loading branch information
enriquebm12 authored Aug 28, 2024
2 parents 670725e + 8cdb501 commit 654ccac
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

## [3.4.6](https://github.com/packlink-dev/ecommerce_module_core/compare/v3.4.5...v3.4.6) - 2024-08-28
### Changed
- Add detection of expired drafts

## [3.4.5](https://github.com/packlink-dev/ecommerce_module_core/compare/v3.4.4...v3.4.5) - 2024-07-30
### Changed
- Change BatchTaskCleanupTask to delete tasks in batches
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class OrderShipmentDetails extends Entity
'deleted',
'currency',
'customsInvoiceId',
'isExpired',
);
/**
* Shop order ID.
Expand Down Expand Up @@ -117,6 +118,10 @@ class OrderShipmentDetails extends Entity
* @var string
*/
private $customsInvoiceId = '';
/**
* @var bool
*/
private $isExpired = false;

/**
* Returns entity configuration object.
Expand Down Expand Up @@ -457,4 +462,20 @@ public function setCustomsInvoiceId($customsInvoiceId)
{
$this->customsInvoiceId = $customsInvoiceId;
}

/**
* @return bool
*/
public function isExpired()
{
return $this->isExpired;
}

/**
* @param bool $isExpired
*/
public function setIsExpired($isExpired)
{
$this->isExpired = $isExpired;
}
}
37 changes: 37 additions & 0 deletions src/BusinessLogic/ShipmentDraft/ShipmentDraftService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

use DateInterval;
use Logeecom\Infrastructure\Configuration\Configuration;
use Logeecom\Infrastructure\Logger\Logger;
use Logeecom\Infrastructure\ORM\RepositoryRegistry;
use Logeecom\Infrastructure\ServiceRegister;
use Logeecom\Infrastructure\TaskExecution\QueueItem;
use Logeecom\Infrastructure\TaskExecution\QueueService;
use Logeecom\Infrastructure\Utility\TimeProvider;
use Packlink\BusinessLogic\BaseService;
use Packlink\BusinessLogic\Http\Proxy;
use Packlink\BusinessLogic\OrderShipmentDetails\OrderShipmentDetailsService;
use Packlink\BusinessLogic\Scheduler\Models\HourlySchedule;
use Packlink\BusinessLogic\Scheduler\Models\Schedule;
use Packlink\BusinessLogic\ShipmentDraft\Objects\ShipmentDraftStatus;
Expand Down Expand Up @@ -115,6 +118,30 @@ public function getDraftStatus($orderId)
return $status;
}

/**
* Checks if draft is expired.
*
* @param string $reference
*
* @return bool
*/
public function isDraftExpired($reference)
{
try {
$shipment = $this->getProxy()->getShipment($reference);

if ($shipment) {
return false;
}

return true;
} catch (\Exception $e) {
Logger::logError($e->getMessage());

return false;
}
}

/**
* Enqueues delayed send draft task.
*
Expand Down Expand Up @@ -155,4 +182,14 @@ private function getConfigService()
{
return ServiceRegister::getService(Configuration::CLASS_NAME);
}

/**
* Retrieves proxy.
*
* @return Proxy | object
*/
private function getProxy()
{
return ServiceRegister::getService(Proxy::CLASS_NAME);
}
}
19 changes: 14 additions & 5 deletions src/BusinessLogic/Tasks/SendDraftTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SendDraftTask extends Task
*
* @var string
*/
private $orderId;
protected $orderId;
/**
* Order service instance.
*
Expand All @@ -55,7 +55,7 @@ class SendDraftTask extends Task
*
* @var OrderShipmentDetailsService
*/
private $orderShipmentDetailsService;
protected $orderShipmentDetailsService;
/**
* @var CustomsService
*/
Expand Down Expand Up @@ -159,8 +159,7 @@ public function execute()
{
$this->setExecution();

$isRepositoryRegistered = RepositoryRegistry::isRegistered(OrderShipmentDetails::getClassName());
if ($isRepositoryRegistered && $this->isDraftCreated($this->orderId)) {
if ($this->shouldNotSynchronize()) {
Logger::logInfo("Draft for order [{$this->orderId}] has been already created. Task is terminating.");
$this->reportProgress(100);

Expand Down Expand Up @@ -201,6 +200,16 @@ public function execute()
$this->reportProgress(100);
}

/**
* @return bool
*/
protected function shouldNotSynchronize()
{
$isRepositoryRegistered = RepositoryRegistry::isRegistered(OrderShipmentDetails::getClassName());

return $isRepositoryRegistered && $this->isDraftCreated($this->orderId);
}

/**
* @param Draft $draft
* @param Order $order
Expand Down Expand Up @@ -281,7 +290,7 @@ private function getOrderService()
*
* @return OrderShipmentDetailsService Service instance.
*/
private function getOrderShipmentDetailsService()
protected function getOrderShipmentDetailsService()
{
if ($this->orderShipmentDetailsService === null) {
$this->orderShipmentDetailsService = ServiceRegister::getService(OrderShipmentDetailsService::CLASS_NAME);
Expand Down
28 changes: 28 additions & 0 deletions tests/BusinessLogic/Order/ShipmentDraftServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,34 @@ public function testStatusFailed()
$this->assertEquals('Attempt 7: Error in task.', $draftStatus->message);
}

public function testDraftExpired()
{
// arrange
/** @var TestHttpClient $httpClient */
$httpClient = ServiceRegister::getService(HttpClient::CLASS_NAME);
$httpClient->setMockResponses(array(new HttpResponse(404, array(), '')));

// act
$result = $this->draftShipmentService->isDraftExpired('PRO202401234567');

// assert
$this->assertTrue($result);
}

public function testDraftNotExpired()
{
// arrange
/** @var TestHttpClient $httpClient */
$httpClient = ServiceRegister::getService(HttpClient::CLASS_NAME);
$httpClient->setMockResponses(array(new HttpResponse(200, array(), file_get_contents(__DIR__ . '/../Common/ApiResponses/shipment.json'))));

// act
$result = $this->draftShipmentService->isDraftExpired('PRO202401234567');

// assert
$this->assertFalse($result);
}

private function getScheduleRepository()
{
return RepositoryRegistry::getRepository(Schedule::getClassName());
Expand Down

0 comments on commit 654ccac

Please sign in to comment.