498 lines
20 KiB
PHP
498 lines
20 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\v1;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Resources\CustomerResource;
|
|
use App\Http\Resources\StatusResource;
|
|
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\Facades\Validator;
|
|
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 Request $request
|
|
* @return CustomerResource|StatusResource
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'email' => 'required|email',
|
|
'password' => 'required',
|
|
'firstname' => 'required',
|
|
'lastname' => 'required',
|
|
'opt_in' => 'required',
|
|
'birth_date' => 'date_format:Y-m-d'
|
|
]);
|
|
|
|
if ($validator->fails())
|
|
return new StatusResource(['status' => 'KO', 'code' => 102, 'message' => 'Error on required fields.', 'errors' => $validator->errors()]);
|
|
|
|
$brandId = $request->brand_id;
|
|
$email = filter_var($request->email, FILTER_SANITIZE_EMAIL);
|
|
$firstname = filter_var($request->firstname, FILTER_SANITIZE_STRING);
|
|
$lastname = filter_var($request->lastname, FILTER_SANITIZE_STRING);
|
|
$optIn = filter_var($request->opt_in, FILTER_SANITIZE_STRING);
|
|
$hashcode = Str::uuid();
|
|
|
|
// 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)
|
|
return new StatusResource(['status' => 'KO', 'code' => 103, 'message' => 'User already registered on this site.']);
|
|
}
|
|
|
|
$customerInfos = new CustomerInfos();
|
|
$customerInfos->firstname = $firstname;
|
|
$customerInfos->lastname = $lastname;
|
|
$customerInfos->birth_date = !empty($request->birth_date) ? filter_var($request->birth_date, FILTER_SANITIZE_STRING) : null;
|
|
$customerInfos->address = !empty($request->address) ? filter_var($request->address, FILTER_SANITIZE_STRING) : null;
|
|
$customerInfos->address2 = !empty($request->address2) ? filter_var($request->address2, FILTER_SANITIZE_STRING) : null;
|
|
$customerInfos->phone = !empty($request->phone) ? filter_var($request->phone, FILTER_SANITIZE_STRING) : null;
|
|
$customerInfos->country = !empty($request->country) ? filter_var($request->country, FILTER_SANITIZE_STRING) : null;
|
|
$customerInfos->city = !empty($request->city) ? filter_var($request->city, FILTER_SANITIZE_STRING) : null;
|
|
$customerInfos->postcode = !empty($request->postcode) ? filter_var($request->postcode, FILTER_SANITIZE_STRING) : null;
|
|
$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 = 0;
|
|
// Par défaut à 0 pour activation par mail
|
|
//$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 CustomerResource | StatusResource
|
|
*/
|
|
public function show(Request $request, $type, $value)
|
|
{
|
|
$brandId = $request->brand_id;
|
|
|
|
if ($type === 'id'){
|
|
$customer = Customer::find($value);
|
|
if (!$customer)
|
|
return new StatusResource(['status' => 'KO', 'code' => 101, 'message' => 'User not found.']);
|
|
}else if($type === 'email'){
|
|
$customer = Customer::where('email', $value)->first();
|
|
if (!$customer)
|
|
return new StatusResource(['status' => 'KO', 'code' => 101, 'message' => 'User not found.']);
|
|
|
|
$customerInfos = $customer->infos()->where('brand_id', $brandId)->first();
|
|
|
|
if (!$customerInfos)
|
|
return new StatusResource(['status' => 'KO', 'code' => 101, 'message' => 'User not found.']);
|
|
}else{
|
|
return new StatusResource(['status' => 'KO', 'code' => 101, 'message' => 'User not found.']);
|
|
}
|
|
|
|
return new CustomerResource($customer, $brandId);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return CustomerResource|StatusResource
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'birth_date' => 'date_format:Y-m-d'
|
|
]);
|
|
|
|
if ($validator->fails())
|
|
return new StatusResource(['status' => 'KO', 'code' => 104, 'message' => 'Error on required fields.', 'errors' => $validator->errors()]);
|
|
|
|
$brandId = $request->brand_id;
|
|
$customer = Customer::find($id);
|
|
|
|
if (!$customer)
|
|
return new StatusResource(['status' => 'KO', 'code' => 105, 'message' => 'User not found.']);
|
|
|
|
$customerInfos = $customer->infos()->where('brand_id', $brandId)->first();
|
|
if (!$customerInfos)
|
|
return new StatusResource(['status' => 'KO', 'code' => 105, 'message' => 'User not found.']);
|
|
|
|
if (isset($request->firstname))
|
|
$customerInfos->firstname = filter_var($request->firstname, FILTER_SANITIZE_STRING);
|
|
|
|
if (isset($request->lastname))
|
|
$customerInfos->lastname = filter_var($request->lastname, FILTER_SANITIZE_STRING);
|
|
|
|
if (isset($request->birth_date)) {
|
|
if (!empty($request->birth_date)){
|
|
$customerInfos->birth_date = filter_var($request->birth_date, FILTER_SANITIZE_STRING);
|
|
}else{
|
|
$customerInfos->birth_date = null;
|
|
}
|
|
}
|
|
|
|
if (isset($request->address)) {
|
|
if (!empty($request->address)){
|
|
$customerInfos->address = filter_var($request->address, FILTER_SANITIZE_STRING);
|
|
}else{
|
|
$customerInfos->address = null;
|
|
}
|
|
}
|
|
|
|
if (isset($request->address2)) {
|
|
if (!empty($request->address2)){
|
|
$customerInfos->address2 = filter_var($request->address2, FILTER_SANITIZE_STRING);
|
|
}else{
|
|
$customerInfos->address2 = null;
|
|
}
|
|
}
|
|
|
|
if (isset($request->phone)) {
|
|
if (!empty($request->phone)){
|
|
$customerInfos->phone = filter_var($request->phone, FILTER_SANITIZE_STRING);
|
|
}else{
|
|
$customerInfos->phone = null;
|
|
}
|
|
}
|
|
|
|
if (isset($request->country)) {
|
|
if (!empty($request->country)){
|
|
$customerInfos->country = filter_var($request->country, FILTER_SANITIZE_STRING);
|
|
}else{
|
|
$customerInfos->country = null;
|
|
}
|
|
}
|
|
|
|
if (isset($request->city)) {
|
|
if (!empty($request->city)){
|
|
$customerInfos->city = filter_var($request->city, FILTER_SANITIZE_STRING);
|
|
}else{
|
|
$customerInfos->city = null;
|
|
}
|
|
}
|
|
|
|
if (isset($request->postcode)) {
|
|
if (!empty($request->postcode)){
|
|
$customerInfos->postcode = filter_var($request->postcode, FILTER_SANITIZE_STRING);
|
|
}else{
|
|
$customerInfos->postcode = null;
|
|
}
|
|
}
|
|
|
|
if (isset($request->opt_in)) {
|
|
$optIn = filter_var($request->opt_in, FILTER_SANITIZE_STRING);
|
|
|
|
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->save();
|
|
|
|
return new CustomerResource($customer, $brandId);
|
|
}
|
|
|
|
/**
|
|
* Authenticate the user
|
|
* @param Request $request
|
|
* @return CustomerResource|StatusResource
|
|
*/
|
|
public function authenticate(Request $request) {
|
|
$validator = Validator::make($request->all(), [
|
|
'email' => 'required|email',
|
|
'password' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails())
|
|
return new StatusResource(['status' => 'KO', 'code' => 115, 'message' => 'Error on required fields.', 'errors' => $validator->errors()]);
|
|
|
|
$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 (Hash::check($password, $login->password)) {
|
|
if ($login->status === 0) {
|
|
$infos = $customer->infos()->where('brand_id', $brandId)->first();
|
|
// "Le compte de l'utilisateur n'est pas activé"
|
|
return new StatusResource(
|
|
[
|
|
'status' => 'KO',
|
|
'code' => 116,
|
|
'message' => 'Account not activated.',
|
|
'user' => [
|
|
'crm_id' => $customer->id,
|
|
'firstname' => $infos->firstname,
|
|
'lastname' => $infos->lastname,
|
|
'birth_date' => $infos->birth_date,
|
|
'address' => $infos->address,
|
|
'address2' => $infos->address2,
|
|
'phone' => $infos->phone,
|
|
'country' => $infos->country,
|
|
'city' => $infos->city,
|
|
'postcode' => $infos->postcode,
|
|
'opt_in' => $infos->opt_in,
|
|
'hashcode' => $infos->hashcode,
|
|
'status' => $login->status,
|
|
]
|
|
]);
|
|
}
|
|
|
|
$login->last_connection = now();
|
|
$customer->logins()->save($login);
|
|
return new CustomerResource($customer, $brandId);
|
|
}
|
|
// "Mot de passe incorrect"
|
|
return new StatusResource(['status' => 'KO', 'code' => 117, 'message' => 'Authentication failed.']);
|
|
} else {
|
|
// "Ce compte n'est pas enregistré pour cette marque"
|
|
return new StatusResource(['status' => 'KO', 'code' => 118, 'message' => 'User not found.']);
|
|
}
|
|
} else {
|
|
// "Ce compte n'existe pas dans la CRM"
|
|
return new StatusResource(['status' => 'KO', 'code' => 119, 'message' => 'User not found.']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Change the password of the user for the brand
|
|
* @param Request $request
|
|
* @param $id
|
|
*/
|
|
public function changePassword(Request $request, $id) {
|
|
$validator = Validator::make($request->all(), [
|
|
'password' => 'required',
|
|
]);
|
|
|
|
if ($validator->fails())
|
|
return new StatusResource(['status' => 'KO', 'code' => 107, 'message' => 'Error on required fields.', 'errors' => $validator->errors()]);
|
|
|
|
$brandId = $request->brand_id;
|
|
$customer = Customer::find($id);
|
|
$password = trim($request->password);
|
|
|
|
if ($customer) {
|
|
$customerLogin = $customer->logins()->where('brand_id', $brandId)->first();
|
|
if ($customerLogin) {
|
|
// Check if same password
|
|
if (Hash::check($password, $customerLogin->password)){
|
|
return new StatusResource(['status' => 'KO', 'code' => 123, 'message' => 'Same password.']);
|
|
}
|
|
|
|
$customerLogin->password = Hash::make($password);
|
|
$customerLogin->save();
|
|
} else {
|
|
// "Ce compte n'est pas enregistré pour cette marque"
|
|
return new StatusResource(['status' => 'KO', 'code' => 108, 'message' => 'User not found.']);
|
|
}
|
|
} else {
|
|
// "Ce compte n'existe pas"
|
|
return new StatusResource(['status' => 'KO', 'code' => 109, 'message' => 'User not found.']);
|
|
}
|
|
return new StatusResource(['status' => 'OK', 'message' => 'Update complete.']);
|
|
}
|
|
|
|
/**
|
|
* Activate the account of the user
|
|
* @param Request $request
|
|
* @param $id
|
|
*/
|
|
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();
|
|
// "Le compte a bien été activé"
|
|
return new StatusResource(['status' => 'OK', 'message' => 'Update complete.']);
|
|
} else {
|
|
// "Ce compte est déjà activé"
|
|
return new StatusResource(['status' => 'KO', 'code' => 110, 'message' => 'User already active.']);
|
|
}
|
|
} else {
|
|
// "Ce compte n'est pas enregistré pour cette marque"
|
|
return new StatusResource(['status' => 'KO', 'code' => 111, 'message' => 'User not found.']);
|
|
}
|
|
} else {
|
|
// "Cet utilisateur n'existe pas dans la CRM"
|
|
return new StatusResource(['status' => 'KO', 'code' => 112, 'message' => 'User not found.']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete the user account for the brand
|
|
* @param Request $request
|
|
* @param $id
|
|
*/
|
|
public function delete(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
|
|
// TODO : 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();
|
|
// Suppression du compte de la CRM terminée
|
|
return new StatusResource(['status' => 'OK', 'message' => 'Deletion complete.']);
|
|
} else {
|
|
// Suppression des informations du compte pour la marque terminée
|
|
return new StatusResource(['status' => 'OK', 'message' => 'Deletion complete.']);
|
|
}
|
|
} else {
|
|
return new StatusResource(['status' => 'KO', 'code' => 106, 'message' => 'User not found.']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Change the status of the opt-in for the customer on this brand
|
|
* @param Request $request
|
|
* @param $id
|
|
*/
|
|
public function toggleOptin(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();
|
|
// "Changement effectué"
|
|
return new StatusResource(['status' => 'OK', 'message' => 'Update complete.']);
|
|
} else {
|
|
// "Ce compte n'a pas d'information liés à cette marque"
|
|
return new StatusResource(['status' => 'KO', 'code' => 113, 'message' => 'User not found.']);
|
|
}
|
|
} else {
|
|
// "Cet utilisateur n'existe pas dans la CRM"
|
|
return new StatusResource(['status' => 'KO', 'code' => 114, 'message' => 'User not found.']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove opt-in for the customer on this brand
|
|
* @param Request $request
|
|
* @param $hashcode
|
|
*/
|
|
public function removeOptin(Request $request, $hashcode) {
|
|
// TODO : Liaison des opt-in avec MailJet
|
|
$brandId = $request->brand_id;
|
|
|
|
$infos = CustomerInfos::where([
|
|
['brand_id', $brandId],
|
|
['hashcode', $hashcode]
|
|
])->first();
|
|
|
|
if ($infos) {
|
|
$infos->opt_in = 0;
|
|
|
|
// On enregistre l'historique du changement d'optin
|
|
$optinHistory = new CustomerOptInHistory();
|
|
$optinHistory->customer_id = $infos->customer_id;
|
|
$optinHistory->new_value = 0;
|
|
$optinHistory->brand_id = $brandId;
|
|
$optinHistory->save();
|
|
|
|
$infos->save();
|
|
// "Changement effectué"
|
|
return new StatusResource(['status' => 'OK', 'message' => 'Update complete.']);
|
|
} else {
|
|
// "Ce compte n'a pas d'information liés à cette marque"
|
|
return new StatusResource(['status' => 'KO', 'code' => 122, 'message' => 'User not found.']);
|
|
}
|
|
}
|
|
|
|
public function addSingleOptin(Request $request) {
|
|
$validator = Validator::make($request->all(), [
|
|
'email' => 'required|email',
|
|
]);
|
|
|
|
if ($validator->fails())
|
|
return new StatusResource(['status' => 'KO', 'code' => 120, 'message' => 'Error on required fields.', 'errors' => $validator->errors()]);
|
|
|
|
$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 {
|
|
// "Cet utilisateur est déjà inscrit à la newsletter pour cette marque"
|
|
return new StatusResource(['status' => 'KO', 'code' => 121, 'message' => 'This user is already subscribed to the newsletter for this brand.']);
|
|
}
|
|
return new StatusResource(['status' => 'OK' , 'message' => 'Optin added.']);
|
|
}
|
|
}
|