Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(returns): prevent validation error on export return #216

Merged
merged 6 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/Shipment/Request/PostReturnShipmentsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
namespace MyParcelNL\Pdk\Shipment\Request;

use MyParcelNL\Pdk\Api\Request\Request;
use MyParcelNL\Pdk\App\Api\Backend\PdkBackendActions;
use MyParcelNL\Pdk\Carrier\Model\Carrier;
use MyParcelNL\Pdk\Facade\Notifications;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\Pdk\Facade\Platform;
use MyParcelNL\Pdk\Notification\Model\Notification;
use MyParcelNL\Pdk\Shipment\Collection\ShipmentCollection;
use MyParcelNL\Pdk\Shipment\Model\Shipment;
use MyParcelNL\Pdk\Types\Service\TriStateService;
Expand Down Expand Up @@ -84,9 +89,9 @@ private function encodeReturnOptions(Shipment $shipment): array
return array_filter(
[
'package_type' => $shipment->deliveryOptions->getPackageTypeId(),
'insurance' => $shipmentOptions->insurance
'insurance' => $options['insurance']
? [
'amount' => $shipmentOptions->insurance * 100,
'amount' => $options['insurance'],
'currency' => 'EUR',
] : null,
] + $options
Expand All @@ -99,6 +104,8 @@ private function encodeReturnOptions(Shipment $shipment): array
private function encodeReturnShipments(): array
{
return $this->collection->map(function (Shipment $shipment) {
$shipment = $this->ensureReturnCapabilities($shipment);

return [
'parent' => $shipment->id,
'reference_identifier' => $shipment->referenceIdentifier,
Expand All @@ -110,4 +117,34 @@ private function encodeReturnShipments(): array
})
->toArray();
}

/**
* If the carrier cannot handle return shipments, the carrier will be set to the platform default carrier.
* In that case a notification is emitted.
*
* @param \MyParcelNL\Pdk\Shipment\Model\Shipment $shipment
*
* @return \MyParcelNL\Pdk\Shipment\Model\Shipment
*/
private function ensureReturnCapabilities(Shipment $shipment): Shipment
joerivanveen marked this conversation as resolved.
Show resolved Hide resolved
{
$carrierId = $shipment->carrier->id;
$carrier = Platform::getCarriers()
->firstWhere('id', $carrierId);

if (! $carrier || ! $carrier->returnCapabilities) {
Notifications::warning(
"{$shipment->carrier->human} has no return capabilities",
'Return shipment exported with default carrier ' . Platform::get('defaultCarrier'),
Notification::CATEGORY_ACTION,
[
'action' => PdkBackendActions::EXPORT_RETURN,
'orderIds' => $shipment->referenceIdentifier,
]
);
$shipment->carrier = new Carrier(['carrierId' => Platform::get('defaultCarrierId')]);
}

return $shipment;
}
}
162 changes: 107 additions & 55 deletions tests/Unit/App/Action/Backend/Order/ExportReturnActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,28 @@
namespace MyParcelNL\Pdk\App\Action\Backend\Order;

use MyParcelNL\Pdk\App\Api\Backend\PdkBackendActions;
use MyParcelNL\Pdk\App\Order\Contract\PdkOrderRepositoryInterface;
use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Pdk\App\Order\Collection\PdkOrderCollection;
use MyParcelNL\Pdk\App\Order\Collection\PdkOrderCollectionFactory;
use MyParcelNL\Pdk\App\Order\Model\PdkOrder;
use MyParcelNL\Pdk\Carrier\Model\Carrier;
use MyParcelNL\Pdk\Facade\Actions;
use MyParcelNL\Pdk\Shipment\Model\DeliveryOptions;
use MyParcelNL\Pdk\Shipment\Model\Shipment;
use MyParcelNL\Pdk\Shipment\Model\ShipmentOptions;
use MyParcelNL\Pdk\Tests\Api\Response\ExampleGetShipmentsResponse;
use MyParcelNL\Pdk\Tests\Api\Response\ExamplePostIdsResponse;
use MyParcelNL\Pdk\Tests\Bootstrap\MockApi;
use MyParcelNL\Pdk\Tests\Bootstrap\MockPdkOrderRepository;
use MyParcelNL\Pdk\Tests\Uses\UsesApiMock;
use MyParcelNL\Pdk\Tests\Uses\UsesMockPdkInstance;
use Symfony\Component\HttpFoundation\Response;
use function DI\autowire;
use function MyParcelNL\Pdk\Tests\factory;
use function MyParcelNL\Pdk\Tests\usesShared;
use function Spatie\Snapshots\assertMatchesJsonSnapshot;

usesShared(
new UsesMockPdkInstance([
PdkOrderRepositoryInterface::class => autowire(MockPdkOrderRepository::class)->constructor([
[
'externalIdentifier' => '701',
'shipments' => [
[
'id' => 100001,
'referenceIdentifier' => '1',
],
[
'id' => 100002,
'referenceIdentifier' => '2',
'deliveryOptions' => [
'carrier' => Carrier::CARRIER_POSTNL_NAME,
'deliveryType' => DeliveryOptions::DELIVERY_TYPE_MORNING_NAME,
'shipmentOptions' => [
'signature' => true,
],
],
],
],
],
[
'externalIdentifier' => '247',
'deliveryOptions' => [
'carrier' => Carrier::CARRIER_POSTNL_NAME,
'deliveryType' => DeliveryOptions::DELIVERY_TYPE_EVENING_NAME,
],
],
]),
]),
new UsesApiMock()
);
usesShared(new UsesMockPdkInstance());

it('exports return', function (PdkOrderCollectionFactory $ordersFactory) {
$ordersFactory->store();

it('exports return', function () {
MockApi::enqueue(
new ExamplePostIdsResponse([['id' => 30011], ['id' => 30012]]),
new ExampleGetShipmentsResponse()
Expand All @@ -66,22 +37,103 @@
'orderIds' => ['701', '247'],
]);

$content = json_decode($response->getContent(), true);

assertMatchesJsonSnapshot($response->getContent());
expect($response)
->toBeInstanceOf(Response::class)
->and(Arr::dot($content))
->toHaveKeysAndValues([
'data.orders.0.externalIdentifier' => '701',
'data.orders.0.deliveryOptions.carrier.name' => Carrier::CARRIER_POSTNL_NAME,
'data.orders.0.deliveryOptions.labelAmount' => 1,
'data.orders.0.deliveryOptions.packageType' => DeliveryOptions::PACKAGE_TYPE_PACKAGE_NAME,
'data.orders.1.externalIdentifier' => '247',
'data.orders.1.deliveryOptions.carrier.name' => Carrier::CARRIER_POSTNL_NAME,
'data.orders.1.deliveryOptions.deliveryType' => DeliveryOptions::DELIVERY_TYPE_EVENING_NAME,
'data.orders.1.deliveryOptions.labelAmount' => 1,
'data.orders.1.deliveryOptions.packageType' => DeliveryOptions::PACKAGE_TYPE_PACKAGE_NAME,
])
->and($response->getStatusCode())
->toBe(200);
});
})->with([
'simple orders' => [
function () {
return factory(PdkOrderCollection::class)->push(
joerivanveen marked this conversation as resolved.
Show resolved Hide resolved
factory(PdkOrder::class)
->withExternalIdentifier('701')
->withShipments([
factory(Shipment::class)
->withId(100001)
->withReferenceIdentifier('1'),
factory(Shipment::class)
->withId(100002)
->withReferenceIdentifier('2')
->withDeliveryOptions(
factory(DeliveryOptions::class)
->withCarrier(Carrier::CARRIER_POSTNL_NAME)
->withDeliveryType(DeliveryOptions::DELIVERY_TYPE_MORNING_NAME)
->withShipmentOptions(factory(ShipmentOptions::class)->withSignature(1))
),
]),
factory(PdkOrder::class)
->withExternalIdentifier('247')
->withDeliveryOptions(
factory(DeliveryOptions::class)
->withCarrier(Carrier::CARRIER_POSTNL_NAME)
->withDeliveryType(DeliveryOptions::DELIVERY_TYPE_EVENING_NAME)
)
);
},
],
'insurance' => [
function () {
return factory(PdkOrderCollection::class)->push(
factory(PdkOrder::class)
->withExternalIdentifier('701')
->withShipments([
factory(Shipment::class)
->withId(100001)
->withReferenceIdentifier('1')
->withDeliveryOptions(
factory(DeliveryOptions::class)
->withCarrier(Carrier::CARRIER_POSTNL_NAME)
->withShipmentOptions(
factory(ShipmentOptions::class)
->withInsurance(0)
)
),
factory(Shipment::class)
->withId(100002)
->withReferenceIdentifier('2')
->withDeliveryOptions(
factory(DeliveryOptions::class)
->withCarrier(Carrier::CARRIER_POSTNL_NAME)
->withShipmentOptions(
factory(ShipmentOptions::class)
->withInsurance(500)
)
),
])
);
},
],
'no return capabilities' => [
function () {
return factory(PdkOrderCollection::class)->push(
factory(PdkOrder::class)
->withExternalIdentifier('701')
->withShipments([
factory(Shipment::class)
->withId(100001)
->withReferenceIdentifier('1')
->withDeliveryOptions(
factory(DeliveryOptions::class)
->withCarrier(Carrier::CARRIER_DHL_EUROPLUS_NAME)
->withShipmentOptions(
factory(ShipmentOptions::class)
->withInsurance(0)
)
),
factory(Shipment::class)
->withId(100002)
->withReferenceIdentifier('2')
->withDeliveryOptions(
factory(DeliveryOptions::class)
->withCarrier(Carrier::CARRIER_DHL_EUROPLUS_NAME)
->withShipmentOptions(
factory(ShipmentOptions::class)
->withInsurance(500)
)
),
])
);
},
],
]);
Loading