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\Requests\Api\V2\StoreCustomerAnswers;
|
|
|
|
|
use App\Models\Customer;
|
2025-09-08 14:01:14 +02:00
|
|
|
use App\Models\ModelAnswer;
|
2024-08-12 19:04:17 +02:00
|
|
|
use App\Models\Question;
|
2021-08-10 12:09:49 +02:00
|
|
|
|
|
|
|
|
class AnswerController extends Controller
|
|
|
|
|
{
|
2021-08-11 17:25:24 +02:00
|
|
|
/**
|
2023-09-06 17:24:53 +02:00
|
|
|
* Store the customer answers.
|
2021-08-11 17:25:24 +02:00
|
|
|
*
|
2023-06-23 17:18:59 +02:00
|
|
|
* @param \App\Http\Requests\Api\V2\StoreCustomerAnswers $request
|
|
|
|
|
* @param \App\Models\Customer $customer
|
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-08-11 17:25:24 +02:00
|
|
|
*/
|
2021-08-23 20:00:44 +02:00
|
|
|
public function storeCustomerAnswers(
|
|
|
|
|
StoreCustomerAnswers $request,
|
|
|
|
|
Customer $customer
|
2025-09-02 17:48:35 +02:00
|
|
|
): \Illuminate\Http\JsonResponse {
|
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
|
|
|
|
2024-08-12 19:04:17 +02:00
|
|
|
$question = Question::find($request->validated('question'));
|
|
|
|
|
|
2025-09-08 14:01:14 +02:00
|
|
|
// Column model_id is varchar because of singleoptin uuid, so force cast in string.
|
|
|
|
|
ModelAnswer::query()
|
|
|
|
|
->where('model_type', Customer::class)
|
|
|
|
|
->where('model_id', (string) $customer->getKey())
|
|
|
|
|
->whereIn('answer_id', $question->answers->modelKeys())
|
|
|
|
|
->delete();
|
|
|
|
|
|
|
|
|
|
foreach ($request->validated('answers') as $answerId) {
|
|
|
|
|
ModelAnswer::create([
|
|
|
|
|
'model_type' => Customer::class,
|
|
|
|
|
'model_id' => (string) $customer->getKey(),
|
|
|
|
|
'answer_id' => $answerId,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2024-08-12 19:04:17 +02:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
return response()->json();
|
2021-08-11 17:25:24 +02:00
|
|
|
}
|
2021-08-10 12:09:49 +02:00
|
|
|
}
|