115 lines
3.7 KiB
PHP
115 lines
3.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Api\v0;
|
||
|
|
|
||
|
|
use App\Models\Contact;
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Models\Site;
|
||
|
|
use App\Traits\CouponTrait;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
|
||
|
|
class ContactController extends Controller
|
||
|
|
{
|
||
|
|
use CouponTrait;
|
||
|
|
|
||
|
|
public function updateContact(Request $request)
|
||
|
|
{
|
||
|
|
// Validation des paramètres requis
|
||
|
|
$requiredFields = [
|
||
|
|
'refadherent' => 'string',
|
||
|
|
'nom' => 'string',
|
||
|
|
'refsite' => 'string',
|
||
|
|
'prenom' => 'string',
|
||
|
|
'email' => 'email',
|
||
|
|
'code_postal' => 'string',
|
||
|
|
'commune' => 'string',
|
||
|
|
];
|
||
|
|
|
||
|
|
$validated = [];
|
||
|
|
foreach ($requiredFields as $field => $type) {
|
||
|
|
$value = $request->input($field);
|
||
|
|
if (empty($value)) {
|
||
|
|
return response()->json([
|
||
|
|
'Statut' => 'KO',
|
||
|
|
'Code' => 101,
|
||
|
|
'Message' => 'Des paramètres de la requête sont mal renseignés ou non présents'
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
$validated[$field] = $value;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Validation spécifique pour l'email
|
||
|
|
if (!filter_var($validated['email'], FILTER_VALIDATE_EMAIL)) {
|
||
|
|
return response()->json([
|
||
|
|
'Statut' => 'KO',
|
||
|
|
'Code' => 101,
|
||
|
|
'Message' => 'L\'email fourni n\'est pas valide'
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Validation du pays (entier ou par défaut)
|
||
|
|
$pays = $request->input('pays');
|
||
|
|
if ($pays !== null && $pays !== '') {
|
||
|
|
if (!filter_var($pays, FILTER_VALIDATE_INT)) {
|
||
|
|
return response()->json([
|
||
|
|
'Statut' => 'KO',
|
||
|
|
'Code' => 101,
|
||
|
|
'Message' => 'Le pays doit être un entier'
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
$validated['pays'] = $pays;
|
||
|
|
} else {
|
||
|
|
$validated['pays'] = 250;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Recherche du contact existant
|
||
|
|
$contact = Contact::where([
|
||
|
|
'contact_siteId' => $validated['refsite'],
|
||
|
|
'contact_ref' => $validated['refadherent']
|
||
|
|
])->first();
|
||
|
|
|
||
|
|
// Si le contact n'existe pas, on vérifie le site
|
||
|
|
if (!$contact) {
|
||
|
|
$site = Site::where('site_id', $validated['refsite'])->first();
|
||
|
|
if (!$site) {
|
||
|
|
return response()->json([
|
||
|
|
'Statut' => 'KO',
|
||
|
|
'Code' => 102,
|
||
|
|
'Message' => 'Le site spécifié n\'existe pas'
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
$contact = new Contact();
|
||
|
|
$contact->contact_ref = $validated['refadherent'];
|
||
|
|
$contact->contact_siteId = $validated['refsite'];
|
||
|
|
$contact->contact_hash = $request->input('hashcode') ?: strtoupper(md5(uniqid(time(), true)));
|
||
|
|
}
|
||
|
|
|
||
|
|
// Mise à jour des champs
|
||
|
|
$contact->contact_prenom = $validated['prenom'];
|
||
|
|
$contact->contact_nom = $validated['nom'];
|
||
|
|
$contact->contact_email = $validated['email'];
|
||
|
|
$contact->contact_cp = $validated['code_postal'];
|
||
|
|
$contact->contact_ville = $validated['commune'];
|
||
|
|
$contact->contact_country = $validated['pays'];
|
||
|
|
$contact->contact_updatedate = now();
|
||
|
|
|
||
|
|
if ($request->filled('hashcode')) {
|
||
|
|
$contact->contact_hash = $request->hashcode;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Sauvegarde et réponse
|
||
|
|
if ($contact->save()) {
|
||
|
|
return response()->json([
|
||
|
|
'Statut' => 'OK',
|
||
|
|
'contact' => $contact
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'Statut' => 'KO',
|
||
|
|
'Code' => 109,
|
||
|
|
'Message' => 'Une erreur est survenue'
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|