diff --git a/src/Concerns/MayBeApproved.php b/src/Concerns/MayBeApproved.php new file mode 100644 index 0000000..ddcf757 --- /dev/null +++ b/src/Concerns/MayBeApproved.php @@ -0,0 +1,47 @@ +id "; + print_r($model->getDirty()); + return Approval::where([ + ['state', '=', ApprovalStatus::Pending], + ['new_data', '=', json_encode($model->getDirty())], + ['original_data', '=', json_encode($model->getOriginalMatchingChanges())], + ])->exists(); + return Approval::where('state', ApprovalStatus::Pending) + ->where('approvalable_id', $model->id) + ->where('approvalable_type', get_class($model)) + ->whereJsonContains('new_data', $model->getDirty()) + ->exists(); + } +} diff --git a/src/Concerns/MustBeApproved.php b/src/Concerns/MustBeApproved.php index 4a1db5f..5908432 100644 --- a/src/Concerns/MustBeApproved.php +++ b/src/Concerns/MustBeApproved.php @@ -72,7 +72,7 @@ protected function getOriginalMatchingChanges(): array } /** - * Check is the approval can be bypassed. + * Check if the approval can be bypassed. */ public function isApprovalBypassed(): bool { @@ -101,4 +101,5 @@ public function withoutApproval(): static return $this; } + } diff --git a/src/Models/Approval.php b/src/Models/Approval.php index 4fddfea..b656863 100644 --- a/src/Models/Approval.php +++ b/src/Models/Approval.php @@ -10,21 +10,56 @@ class Approval extends Model { - protected $guarded = []; + protected $guarded = []; - protected $casts = [ - 'new_data' => AsArrayObject::class, - 'original_data' => AsArrayObject::class, - 'state' => ApprovalStatus::class, - ]; + protected $casts = [ + 'new_data' => AsArrayObject::class, + 'original_data' => AsArrayObject::class, + 'state' => ApprovalStatus::class, + ]; - public static function booted() - { - static::addGlobalScope(new ApprovalStateScope()); - } + public static function booted() + { + static::addGlobalScope(new ApprovalStateScope()); + } + + public function approvalable(): MorphTo + { + return $this->morphTo(); + } - public function approvalable(): MorphTo - { - return $this->morphTo(); + /** + * Mark the approval as Rejected. + */ + public function reject() + { + $this->update(['state' => ApprovalStatus::Rejected]); + } + + /** + * Commit the change to the DB and mark the approval as Approved. + */ + public function commit() + { + if ($this->state != ApprovalStatus::Pending) return false; + if ($this->approvalable_id) { + $model = $this->approvalable_type::find($this->approvalable_id)->withoutApproval()->update($this->new_data->toArray()); + $this->update(['state' => ApprovalStatus::Approved]); + return $model; + } else { + $model = new $this->approvalable_type; + $model->fill($this->new_data->toArray()); + $model->save(); + $this->update(['state' => ApprovalStatus::Approved]); + return $model; } + } + + /** + * Purge database of completed approvals older than $days. + */ + public static function purge(int $days = -1) + { + self::where('created_at', '<', now()->subDays($days))->where('state', '!=', ApprovalStatus::Pending)->forceDelete(); + } }