Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed Jul 18, 2024
1 parent f4d1b11 commit 5909817
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 40 deletions.
1 change: 1 addition & 0 deletions resources/lang/en/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
'error_covered_area' => 'This restaurant currently does not deliver to your address',
'error_delivery_less_current_time' => 'The %s Time can not be less than current time!',
'error_invalid_payment' => 'The selected payment is invalid, please contact us',
'error_payment_profile_not_found' => 'The payment profile is not found, please contact us',
'error_payment_partial' => 'View [%s] not found for payment: %s.',
'error_email_exists' => 'An account exists with this email address. Please login to continue or use a different email address',

Expand Down
1 change: 1 addition & 0 deletions resources/models/order.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
'select' => 'status_name',
'type' => 'partial',
'path' => 'statuses/status_column',
'cssClass' => 'overflow-visible',
],
'payment' => [
'label' => 'lang:igniter.cart::default.orders.column_payment',
Expand Down
17 changes: 11 additions & 6 deletions src/Classes/OrderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ public function loadOrder()
$this->applyRequiredAttributes($order);
}

if (!$order->exists) {
$order->save();
$this->setCurrentOrderId($order->order_id);
$order->addOrderMenus($this->cart->content()->all());
$order->addOrderTotals($this->getCartTotals());
}

return $order;
}

Expand Down Expand Up @@ -212,8 +219,6 @@ public function saveOrder($order, array $data)

$order->save();

$this->setCurrentOrderId($order->order_id);

$order->addOrderMenus($this->cart->content()->all());
$order->addOrderTotals($this->getCartTotals());

Expand Down Expand Up @@ -288,10 +293,10 @@ protected function getCustomerAttributes()
$customer = $this->customer;

return [
'first_name' => $customer->first_name ?? null,
'last_name' => $customer->last_name ?? null,
'email' => $customer->email ?? null,
'telephone' => $customer->telephone ?? null,
'first_name' => $customer->first_name ?? '',
'last_name' => $customer->last_name ?? '',
'email' => $customer->email ?? '',
'telephone' => $customer->telephone ?? '',
'address_id' => $customer->address_id ?? null,
];
}
Expand Down
41 changes: 8 additions & 33 deletions tests/Classes/OrderManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Igniter\Cart\Models\Order;
use Igniter\Local\Models\Location as LocationModel;
use Igniter\PayRegister\Models\Payment;
use Igniter\PayRegister\Payments\Cod;
use Igniter\User\Models\Address;
use Igniter\User\Models\Customer;
use Illuminate\Support\Facades\App;
Expand Down Expand Up @@ -47,35 +46,16 @@
});

it('gets default payment with valid class_name', function() {
Payment::factory()->create([
'code' => 'cod',
'class_name' => Cod::class,
'status' => 1,
'is_default' => 1,
]);

expect($this->manager->getDefaultPayment())->toBeInstanceOf(Payment::class);
});

it('gets payment with valid class_name', function() {
$payment = Payment::factory()->create([
'code' => 'cod',
'class_name' => Cod::class,
'status' => 1,
'is_default' => 1,
]);
$payment = Payment::firstWhere('code', 'cod');

expect($this->manager->getPayment($payment->code))->toBeInstanceOf(Payment::class);
});

it('has location payments', function() {
Payment::factory()->create([
'code' => 'cod',
'class_name' => Cod::class,
'status' => 1,
'is_default' => 1,
]);

$this->location->settings()->create([
'item' => 'checkout.payments',
'data' => ['invalid-code'],
Expand Down Expand Up @@ -114,12 +94,7 @@
'admin.order.paymentProcessed',
]);

$payment = Payment::factory()->create([
'code' => 'cod',
'class_name' => Cod::class,
'status' => 1,
'is_default' => 1,
]);
$payment = Payment::firstWhere('code', 'cod');

$order = Order::factory()->create([
'payment' => $payment->code,
Expand Down Expand Up @@ -179,16 +154,16 @@
});

it('returns default payment code when current payment code is not set', function() {
$payment = Payment::factory()->create([
'code' => 'cod',
'class_name' => Cod::class,
'status' => 1,
'is_default' => 1,
]);
$payment = Payment::firstWhere('code', 'cod');

expect($this->manager->getCurrentPaymentCode())->toBe($payment->code);
});

it('returns null when default payment is not set', function() {
$payment = Payment::firstWhere('code', 'cod');
$payment->status = 0;
$payment->is_default = 0;
$payment->save();

expect($this->manager->getCurrentPaymentCode())->toBeNull();
});
2 changes: 1 addition & 1 deletion tests/ExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@

it('adds tax info to paypalexpress request parameters', function() {
$fields = $data = [];
$payment = Payment::factory()->create(['code' => 'paypalexpress']);
$payment = Payment::firstWhere('code', 'paypalexpress');
$order = Order::factory()->create();
$order->totals()->create([
'code' => 'tax',
Expand Down

0 comments on commit 5909817

Please sign in to comment.