From 7f50e06c13aca688fa078027deeb4eb774523b78 Mon Sep 17 00:00:00 2001 From: Romain Bonhomme Date: Wed, 2 Dec 2020 10:08:27 +0100 Subject: [PATCH] Ajout des optin simple + methode API --- .../Controllers/Api/v1/CustomerController.php | 22 +++++++++++++++++++ app/Models/Brand.php | 4 ++++ app/Models/{OptIn.php => SingleOptin.php} | 4 ++-- routes/api.php | 3 +++ 4 files changed, 31 insertions(+), 2 deletions(-) rename app/Models/{OptIn.php => SingleOptin.php} (78%) diff --git a/app/Http/Controllers/Api/v1/CustomerController.php b/app/Http/Controllers/Api/v1/CustomerController.php index 8ae7744..e0862d7 100644 --- a/app/Http/Controllers/Api/v1/CustomerController.php +++ b/app/Http/Controllers/Api/v1/CustomerController.php @@ -8,6 +8,7 @@ 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; @@ -56,6 +57,9 @@ class CustomerController extends Controller $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(); @@ -311,6 +315,24 @@ class CustomerController extends Controller } } + 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; diff --git a/app/Models/Brand.php b/app/Models/Brand.php index 8255665..6e7c660 100644 --- a/app/Models/Brand.php +++ b/app/Models/Brand.php @@ -22,4 +22,8 @@ class Brand extends Model public function services_stats() { return $this->hasMany('App\Models\ServiceStat', 'brand_id'); } + + public function single_optins() { + return $this->hasMany('App\Models\SingleOptin', 'brand_id'); + } } diff --git a/app/Models/OptIn.php b/app/Models/SingleOptin.php similarity index 78% rename from app/Models/OptIn.php rename to app/Models/SingleOptin.php index f41781c..29f8465 100644 --- a/app/Models/OptIn.php +++ b/app/Models/SingleOptin.php @@ -5,11 +5,11 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; -class OptIn extends Model +class SingleOptin extends Model { use HasFactory; - protected $table = "opt_in"; + protected $table = "single_optin"; public function brand() { return $this->belongsTo('App\Models\Brand', 'brand_id'); diff --git a/routes/api.php b/routes/api.php index 3718382..ee0f37e 100644 --- a/routes/api.php +++ b/routes/api.php @@ -15,6 +15,7 @@ use App\Http\Controllers\Api\v1\CustomerController; */ Route::prefix('v1')->middleware(['auth:api', 'check-brand'])->group(function() { + // Clients Route::get('/customer/{id}', [CustomerController::class, 'show']); Route::post('/customer/create', [CustomerController::class, 'store']); Route::post('/customer/update/{id}', [CustomerController::class, 'update']); @@ -23,4 +24,6 @@ Route::prefix('v1')->middleware(['auth:api', 'check-brand'])->group(function() { Route::get('/customer/activate/{id}', [CustomerController::class, 'activateAccount']); Route::get('/customer/change-optin/{id}', [CustomerController::class, 'changeOptin']); Route::get('/customer/delete/{id}', [CustomerController::class, 'deleteUser']); + // Optin simple + Route::post('/single-optin', [CustomerController::class, 'addSingleOptin']); });