feat: add checkout editing policy to tenants and update related logic across services and resources
This commit is contained in:
parent
c8ce3b0da3
commit
9c46eff880
|
|
@ -11,9 +11,11 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
#[Fillable([
|
#[Fillable([
|
||||||
'tenant_code',
|
'tenant_code',
|
||||||
|
'code',
|
||||||
'source_type',
|
'source_type',
|
||||||
'category_id',
|
'category_id',
|
||||||
'product_layout',
|
'product_layout',
|
||||||
|
|
@ -33,6 +35,29 @@ class FeaturedGroup extends Model
|
||||||
'source_type' => FeaturedGroupSource::Manual->value,
|
'source_type' => FeaturedGroupSource::Manual->value,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
protected static function booted(): void
|
||||||
|
{
|
||||||
|
static::creating(function (FeaturedGroup $featuredGroup): void {
|
||||||
|
if (filled($featuredGroup->code)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$baseCode = Str::slug($featuredGroup->group_name) ?: 'group';
|
||||||
|
$code = $baseCode;
|
||||||
|
$suffix = 2;
|
||||||
|
|
||||||
|
while (static::query()
|
||||||
|
->where('tenant_code', $featuredGroup->tenant_code)
|
||||||
|
->where('code', $code)
|
||||||
|
->exists()) {
|
||||||
|
$code = "{$baseCode}-{$suffix}";
|
||||||
|
$suffix++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$featuredGroup->code = $code;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
protected function casts(): array
|
protected function casts(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ class OnTicketFeaturedGroupResource extends JsonResource
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'id' => $this->id,
|
'id' => $this->id,
|
||||||
|
'code' => $this->code,
|
||||||
'category_id' => $this->category_id,
|
'category_id' => $this->category_id,
|
||||||
'category_name' => $this->category->nombre,
|
'category_name' => $this->category->nombre,
|
||||||
'group_name' => $this->group_name,
|
'group_name' => $this->group_name,
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ class CatalogFeaturedGroupResource extends JsonResource
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'id' => $this->id,
|
'id' => $this->id,
|
||||||
|
'code' => $this->code,
|
||||||
'title' => $this->group_name,
|
'title' => $this->group_name,
|
||||||
'layout' => $this->product_layout->value,
|
'layout' => $this->product_layout->value,
|
||||||
'group_layout' => $this->group_layout->value,
|
'group_layout' => $this->group_layout->value,
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,9 @@ use App\Domains\Catalog\Models\StockReservation;
|
||||||
use App\Domains\Catalog\Services\StockReservationService;
|
use App\Domains\Catalog\Services\StockReservationService;
|
||||||
use App\Domains\Purchase\Models\Purchase;
|
use App\Domains\Purchase\Models\Purchase;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
class ReleaseCheckoutService
|
class ReleaseCheckoutService
|
||||||
{
|
{
|
||||||
|
|
@ -40,7 +42,21 @@ class ReleaseCheckoutService
|
||||||
->where('expires_at', '<=', now())
|
->where('expires_at', '<=', now())
|
||||||
->orderBy('id')
|
->orderBy('id')
|
||||||
->eachById(function (Purchase $purchase) use (&$expiredCount): void {
|
->eachById(function (Purchase $purchase) use (&$expiredCount): void {
|
||||||
$purchase = $this->expire($purchase);
|
try {
|
||||||
|
$purchase = $this->expire($purchase);
|
||||||
|
} catch (Throwable $exception) {
|
||||||
|
Log::channel('commands')->error('Failed to expire overdue purchase.', [
|
||||||
|
'command' => 'reservations:expire',
|
||||||
|
'purchase_id' => $purchase->getKey(),
|
||||||
|
'tenant_codigo' => $purchase->tenant_codigo,
|
||||||
|
'cart_id' => $purchase->cart_id,
|
||||||
|
'status' => $purchase->status,
|
||||||
|
'expires_at' => $purchase->expires_at,
|
||||||
|
'exception' => $exception,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if ($purchase->status === Purchase::STATUS_EXPIRED) {
|
if ($purchase->status === Purchase::STATUS_EXPIRED) {
|
||||||
$expiredCount++;
|
$expiredCount++;
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,12 @@
|
||||||
|
|
||||||
namespace App\Domains\Sale\Resources\AdminApp;
|
namespace App\Domains\Sale\Resources\AdminApp;
|
||||||
|
|
||||||
|
use App\Domains\Cart\Models\CartItem;
|
||||||
use App\Domains\Purchase\Models\Purchase;
|
use App\Domains\Purchase\Models\Purchase;
|
||||||
use App\Domains\Purchase\Models\PurchaseItem;
|
use App\Domains\Purchase\Models\PurchaseItem;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
/** @mixin Purchase */
|
/** @mixin Purchase */
|
||||||
class SaleDetailResource extends JsonResource
|
class SaleDetailResource extends JsonResource
|
||||||
|
|
@ -13,23 +15,44 @@ class SaleDetailResource extends JsonResource
|
||||||
/** @return array<string, mixed> */
|
/** @return array<string, mixed> */
|
||||||
public function toArray(Request $request): array
|
public function toArray(Request $request): array
|
||||||
{
|
{
|
||||||
|
$items = $this->saleItems();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'id' => $this->id,
|
'id' => $this->id,
|
||||||
'items' => $this->items->map(fn (PurchaseItem $item): array => [
|
'items' => $items->map(fn (PurchaseItem|CartItem $item): array => [
|
||||||
'id' => $item->id,
|
'id' => $item->id,
|
||||||
'product' => $item->item_nombre,
|
'product' => $item instanceof PurchaseItem
|
||||||
|
? $item->item_nombre
|
||||||
|
: $item->selectedItem()?->getName(),
|
||||||
'event_dates' => $this->eventDates($item),
|
'event_dates' => $this->eventDates($item),
|
||||||
'quantity' => (int) $item->cantidad,
|
'quantity' => (int) $item->cantidad,
|
||||||
'unit_price' => $this->formatMoney($item->precio_unitario),
|
'unit_price' => $this->formatMoney($this->unitPrice($item)),
|
||||||
'total' => $this->formatMoney($item->total),
|
'total' => $this->formatMoney($this->lineTotal($item)),
|
||||||
])->values(),
|
])->values(),
|
||||||
'total' => $this->formatMoney($this->total),
|
'total' => $this->formatMoney($this->total),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return list<string> */
|
/** @return Collection<int, PurchaseItem|CartItem> */
|
||||||
private function eventDates(PurchaseItem $item): array
|
private function saleItems(): Collection
|
||||||
{
|
{
|
||||||
|
if ($this->items->isNotEmpty()) {
|
||||||
|
return $this->items;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->cart?->items ?? collect();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return list<string> */
|
||||||
|
private function eventDates(PurchaseItem|CartItem $item): array
|
||||||
|
{
|
||||||
|
if ($item instanceof CartItem) {
|
||||||
|
return $item->variant?->selectedEventDates()
|
||||||
|
->map(fn ($eventDate): string => $eventDate->date->format('Y-m-d'))
|
||||||
|
->values()
|
||||||
|
->all() ?? [];
|
||||||
|
}
|
||||||
|
|
||||||
return collect($item->variant_attributes ?? [])
|
return collect($item->variant_attributes ?? [])
|
||||||
->filter(fn (mixed $attribute): bool => is_array($attribute)
|
->filter(fn (mixed $attribute): bool => is_array($attribute)
|
||||||
&& mb_strtolower(trim((string) ($attribute['name'] ?? ''))) === 'fecha')
|
&& mb_strtolower(trim((string) ($attribute['name'] ?? ''))) === 'fecha')
|
||||||
|
|
@ -43,6 +66,20 @@ class SaleDetailResource extends JsonResource
|
||||||
->all();
|
->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function unitPrice(PurchaseItem|CartItem $item): float|int|string|null
|
||||||
|
{
|
||||||
|
return $item instanceof PurchaseItem
|
||||||
|
? $item->precio_unitario
|
||||||
|
: $item->selectedItem()?->getPrice();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function lineTotal(PurchaseItem|CartItem $item): float|int|string|null
|
||||||
|
{
|
||||||
|
return $item instanceof PurchaseItem
|
||||||
|
? $item->total
|
||||||
|
: ($item->selectedItem()?->getPrice() ?? 0) * $item->cantidad;
|
||||||
|
}
|
||||||
|
|
||||||
private function formatMoney(float|int|string|null $amount): string
|
private function formatMoney(float|int|string|null $amount): string
|
||||||
{
|
{
|
||||||
return number_format((float) ($amount ?? 0), 2, '.', '');
|
return number_format((float) ($amount ?? 0), 2, '.', '');
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,13 @@ class AdminAppSaleService
|
||||||
{
|
{
|
||||||
return Purchase::query()
|
return Purchase::query()
|
||||||
->where('tenant_codigo', $tenant->codigo)
|
->where('tenant_codigo', $tenant->codigo)
|
||||||
->with('items')
|
->with([
|
||||||
|
'items',
|
||||||
|
'cart.items.catalogItem',
|
||||||
|
'cart.items.variant.catalogItem',
|
||||||
|
'cart.items.variant.eventDates',
|
||||||
|
'cart.items.variant.eventDate',
|
||||||
|
])
|
||||||
->findOrFail($saleId);
|
->findOrFail($saleId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,8 @@ use Illuminate\Support\Facades\Schema;
|
||||||
'dominio',
|
'dominio',
|
||||||
'base_path',
|
'base_path',
|
||||||
'site_title',
|
'site_title',
|
||||||
|
'address',
|
||||||
|
'phone',
|
||||||
'primary_color',
|
'primary_color',
|
||||||
'secondary_color',
|
'secondary_color',
|
||||||
'danger_color',
|
'danger_color',
|
||||||
|
|
|
||||||
|
|
@ -82,6 +82,8 @@ class StoreTenantRequest extends FormRequest
|
||||||
->where('dominio', $this->input('dominio')),
|
->where('dominio', $this->input('dominio')),
|
||||||
],
|
],
|
||||||
'site_title' => ['sometimes', 'nullable', 'string', 'max:255'],
|
'site_title' => ['sometimes', 'nullable', 'string', 'max:255'],
|
||||||
|
'address' => ['sometimes', 'nullable', 'string', 'max:255'],
|
||||||
|
'phone' => ['sometimes', 'nullable', 'string', 'max:255'],
|
||||||
'primary_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
'primary_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
||||||
'secondary_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
'secondary_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
||||||
'danger_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
'danger_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
||||||
|
|
|
||||||
|
|
@ -103,6 +103,8 @@ class UpdateTenantRequest extends FormRequest
|
||||||
->ignore($tenant?->id),
|
->ignore($tenant?->id),
|
||||||
],
|
],
|
||||||
'site_title' => ['sometimes', 'nullable', 'string', 'max:255'],
|
'site_title' => ['sometimes', 'nullable', 'string', 'max:255'],
|
||||||
|
'address' => ['sometimes', 'nullable', 'string', 'max:255'],
|
||||||
|
'phone' => ['sometimes', 'nullable', 'string', 'max:255'],
|
||||||
'primary_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
'primary_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
||||||
'secondary_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
'secondary_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
||||||
'danger_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
'danger_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,8 @@ class TenantResource extends JsonResource
|
||||||
'site_title' => $this->site_title
|
'site_title' => $this->site_title
|
||||||
?? $this->websiteType?->site_title
|
?? $this->websiteType?->site_title
|
||||||
?? 'ShopitFront',
|
?? 'ShopitFront',
|
||||||
|
'address' => $this->address,
|
||||||
|
'phone' => $this->phone,
|
||||||
'favicon' => ($this->favicon ?? $this->websiteType?->favicon)
|
'favicon' => ($this->favicon ?? $this->websiteType?->favicon)
|
||||||
?->getTemporaryUrl(1440),
|
?->getTemporaryUrl(1440),
|
||||||
'primary_color' => $this->primary_color,
|
'primary_color' => $this->primary_color,
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('tenants', function (Blueprint $table): void {
|
||||||
|
$table->string('address')->nullable()->after('site_title');
|
||||||
|
$table->string('phone')->nullable()->after('address');
|
||||||
|
});
|
||||||
|
|
||||||
|
DB::table('tenants')->update([
|
||||||
|
'address' => 'Av. San Lorenzo 1542, Rosario',
|
||||||
|
'phone' => '54 9 (0341) 6658247',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('tenants', function (Blueprint $table): void {
|
||||||
|
$table->dropColumn(['address', 'phone']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('featured_groups', function (Blueprint $table): void {
|
||||||
|
$table->string('code')->nullable()->after('tenant_code');
|
||||||
|
});
|
||||||
|
|
||||||
|
$usedCodes = [];
|
||||||
|
|
||||||
|
DB::table('featured_groups')
|
||||||
|
->select(['id', 'tenant_code', 'group_name'])
|
||||||
|
->orderBy('id')
|
||||||
|
->get()
|
||||||
|
->each(function (object $group) use (&$usedCodes): void {
|
||||||
|
$baseCode = Str::slug($group->group_name) ?: "group-{$group->id}";
|
||||||
|
$code = $baseCode;
|
||||||
|
$suffix = 2;
|
||||||
|
|
||||||
|
while (isset($usedCodes[$group->tenant_code][$code])) {
|
||||||
|
$code = "{$baseCode}-{$suffix}";
|
||||||
|
$suffix++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$usedCodes[$group->tenant_code][$code] = true;
|
||||||
|
|
||||||
|
DB::table('featured_groups')
|
||||||
|
->where('id', $group->id)
|
||||||
|
->update(['code' => $code]);
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::table('featured_groups', function (Blueprint $table): void {
|
||||||
|
$table->string('code')->nullable(false)->change();
|
||||||
|
$table->unique(['tenant_code', 'code']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('featured_groups', function (Blueprint $table): void {
|
||||||
|
$table->dropUnique(['tenant_code', 'code']);
|
||||||
|
$table->dropColumn('code');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -81,6 +81,7 @@ class OnTicketFeaturedGroupControllerTest extends TestCase
|
||||||
'is_featured' => true,
|
'is_featured' => true,
|
||||||
])
|
])
|
||||||
->assertCreated()
|
->assertCreated()
|
||||||
|
->assertJsonPath('data.code', 'food')
|
||||||
->assertJsonPath('data.category_name', 'Food')
|
->assertJsonPath('data.category_name', 'Food')
|
||||||
->assertJsonPath('data.group_name', 'Food')
|
->assertJsonPath('data.group_name', 'Food')
|
||||||
->assertJsonPath('data.is_featured', true)
|
->assertJsonPath('data.is_featured', true)
|
||||||
|
|
@ -96,6 +97,7 @@ class OnTicketFeaturedGroupControllerTest extends TestCase
|
||||||
]);
|
]);
|
||||||
$this->assertDatabaseHas('featured_groups', [
|
$this->assertDatabaseHas('featured_groups', [
|
||||||
'tenant_code' => $tenant->codigo,
|
'tenant_code' => $tenant->codigo,
|
||||||
|
'code' => 'food',
|
||||||
'source_type' => 'category',
|
'source_type' => 'category',
|
||||||
'category_id' => $categoryId,
|
'category_id' => $categoryId,
|
||||||
'product_layout' => 'row',
|
'product_layout' => 'row',
|
||||||
|
|
@ -130,6 +132,7 @@ class OnTicketFeaturedGroupControllerTest extends TestCase
|
||||||
'is_featured' => true,
|
'is_featured' => true,
|
||||||
])
|
])
|
||||||
->assertOk()
|
->assertOk()
|
||||||
|
->assertJsonPath('data.code', 'old-name')
|
||||||
->assertJsonPath('data.category_name', 'New name')
|
->assertJsonPath('data.category_name', 'New name')
|
||||||
->assertJsonPath('data.group_name', 'New name')
|
->assertJsonPath('data.group_name', 'New name')
|
||||||
->assertJsonPath('data.is_featured', true)
|
->assertJsonPath('data.is_featured', true)
|
||||||
|
|
@ -142,6 +145,7 @@ class OnTicketFeaturedGroupControllerTest extends TestCase
|
||||||
]);
|
]);
|
||||||
$this->assertDatabaseHas('featured_groups', [
|
$this->assertDatabaseHas('featured_groups', [
|
||||||
'id' => $group->id,
|
'id' => $group->id,
|
||||||
|
'code' => 'old-name',
|
||||||
'group_name' => 'New name',
|
'group_name' => 'New name',
|
||||||
'product_layout' => 'row',
|
'product_layout' => 'row',
|
||||||
'group_layout' => 'paginated',
|
'group_layout' => 'paginated',
|
||||||
|
|
|
||||||
|
|
@ -1118,6 +1118,43 @@ class StorePurchaseTest extends TestCase
|
||||||
->assertSuccessful();
|
->assertSuccessful();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_it_continues_expiring_purchases_after_an_inconsistent_reservation(): void
|
||||||
|
{
|
||||||
|
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$variant = $this->createVariantForTenant('sonder', 10, '50.00');
|
||||||
|
$inconsistentPurchase = $this->createCheckoutPurchase($user, 'sonder', $variant, 1);
|
||||||
|
$validPurchase = $this->createCheckoutPurchase($user, 'sonder', $variant, 1);
|
||||||
|
|
||||||
|
$inconsistentPurchase->items()->create([
|
||||||
|
'source_catalog_item_id' => $variant->catalog_item_id,
|
||||||
|
'source_variant_id' => $variant->id,
|
||||||
|
'nombre' => 'Inconsistent item',
|
||||||
|
'slug' => 'inconsistent-item',
|
||||||
|
'cantidad' => 1,
|
||||||
|
'precio_unitario' => '50.00',
|
||||||
|
'discount_total' => '0.00',
|
||||||
|
'tax_total' => '0.00',
|
||||||
|
'total' => '50.00',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->travel(31)->minutes();
|
||||||
|
|
||||||
|
$this->artisan('reservations:expire')
|
||||||
|
->expectsOutput('Expired purchases: 1')
|
||||||
|
->expectsOutput('Expired cart items: 0')
|
||||||
|
->assertSuccessful();
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('compras', [
|
||||||
|
'id' => $inconsistentPurchase->id,
|
||||||
|
'status' => Purchase::STATUS_CREATED,
|
||||||
|
]);
|
||||||
|
$this->assertDatabaseHas('compras', [
|
||||||
|
'id' => $validPurchase->id,
|
||||||
|
'status' => Purchase::STATUS_EXPIRED,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
public function test_purchase_detail_uses_cart_items_for_created_purchase(): void
|
public function test_purchase_detail_uses_cart_items_for_created_purchase(): void
|
||||||
{
|
{
|
||||||
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ namespace Tests\Feature\Sale;
|
||||||
use App\Domains\Auth\Models\User;
|
use App\Domains\Auth\Models\User;
|
||||||
use App\Domains\Authorization\Enums\RoleCode;
|
use App\Domains\Authorization\Enums\RoleCode;
|
||||||
use App\Domains\Cart\Models\Cart;
|
use App\Domains\Cart\Models\Cart;
|
||||||
|
use App\Domains\Cart\Models\CartItem;
|
||||||
use App\Domains\Catalog\Models\CatalogItem;
|
use App\Domains\Catalog\Models\CatalogItem;
|
||||||
use App\Domains\Purchase\Events\PurchasePaid;
|
use App\Domains\Purchase\Events\PurchasePaid;
|
||||||
use App\Domains\Purchase\Models\Purchase;
|
use App\Domains\Purchase\Models\Purchase;
|
||||||
|
|
@ -71,6 +72,44 @@ class AdminAppSaleControllerTest extends TestCase
|
||||||
->assertJsonPath('data.total', '40000.00');
|
->assertJsonPath('data.total', '40000.00');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_an_adminapp_user_can_read_cart_items_from_an_unconfirmed_sale(): void
|
||||||
|
{
|
||||||
|
$tenant = $this->createTenant('acme');
|
||||||
|
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
||||||
|
|
||||||
|
$catalogItem = CatalogItem::query()->create([
|
||||||
|
'tenant_code' => $tenant->codigo,
|
||||||
|
'slug' => 'entrada-general',
|
||||||
|
'nombre' => 'Entrada general',
|
||||||
|
'descripcion' => 'Acceso general',
|
||||||
|
'precio' => '12500.00',
|
||||||
|
]);
|
||||||
|
$cart = Cart::query()->create([
|
||||||
|
'tenant_codigo' => $tenant->codigo,
|
||||||
|
'status' => 'checkout',
|
||||||
|
]);
|
||||||
|
$cartItem = CartItem::query()->create([
|
||||||
|
'cart_id' => $cart->id,
|
||||||
|
'catalog_item_id' => $catalogItem->id,
|
||||||
|
'cantidad' => 2,
|
||||||
|
]);
|
||||||
|
$purchase = Purchase::query()->create([
|
||||||
|
'cart_id' => $cart->id,
|
||||||
|
'tenant_codigo' => $tenant->codigo,
|
||||||
|
'status' => Purchase::STATUS_PENDING_PAYMENT,
|
||||||
|
'total' => '25000.00',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->getJson("/api/v1/adminapp/tenant/sales/{$purchase->id}")
|
||||||
|
->assertOk()
|
||||||
|
->assertJsonPath('data.items.0.id', $cartItem->id)
|
||||||
|
->assertJsonPath('data.items.0.product', 'Entrada general')
|
||||||
|
->assertJsonPath('data.items.0.event_dates', [])
|
||||||
|
->assertJsonPath('data.items.0.quantity', 2)
|
||||||
|
->assertJsonPath('data.items.0.unit_price', '12500.00')
|
||||||
|
->assertJsonPath('data.items.0.total', '25000.00');
|
||||||
|
}
|
||||||
|
|
||||||
public function test_an_adminapp_user_cannot_read_a_sale_from_another_tenant(): void
|
public function test_an_adminapp_user_cannot_read_a_sale_from_another_tenant(): void
|
||||||
{
|
{
|
||||||
$tenant = $this->createTenant('acme');
|
$tenant = $this->createTenant('acme');
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,8 @@ class BootstrapTenantControllerTest extends TestCase
|
||||||
'codigo' => 'acme',
|
'codigo' => 'acme',
|
||||||
'nombre' => 'Acme',
|
'nombre' => 'Acme',
|
||||||
'dominio' => 'acme.com',
|
'dominio' => 'acme.com',
|
||||||
|
'address' => 'Calle Test 123, Rosario',
|
||||||
|
'phone' => '+54 341 555 1234',
|
||||||
'primary_color' => '#ff0000',
|
'primary_color' => '#ff0000',
|
||||||
'secondary_color' => '#00ff00',
|
'secondary_color' => '#00ff00',
|
||||||
'danger_color' => '#0000ff',
|
'danger_color' => '#0000ff',
|
||||||
|
|
@ -84,6 +86,8 @@ class BootstrapTenantControllerTest extends TestCase
|
||||||
->assertOk()
|
->assertOk()
|
||||||
->assertJsonPath('data.codigo', 'acme')
|
->assertJsonPath('data.codigo', 'acme')
|
||||||
->assertJsonPath('data.dominio', 'acme.com')
|
->assertJsonPath('data.dominio', 'acme.com')
|
||||||
|
->assertJsonPath('data.address', 'Calle Test 123, Rosario')
|
||||||
|
->assertJsonPath('data.phone', '+54 341 555 1234')
|
||||||
->assertJsonPath('data.primary_color', '#ff0000')
|
->assertJsonPath('data.primary_color', '#ff0000')
|
||||||
->assertJsonPath('data.secondary_color', '#00ff00')
|
->assertJsonPath('data.secondary_color', '#00ff00')
|
||||||
->assertJsonPath('data.danger_color', '#0000ff')
|
->assertJsonPath('data.danger_color', '#0000ff')
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,9 @@
|
||||||
|
|
||||||
namespace Tests\Unit\Sale;
|
namespace Tests\Unit\Sale;
|
||||||
|
|
||||||
|
use App\Domains\Cart\Models\Cart;
|
||||||
|
use App\Domains\Cart\Models\CartItem;
|
||||||
|
use App\Domains\Catalog\Models\CatalogItem;
|
||||||
use App\Domains\Purchase\Models\Purchase;
|
use App\Domains\Purchase\Models\Purchase;
|
||||||
use App\Domains\Purchase\Models\PurchaseItem;
|
use App\Domains\Purchase\Models\PurchaseItem;
|
||||||
use App\Domains\Sale\Resources\AdminApp\SaleDetailResource;
|
use App\Domains\Sale\Resources\AdminApp\SaleDetailResource;
|
||||||
|
|
@ -38,4 +41,39 @@ class SaleDetailResourceTest extends TestCase
|
||||||
$this->assertSame('30000.00', $data['items'][0]['total']);
|
$this->assertSame('30000.00', $data['items'][0]['total']);
|
||||||
$this->assertSame('30000.00', $data['total']);
|
$this->assertSame('30000.00', $data['total']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_it_uses_the_associated_cart_items_when_the_purchase_has_no_items(): void
|
||||||
|
{
|
||||||
|
$catalogItem = (new CatalogItem)->forceFill([
|
||||||
|
'id' => 21,
|
||||||
|
'nombre' => 'Entrada general',
|
||||||
|
'precio' => '12500.00',
|
||||||
|
]);
|
||||||
|
$cartItem = (new CartItem)->forceFill([
|
||||||
|
'id' => 34,
|
||||||
|
'catalog_item_id' => $catalogItem->id,
|
||||||
|
'cantidad' => 2,
|
||||||
|
]);
|
||||||
|
$cartItem->setRelation('catalogItem', $catalogItem);
|
||||||
|
$cartItem->setRelation('variant', null);
|
||||||
|
|
||||||
|
$cart = new Cart;
|
||||||
|
$cart->setRelation('items', collect([$cartItem]));
|
||||||
|
|
||||||
|
$purchase = (new Purchase)->forceFill([
|
||||||
|
'id' => 16,
|
||||||
|
'total' => '25000.00',
|
||||||
|
]);
|
||||||
|
$purchase->setRelation('items', collect());
|
||||||
|
$purchase->setRelation('cart', $cart);
|
||||||
|
|
||||||
|
$data = (new SaleDetailResource($purchase))->resolve(Request::create('/'));
|
||||||
|
|
||||||
|
$this->assertSame(34, $data['items'][0]['id']);
|
||||||
|
$this->assertSame('Entrada general', $data['items'][0]['product']);
|
||||||
|
$this->assertSame([], $data['items'][0]['event_dates']);
|
||||||
|
$this->assertSame(2, $data['items'][0]['quantity']);
|
||||||
|
$this->assertSame('12500.00', $data['items'][0]['unit_price']);
|
||||||
|
$this->assertSame('25000.00', $data['items'][0]['total']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue