|
|
@@ -2,10 +2,15 @@
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
+use App\Enums\ApprovalStatusEnum;
|
|
|
+use App\Exceptions\FavoriteProviderException;
|
|
|
use App\Http\Resources\ClientFavoriteProviderResource;
|
|
|
use App\Models\ClientFavoriteProvider;
|
|
|
+use App\Models\ClientProviderBlock;
|
|
|
use App\Models\Provider;
|
|
|
+use App\Models\ProviderClientBlock;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
class ClientFavoriteProviderService
|
|
|
{
|
|
|
@@ -79,6 +84,66 @@ class ClientFavoriteProviderService
|
|
|
|
|
|
//
|
|
|
|
|
|
+ public function createByCode(int $clientId, string $code): ClientFavoriteProviderResource
|
|
|
+ {
|
|
|
+ $code = Provider::normalizeShareCode($code);
|
|
|
+
|
|
|
+ $provider = Provider::query()
|
|
|
+ ->where('share_code', $code)
|
|
|
+ ->where('approval_status', ApprovalStatusEnum::ACCEPTED->value)
|
|
|
+ ->visibleToCustomers()
|
|
|
+ ->with(['user', 'profileMedia'])
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ if (! $provider) {
|
|
|
+ throw new FavoriteProviderException('favorite_code_not_found', 404);
|
|
|
+ }
|
|
|
+
|
|
|
+ $isBlocked = ProviderClientBlock::where('provider_id', $provider->id)
|
|
|
+ ->where('client_id', $clientId)
|
|
|
+ ->exists()
|
|
|
+ || ClientProviderBlock::where('client_id', $clientId)
|
|
|
+ ->where('provider_id', $provider->id)
|
|
|
+ ->exists();
|
|
|
+
|
|
|
+ if ($isBlocked) {
|
|
|
+ throw new FavoriteProviderException('favorite_code_blocked');
|
|
|
+ }
|
|
|
+
|
|
|
+ $favorite = DB::transaction(function () use ($clientId, $provider) {
|
|
|
+ $alreadyFavorite = ClientFavoriteProvider::where('client_id', $clientId)
|
|
|
+ ->where('provider_id', $provider->id)
|
|
|
+ ->exists();
|
|
|
+
|
|
|
+ if ($alreadyFavorite) {
|
|
|
+ throw new FavoriteProviderException('favorite_code_already_favorite');
|
|
|
+ }
|
|
|
+
|
|
|
+ $removed = ClientFavoriteProvider::onlyTrashed()
|
|
|
+ ->where('client_id', $clientId)
|
|
|
+ ->where('provider_id', $provider->id)
|
|
|
+ ->latest('id')
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ if ($removed) {
|
|
|
+ $removed->restore();
|
|
|
+
|
|
|
+ return $removed;
|
|
|
+ }
|
|
|
+
|
|
|
+ return ClientFavoriteProvider::create([
|
|
|
+ 'client_id' => $clientId,
|
|
|
+ 'provider_id' => $provider->id,
|
|
|
+ ]);
|
|
|
+ });
|
|
|
+
|
|
|
+ $favorite->setRelation('provider', $provider);
|
|
|
+
|
|
|
+ return new ClientFavoriteProviderResource($favorite);
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
public function getFavoritedProviderIds(int $clientId): array
|
|
|
{
|
|
|
return ClientFavoriteProvider::where('client_id', $clientId)
|