2025-07-24 17:30:46 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api\V4;
|
|
|
|
|
|
|
|
|
|
use App\Exceptions\Api\V4\CrmException;
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Http\Requests\Api\V4\SendClaimRequest;
|
|
|
|
|
use App\Models\Claim;
|
2026-03-31 14:59:48 +02:00
|
|
|
use App\Models\CustomerInfo;
|
2025-07-24 17:30:46 +02:00
|
|
|
use App\Models\Service;
|
2025-08-26 09:34:58 +02:00
|
|
|
use Illuminate\Support\Arr;
|
2025-08-28 11:59:23 +02:00
|
|
|
use Uwil\LaravelCommon\Helpers\FileHelper;
|
2025-07-24 17:30:46 +02:00
|
|
|
|
|
|
|
|
class ClaimController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Just returns HTTP 200 response.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Http\Requests\Api\V4\SendClaimRequest $request
|
|
|
|
|
* @param \App\Models\Service $service
|
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
|
* @throws \App\Exceptions\Api\V4\CrmException If the single optin could not be added or removed.
|
|
|
|
|
*/
|
|
|
|
|
public function store(SendClaimRequest $request, Service $service): \Illuminate\Http\JsonResponse
|
|
|
|
|
{
|
2025-08-26 09:34:58 +02:00
|
|
|
$claimModel = (new Claim())->fill(Arr::except($request->validated(), 'file'));
|
2025-08-28 11:59:23 +02:00
|
|
|
if ($request->filled('file')) {
|
2026-03-31 14:59:48 +02:00
|
|
|
$claimModel->file = FileHelper::storeBase64File(
|
|
|
|
|
tableName: 'claim',
|
|
|
|
|
fileBase64: $request->file,
|
|
|
|
|
private: true,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 12:22:56 +02:00
|
|
|
// Add brand id from service.
|
|
|
|
|
$claimModel->brand_id = $service->brand->getKey();
|
|
|
|
|
|
2026-03-31 14:59:48 +02:00
|
|
|
// Add customer relation if exist.
|
|
|
|
|
$crmCustomer = CustomerInfo::getFromEmail($claimModel->brand, $claimModel->email)->first();
|
|
|
|
|
if (!is_null($crmCustomer)) {
|
|
|
|
|
$claimModel->hashcode = $crmCustomer->hashcode;
|
2025-08-26 09:34:58 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-24 17:30:46 +02:00
|
|
|
if (!$claimModel->saveOrFail()) {
|
|
|
|
|
throw new CrmException('Api failed to store claim.');
|
|
|
|
|
}
|
|
|
|
|
$claimModel->brand()->associate($service->brand);
|
|
|
|
|
|
|
|
|
|
return response()->json();
|
|
|
|
|
}
|
|
|
|
|
}
|