2020-12-01 17:02:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
2021-07-28 14:18:47 +02:00
|
|
|
namespace App\Http\Controllers\Api\V1;
|
2020-12-01 17:02:10 +01:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
use App\Enums\Country;
|
2020-12-01 17:02:10 +01:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Http\Resources\CustomerResource;
|
2020-12-02 19:16:22 +01:00
|
|
|
use App\Http\Resources\StatusResource;
|
2020-12-01 17:02:10 +01:00
|
|
|
use App\Models\Customer;
|
2021-02-28 12:04:49 +01:00
|
|
|
use App\Models\CustomerInfo;
|
2020-12-01 17:02:10 +01:00
|
|
|
use App\Models\CustomerLogin;
|
|
|
|
|
use App\Models\CustomerOptInHistory;
|
2020-12-02 10:08:27 +01:00
|
|
|
use App\Models\SingleOptin;
|
2020-12-01 17:02:10 +01:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
2020-12-02 19:16:22 +01:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2020-12-01 17:02:10 +01:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
|
*/
|
2020-12-01 17:02:10 +01:00
|
|
|
class CustomerController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
2020-12-02 18:29:16 +01:00
|
|
|
* Store a newly created resource in storage
|
2023-06-23 17:18:59 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @return \App\Http\Resources\CustomerResource|\App\Http\Resources\StatusResource
|
2020-12-01 17:02:10 +01:00
|
|
|
*/
|
2021-12-19 15:57:24 +01:00
|
|
|
// phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh, Squiz.Commenting.FunctionComment.Missing
|
2020-12-01 17:02:10 +01:00
|
|
|
public function store(Request $request)
|
|
|
|
|
{
|
2020-12-02 19:16:22 +01:00
|
|
|
$validator = Validator::make($request->all(), [
|
2020-12-02 18:29:16 +01:00
|
|
|
'email' => 'required|email',
|
|
|
|
|
'password' => 'required',
|
|
|
|
|
'firstname' => 'required',
|
|
|
|
|
'lastname' => 'required',
|
|
|
|
|
'opt_in' => 'required',
|
|
|
|
|
'birth_date' => 'date_format:Y-m-d'
|
|
|
|
|
]);
|
2020-12-01 17:02:10 +01:00
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
if ($validator->fails()) {
|
|
|
|
|
return new StatusResource([
|
|
|
|
|
'status' => 'KO', 'code' => 102,
|
|
|
|
|
'message' => 'Error on required fields.', 'errors' => $validator->errors()
|
|
|
|
|
]);
|
|
|
|
|
}
|
2020-12-02 19:16:22 +01:00
|
|
|
|
2021-03-14 10:45:42 +01:00
|
|
|
$brandId = $request->brand_id;
|
2021-10-25 17:42:10 +02:00
|
|
|
$email = strtolower(filter_var($request->email, FILTER_SANITIZE_EMAIL));
|
2020-12-01 17:02:10 +01:00
|
|
|
$firstname = filter_var($request->firstname, FILTER_SANITIZE_STRING);
|
2021-03-14 10:45:42 +01:00
|
|
|
$lastname = filter_var($request->lastname, FILTER_SANITIZE_STRING);
|
|
|
|
|
$optIn = filter_var($request->opt_in, FILTER_SANITIZE_STRING);
|
|
|
|
|
$hashcode = Str::uuid();
|
2020-12-01 17:02:10 +01:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
// Récupère le customer s'il existe déjà.
|
2020-12-01 17:02:10 +01:00
|
|
|
$customer = Customer::where('email', $email)->first();
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
// Si le customer n'existe pas déjà.
|
2020-12-01 17:02:10 +01:00
|
|
|
if (!$customer) {
|
2021-07-22 15:14:07 +02:00
|
|
|
$customer = new Customer();
|
2021-10-25 17:42:10 +02:00
|
|
|
$customer->email = $email;
|
2020-12-01 17:02:10 +01:00
|
|
|
$customer->save();
|
2020-12-02 10:08:27 +01:00
|
|
|
$singleOptin = SingleOptin::where([['email', $email], ['brand_id', $brandId]])->first();
|
2021-02-28 12:04:49 +01:00
|
|
|
if ($singleOptin) {
|
2020-12-02 10:08:27 +01:00
|
|
|
$singleOptin->delete();
|
2021-02-28 12:04:49 +01:00
|
|
|
}
|
2020-12-01 17:02:10 +01:00
|
|
|
} else {
|
2021-12-19 15:57:24 +01:00
|
|
|
// Récupère les infos s'ils existent déjà.
|
2021-02-28 12:04:49 +01:00
|
|
|
$CustomerInfo = CustomerInfo::where([['customer_id', $customer->id], ['brand_id', $brandId]])->first();
|
|
|
|
|
if ($CustomerInfo) {
|
|
|
|
|
return new StatusResource([
|
|
|
|
|
'status' => 'KO', 'code' => 103,
|
|
|
|
|
'message' => 'User already registered on this site.'
|
|
|
|
|
]);
|
|
|
|
|
}
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
$customerInfos = new CustomerInfo();
|
|
|
|
|
$customerInfos->firstname = ucfirst(strtolower($firstname));
|
|
|
|
|
$customerInfos->lastname = ucfirst(strtolower($lastname));
|
|
|
|
|
$customerInfos->birth_date = !empty($request->birth_date) ?
|
2021-07-22 15:14:07 +02:00
|
|
|
filter_var($request->birth_date, FILTER_SANITIZE_STRING) : null;
|
2021-12-19 15:57:24 +01:00
|
|
|
$customerInfos->address = !empty($request->address) ?
|
2021-07-22 15:14:07 +02:00
|
|
|
filter_var($request->address, FILTER_SANITIZE_STRING) : null;
|
2021-12-19 15:57:24 +01:00
|
|
|
$customerInfos->address2 = !empty($request->address2) ?
|
2021-07-22 15:14:07 +02:00
|
|
|
filter_var($request->address2, FILTER_SANITIZE_STRING) : null;
|
2021-12-19 15:57:24 +01:00
|
|
|
$customerInfos->phone = !empty($request->phone) ?
|
2021-07-22 15:14:07 +02:00
|
|
|
filter_var($request->phone, FILTER_SANITIZE_STRING) : null;
|
2021-12-19 15:57:24 +01:00
|
|
|
try {
|
|
|
|
|
$customerInfos->country = Country::make(filter_var($request->country, FILTER_SANITIZE_STRING));
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$customerInfos->country = Country::france();
|
|
|
|
|
}
|
2021-07-22 15:14:07 +02:00
|
|
|
$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;
|
2020-12-01 17:02:10 +01:00
|
|
|
$customerInfos->customer_id = $customer->id;
|
|
|
|
|
|
|
|
|
|
$customer->infos()->save($customerInfos);
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
// Création du login pour cet utilisateur sur ce service.
|
2021-03-14 10:45:42 +01:00
|
|
|
$customerLogin = new CustomerLogin();
|
|
|
|
|
$customerLogin->customer_id = $customer->id;
|
|
|
|
|
$customerLogin->password = Hash::make(trim($request->password));
|
2020-12-01 17:02:10 +01:00
|
|
|
$customerLogin->last_connection = now();
|
2021-03-14 10:45:42 +01:00
|
|
|
$customerLogin->brand_id = $brandId;
|
|
|
|
|
$customerLogin->status = 0;
|
2020-12-01 17:02:10 +01:00
|
|
|
|
|
|
|
|
$customer->logins()->save($customerLogin);
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
// Ajout dans la table d'historique des opt-in de la nouvelle valeur.
|
2021-03-14 10:45:42 +01:00
|
|
|
$optInHistory = new CustomerOptInHistory();
|
2020-12-01 17:02:10 +01:00
|
|
|
$optInHistory->customer_id = $customer->id;
|
2021-03-14 10:45:42 +01:00
|
|
|
$optInHistory->brand_id = $brandId;
|
|
|
|
|
$optInHistory->new_value = $optIn;
|
2020-12-01 17:02:10 +01:00
|
|
|
$optInHistory->save();
|
|
|
|
|
|
|
|
|
|
return new CustomerResource($customer, $brandId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display the specified resource.
|
|
|
|
|
*
|
2023-06-23 17:18:59 +02:00
|
|
|
* @param Illuminate\Http\Request $request
|
|
|
|
|
* @param mixed $type
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
* @return \App\Http\Resources\CustomerResource|\App\Http\Resources\StatusResource
|
2020-12-01 17:02:10 +01:00
|
|
|
*/
|
2021-02-17 16:05:11 +01:00
|
|
|
public function show(Request $request, $type, $value)
|
2020-12-01 17:02:10 +01:00
|
|
|
{
|
|
|
|
|
$brandId = $request->brand_id;
|
2021-02-17 16:05:11 +01:00
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
if ($type === 'id') {
|
2021-02-17 16:05:11 +01:00
|
|
|
$customer = Customer::find($value);
|
2021-02-28 12:04:49 +01:00
|
|
|
if (!$customer) {
|
2021-02-17 16:05:11 +01:00
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 101, 'message' => 'User not found.']);
|
2021-02-28 12:04:49 +01:00
|
|
|
}
|
|
|
|
|
} elseif ($type === 'email') {
|
2021-02-17 16:05:11 +01:00
|
|
|
$customer = Customer::where('email', $value)->first();
|
2021-02-28 12:04:49 +01:00
|
|
|
if (!$customer) {
|
2021-02-17 16:05:11 +01:00
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 101, 'message' => 'User not found.']);
|
2021-02-28 12:04:49 +01:00
|
|
|
}
|
2021-02-17 16:05:11 +01:00
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
$CustomerInfo = $customer->infos()->where('brand_id', $brandId)->first();
|
2021-02-17 16:05:11 +01:00
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
if (!$CustomerInfo) {
|
2021-02-17 16:05:11 +01:00
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 101, 'message' => 'User not found.']);
|
2021-02-28 12:04:49 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2020-12-02 19:16:22 +01:00
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 101, 'message' => 'User not found.']);
|
2021-02-17 16:05:11 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-02 18:29:16 +01:00
|
|
|
return new CustomerResource($customer, $brandId);
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
|
*
|
2023-06-23 17:18:59 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @return \App\Http\Resources\CustomerResource|\App\Http\Resources\StatusResource
|
2020-12-01 17:02:10 +01:00
|
|
|
*/
|
2021-12-19 15:57:24 +01:00
|
|
|
// phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded, Squiz.Commenting.FunctionComment.Missing
|
2020-12-01 17:02:10 +01:00
|
|
|
public function update(Request $request, $id)
|
|
|
|
|
{
|
2020-12-02 19:16:22 +01:00
|
|
|
$validator = Validator::make($request->all(), [
|
2020-12-02 18:29:16 +01:00
|
|
|
'birth_date' => 'date_format:Y-m-d'
|
|
|
|
|
]);
|
|
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
if ($validator->fails()) {
|
|
|
|
|
return new StatusResource([
|
|
|
|
|
'status' => 'KO', 'code' => 104,
|
|
|
|
|
'message' => 'Error on required fields.', 'errors' => $validator->errors()
|
|
|
|
|
]);
|
|
|
|
|
}
|
2020-12-02 19:16:22 +01:00
|
|
|
|
2021-03-14 10:45:42 +01:00
|
|
|
$brandId = $request->brand_id;
|
2020-12-01 17:02:10 +01:00
|
|
|
$customer = Customer::find($id);
|
|
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
if (!$customer) {
|
2020-12-02 19:16:22 +01:00
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 105, 'message' => 'User not found.']);
|
2021-02-28 12:04:49 +01:00
|
|
|
}
|
2020-12-01 17:02:10 +01:00
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
$CustomerInfo = $customer->infos()->where('brand_id', $brandId)->first();
|
|
|
|
|
if (!$CustomerInfo) {
|
2021-02-19 16:32:08 +01:00
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 105, 'message' => 'User not found.']);
|
2021-02-28 12:04:49 +01:00
|
|
|
}
|
2020-12-01 17:02:10 +01:00
|
|
|
|
2021-07-22 15:14:07 +02:00
|
|
|
if ($request->has('firstname')) {
|
2021-02-28 12:04:49 +01:00
|
|
|
$CustomerInfo->firstname = filter_var($request->firstname, FILTER_SANITIZE_STRING);
|
2021-07-22 15:14:07 +02:00
|
|
|
}
|
2020-12-01 17:02:10 +01:00
|
|
|
|
2021-07-22 15:14:07 +02:00
|
|
|
if ($request->has('lastname')) {
|
2021-02-28 12:04:49 +01:00
|
|
|
$CustomerInfo->lastname = filter_var($request->lastname, FILTER_SANITIZE_STRING);
|
2021-07-22 15:14:07 +02:00
|
|
|
}
|
2020-12-01 17:02:10 +01:00
|
|
|
|
2021-04-30 11:37:48 +02:00
|
|
|
if ($request->has('birth_date')) {
|
2021-07-22 15:14:07 +02:00
|
|
|
if (!empty($request->birth_date)) {
|
2021-07-22 14:44:54 +02:00
|
|
|
$CustomerInfo->birth_date = filter_var($request->birth_date, FILTER_SANITIZE_STRING);
|
2021-07-22 15:14:07 +02:00
|
|
|
} else {
|
2021-07-22 14:44:54 +02:00
|
|
|
$CustomerInfo->birth_date = null;
|
2021-04-30 11:12:11 +02:00
|
|
|
}
|
2021-02-28 12:04:49 +01:00
|
|
|
}
|
2021-02-23 17:39:23 +01:00
|
|
|
|
2021-04-30 11:37:48 +02:00
|
|
|
if ($request->has('address')) {
|
2021-07-22 15:14:07 +02:00
|
|
|
if (!empty($request->address)) {
|
2021-07-22 14:44:54 +02:00
|
|
|
$CustomerInfo->address = filter_var($request->address, FILTER_SANITIZE_STRING);
|
2021-07-22 15:14:07 +02:00
|
|
|
} else {
|
2021-07-22 14:44:54 +02:00
|
|
|
$CustomerInfo->address = null;
|
2021-04-30 11:12:11 +02:00
|
|
|
}
|
2021-02-28 12:04:49 +01:00
|
|
|
}
|
2021-02-23 17:39:23 +01:00
|
|
|
|
2021-04-30 11:37:48 +02:00
|
|
|
if ($request->has('address2')) {
|
2021-07-22 15:14:07 +02:00
|
|
|
if (!empty($request->address2)) {
|
2021-07-22 14:44:54 +02:00
|
|
|
$CustomerInfo->address2 = filter_var($request->address2, FILTER_SANITIZE_STRING);
|
2021-07-22 15:14:07 +02:00
|
|
|
} else {
|
2021-07-22 14:44:54 +02:00
|
|
|
$CustomerInfo->address2 = null;
|
2021-04-30 11:12:11 +02:00
|
|
|
}
|
2021-02-28 12:04:49 +01:00
|
|
|
}
|
2021-02-23 17:39:23 +01:00
|
|
|
|
2021-04-30 11:37:48 +02:00
|
|
|
if ($request->has('phone')) {
|
2021-07-22 15:14:07 +02:00
|
|
|
if (!empty($request->phone)) {
|
2021-07-22 14:44:54 +02:00
|
|
|
$CustomerInfo->phone = filter_var($request->phone, FILTER_SANITIZE_STRING);
|
2021-07-22 15:14:07 +02:00
|
|
|
} else {
|
2021-07-22 14:44:54 +02:00
|
|
|
$CustomerInfo->phone = null;
|
2021-04-30 11:12:11 +02:00
|
|
|
}
|
2021-02-28 12:04:49 +01:00
|
|
|
}
|
2021-02-23 17:39:23 +01:00
|
|
|
|
2021-04-30 11:37:48 +02:00
|
|
|
if ($request->has('country')) {
|
2021-12-19 15:57:24 +01:00
|
|
|
try {
|
|
|
|
|
$CustomerInfo->country = Country::make(filter_var($request->country, FILTER_SANITIZE_STRING));
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$CustomerInfo->country = Country::france();
|
2021-04-30 11:12:11 +02:00
|
|
|
}
|
2021-02-28 12:04:49 +01:00
|
|
|
}
|
2021-02-23 17:39:23 +01:00
|
|
|
|
2021-04-30 11:37:48 +02:00
|
|
|
if ($request->has('city')) {
|
2021-07-22 15:14:07 +02:00
|
|
|
if (!empty($request->city)) {
|
2021-07-22 14:44:54 +02:00
|
|
|
$CustomerInfo->city = filter_var($request->city, FILTER_SANITIZE_STRING);
|
2021-07-22 15:14:07 +02:00
|
|
|
} else {
|
2021-07-22 14:44:54 +02:00
|
|
|
$CustomerInfo->city = null;
|
2021-04-30 11:12:11 +02:00
|
|
|
}
|
2021-02-28 12:04:49 +01:00
|
|
|
}
|
2021-02-23 17:39:23 +01:00
|
|
|
|
2021-04-30 11:37:48 +02:00
|
|
|
if ($request->has('postcode')) {
|
2021-07-22 15:14:07 +02:00
|
|
|
if (!empty($request->postcode)) {
|
2021-07-22 14:44:54 +02:00
|
|
|
$CustomerInfo->postcode = filter_var($request->postcode, FILTER_SANITIZE_STRING);
|
2021-07-22 15:14:07 +02:00
|
|
|
} else {
|
2021-07-22 14:44:54 +02:00
|
|
|
$CustomerInfo->postcode = null;
|
2021-04-30 11:12:11 +02:00
|
|
|
}
|
2021-02-28 12:04:49 +01:00
|
|
|
}
|
2021-02-23 17:39:23 +01:00
|
|
|
|
2021-04-30 11:37:48 +02:00
|
|
|
if ($request->has('opt_in')) {
|
2021-02-23 17:39:23 +01:00
|
|
|
$optIn = filter_var($request->opt_in, FILTER_SANITIZE_STRING);
|
|
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
if ($CustomerInfo->opt_in != $optIn) {
|
2021-12-19 15:57:24 +01:00
|
|
|
// Ajout dans la table d'historique des opt-in de la nouvelle valeur.
|
2021-03-14 10:45:42 +01:00
|
|
|
$optInHistory = new CustomerOptInHistory();
|
2021-02-23 17:39:23 +01:00
|
|
|
$optInHistory->customer_id = $customer->id;
|
2021-03-14 10:45:42 +01:00
|
|
|
$optInHistory->brand_id = $brandId;
|
|
|
|
|
$optInHistory->new_value = $optIn;
|
2021-02-23 17:39:23 +01:00
|
|
|
$optInHistory->save();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
$CustomerInfo->opt_in = $optIn;
|
2021-02-23 17:39:23 +01:00
|
|
|
}
|
2020-12-01 17:02:10 +01:00
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
$CustomerInfo->save();
|
2020-12-01 17:02:10 +01:00
|
|
|
|
|
|
|
|
return new CustomerResource($customer, $brandId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Authenticate the user
|
2023-06-23 17:18:59 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @return \App\Http\Resources\CustomerResource|\App\Http\Resources\StatusResource
|
2020-12-01 17:02:10 +01:00
|
|
|
*/
|
2021-02-28 12:04:49 +01:00
|
|
|
public function authenticate(Request $request)
|
|
|
|
|
{
|
2020-12-02 19:16:22 +01:00
|
|
|
$validator = Validator::make($request->all(), [
|
2020-12-02 18:29:16 +01:00
|
|
|
'email' => 'required|email',
|
|
|
|
|
'password' => 'required',
|
|
|
|
|
]);
|
|
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
if ($validator->fails()) {
|
|
|
|
|
return new StatusResource([
|
|
|
|
|
'status' => 'KO', 'code' => 115,
|
|
|
|
|
'message' => 'Error on required fields.', 'errors' => $validator->errors()
|
|
|
|
|
]);
|
|
|
|
|
}
|
2020-12-02 19:16:22 +01:00
|
|
|
|
2021-03-14 10:45:42 +01:00
|
|
|
$brandId = $request->brand_id;
|
|
|
|
|
$email = filter_var($request->email, FILTER_SANITIZE_EMAIL);
|
2020-12-01 17:02:10 +01:00
|
|
|
$password = trim($request->password);
|
|
|
|
|
$customer = Customer::where('email', $email)->first();
|
|
|
|
|
if ($customer) {
|
|
|
|
|
$login = $customer->logins()->where('brand_id', $brandId)->first();
|
|
|
|
|
if ($login) {
|
2021-02-17 14:38:37 +01:00
|
|
|
if (Hash::check($password, $login->password)) {
|
2021-10-26 12:01:40 +02:00
|
|
|
if ($login->status === false) {
|
2021-02-17 15:24:31 +01:00
|
|
|
$infos = $customer->infos()->where('brand_id', $brandId)->first();
|
2021-02-17 14:38:37 +01:00
|
|
|
// "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,
|
2021-02-17 15:24:31 +01:00
|
|
|
'firstname' => $infos->firstname,
|
|
|
|
|
'lastname' => $infos->lastname,
|
2021-06-23 10:20:57 +02:00
|
|
|
'email' => $customer->email,
|
2021-02-17 15:24:31 +01:00
|
|
|
'birth_date' => $infos->birth_date,
|
|
|
|
|
'address' => $infos->address,
|
|
|
|
|
'address2' => $infos->address2,
|
|
|
|
|
'phone' => $infos->phone,
|
2021-12-19 15:57:24 +01:00
|
|
|
'country' => $infos->country->label,
|
2021-02-17 15:24:31 +01:00
|
|
|
'city' => $infos->city,
|
|
|
|
|
'postcode' => $infos->postcode,
|
|
|
|
|
'opt_in' => $infos->opt_in,
|
|
|
|
|
'hashcode' => $infos->hashcode,
|
2021-02-17 14:38:37 +01:00
|
|
|
'status' => $login->status,
|
|
|
|
|
]
|
2021-02-28 12:04:49 +01:00
|
|
|
]
|
|
|
|
|
);
|
2021-12-19 15:57:24 +01:00
|
|
|
}//end if
|
2021-02-17 14:38:37 +01:00
|
|
|
|
|
|
|
|
$login->last_connection = now();
|
|
|
|
|
$customer->logins()->save($login);
|
|
|
|
|
return new CustomerResource($customer, $brandId);
|
2021-12-19 15:57:24 +01:00
|
|
|
}//end if
|
2021-02-17 14:38:37 +01:00
|
|
|
// "Mot de passe incorrect"
|
|
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 117, 'message' => 'Authentication failed.']);
|
2020-12-01 17:02:10 +01:00
|
|
|
} else {
|
2020-12-02 19:16:22 +01:00
|
|
|
// "Ce compte n'est pas enregistré pour cette marque"
|
|
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 118, 'message' => 'User not found.']);
|
2021-12-19 15:57:24 +01:00
|
|
|
}//end if
|
2020-12-01 17:02:10 +01:00
|
|
|
} else {
|
2020-12-02 19:16:22 +01:00
|
|
|
// "Ce compte n'existe pas dans la CRM"
|
|
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 119, 'message' => 'User not found.']);
|
2021-12-19 15:57:24 +01:00
|
|
|
}//end if
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Change the password of the user for the brand
|
2023-06-23 17:18:59 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @param mixed $id
|
|
|
|
|
* @return \App\Http\Resources\StatusResource
|
2020-12-01 17:02:10 +01:00
|
|
|
*/
|
2021-02-28 12:04:49 +01:00
|
|
|
public function changePassword(Request $request, $id)
|
|
|
|
|
{
|
2020-12-02 19:16:22 +01:00
|
|
|
$validator = Validator::make($request->all(), [
|
2020-12-02 18:29:16 +01:00
|
|
|
'password' => 'required',
|
|
|
|
|
]);
|
2020-12-01 17:02:10 +01:00
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
if ($validator->fails()) {
|
|
|
|
|
return new StatusResource([
|
|
|
|
|
'status' => 'KO', 'code' => 107,
|
|
|
|
|
'message' => 'Error on required fields.', 'errors' => $validator->errors()
|
|
|
|
|
]);
|
|
|
|
|
}
|
2020-12-02 19:16:22 +01:00
|
|
|
|
2021-03-14 10:45:42 +01:00
|
|
|
$brandId = $request->brand_id;
|
2020-12-02 18:29:16 +01:00
|
|
|
$customer = Customer::find($id);
|
2020-12-01 17:02:10 +01:00
|
|
|
$password = trim($request->password);
|
|
|
|
|
|
|
|
|
|
if ($customer) {
|
|
|
|
|
$customerLogin = $customer->logins()->where('brand_id', $brandId)->first();
|
|
|
|
|
if ($customerLogin) {
|
2021-12-19 15:57:24 +01:00
|
|
|
// Check if same password.
|
2021-03-14 10:45:42 +01:00
|
|
|
if (Hash::check($password, $customerLogin->password)) {
|
2021-03-08 11:37:54 +01:00
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 123, 'message' => 'Same password.']);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 17:02:10 +01:00
|
|
|
$customerLogin->password = Hash::make($password);
|
|
|
|
|
$customerLogin->save();
|
|
|
|
|
} else {
|
2020-12-02 19:16:22 +01:00
|
|
|
// "Ce compte n'est pas enregistré pour cette marque"
|
|
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 108, 'message' => 'User not found.']);
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2020-12-02 19:16:22 +01:00
|
|
|
// "Ce compte n'existe pas"
|
|
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 109, 'message' => 'User not found.']);
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
2020-12-02 19:16:22 +01:00
|
|
|
return new StatusResource(['status' => 'OK', 'message' => 'Update complete.']);
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Activate the account of the user
|
2023-06-23 17:18:59 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @param mixed $id
|
|
|
|
|
* @return \App\Http\Resources\StatusResource
|
2020-12-01 17:02:10 +01:00
|
|
|
*/
|
2021-12-19 15:57:24 +01:00
|
|
|
public function activateAccount(Request $request, $id)
|
|
|
|
|
{
|
2020-12-01 17:02:10 +01:00
|
|
|
$brandId = $request->brand_id;
|
|
|
|
|
|
|
|
|
|
$customer = Customer::find($id);
|
|
|
|
|
if ($customer) {
|
|
|
|
|
$login = $customer->logins()->where('brand_id', $brandId)->first();
|
|
|
|
|
if ($login) {
|
2021-10-26 12:01:40 +02:00
|
|
|
if ($login->status === false) {
|
|
|
|
|
$login->status = true;
|
2020-12-01 17:02:10 +01:00
|
|
|
$login->save();
|
2020-12-02 19:16:22 +01:00
|
|
|
// "Le compte a bien été activé"
|
|
|
|
|
return new StatusResource(['status' => 'OK', 'message' => 'Update complete.']);
|
2020-12-01 17:02:10 +01:00
|
|
|
} else {
|
2020-12-02 19:16:22 +01:00
|
|
|
// "Ce compte est déjà activé"
|
|
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 110, 'message' => 'User already active.']);
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2020-12-02 19:16:22 +01:00
|
|
|
// "Ce compte n'est pas enregistré pour cette marque"
|
|
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 111, 'message' => 'User not found.']);
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2020-12-02 19:16:22 +01:00
|
|
|
// "Cet utilisateur n'existe pas dans la CRM"
|
|
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 112, 'message' => 'User not found.']);
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete the user account for the brand
|
2023-06-23 17:18:59 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @param mixed $id
|
2020-12-01 17:02:10 +01:00
|
|
|
*/
|
2021-12-19 15:57:24 +01:00
|
|
|
// phpcs:disable Squiz.PHP.NonExecutableCode.Unreachable, Squiz.Commenting.FunctionComment.Missing
|
2021-02-28 12:04:49 +01:00
|
|
|
public function delete(Request $request, $id)
|
|
|
|
|
{
|
2020-12-01 17:14:02 +01:00
|
|
|
die("Not implemented yet");
|
2021-12-19 15:57:24 +01:00
|
|
|
// TODO : Gestion avec mesBR.
|
2020-12-01 17:02:10 +01:00
|
|
|
$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();
|
2021-12-19 15:57:24 +01:00
|
|
|
// Suppression du compte de la CRM terminée.
|
2020-12-02 19:16:22 +01:00
|
|
|
return new StatusResource(['status' => 'OK', 'message' => 'Deletion complete.']);
|
2020-12-01 17:02:10 +01:00
|
|
|
} else {
|
2021-12-19 15:57:24 +01:00
|
|
|
// Suppression des informations du compte pour la marque terminée.
|
2020-12-02 19:16:22 +01:00
|
|
|
return new StatusResource(['status' => 'OK', 'message' => 'Deletion complete.']);
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2020-12-02 19:16:22 +01:00
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 106, 'message' => 'User not found.']);
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
2021-12-19 15:57:24 +01:00
|
|
|
// phpcs:enable Squiz.PHP.NonExecutableCode.Unreachable, Squiz.Commenting.FunctionComment.Missing
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Change the status of the opt-in for the customer on this brand
|
2023-06-23 17:18:59 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @param mixed $id
|
|
|
|
|
* @return \App\Http\Resources\StatusResource
|
2020-12-01 17:02:10 +01:00
|
|
|
*/
|
2021-02-28 12:04:49 +01:00
|
|
|
public function toggleOptin(Request $request, $id)
|
|
|
|
|
{
|
2021-12-19 15:57:24 +01:00
|
|
|
// TODO : Liaison des opt-in avec MailJet.
|
2020-12-01 17:02:10 +01:00
|
|
|
$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;
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
// On enregistre l'historique du changement d'optin.
|
2021-03-14 10:45:42 +01:00
|
|
|
$optinHistory = new CustomerOptInHistory();
|
2020-12-01 17:02:10 +01:00
|
|
|
$optinHistory->customer_id = $customer->id;
|
2021-03-14 10:45:42 +01:00
|
|
|
$optinHistory->new_value = $infos->opt_in;
|
|
|
|
|
$optinHistory->brand_id = $brandId;
|
2020-12-01 17:02:10 +01:00
|
|
|
$optinHistory->save();
|
|
|
|
|
|
|
|
|
|
$infos->save();
|
2020-12-02 19:16:22 +01:00
|
|
|
// "Changement effectué"
|
|
|
|
|
return new StatusResource(['status' => 'OK', 'message' => 'Update complete.']);
|
2020-12-01 17:02:10 +01:00
|
|
|
} else {
|
2020-12-02 19:16:22 +01:00
|
|
|
// "Ce compte n'a pas d'information liés à cette marque"
|
|
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 113, 'message' => 'User not found.']);
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2020-12-02 19:16:22 +01:00
|
|
|
// "Cet utilisateur n'existe pas dans la CRM"
|
|
|
|
|
return new StatusResource(['status' => 'KO', 'code' => 114, 'message' => 'User not found.']);
|
2021-12-19 15:57:24 +01:00
|
|
|
}//end if
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-08 11:37:54 +01:00
|
|
|
/**
|
|
|
|
|
* Remove opt-in for the customer on this brand
|
2023-06-23 17:18:59 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @param mixed $hashcode
|
|
|
|
|
* @return \App\Http\Resources\StatusResource
|
2021-03-08 11:37:54 +01:00
|
|
|
*/
|
2021-03-14 10:45:42 +01:00
|
|
|
public function removeOptin(Request $request, $hashcode)
|
|
|
|
|
{
|
2021-12-19 15:57:24 +01:00
|
|
|
// TODO : Liaison des opt-in avec MailJet.
|
2021-03-08 11:37:54 +01:00
|
|
|
$brandId = $request->brand_id;
|
|
|
|
|
|
2021-03-14 10:45:42 +01:00
|
|
|
$infos = CustomerInfo::where([
|
2021-03-08 11:37:54 +01:00
|
|
|
['brand_id', $brandId],
|
|
|
|
|
['hashcode', $hashcode]
|
|
|
|
|
])->first();
|
|
|
|
|
|
|
|
|
|
if ($infos) {
|
|
|
|
|
$infos->opt_in = 0;
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
// On enregistre l'historique du changement d'optin.
|
2021-03-14 10:45:42 +01:00
|
|
|
$optinHistory = new CustomerOptInHistory();
|
2021-03-08 11:37:54 +01:00
|
|
|
$optinHistory->customer_id = $infos->customer_id;
|
2021-03-14 10:45:42 +01:00
|
|
|
$optinHistory->new_value = 0;
|
|
|
|
|
$optinHistory->brand_id = $brandId;
|
2021-03-08 11:37:54 +01:00
|
|
|
$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.']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
2023-06-23 17:18:59 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2021-12-19 15:57:24 +01:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
// phpcs:ignore Squiz.PHP.NonExecutableCode.Unreachable, Squiz.Commenting.FunctionComment.Missing
|
2021-03-14 10:45:42 +01:00
|
|
|
public function addSingleOptin(Request $request)
|
|
|
|
|
{
|
2020-12-02 19:16:22 +01:00
|
|
|
$validator = Validator::make($request->all(), [
|
2020-12-02 18:29:16 +01:00
|
|
|
'email' => 'required|email',
|
|
|
|
|
]);
|
2020-12-02 19:16:22 +01:00
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
if ($validator->fails()) {
|
|
|
|
|
return new StatusResource([
|
|
|
|
|
'status' => 'KO', 'code' => 120,
|
|
|
|
|
'message' => 'Error on required fields.', 'errors' => $validator->errors()
|
|
|
|
|
]);
|
|
|
|
|
}
|
2020-12-02 19:16:22 +01:00
|
|
|
|
2020-12-02 10:08:27 +01:00
|
|
|
$brandId = $request->brand_id;
|
2021-03-14 10:45:42 +01:00
|
|
|
$email = filter_var($request->email, FILTER_SANITIZE_EMAIL);
|
2020-12-02 10:08:27 +01:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
// Check si le customer ou l'optin simple existe avant de l'ajouter en optin simple.
|
2021-02-28 12:04:49 +01:00
|
|
|
if (
|
|
|
|
|
!SingleOptin::where([['email', $email], ['brand_id', $brandId]])->exists() &&
|
|
|
|
|
!CustomerInfo::join("customers", "customer_infos.customer_id", "=", "customers.id")->where([
|
|
|
|
|
['brand_id', $brandId], ['email', $email]
|
|
|
|
|
])->exists()
|
|
|
|
|
) {
|
2021-03-14 10:45:42 +01:00
|
|
|
$singleOptin = new SingleOptin();
|
|
|
|
|
$singleOptin->email = $email;
|
2020-12-02 10:08:27 +01:00
|
|
|
$singleOptin->brand_id = $brandId;
|
|
|
|
|
$singleOptin->save();
|
|
|
|
|
} else {
|
2020-12-02 19:16:22 +01:00
|
|
|
// "Cet utilisateur est déjà inscrit à la newsletter pour cette marque"
|
2021-02-28 12:04:49 +01:00
|
|
|
return new StatusResource([
|
|
|
|
|
'status' => 'KO', 'code' => 121,
|
|
|
|
|
'message' => 'This user is already subscribed to the newsletter for this brand.'
|
|
|
|
|
]);
|
2020-12-02 10:08:27 +01:00
|
|
|
}
|
2021-02-28 12:04:49 +01:00
|
|
|
return new StatusResource(['status' => 'OK', 'message' => 'Optin added.']);
|
2020-12-02 10:08:27 +01:00
|
|
|
}
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|