Skip to content

Commit

Permalink
feat: add audits
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Ernst committed Nov 15, 2023
1 parent 6e748da commit 1c19573
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 92 deletions.
28 changes: 11 additions & 17 deletions src/App/Action/Backend/Order/ExportOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use MyParcelNL\Pdk\App\Order\Contract\PdkOrderOptionsServiceInterface;
use MyParcelNL\Pdk\App\Order\Contract\PdkOrderRepositoryInterface;
use MyParcelNL\Pdk\App\Order\Model\PdkOrder;
use MyParcelNL\Pdk\Audit\Contract\AuditServiceInterface;
use MyParcelNL\Pdk\Audit\Model\Audit;
use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Pdk\Facade\Actions;
use MyParcelNL\Pdk\Facade\Logger;
Expand All @@ -30,11 +28,6 @@

class ExportOrderAction extends AbstractOrderAction
{
/**
* @var \MyParcelNL\Pdk\Audit\Contract\AuditServiceInterface
*/
private $auditService;

/**
* @var \MyParcelNL\Pdk\Fulfilment\Repository\OrderRepository
*/
Expand All @@ -49,18 +42,15 @@ class ExportOrderAction extends AbstractOrderAction
* @param \MyParcelNL\Pdk\App\Order\Contract\PdkOrderRepositoryInterface $pdkOrderRepository
* @param \MyParcelNL\Pdk\Fulfilment\Repository\OrderRepository $orderRepository
* @param \MyParcelNL\Pdk\Shipment\Repository\ShipmentRepository $shipmentRepository
* @param \MyParcelNL\Pdk\Audit\Contract\AuditServiceInterface $auditService
*/
public function __construct(
PdkOrderRepositoryInterface $pdkOrderRepository,
OrderRepository $orderRepository,
ShipmentRepository $shipmentRepository,
AuditServiceInterface $auditService
ShipmentRepository $shipmentRepository
) {
parent::__construct($pdkOrderRepository);
$this->orderRepository = $orderRepository;
$this->shipmentRepository = $shipmentRepository;
$this->auditService = $auditService;
}

/**
Expand Down Expand Up @@ -172,7 +162,7 @@ protected function validateOrders(PdkOrderCollection $orders): PdkOrderCollectio

return $orders
->filter(function (PdkOrder $order) {
return $order->audits
return ! $order->audits
->automatic()
->hasAction(PdkBackendActions::EXPORT_ORDERS);
})
Expand Down Expand Up @@ -210,15 +200,19 @@ protected function validateOrders(PdkOrderCollection $orders): PdkOrderCollectio
});
}

private function addAudits(Request $request, PdkOrderCollection $exportedOrders)
/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \MyParcelNL\Pdk\App\Order\Collection\PdkOrderCollection $exportedOrders
*
* @return void
* @throws \MyParcelNL\Pdk\Base\Exception\InvalidCastException
*/
private function addAudits(Request $request, PdkOrderCollection $exportedOrders): void
{
$exportedOrders->each(function (PdkOrder $order) use ($request) {
$order->addAudit(
PdkBackendActions::EXPORT_ORDERS,

// dit wil je niet:
$request->get('automatic') ? Audit::TYPE_AUTOMATIC : Audit::TYPE_MANUAL,
// additionele context, mag in "arguments", "parameters" oid? kijk maar wat mooi is :)
$request->get('type'),
[
'mode' => Settings::get(OrderSettings::ORDER_MODE, OrderSettings::ID) ? 'order' : 'shipment',
]
Expand Down
7 changes: 5 additions & 2 deletions src/App/Audit/Concern/HasAudits.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ trait HasAudits
/**
* @param string $action
* @param string|null $type
* @param null|array $arguments
*
* @return void
* @throws \MyParcelNL\Pdk\Base\Exception\InvalidCastException
*/
public function addAudit(string $action, string $type = null): void
public function addAudit(string $action, string $type = null, array $arguments = null): void
{
$id = uniqid('', true);
$audit = new Audit([
'id' => $id,
'arguments' => $arguments,
'action' => $action,
'model' => static::class,
'modelIdentifier' => $this->auditIdentifier,
'modelIdentifier' => $this->getAttribute($this->auditIdentifier),
'created' => new DateTime(),
'type' => $type,
]);
Expand Down
7 changes: 0 additions & 7 deletions src/Audit/Contract/AuditRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,4 @@ public function all(): AuditCollection;
* @return void
*/
public function store(Audit $audit): void;

/**
* @param \MyParcelNL\Pdk\Audit\Model\Audit $audit
*
* @return mixed
*/
public function update(Audit $audit);
}
7 changes: 0 additions & 7 deletions src/Audit/Repository/AbstractAuditRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,4 @@ abstract public function all(): AuditCollection;
* @return void
*/
abstract public function store(Audit $audit): void;

/**
* @param \MyParcelNL\Pdk\Audit\Model\Audit $audit
*
* @return void
*/
abstract public function update(Audit $audit): void;
}
20 changes: 0 additions & 20 deletions tests/Bootstrap/MockAuditRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,4 @@ public function store(Audit $audit): void
{
$this->audits->push($audit);
}

/**
* @param \MyParcelNL\Pdk\Audit\Model\Audit $audit
*
* @return void
*/
public function update(Audit $audit): void
{
$auditCollection = $this->all();

$updatedAudits = $auditCollection->map(function (Audit $existingAudit) use ($audit) {
if ($existingAudit->id === $audit->id) {
return $audit;
}

return $existingAudit;
});

$this->audits = $updatedAudits;
}
}
32 changes: 32 additions & 0 deletions tests/Unit/App/Action/Backend/Order/ExportOrderActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
use MyParcelNL\Pdk\App\Order\Collection\PdkOrderCollection;
use MyParcelNL\Pdk\App\Order\Collection\PdkOrderCollectionFactory;
use MyParcelNL\Pdk\App\Order\Model\PdkOrder;
use MyParcelNL\Pdk\Audit\Contract\AuditServiceInterface;
use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Pdk\Base\Support\Collection;
use MyParcelNL\Pdk\Carrier\Model\Carrier;
use MyParcelNL\Pdk\Facade\Actions;
use MyParcelNL\Pdk\Facade\Notifications;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\Pdk\Notification\Model\Notification;
use MyParcelNL\Pdk\Settings\Model\CarrierSettings;
use MyParcelNL\Pdk\Settings\Model\CarrierSettingsFactory;
Expand Down Expand Up @@ -339,3 +341,33 @@ function () {
],
])
->with('orderModeToggle');

it('creates audit after export', function () {
factory(OrderSettings::class)
->withConceptShipments(true)
->store();

$order = factory(PdkOrder::class)
->store()
->make();

MockApi::enqueue(new ExamplePostShipmentsResponse(), new ExampleGetShipmentsResponse());

Actions::execute(PdkBackendActions::EXPORT_ORDERS, ['orderIds' => [$order->externalIdentifier]]);

$auditService = Pdk::get(AuditServiceInterface::class);
$audit = $auditService->all()
->first();

expect($audit)->not->toBeNull()
->and($audit->modelIdentifier)
->toBe($order->externalIdentifier)
->and($audit->model)
->toBe(PdkOrder::class)
->and($audit->action)
->toBe(PdkBackendActions::EXPORT_ORDERS)
->and($audit->arguments)
->toBe([
'mode' => 'shipment',
]);
});
44 changes: 21 additions & 23 deletions tests/Unit/Audit/Repository/AbstractAuditRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,41 @@

namespace MyParcelNL\Pdk\App\Order\Repository;

use MyParcelNL\Pdk\App\Order\Contract\PdkOrderRepositoryInterface;
use MyParcelNL\Pdk\App\Order\Model\PdkOrder;
use MyParcelNL\Pdk\Audit\Collection\AuditCollection;
use MyParcelNL\Pdk\Audit\Contract\AuditRepositoryInterface;
use MyParcelNL\Pdk\Audit\Model\Audit;
use MyParcelNL\Pdk\Facade\Audits;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\Pdk\Tests\Uses\UsesMockPdkInstance;
use function MyParcelNL\Pdk\Tests\factory;
use function MyParcelNL\Pdk\Tests\usesShared;

usesShared(new UsesMockPdkInstance());

it('gets all audits', function () {
/** @var \MyParcelNL\Pdk\Tests\Bootstrap\MockAuditRepository $repository */
$repository = Pdk::get(AuditRepositoryInterface::class);
$order = $repository->all();
$audits = $repository->all();

expect($order)->toBeInstanceOf(PdkOrder::class);
expect($audits)->toBeInstanceOf(AuditCollection::class);
});

it('gets multiple orders', function () {
/** @var \MyParcelNL\Pdk\Tests\Bootstrap\MockPdkOrderRepository $repository */
$repository = Pdk::get(PdkOrderRepositoryInterface::class);
$orders = $repository->getMany(['1;2', 3]);
it('stores an audit', function () {
/** @var \MyParcelNL\Pdk\Tests\Bootstrap\MockAuditRepository $repository */
$repository = Pdk::get(AuditRepositoryInterface::class);
$audit = factory(Audit::class)
->withId('123')
->make();

expect($orders)
->toHaveLength(3)
->and(
$orders->every(function ($order) {
return is_a($order, PdkOrder::class);
})
)
->toBeTrue();
});
Audits::add($audit);

it('updates order', function () {
/** @var \MyParcelNL\Pdk\Tests\Bootstrap\MockPdkOrderRepository $repository */
$repository = Pdk::get(PdkOrderRepositoryInterface::class);
$order = new PdkOrder(['externalIdentifier' => 'PS-123']);
$newOrder = $repository->update($order);
$audits = $repository->all();

expect($newOrder)->toBeInstanceOf(PdkOrder::class);
expect($audits)
->toHaveLength(1)
->and(
$audits->first()
->getId()
)
->toBe('123');
});
10 changes: 4 additions & 6 deletions tests/Unit/Audit/Service/AuditServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

usesShared(new UsesMockPdkInstance());

it('gets all audits', static function () {
it('gets all audits', function () {
$auditsFactory = factory(AuditCollection::class)
->push(
factory(Audit::class)
Expand All @@ -41,13 +41,11 @@
$audit = $auditService->add(
factory(Audit::class)
->withModelIdentifier('foo')
->make()
);

expect($audit)
->toBeInstanceOf(Audit::class)
->and($audit->id)
->toBeString()
->and($audit->id)
->not()
->toBeEmpty();
->and($audit->modelIdentifier)
->toBe('foo');
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"emptyMailboxWeight": 0,
"emptyParcelWeight": 0,
"orderMode": false,
"processDirectly": false,
"processDirectly": "-1",
"saveCustomerAddress": false,
"sendNotificationAfter": -1,
"sendReturnEmail": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@
"emptyMailboxWeight": 0,
"emptyParcelWeight": 0,
"orderMode": false,
"processDirectly": false,
"processDirectly": "-1",
"saveCustomerAddress": false,
"sendNotificationAfter": -1,
"sendReturnEmail": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,40 @@
},
{
"name": "processDirectly",
"$component": "ToggleInput",
"$component": "SelectInput",
"label": "settings_order_process_directly",
"description": "settings_order_process_directly_description"
"description": "settings_order_process_directly_description",
"options": [
{
"value": -1,
"label": "settings_none"
},
{
"plainLabel": "Pending",
"value": "pending"
},
{
"plainLabel": "Paid",
"value": "paid"
},
{
"plainLabel": "Shipped",
"value": "shipped"
},
{
"plainLabel": "Completed",
"value": "completed"
},
{
"plainLabel": "Cancelled",
"value": "cancelled"
},
{
"plainLabel": "Refunded",
"value": "refunded"
}
],
"sort": "desc"
},
{
"name": "sendReturnEmail",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"emptyMailboxWeight": 0,
"emptyParcelWeight": 0,
"orderMode": false,
"processDirectly": false,
"processDirectly": "-1",
"saveCustomerAddress": false,
"sendNotificationAfter": -1,
"sendReturnEmail": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"emptyMailboxWeight": 0,
"emptyParcelWeight": 0,
"orderMode": false,
"processDirectly": false,
"processDirectly": "-1",
"saveCustomerAddress": false,
"sendNotificationAfter": -1,
"sendReturnEmail": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"emptyMailboxWeight": 0,
"emptyParcelWeight": 0,
"orderMode": false,
"processDirectly": false,
"processDirectly": "-1",
"saveCustomerAddress": false,
"sendNotificationAfter": -1,
"sendReturnEmail": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"emptyMailboxWeight": 0,
"emptyParcelWeight": 0,
"orderMode": false,
"processDirectly": false,
"processDirectly": "-1",
"saveCustomerAddress": false,
"sendNotificationAfter": -1,
"sendReturnEmail": false,
Expand Down
Loading

0 comments on commit 1c19573

Please sign in to comment.