2021-08-10 12:09:49 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api\V2;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2021-08-11 17:25:24 +02:00
|
|
|
use App\Http\Resources\Api\V2\QuestionsResource;
|
|
|
|
|
use App\Models\Answer;
|
|
|
|
|
use App\Models\Customer;
|
|
|
|
|
use App\Models\Question;
|
|
|
|
|
use App\Models\Service;
|
|
|
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
|
use Illuminate\Http\Resources\Json\ResourceCollection;
|
2021-08-10 12:09:49 +02:00
|
|
|
|
|
|
|
|
class QuestionController extends Controller
|
|
|
|
|
{
|
2021-08-11 17:25:24 +02:00
|
|
|
/**
|
2021-12-19 15:57:24 +01:00
|
|
|
* Returns all service questions.
|
2021-08-11 17:25:24 +02:00
|
|
|
*
|
2023-06-23 17:18:59 +02:00
|
|
|
* @param \App\Models\Service $service
|
|
|
|
|
* @return \Illuminate\Http\Resources\Json\ResourceCollection
|
2021-08-11 17:25:24 +02:00
|
|
|
*/
|
|
|
|
|
public function index(Service $service): ResourceCollection
|
|
|
|
|
{
|
|
|
|
|
return new QuestionsResource($service->brand->questions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-12-19 15:57:24 +01:00
|
|
|
* Returns all service questions with user answers for all of them.
|
2021-08-11 17:25:24 +02:00
|
|
|
*
|
2023-06-23 17:18:59 +02:00
|
|
|
* @param \App\Models\Service $service
|
|
|
|
|
* @param \App\Models\Customer $customer
|
|
|
|
|
* @return \Illuminate\Http\Resources\Json\ResourceCollection
|
2021-08-11 17:25:24 +02:00
|
|
|
*/
|
|
|
|
|
public function indexWithResponse(Service $service, Customer $customer): ResourceCollection
|
|
|
|
|
{
|
2021-12-19 15:57:24 +01:00
|
|
|
// * Make sure we are accessing a customer that belongs to our service.
|
2021-08-11 17:25:24 +02:00
|
|
|
$this->authorize('customerBelongsToService', $customer);
|
2021-12-19 15:57:24 +01:00
|
|
|
|
2021-08-18 08:26:40 +02:00
|
|
|
$customerAnswers = $customer->getAnswers($service);
|
2021-08-11 17:25:24 +02:00
|
|
|
return new QuestionsResource($service->brand->questions, $customerAnswers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-12-19 15:57:24 +01:00
|
|
|
* Returns all service questions that was not answered by a user.
|
2021-08-11 17:25:24 +02:00
|
|
|
*
|
2023-06-23 17:18:59 +02:00
|
|
|
* @param \App\Models\Service $service
|
|
|
|
|
* @param \App\Models\Customer $customer
|
|
|
|
|
* @return \Illuminate\Http\Resources\Json\JsonResource
|
2021-08-11 17:25:24 +02:00
|
|
|
*/
|
|
|
|
|
public function unAnsweredBy(Service $service, Customer $customer): JsonResource
|
|
|
|
|
{
|
2021-12-19 15:57:24 +01:00
|
|
|
// * Make sure we are accessing a customer that belongs to our service.
|
2021-08-11 17:25:24 +02:00
|
|
|
$this->authorize('customerBelongsToService', $customer);
|
2021-12-19 15:57:24 +01:00
|
|
|
|
2021-08-11 17:25:24 +02:00
|
|
|
$questions = Question::whereHas('brand', function ($q) use ($service) {
|
|
|
|
|
$q->where('id', $service->brand->id);
|
|
|
|
|
})->whereDoesntHave('answers', function ($q) use ($customer) {
|
|
|
|
|
$q->whereIn('id', Answer::whereHas('customers', function ($q) use ($customer) {
|
|
|
|
|
$q->where('id', $customer->id);
|
|
|
|
|
})->pluck('id'));
|
|
|
|
|
})->with('answers')->get();
|
|
|
|
|
return new QuestionsResource($questions);
|
|
|
|
|
}
|
2021-08-10 12:09:49 +02:00
|
|
|
}
|