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