341 lines
13 KiB
PHP
341 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\v1;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Resources\CustomerResource;
|
|
use App\Models\Customer;
|
|
use App\Models\CustomerInfos;
|
|
use App\Models\CustomerLogin;
|
|
use App\Models\CustomerOptInHistory;
|
|
use App\Models\SingleOptin;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Str;
|
|
|
|
class CustomerController extends Controller
|
|
{
|
|
// TODO : Voir pour les contraintes de mot de passe
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$email = filter_var($request->email, FILTER_SANITIZE_EMAIL);
|
|
$brandId = $request->brand_id;
|
|
|
|
$firstname = filter_var($request->firstname, FILTER_SANITIZE_STRING);
|
|
$lastname = filter_var($request->lastname, FILTER_SANITIZE_STRING);
|
|
$birthDate = filter_var($request->birth_date, FILTER_SANITIZE_STRING);
|
|
$address = filter_var($request->address, FILTER_SANITIZE_STRING);
|
|
$address2 = filter_var($request->address2, FILTER_SANITIZE_STRING);
|
|
$phone = filter_var($request->phone, FILTER_SANITIZE_STRING);
|
|
$country = filter_var($request->country, FILTER_SANITIZE_STRING);
|
|
$city = filter_var($request->city, FILTER_SANITIZE_STRING);
|
|
$postcode = filter_var($request->postcode, FILTER_SANITIZE_STRING);
|
|
$optIn = filter_var($request->opt_in, FILTER_SANITIZE_STRING);
|
|
$hashcode = Str::uuid();
|
|
|
|
if ($firstname === null)
|
|
die("Le prénom est obligatoire pour poursuivre l'inscription");
|
|
|
|
if ($lastname === null)
|
|
die("Le nom de famille est obligatoire pour poursuivre l'inscription");
|
|
|
|
if (!empty($birthDate) && !$this->isSQLDate($birthDate))
|
|
die("La date n'est pas bonne");
|
|
|
|
// Récupère le customer s'il existe déjà
|
|
$customer = Customer::where('email', $email)->first();
|
|
|
|
// Si le customer n'existe pas déjà
|
|
if (!$customer) {
|
|
$customer = new Customer();
|
|
$customer->email = $email;
|
|
$customer->save();
|
|
$singleOptin = SingleOptin::where([['email', $email], ['brand_id', $brandId]])->first();
|
|
if ($singleOptin)
|
|
$singleOptin->delete();
|
|
} else {
|
|
// Récupère les infos s'ils existent déjà
|
|
$customerInfos = CustomerInfos::where([['customer_id', $customer->id], ['brand_id', $brandId]])->first();
|
|
if ($customerInfos)
|
|
die("L'utilisateur est déjà inscrit sur ce site");
|
|
}
|
|
|
|
$customerInfos = new CustomerInfos();
|
|
$customerInfos->firstname = $firstname;
|
|
$customerInfos->lastname = $lastname;
|
|
$customerInfos->birth_date = !empty($birthDate) ? $birthDate : null;
|
|
$customerInfos->address = $address;
|
|
$customerInfos->address2 = $address2;
|
|
$customerInfos->phone = $phone;
|
|
$customerInfos->country = $country;
|
|
$customerInfos->city = $city;
|
|
$customerInfos->postcode = $postcode;
|
|
$customerInfos->opt_in = $optIn;
|
|
$customerInfos->hashcode = $hashcode;
|
|
$customerInfos->brand_id = $brandId;
|
|
$customerInfos->customer_id = $customer->id;
|
|
|
|
$customer->infos()->save($customerInfos);
|
|
|
|
// Création du login pour cet utilisateur sur ce service
|
|
$customerLogin = new CustomerLogin();
|
|
$customerLogin->customer_id = $customer->id;
|
|
$customerLogin->password = Hash::make(trim($request->password));
|
|
$customerLogin->last_connection = now();
|
|
$customerLogin->brand_id = $brandId;
|
|
$customerLogin->status = $request->status !== null ? 1 : 0;
|
|
|
|
$customer->logins()->save($customerLogin);
|
|
|
|
// Ajout dans la table d'historique des opt-in de la nouvelle valeur
|
|
$optInHistory = new CustomerOptInHistory();
|
|
$optInHistory->customer_id = $customer->id;
|
|
$optInHistory->brand_id = $brandId;
|
|
$optInHistory->new_value = $optIn;
|
|
$optInHistory->save();
|
|
|
|
return new CustomerResource($customer, $brandId);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show(Request $request, $id)
|
|
{
|
|
$brandId = $request->brand_id;
|
|
return new CustomerResource(Customer::find($id), $brandId);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
$brandId = $request->brand_id;
|
|
$customer = Customer::find($id);
|
|
|
|
if (!$customer)
|
|
die("Cet utilisateur n'existe pas");
|
|
|
|
$firstname = filter_var($request->firstname, FILTER_SANITIZE_STRING);
|
|
$lastname = filter_var($request->lastname, FILTER_SANITIZE_STRING);
|
|
$birthDate = filter_var($request->birth_date, FILTER_SANITIZE_STRING);
|
|
$address = filter_var($request->address, FILTER_SANITIZE_STRING);
|
|
$address2 = filter_var($request->address2, FILTER_SANITIZE_STRING);
|
|
$phone = filter_var($request->phone, FILTER_SANITIZE_STRING);
|
|
$country = filter_var($request->country, FILTER_SANITIZE_STRING);
|
|
$city = filter_var($request->city, FILTER_SANITIZE_STRING);
|
|
$postcode = filter_var($request->postcode, FILTER_SANITIZE_STRING);
|
|
$optIn = filter_var($request->opt_in, FILTER_SANITIZE_STRING);
|
|
|
|
if ($firstname === null)
|
|
die("Le prénom est obligatoire pour poursuivre l'inscription");
|
|
|
|
if ($lastname === null)
|
|
die("Le nom de famille est obligatoire pour poursuivre l'inscription");
|
|
|
|
if (!empty($birthDate) && !$this->isSQLDate($birthDate))
|
|
die("La date n'est pas bonne");
|
|
|
|
$customerInfos = $customer->infos()->where('brand_id', $brandId)->first();
|
|
|
|
$customerInfos->firstname = $firstname;
|
|
$customerInfos->lastname = $lastname;
|
|
$customerInfos->birth_date = !empty($birthDate) ? $birthDate : null;
|
|
$customerInfos->address = $address;
|
|
$customerInfos->address2 = $address2;
|
|
$customerInfos->phone = $phone;
|
|
$customerInfos->country = $country;
|
|
$customerInfos->city = $city;
|
|
$customerInfos->postcode = $postcode;
|
|
|
|
if ($customerInfos->opt_in != $optIn) {
|
|
// Ajout dans la table d'historique des opt-in de la nouvelle valeur
|
|
$optInHistory = new CustomerOptInHistory();
|
|
$optInHistory->customer_id = $customer->id;
|
|
$optInHistory->brand_id = $brandId;
|
|
$optInHistory->new_value = $optIn;
|
|
$optInHistory->save();
|
|
}
|
|
|
|
$customerInfos->opt_in = $optIn;
|
|
$customerInfos->brand_id = 1;
|
|
|
|
$customerInfos->save();
|
|
|
|
return new CustomerResource($customer, $brandId);
|
|
}
|
|
|
|
/**
|
|
* Authenticate the user
|
|
*/
|
|
public function authenticate(Request $request) {
|
|
$brandId = $request->brand_id;
|
|
$email = filter_var($request->email, FILTER_SANITIZE_EMAIL);
|
|
$password = trim($request->password);
|
|
$customer = Customer::where('email', $email)->first();
|
|
if ($customer) {
|
|
$login = $customer->logins()->where('brand_id', $brandId)->first();
|
|
if ($login) {
|
|
if ($login->status === 0) {
|
|
die("Le compte de l'utilisateur n'est pas activé");
|
|
} else {
|
|
if (Hash::check($password, $login->password)) {
|
|
$login->last_connection = now();
|
|
$customer->logins()->save($login);
|
|
return new CustomerResource($customer, $brandId);
|
|
}
|
|
die("Mot de passe incorrect");
|
|
}
|
|
} else {
|
|
die("Ce compte n'est pas enregistré pour cette marque");
|
|
}
|
|
} else {
|
|
die ("Ce compte n'existe pas dans la CRM");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Change the password of the user for the brand
|
|
*/
|
|
public function changePassword(Request $request) {
|
|
$brandId = $request->brand_id;
|
|
|
|
$crmId = filter_var($request->crm_id, FILTER_SANITIZE_NUMBER_INT);
|
|
$customer = Customer::find($crmId);
|
|
$password = trim($request->password);
|
|
|
|
if ($customer) {
|
|
$customerLogin = $customer->logins()->where('brand_id', $brandId)->first();
|
|
if ($customerLogin) {
|
|
$customerLogin->password = Hash::make($password);
|
|
$customerLogin->save();
|
|
} else {
|
|
die("Ce compte n'est pas enregistré pour cette marque");
|
|
}
|
|
} else {
|
|
die("Ce compte n'existe pas");
|
|
}
|
|
|
|
die("Le mot de passe a bien été changé");
|
|
}
|
|
|
|
/**
|
|
* Activate the account of the user
|
|
*/
|
|
public function activateAccount(Request $request, $id) {
|
|
$brandId = $request->brand_id;
|
|
|
|
$customer = Customer::find($id);
|
|
if ($customer) {
|
|
$login = $customer->logins()->where('brand_id', $brandId)->first();
|
|
if ($login) {
|
|
if ($login->status === 0) {
|
|
$login->status = 1;
|
|
$login->save();
|
|
die("Le compte a bien été activé");
|
|
} else {
|
|
die("Ce compte est déjà activé");
|
|
}
|
|
} else {
|
|
die("Ce compte n'est pas enregistré pour cette marque");
|
|
}
|
|
} else {
|
|
die("Cet utilisateur n'existe pas dans la CRM");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete the user account for the brand
|
|
*/
|
|
public function deleteUser(Request $request, $id) {
|
|
die("Not implemented yet");
|
|
// TODO : A voir pour les stats, obuton dans le bo qui permet de faire les stats sur les customer puis supprimer
|
|
// Gestion avec mesBR
|
|
$brandId = $request->brand_id;
|
|
|
|
$customer = Customer::find($id);
|
|
if ($customer) {
|
|
$customer->infos()->where('brand_id', $brandId)->first()->delete();
|
|
$customer->logins()->where('brand_id', $brandId)->first()->delete();
|
|
|
|
if (count($customer->infos) === 0 && count($customer->logins) === 0) {
|
|
$customer->optInHistory()->delete();
|
|
$customer->delete();
|
|
die("Suppression du compte de la CRM terminée");
|
|
} else {
|
|
die("Suppression des informations du compte pour la marque terminée");
|
|
}
|
|
} else {
|
|
die("Cet utilisateur n'existe pas dans la CRM");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Change the status of the opt-in for the customer on this brand
|
|
*/
|
|
public function changeOptin(Request $request, $id) {
|
|
// TODO : Liaison des opt-in avec MailJet
|
|
$brandId = $request->brand_id;
|
|
|
|
$customer = Customer::find($id);
|
|
if ($customer) {
|
|
$infos = $customer->infos()->where('brand_id', $brandId)->first();
|
|
if ($infos) {
|
|
$infos->opt_in = $infos->opt_in === 0 ? 1 : 0;
|
|
|
|
// On enregistre l'historique du changement d'optin
|
|
$optinHistory = new CustomerOptInHistory();
|
|
$optinHistory->customer_id = $customer->id;
|
|
$optinHistory->new_value = $infos->opt_in;
|
|
$optinHistory->brand_id = $brandId;
|
|
$optinHistory->save();
|
|
|
|
$infos->save();
|
|
die("Changement effectué");
|
|
} else {
|
|
die("Ce compte n'a pas d'information liés à cette marque");
|
|
}
|
|
} else {
|
|
die("Cet utilisateur n'existe pas dans la CRM");
|
|
}
|
|
}
|
|
|
|
public function addSingleOptin(Request $request) {
|
|
$brandId = $request->brand_id;
|
|
$email = filter_var($request->email, FILTER_SANITIZE_EMAIL);
|
|
|
|
// Check si le customer ou l'optin simple existe avant de l'ajouter en optin simple
|
|
if (!SingleOptin::where([['email', $email], ['brand_id', $brandId]])->exists() &&
|
|
!CustomerInfos::join("customers", "customer_infos.customer_id", "=", "customers.id")->where([['brand_id', $brandId], ['email', $email]])->exists()) {
|
|
$singleOptin = new SingleOptin();
|
|
$singleOptin->email = $email;
|
|
$singleOptin->brand_id = $brandId;
|
|
$singleOptin->save();
|
|
} else {
|
|
die("Cet utilisateur est déjà inscrit à la newsletter pour cette marque");
|
|
}
|
|
|
|
die("Optin ajoutée");
|
|
}
|
|
|
|
private function isSQLDate($date){
|
|
if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $date))
|
|
return true;
|
|
return false;
|
|
}
|
|
}
|