diff --git a/app/Http/Controllers/Api/v1/CustomerController.php b/app/Http/Controllers/Api/v1/CustomerController.php index f5d7302..7acd931 100644 --- a/app/Http/Controllers/Api/v1/CustomerController.php +++ b/app/Http/Controllers/Api/v1/CustomerController.php @@ -334,6 +334,11 @@ class CustomerController extends Controller 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 { @@ -448,8 +453,40 @@ class CustomerController extends Controller } } - public function addSingleOptin(Request $request) - { + /** + * 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', ]); diff --git a/app/Http/Resources/CustomerResource.php b/app/Http/Resources/CustomerResource.php index 9bfda5f..f01f773 100644 --- a/app/Http/Resources/CustomerResource.php +++ b/app/Http/Resources/CustomerResource.php @@ -30,6 +30,7 @@ class CustomerResource extends JsonResource 'crm_id' => $this->id, 'firstname' => $infos->firstname, 'lastname' => $infos->lastname, + 'email' => $this->email, 'birth_date' => $infos->birth_date, 'address' => $infos->address, 'address2' => $infos->address2, diff --git a/app/Models/Brand.php b/app/Models/Brand.php index f4c2494..2140143 100644 --- a/app/Models/Brand.php +++ b/app/Models/Brand.php @@ -48,4 +48,12 @@ class Brand extends Model { return $this->hasMany(SingleOptin::class); } + + public function getAccountsAttribute(){ + return $this->customer_infos()->count(); + } + + public function getTotalOptinsAttribute(){ + return $this->customer_infos()->where('opt_in', 1)->count() + $this->single_optins()->count(); + } } diff --git a/clients/LDCCRM_V1.php b/clients/LDCCRM_V1.php index 7499bf9..6e1959d 100644 --- a/clients/LDCCRM_V1.php +++ b/clients/LDCCRM_V1.php @@ -92,6 +92,10 @@ class ApiV1 return $this->callAPI('POST', "/customer/$id/toggle-optin"); } + public function removeOptInCustomer($hashcode){ + return $this->callAPI('POST', "/customer/$hashcode/remove-optin"); + } + public function loginCustomer($email, $password){ return $this->callAPI('POST', "/customer/login", ['email' => $email, 'password' => $password]); } diff --git a/resources/views/brands/index.blade.php b/resources/views/brands/index.blade.php index ac56f96..24965ab 100644 --- a/resources/views/brands/index.blade.php +++ b/resources/views/brands/index.blade.php @@ -8,6 +8,8 @@
| Nom de la marque | +Nombre de comptes | +Nombre d'optins | Date d'ajout en base | @@ -15,6 +17,8 @@ @foreach ($brands as $brand) |
|---|---|---|---|---|
| {{ $brand->name }} | +{{ $brand->accounts }} | +{{ $brand->total_optins }} | {{ \Carbon\Carbon::parse($brand->created_at)->format('d/m/Y à H:i') }} | Modifier |