|
|
@@ -2,11 +2,14 @@
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
+use App\Enums\PartnerAgreementStatusEnum;
|
|
|
+use App\Enums\UserStatusEnum;
|
|
|
use App\Enums\UserTypeEnum;
|
|
|
use App\Models\PartnerAgreement;
|
|
|
use App\Models\User;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
|
|
class PartnerAgreementService
|
|
|
@@ -74,7 +77,11 @@ class PartnerAgreementService
|
|
|
->where('user_id', $userId)
|
|
|
->first();
|
|
|
|
|
|
- if(!$partner) {
|
|
|
+ if (!$partner) {
|
|
|
+ if (PartnerAgreement::onlyTrashed()->where('user_id', $userId)->exists()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
$partner = new PartnerAgreement();
|
|
|
$partner->company_name = 'Novo Parceiro';
|
|
|
$partner->user_id = $userId;
|
|
|
@@ -135,6 +142,34 @@ class PartnerAgreementService
|
|
|
return $model->fresh(['category', 'city', 'state', 'logo', 'media']);
|
|
|
}
|
|
|
|
|
|
+ public function getTrashed(array $filters = []): Collection
|
|
|
+ {
|
|
|
+ $query = PartnerAgreement::onlyTrashed()->orderByDesc('deleted_at');
|
|
|
+
|
|
|
+ if (!empty($filters['type'])) {
|
|
|
+ $query->where('type', $filters['type']);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $query->get(['id', 'company_name', 'type', 'deleted_at']);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function restore(int $id): ?PartnerAgreement
|
|
|
+ {
|
|
|
+ $model = PartnerAgreement::onlyTrashed()->find($id);
|
|
|
+
|
|
|
+ if (!$model) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return DB::transaction(function () use ($model): PartnerAgreement {
|
|
|
+ $model->restore();
|
|
|
+ $model->update(['status' => PartnerAgreementStatusEnum::ACTIVE]);
|
|
|
+ $model->user?->update(['status' => UserStatusEnum::ACTIVE]);
|
|
|
+
|
|
|
+ return $model->fresh();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
public function delete(int $id): bool
|
|
|
{
|
|
|
$model = PartnerAgreement::find($id);
|
|
|
@@ -143,6 +178,14 @@ class PartnerAgreementService
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- return $model->delete();
|
|
|
+ return DB::transaction(function () use ($model): bool {
|
|
|
+ $model->services()->delete();
|
|
|
+
|
|
|
+ $model->user?->update(['status' => UserStatusEnum::INACTIVE]);
|
|
|
+
|
|
|
+ $model->update(['status' => PartnerAgreementStatusEnum::INACTIVE]);
|
|
|
+
|
|
|
+ return (bool) $model->delete();
|
|
|
+ });
|
|
|
}
|
|
|
}
|