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

Update cart addresses #192

Merged
merged 8 commits into from
Oct 24, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ public function store(
): DataResponse {
$this->authorize('create', CartAddress::modelClass());

$validatedRequest = $request->validated();

$meta = [
...$validatedRequest['meta'] ?? [],
...$request->validated('meta') ?? [],
'company_in' => $request->validated('company_in', null),
'company_tin' => $request->validated('company_tin', null),
];
Expand Down Expand Up @@ -66,10 +64,8 @@ public function update(
): DataResponse {
$this->authorize('update', $cartAddress);

$validatedRequest = $request->validated();

$meta = array_merge($cartAddress->meta?->toArray() ?? [], [
...$validatedRequest['meta'] ?? [],
...$request->validated('meta') ?? [],
'company_in' => $request->validated('company_in', null),
'company_tin' => $request->validated('company_tin', null),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@

class ContinuousUpdateCartAddressController extends Controller implements ContinuousUpdateCartAddressControllerContract
{
/**
* Update an existing resource.
*
* @return \Illuminate\Contracts\Support\Responsable|\Illuminate\Http\Response
*/
public function continuousUpdate(
CartAddressSchema $schema,
CartAddressContinuousUpdateRequest $request,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,6 @@ public function rules(): array
*/
public function messages(): array
{
return [
'title.string' => __('lunar-api::validation.cart_addresses.title.string'),
'first_name.string' => __('lunar-api::validation.cart_addresses.first_name.string'),
'last_name.string' => __('lunar-api::validation.cart_addresses.last_name.string'),
'company_name.string' => __('lunar-api::validation.cart_addresses.company_name.string'),
'company_in.string' => __('lunar-api::validation.cart_addresses.company_in.string'),
'company_tin.string' => __('lunar-api::validation.cart_addresses.company_tin.string'),
'line_one.string' => __('lunar-api::validation.cart_addresses.line_one.string'),
'line_two.string' => __('lunar-api::validation.cart_addresses.line_two.string'),
'line_three.string' => __('lunar-api::validation.cart_addresses.line_three.string'),
'city.string' => __('lunar-api::validation.cart_addresses.city.string'),
'state.string' => __('lunar-api::validation.cart_addresses.state.string'),
'postcode.string' => __('lunar-api::validation.cart_addresses.postcode.string'),
'delivery_instructions.string' => __('lunar-api::validation.cart_addresses.delivery_instructions.string'),
'contact_email.string' => __('lunar-api::validation.cart_addresses.contact_email.string'),
'contact_phone.string' => __('lunar-api::validation.cart_addresses.contact_phone.string'),
'shipping_option.string' => __('lunar-api::validation.cart_addresses.shipping_option.string'),
'address_type.required' => __('lunar-api::validation.cart_addresses.address_type.required'),
'address_type.string' => __('lunar-api::validation.cart_addresses.address_type.string'),
'address_type.in' => __('lunar-api::validation.cart_addresses.address_type.in'),
];
return array_merge(parent::messages(), []);
}
}
19 changes: 16 additions & 3 deletions src/Domain/Carts/Actions/CreateEmptyCartAddresses.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ public function __construct() {}
/**
* Create empty addresses for a cart.
*/
public function handle(CartContract $cart): void
public function handle(CartContract $cart): CartContract
{
/** @var Cart $cart */
$cart->setShippingAddress([
$cart = $this->createShippingAddress($cart);
$cart = $this->createBillingAddress($cart);

return $cart;
}

public function createShippingAddress(CartContract $cart): CartContract
{
/** @var Cart $cart */
return $cart->setShippingAddress([
'country_id' => null,
'title' => null,
'first_name' => null,
Expand All @@ -33,8 +42,12 @@ public function handle(CartContract $cart): void
'contact_phone' => null,
'meta' => null,
]);
}

$cart->setBillingAddress([
public function createBillingAddress(CartContract $cart): CartContract
{
/** @var Cart $cart */
return $cart->setBillingAddress([
'country_id' => null,
'title' => null,
'first_name' => null,
Expand Down
3 changes: 3 additions & 0 deletions src/Domain/Carts/Http/Routing/CartRouteGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function routes(): void
$server->resource($this->getPrefix(), CartsController::class)
->relationships(function (Relationships $relationships) {
$relationships->hasMany('cart_lines')->readOnly();
$relationships->hasMany('cart_addresses')->readOnly();
$relationships->hasOne('shipping_address')->readOnly();
$relationships->hasOne('billing_address')->readOnly();
})
->only('show');

Expand Down
12 changes: 12 additions & 0 deletions src/Domain/Carts/Policies/CartPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ public function viewCartLines(?Authenticatable $user, CartContract $cart): bool
return $this->check($user, $cart);
}

/**
* Authorize a user to view cart's cart addresses.
*/
public function viewCartAddresses(?Authenticatable $user, CartContract $cart): bool
{
if ($this->isFilamentAdmin($user)) {
return true;
}

return $this->check($user, $cart);
}

/**
* Authorize a user to view cart's purchasable.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

use Dystcz\LunarApi\Domain\CartAddresses\Models\CartAddress;
use Dystcz\LunarApi\Domain\Carts\Actions\CreateEmptyCartAddresses;
use Dystcz\LunarApi\Domain\Carts\Models\Cart;
use Dystcz\LunarApi\Domain\Customers\Models\Customer;
use Dystcz\LunarApi\Domain\Users\Models\User;
use Dystcz\LunarApi\Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\App;
use Lunar\Base\CartSessionInterface;

uses(TestCase::class, RefreshDatabase::class)
->group('carts', 'cart_addresses');

beforeEach(function () {
/** @var TestCase $this */
$this->user = User::factory()
->has(Customer::factory())
->create();

$this->cart = Cart::factory()
->for($this->user)
->withLines()
->create();

$this->cart = (new CreateEmptyCartAddresses)->handle($this->cart);

/** @property CartSessionManager $cartSession */
$this->cartSession = App::make(CartSessionInterface::class);

$this->cartSession->use($this->cart);
});

it('can list related cart addresses', function () {
/** @var TestCase $this */
$expected = $this->cart->addresses->map(fn (CartAddress $address) => [
'type' => 'cart_addresses',
'id' => (string) $address->getRouteKey(),
'attributes' => [
//
],
])->all();

$response = $this
->actingAs($this->user)
->jsonApi()
->expects('cart_addresses')
->get(serverUrl("/carts/{$this->cart->getRouteKey()}/cart_addresses"));

$response
->assertSuccessful()
->assertFetchedMany($expected);

});

it('can read related shipping address', function () {
/** @var TestCase $this */
$expected = [
'type' => 'cart_addresses',
'id' => (string) $this->cart->shippingAddress->getRouteKey(),
'attributes' => [
'address_type' => 'shipping',
],
];
$response = $this
->actingAs($this->user)
->jsonApi()
->expects('cart_addresses')
->get(serverUrl("/carts/{$this->cart->getRouteKey()}/shipping_address"));

$response
->assertSuccessful()
->assertFetchedOne($expected);

});

it('can read related billing address', function () {
/** @var TestCase $this */
$expected = [
'type' => 'cart_addresses',
'id' => (string) $this->cart->billingAddress->getRouteKey(),
'attributes' => [
'address_type' => 'billing',
],
];

$response = $this
->actingAs($this->user)
->jsonApi()
->expects('cart_addresses')
->get(serverUrl("/carts/{$this->cart->getRouteKey()}/billing_address"));

$response
->assertSuccessful()
->assertFetchedOne($expected);

});
Loading