Ajout des optin simple + methode API

This commit is contained in:
Romain Bonhomme 2020-12-02 10:08:27 +01:00
parent da734630b0
commit 7f50e06c13
4 changed files with 31 additions and 2 deletions

View file

@ -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;

View file

@ -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');
}
}

View file

@ -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');

View file

@ -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']);
});