Evolution Set hashcode as PK on single optins
This commit is contained in:
parent
b65ad8c19e
commit
97f9f49f2e
13 changed files with 146 additions and 35 deletions
|
|
@ -60,20 +60,24 @@ class CustomerController extends Controller
|
|||
$customer = new Customer();
|
||||
$customer->email = $email;
|
||||
$customer->save();
|
||||
$singleOptin = SingleOptin::where([['email', $email], ['brand_id', $brandId]])->first();
|
||||
$singleOptin = SingleOptin::query()->where([
|
||||
['email', $email], ['brand_id', $brandId]
|
||||
])->first();
|
||||
if ($singleOptin) {
|
||||
$singleOptin->delete();
|
||||
}
|
||||
} else {
|
||||
// Récupère les infos s'ils existent déjà.
|
||||
$CustomerInfo = CustomerInfo::where([['customer_id', $customer->id], ['brand_id', $brandId]])->first();
|
||||
$CustomerInfo = CustomerInfo::query()->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.'
|
||||
]);
|
||||
}
|
||||
}
|
||||
}//end if
|
||||
|
||||
$customerInfos = new CustomerInfo();
|
||||
$customerInfos->firstname = ucfirst(strtolower($firstname));
|
||||
|
|
@ -549,7 +553,7 @@ class CustomerController extends Controller
|
|||
|
||||
// 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() &&
|
||||
!SingleOptin::query()->where([['email', $email], ['brand_id', $brandId]])->exists() &&
|
||||
!CustomerInfo::join("customers", "customer_infos.customer_id", "=", "customers.id")->where([
|
||||
['brand_id', $brandId], ['email', $email]
|
||||
])->exists()
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ use App\Jobs\UpdateCustomerLastConnection;
|
|||
use App\Models\Customer;
|
||||
use App\Models\Service;
|
||||
use App\Models\SingleOptin;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
|
@ -47,8 +48,10 @@ class CustomerController extends Controller
|
|||
|
||||
// * Delete Single optin if exists.
|
||||
/** @var \App\Models\SingleOptin|null $singleOptin */
|
||||
$singleOptin = SingleOptin::where('email', $data->email)
|
||||
->where('brand_id', $service->brand->id)->first();
|
||||
$singleOptin = SingleOptin::query()->where('email', $data->email)
|
||||
->whereHas('brand', function (Builder $q) use ($service) {
|
||||
$q->where('id', $service->brand->id);
|
||||
})->first();
|
||||
if ($singleOptin) {
|
||||
$saved = $singleOptin->delete() && $saved;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,9 +31,9 @@ class OptinController extends Controller
|
|||
}
|
||||
|
||||
/** @var \App\Models\SingleOptin */
|
||||
$singleOptin = SingleOptin::where('email', $request->email)
|
||||
$singleOptin = SingleOptin::query()->where('email', $request->email)
|
||||
->whereHas('brand', function ($q) use ($service) {
|
||||
$q->whereId($service->brand->id);
|
||||
$q->where('id', $service->brand->id);
|
||||
})->firstOrNew();
|
||||
|
||||
if ($request->opt_in) {
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ use App\Models\Customer;
|
|||
use App\Models\CustomerInfo;
|
||||
use App\Models\CustomerLogin;
|
||||
use App\Models\SingleOptin;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
|
@ -271,15 +272,16 @@ class CustomerController extends Controller
|
|||
}
|
||||
// ! Continue
|
||||
case CustomerExportType::singleOptin():
|
||||
SingleOptin::where('brand_id', $request->brand)->get()
|
||||
->map(function (SingleOptin $singleOptin) use ($w) {
|
||||
$w->insertOne([
|
||||
'lastname' => '',
|
||||
'firstname' => '',
|
||||
'email' => $singleOptin->email,
|
||||
'hashcode' => ''
|
||||
]);
|
||||
});
|
||||
SingleOptin::query()->whereHas('brand', function (Builder $q) use ($request) {
|
||||
$q->where('id', $request->brand);
|
||||
})->get()->map(function (SingleOptin $singleOptin) use ($w) {
|
||||
$w->insertOne([
|
||||
'lastname' => '',
|
||||
'firstname' => '',
|
||||
'email' => $singleOptin->email,
|
||||
'hashcode' => $singleOptin->hashcode
|
||||
]);
|
||||
});
|
||||
break;
|
||||
default:
|
||||
throw new \Exception(sprintf('Unhandled CustomerExportType %s', $exportType->label));
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\SingleOptin;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
|
|
@ -23,7 +24,9 @@ class SingleOptinController extends Controller
|
|||
$query = SingleOptin::query();
|
||||
|
||||
if ($request->brand) {
|
||||
$query = $query->where('brand_id', '=', request()->brand);
|
||||
$query = $query->whereHas('brand', function (Builder $q) {
|
||||
$q->where('id', request()->brand);
|
||||
});
|
||||
}
|
||||
if ($request->search) {
|
||||
$this->searchQuery(
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ use App\Models\CustomerLogin;
|
|||
use App\Models\SingleOptin;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
||||
|
|
@ -57,17 +58,29 @@ class StatJob implements ShouldQueue
|
|||
public function handle()
|
||||
{
|
||||
$stats = new BrandStat([
|
||||
'active' => CustomerLogin::whereBrandId($this->brand->id)
|
||||
->whereDate('last_connection', '<', Carbon::now()->subMonths(6))->count(),
|
||||
'inactive' => CustomerLogin::whereBrandId($this->brand->id)
|
||||
'active' => CustomerLogin::query()->whereHas('brand', function (Builder $q) {
|
||||
$q->where('id', $this->brand->id);
|
||||
})->whereDate('last_connection', '<', Carbon::now()->subMonths(6))->count(),
|
||||
'inactive' => CustomerLogin::query()->whereHas('brand', function (Builder $q) {
|
||||
$q->where('id', $this->brand->id);
|
||||
})
|
||||
->whereDate('last_connection', '>', Carbon::now()->subMonths(6))->count(),
|
||||
'new_accounts' => CustomerLogin::whereBrandId($this->brand->id)
|
||||
'new_accounts' => CustomerLogin::query()->whereHas('brand', function (Builder $q) {
|
||||
$q->where('id', $this->brand->id);
|
||||
})
|
||||
->whereDate('created_at', '<', Carbon::now()->subMonths(6))->count(),
|
||||
'old_accounts' => CustomerLogin::whereBrandId($this->brand->id)
|
||||
'old_accounts' => CustomerLogin::query()->whereHas('brand', function (Builder $q) {
|
||||
$q->where('id', $this->brand->id);
|
||||
})
|
||||
->whereDate('created_at', '>', Carbon::now()->subMonths(6))->count(),
|
||||
'total_accounts' => CustomerLogin::whereBrandId($this->brand->id)->count(),
|
||||
'total_opt_in' => CustomerInfo::whereBrandId($this->brand->id)
|
||||
->whereOptIn(true)->count() + SingleOptin::whereBrandId($this->brand->id)->count()
|
||||
'total_accounts' => CustomerLogin::query()->whereHas('brand', function (Builder $q) {
|
||||
$q->where('id', $this->brand->id);
|
||||
})->count(),
|
||||
'total_opt_in' => CustomerInfo::query()->whereHas('brand', function (Builder $q) {
|
||||
$q->where('id', $this->brand->id);
|
||||
})->whereOptIn(true)->count() + SingleOptin::query()->whereHas('brand', function (Builder $q) {
|
||||
$q->where('id', $this->brand->id);
|
||||
})->count()
|
||||
]);
|
||||
$stats->brand()->associate($this->brand);
|
||||
$stats->saveOrFail();
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ use App\Models\CustomerLogin;
|
|||
use App\Models\SingleOptin;
|
||||
use Carbon\Exceptions\InvalidFormatException;
|
||||
use Database\migrations\LeGaulois2016Migration;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
|
@ -85,8 +86,10 @@ class MigrateOldData extends LeGaulois2016Migration
|
|||
|
||||
// * Just save Single optin.
|
||||
if (empty($first_name) && empty($last_name)) {
|
||||
$sglOptin = SingleOptin::where('email', strtolower($email))
|
||||
->where('brand_id', $this->brand->id)->firstOrNew();
|
||||
$sglOptin = SingleOptin::query()->where('email', strtolower($email))
|
||||
->whereHas('brand', function (Builder $q) {
|
||||
$q->where('id', $this->brand->id);
|
||||
})->firstOrNew();
|
||||
$sglOptin->email = strtolower($email);
|
||||
$sglOptin->brand_id = $this->brand->id;
|
||||
$sglOptin->save();
|
||||
|
|
|
|||
|
|
@ -4,11 +4,33 @@ namespace App\Models;
|
|||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class SingleOptin extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The primary key for the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'hashcode';
|
||||
|
||||
/**
|
||||
* The "type" of the primary key ID.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $keyType = 'uuid';
|
||||
|
||||
/**
|
||||
* Indicates if the IDs are auto-incrementing.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
public $incrementing = false;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
|
|
@ -18,6 +40,18 @@ class SingleOptin extends Model
|
|||
'email'
|
||||
];
|
||||
|
||||
/**
|
||||
* Perform any actions required after the model boots.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected static function booted()
|
||||
{
|
||||
static::creating(function (self $model) {
|
||||
$model->hashcode = Str::uuid();
|
||||
});
|
||||
}
|
||||
|
||||
// * RELATIONS
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class AddChangeSingleOptinToUseHashcode extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('single_optins', function (Blueprint $table) {
|
||||
$table->uuid('hashcode')->comment('Single optin hashcode')->after('id');
|
||||
});
|
||||
DB::transaction(function () {
|
||||
DB::table('single_optins')->get()->each(function (object $single_optin) {
|
||||
DB::table('single_optins')->where('id', $single_optin->id)->update([
|
||||
'hashcode' => Str::uuid()
|
||||
]);
|
||||
});
|
||||
});
|
||||
Schema::table('single_optins', function (Blueprint $table) {
|
||||
$table->uuid('hashcode')->comment('Single optin hashcode')->after('id')
|
||||
->unique()->change();
|
||||
$table->dropColumn('id');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('single_optins', function (Blueprint $table) {
|
||||
$table->id()->after('hashcode');
|
||||
});
|
||||
Schema::table('single_optins', function (Blueprint $table) {
|
||||
$table->dropColumn('hashcode');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -683,7 +683,7 @@ class CustomerTest extends ApiBase
|
|||
|
||||
// * Delete Single optin if exists.
|
||||
/** @var \App\Models\SingleOptin|null $singleOptin */
|
||||
$singleOptin = SingleOptin::where('email', $data['email'])
|
||||
$singleOptin = SingleOptin::query()->where('email', $data['email'])
|
||||
->whereHas('brand', function ($q) use ($brand) {
|
||||
$q->where('brands.id', '=', $brand->id);
|
||||
})->first();
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ class SingleOptinTest extends ApiBase
|
|||
$json = $response->decodeResponseJson();
|
||||
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
|
||||
$this->assertTrue(
|
||||
SingleOptin::where('email', 'singleoptin@hotmail.fr')->exists(),
|
||||
SingleOptin::query()->where('email', 'singleoptin@hotmail.fr')->exists(),
|
||||
'Single optin should have been set for email "singleoptin@hotmail.fr"'
|
||||
);
|
||||
}
|
||||
|
|
@ -54,7 +54,7 @@ class SingleOptinTest extends ApiBase
|
|||
$json = $response->decodeResponseJson();
|
||||
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
|
||||
$this->assertFalse(
|
||||
SingleOptin::where('email', 'singleoptin@hotmail.fr')->exists(),
|
||||
SingleOptin::query()->where('email', 'singleoptin@hotmail.fr')->exists(),
|
||||
'Single optin should have been removed for email "singleoptin@hotmail.fr"'
|
||||
);
|
||||
}
|
||||
|
|
@ -71,7 +71,7 @@ class SingleOptinTest extends ApiBase
|
|||
$this->storeCustomer($service);
|
||||
|
||||
// * Make sure a single optin exists.
|
||||
$singleOptin = SingleOptin::whereEmail('singleoptin@hotmail.fr')->firstOrNew();
|
||||
$singleOptin = SingleOptin::query()->where('email', 'singleoptin@hotmail.fr')->firstOrNew();
|
||||
$singleOptin->brand()->associate($service->brand);
|
||||
$singleOptin->email = 'singleoptin@hotmail.fr';
|
||||
$singleOptin->saveOrFail();
|
||||
|
|
|
|||
|
|
@ -507,7 +507,7 @@ class CrmClientV2CustomerTest extends CrmClientTester
|
|||
|
||||
// * Delete Single optin if exists.
|
||||
/** @var \App\Models\SingleOptin|null $singleOptin */
|
||||
$singleOptin = SingleOptin::where('email', $data['email'])
|
||||
$singleOptin = SingleOptin::query()->where('email', $data['email'])
|
||||
->whereHas('brand', function ($q) use ($brand) {
|
||||
$q->where('brands.id', '=', $brand->id);
|
||||
})->first();
|
||||
|
|
|
|||
|
|
@ -24,14 +24,14 @@ class CrmClientV2SingleOptinTest extends CrmClientTester
|
|||
{
|
||||
// Set single optin and check.
|
||||
$this->assertTrue(SingleOptin::setStatus('singleoptinrandom@gmail.com', true));
|
||||
$this->assertTrue(ModelsSingleOptin::where('email', 'singleoptinrandom@gmail.com')->exists());
|
||||
$this->assertTrue(ModelsSingleOptin::query()->where('email', 'singleoptinrandom@gmail.com')->exists());
|
||||
|
||||
// Set single optin again should not fail.
|
||||
$this->assertTrue(CrmApiV2::setStatus('singleoptinrandom@gmail.com', true));
|
||||
|
||||
// Remove single optin test and check.
|
||||
$this->assertTrue(SingleOptin::setStatus('singleoptinrandom@gmail.com', false));
|
||||
$this->assertTrue(!ModelsSingleOptin::where('email', 'singleoptinrandom@gmail.com')->exists());
|
||||
$this->assertTrue(!ModelsSingleOptin::query()->where('email', 'singleoptinrandom@gmail.com')->exists());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue