store/app/Models/OwnerVerificationRequest.php
Yoga Pangestu cf5b300895
Some checks are pending
linter / quality (push) Waiting to run
tests / ci (8.3) (push) Waiting to run
tests / ci (8.4) (push) Waiting to run
tests / ci (8.5) (push) Waiting to run
Refactor product status management: replace is_active with status enum
- Updated the OwnerVerificationRequest model to change pendingToggleIsActive to pendingToggleStatus, reflecting the new status structure.
- Refactored the Product model to replace is_active with status, utilizing the ProductStatus enum for better clarity and type safety.
- Modified the ProductService to accommodate the new status field, including pagination and creation logic.
- Adjusted the AnalysisService to filter products based on their status.
- Updated the VerificationChangeFormatter to handle the new status field.
- Created a migration to remove is_active from the products table and add status with appropriate default values.
- Refactored the ProductSeeder to use the new status field.
- Updated frontend components and types to reflect the changes from is_active to status.
- Adjusted tests to ensure they validate the new status logic correctly.
2026-07-18 16:34:34 +07:00

195 lines
4.9 KiB
PHP

<?php
namespace App\Models;
use App\Enums\OwnerVerificationAction;
use App\Enums\OwnerVerificationStatus;
use App\Enums\Permission;
use App\Models\Concerns\HasModuleMedia;
use App\Models\Concerns\HasRejection;
use App\Models\Concerns\InteractsWithActivityLog;
use App\Support\ActivityLog\ModelLabel;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Spatie\MediaLibrary\HasMedia;
#[Guarded(['id'])]
#[Appends([
'action_label',
'created_at_formatted',
'is_pending',
'status_label',
'subject_label',
'submitted_by_name',
'verified_at_formatted',
'verified_by_name',
])]
class OwnerVerificationRequest extends Model implements HasMedia
{
// 1. Use Trait
use HasFactory, HasModuleMedia, HasRejection, InteractsWithActivityLog;
// 2. Casting
protected function casts(): array
{
return [
'action' => OwnerVerificationAction::class,
'payload' => 'array',
'status' => OwnerVerificationStatus::class,
'verified_at' => 'datetime',
];
}
// 3. Scope (grouped by column, then alphabetical)
// Column Group: status
#[Scope]
protected function approved(Builder $query): void
{
$query->where('status', OwnerVerificationStatus::APPROVED);
}
#[Scope]
protected function pending(Builder $query): void
{
$query->where('status', OwnerVerificationStatus::PENDING);
}
#[Scope]
protected function rejected(Builder $query): void
{
$query->where('status', OwnerVerificationStatus::REJECTED);
}
// Scope lainnya
#[Scope]
protected function forSubmitter(Builder $query, User $user): void
{
$query->where('submitted_by_id', $user->id);
}
#[Scope]
protected function visibleTo(Builder $query, User $user): void
{
if ($user->can(Permission::OWNER_VERIFICATIONS_VERIFY->value)) {
return;
}
$query->forSubmitter($user);
}
// 4. Attribute
public function actionLabel(): Attribute
{
return Attribute::make(
get: fn () => $this->action?->label(),
);
}
public function createdAtFormatted(): Attribute
{
return Attribute::make(
get: fn () => $this->created_at?->translatedFormat('l, d F Y H:i'),
);
}
public function isPending(): Attribute
{
return Attribute::make(
get: fn () => $this->status === OwnerVerificationStatus::PENDING,
);
}
public function statusLabel(): Attribute
{
return Attribute::make(
get: fn () => $this->status?->label(),
);
}
public function subjectLabel(): Attribute
{
return Attribute::make(
get: fn () => ModelLabel::for($this->subject_type),
);
}
public function submittedByName(): Attribute
{
return Attribute::make(
get: fn () => $this->submittedBy?->profile?->full_name
?? $this->submittedBy?->username
?? '-',
);
}
public function verifiedAtFormatted(): Attribute
{
return Attribute::make(
get: fn () => $this->verified_at?->translatedFormat('l, d F Y H:i'),
);
}
public function verifiedByName(): Attribute
{
return Attribute::make(
get: fn () => $this->verifiedBy?->profile?->full_name
?? $this->verifiedBy?->username,
);
}
// 5. Other Methods
public static function mediaModuleName(): string
{
return 'owner_verification_request';
}
public function pendingToggleStatus(): ?string
{
if ($this->action !== OwnerVerificationAction::TOGGLE_STATUS) {
return null;
}
$payload = is_array($this->payload) ? $this->payload : [];
$new = $payload['new'] ?? null;
if (! is_array($new) || ! array_key_exists('status', $new)) {
return null;
}
return $new['status'];
}
public function priceImageCollection(int $index): string
{
return "price_images_{$index}";
}
public function variantImageCollection(int $index): string
{
return "variant_images_{$index}";
}
// 6. Relation
public function subject(): MorphTo
{
return $this->morphTo();
}
public function submittedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'submitted_by_id')->withTrashed();
}
public function verifiedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'verified_by_id')->withTrashed();
}
}