-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: WIP initial commit for reports (#769)
- Loading branch information
Showing
160 changed files
with
3,067 additions
and
1,176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Actions\Models\Admin\Report; | ||
|
||
use App\Enums\Models\Admin\ApprovableStatus; | ||
use App\Enums\Models\Admin\ReportActionType; | ||
use App\Models\Admin\Report\ReportStep; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\Pivot; | ||
use Illuminate\Support\Facades\Date; | ||
|
||
/** | ||
* Class ManageStepAction. | ||
*/ | ||
class ManageStepAction | ||
{ | ||
/** | ||
* Execute the action and approve the step. | ||
* | ||
* @param ReportStep $step | ||
* @return void | ||
*/ | ||
public static function approveStep(ReportStep $step): void | ||
{ | ||
match ($step->action) { | ||
ReportActionType::CREATE => static::approveCreate($step), | ||
ReportActionType::DELETE => static::approveDelete($step), | ||
ReportActionType::UPDATE => static::approveUpdate($step), | ||
ReportActionType::ATTACH => static::approveAttach($step), | ||
ReportActionType::DETACH => static::approveDetach($step), | ||
default => null, | ||
}; | ||
} | ||
|
||
/** | ||
* Create the model according the step. | ||
* | ||
* @param ReportStep $step | ||
* @return void | ||
*/ | ||
protected static function approveCreate(ReportStep $step): void | ||
{ | ||
/** @var Model $model */ | ||
$model = new $step->actionable_type; | ||
|
||
$model::query()->create($step->fields); | ||
|
||
static::markAsApproved($step); | ||
} | ||
|
||
/** | ||
* Delete the model according the step. | ||
* | ||
* @param ReportStep $step | ||
* @return void | ||
*/ | ||
protected static function approveDelete(ReportStep $step): void | ||
{ | ||
$step->actionable->delete(); | ||
|
||
static::markAsApproved($step); | ||
} | ||
|
||
/** | ||
* Update the model according the step. | ||
* | ||
* @param ReportStep $step | ||
* @return void | ||
*/ | ||
protected static function approveUpdate(ReportStep $step): void | ||
{ | ||
$step->actionable->update($step->fields); | ||
|
||
static::markAsApproved($step); | ||
} | ||
|
||
/** | ||
* Attach a model to another according the step. | ||
* | ||
* @param ReportStep $step | ||
* @return void | ||
*/ | ||
protected static function approveAttach(ReportStep $step): void | ||
{ | ||
/** @var Pivot $pivot */ | ||
$pivot = new $step->pivot_class; | ||
|
||
$pivot::query()->create($step->fields); | ||
|
||
static::markAsApproved($step); | ||
} | ||
|
||
/** | ||
* Detach a model from another according the step. | ||
* | ||
* @param ReportStep $step | ||
* @return void | ||
*/ | ||
protected static function approveDetach(ReportStep $step): void | ||
{ | ||
/** @var Pivot $pivot */ | ||
$pivot = new $step->pivot_class; | ||
|
||
$pivot::query()->where($step->fields)->delete(); | ||
|
||
static::markAsApproved($step); | ||
} | ||
|
||
/** | ||
* Approve the step. | ||
* | ||
* @param ReportStep $step | ||
* @return void | ||
*/ | ||
protected static function markAsApproved(ReportStep $step): void | ||
{ | ||
static::updateStatus($step, ApprovableStatus::APPROVED); | ||
} | ||
|
||
/** | ||
* Reject the step. | ||
* | ||
* @param ReportStep $step | ||
* @return void | ||
*/ | ||
protected static function markAsRejected(ReportStep $step): void | ||
{ | ||
static::updateStatus($step, ApprovableStatus::REJECTED); | ||
} | ||
|
||
/** | ||
* Approve partially the step. | ||
* | ||
* @param ReportStep $step | ||
* @return void | ||
*/ | ||
protected static function markAsPartiallyApproved(ReportStep $step): void | ||
{ | ||
static::updateStatus($step, ApprovableStatus::PARTIALLY_APPROVED); | ||
} | ||
|
||
/** | ||
* Update the status of the step. | ||
* | ||
* @param ReportStep $step | ||
* @param ApprovableStatus $status | ||
* @return void | ||
*/ | ||
protected static function updateStatus(ReportStep $step, ApprovableStatus $status): void | ||
{ | ||
$step->query()->update([ | ||
ReportStep::ATTRIBUTE_STATUS => $status->value, | ||
ReportStep::ATTRIBUTE_FINISHED_AT => Date::now(), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Concerns\Models; | ||
|
||
use App\Models\Admin\Report; | ||
use App\Models\Admin\Report\ReportStep; | ||
use App\Models\BaseModel; | ||
use Illuminate\Database\Eloquent\Relations\MorphMany; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\Gate; | ||
|
||
/** | ||
* Trait Reportable. | ||
*/ | ||
trait Reportable | ||
{ | ||
/** | ||
* Get the reports made to the model. | ||
* | ||
* @return MorphMany | ||
*/ | ||
public function reportsteps(): MorphMany | ||
{ | ||
return $this->morphMany(ReportStep::class, ReportStep::ATTRIBUTE_ACTIONABLE); | ||
} | ||
|
||
/** | ||
* Bootstrap the model. | ||
* | ||
* @return void | ||
*/ | ||
protected static function boot(): void | ||
{ | ||
parent::boot(); | ||
|
||
static::creating(function (BaseModel $model) { | ||
|
||
if (Gate::forUser(Auth::user())->check('create', $model)) { | ||
return; | ||
} | ||
|
||
Report::makeReport(ReportStep::makeForCreate($model::class, $model->attributesToArray())); | ||
|
||
return false; | ||
}); | ||
|
||
static::deleting(function (BaseModel $model) { | ||
|
||
if (Gate::forUser(Auth::user())->check('delete', $model)) { | ||
return; | ||
} | ||
|
||
Report::makeReport(ReportStep::makeForDelete($model)); | ||
|
||
return false; | ||
}); | ||
|
||
static::updating(function (BaseModel $model) { | ||
|
||
if (Gate::forUser(Auth::user())->check('update', $model)) { | ||
return; | ||
} | ||
|
||
Report::makeReport(ReportStep::makeForUpdate($model, $model->attributesToArray())); | ||
|
||
return false; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Constants\Config; | ||
|
||
/** | ||
* Class ReportConstants. | ||
*/ | ||
class ReportConstants | ||
{ | ||
final public const MAX_REPORTS_QUALIFIED = 'report.user_max_reports'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Enums\Models\Admin; | ||
|
||
use App\Concerns\Enums\LocalizesName; | ||
|
||
/** | ||
* Enum ApprovableStatus. | ||
*/ | ||
enum ApprovableStatus: int | ||
{ | ||
use LocalizesName; | ||
|
||
case PENDING = 0; | ||
case REJECTED = 1; | ||
case PARTIALLY_APPROVED = 2; | ||
case APPROVED = 3; | ||
|
||
/** | ||
* Get the filament color for the enum. | ||
* | ||
* @return string | ||
*/ | ||
public function color(): string | ||
{ | ||
return match ($this) { | ||
ApprovableStatus::PENDING => 'warning', | ||
ApprovableStatus::REJECTED => 'danger', | ||
ApprovableStatus::PARTIALLY_APPROVED => 'info', | ||
ApprovableStatus::APPROVED => 'success', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Enums\Models\Admin; | ||
|
||
use App\Concerns\Enums\LocalizesName; | ||
|
||
/** | ||
* Enum ReportActionType. | ||
*/ | ||
enum ReportActionType: int | ||
{ | ||
use LocalizesName; | ||
|
||
case CREATE = 0; | ||
case UPDATE = 1; | ||
case DELETE = 2; | ||
case ATTACH = 3; | ||
case DETACH = 4; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Features; | ||
|
||
use App\Enums\Auth\SpecialPermission; | ||
use App\Models\Auth\User; | ||
use Laravel\Pennant\Feature; | ||
|
||
/** | ||
* Class AllowReport. | ||
*/ | ||
class AllowReport | ||
{ | ||
/** | ||
* Resolve feature for scope. | ||
* | ||
* @param User|null $user | ||
* @return bool | ||
*/ | ||
public function resolve(?User $user): bool | ||
{ | ||
if (! empty($user?->can(SpecialPermission::BYPASS_FEATURE_FLAGS->value))) { | ||
return true; | ||
} | ||
|
||
return Feature::for(null)->value(static::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.