ldc-crm/app/Models/CustomerInfo.php

147 lines
4.4 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Models;
2021-03-14 10:45:42 +01:00
use App\Enums\Country;
2023-06-26 14:38:26 +02:00
use Illuminate\Database\Eloquent\Builder;
2021-03-14 10:45:42 +01:00
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property integer $id Id.
* @property integer $customer_id Foreign key Customer.
* @property integer $brand_id Foreign key Brand.
* @property string $firstname Firstname.
* @property string $lastname Lastname.
* @property \Carbon\Carbon|null $birth_date Birth_date.
* @property string|null $address Address.
* @property string|null $address2 Address2.
* @property string $phone Phone.
* @property \App\Enums\Country $country Country.
* @property string|null $city City.
* @property string|null $postcode Postcode.
* @property boolean $opt_in Opt_in.
* @property string $hashcode Hashcode.
* @property \Illuminate\Support\Carbon $created_at Created date.
* @property \Illuminate\Support\Carbon $updated_at Updated date.
*
* @property-read \App\Models\Customer $customer Customer BelongsTo relation.
* @property-read \App\Models\Brand $brand Brand BelongsTo relation.
*/
class CustomerInfo extends Model
{
use HasFactory;
2021-10-26 12:08:37 +02:00
Version 3.0.0 This removes api authentification with token in favor of signature method. Refactorize code and clean up. It add more unit test plus code coverage in test/converage. - Added config.api.timestampTimeout - Added Helper::signRequest This calculates the request signature - Added more unit tests and extend code coverage. - Added rate limiter for web routes - Added RemoveSpaces Sanitizer on api requests - Added stress test suite - Changed AuthServiceProvider api-token to api-hmac - Changed CustomerSeeder to have at least of customer activated (for unit tests) - Changed used transation for saveing multiple models - Changed, in ServiceController generateApiToken to generateServicePassword - Changed, timestamp has to be send in request with a DateTime format, it also has to a UTC value - Composer added brianium/paratest - Composer update - Fixed case where removing single optin could be the wrong one (not for the good service) - Fixed customer delete - Fixed customer would not be resolved by brand. - Fixed Really check that request is json checking body content, preventing sending malformed requests - Remove ApiError::needAuth - Removed api_token - Removed ApiController::authenticate - Removed config.api.tokenTimeout - Removed Header check or usage because of HAProxy doing weird stuff - Removed lock files from gitignore - Removed memcached dependency - Removed unused questions resources api routes - Removed workspace file Migrations - Added unique index on services.bearer_token - Changed customer_id on customer_answer is nullable - Changed services.bearer_pwd length to 312 (the size of the token) - Dropped services.api_token and service.api_token_date
2021-12-19 15:57:24 +01:00
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
Version 3.0.0 This removes api authentification with token in favor of signature method. Refactorize code and clean up. It add more unit test plus code coverage in test/converage. - Added config.api.timestampTimeout - Added Helper::signRequest This calculates the request signature - Added more unit tests and extend code coverage. - Added rate limiter for web routes - Added RemoveSpaces Sanitizer on api requests - Added stress test suite - Changed AuthServiceProvider api-token to api-hmac - Changed CustomerSeeder to have at least of customer activated (for unit tests) - Changed used transation for saveing multiple models - Changed, in ServiceController generateApiToken to generateServicePassword - Changed, timestamp has to be send in request with a DateTime format, it also has to a UTC value - Composer added brianium/paratest - Composer update - Fixed case where removing single optin could be the wrong one (not for the good service) - Fixed customer delete - Fixed customer would not be resolved by brand. - Fixed Really check that request is json checking body content, preventing sending malformed requests - Remove ApiError::needAuth - Removed api_token - Removed ApiController::authenticate - Removed config.api.tokenTimeout - Removed Header check or usage because of HAProxy doing weird stuff - Removed lock files from gitignore - Removed memcached dependency - Removed unused questions resources api routes - Removed workspace file Migrations - Added unique index on services.bearer_token - Changed customer_id on customer_answer is nullable - Changed services.bearer_pwd length to 312 (the size of the token) - Dropped services.api_token and service.api_token_date
2021-12-19 15:57:24 +01:00
*/
2021-03-14 10:45:42 +01:00
protected $fillable = [
'firstname',
'lastname',
'birth_date',
'address',
'address2',
'phone',
'country',
'city',
'postcode',
'opt_in'
];
2023-09-04 13:08:34 +02:00
/**
* The attributes that should be cast to native types.
*
* @var array<string, mixed>
2023-09-04 13:08:34 +02:00
*/
protected $casts = [
'country' => Country::class,
'opt_in' => 'bool',
2023-09-04 13:08:34 +02:00
];
2021-08-23 20:21:05 +02:00
/**
Version 3.0.0 This removes api authentification with token in favor of signature method. Refactorize code and clean up. It add more unit test plus code coverage in test/converage. - Added config.api.timestampTimeout - Added Helper::signRequest This calculates the request signature - Added more unit tests and extend code coverage. - Added rate limiter for web routes - Added RemoveSpaces Sanitizer on api requests - Added stress test suite - Changed AuthServiceProvider api-token to api-hmac - Changed CustomerSeeder to have at least of customer activated (for unit tests) - Changed used transation for saveing multiple models - Changed, in ServiceController generateApiToken to generateServicePassword - Changed, timestamp has to be send in request with a DateTime format, it also has to a UTC value - Composer added brianium/paratest - Composer update - Fixed case where removing single optin could be the wrong one (not for the good service) - Fixed customer delete - Fixed customer would not be resolved by brand. - Fixed Really check that request is json checking body content, preventing sending malformed requests - Remove ApiError::needAuth - Removed api_token - Removed ApiController::authenticate - Removed config.api.tokenTimeout - Removed Header check or usage because of HAProxy doing weird stuff - Removed lock files from gitignore - Removed memcached dependency - Removed unused questions resources api routes - Removed workspace file Migrations - Added unique index on services.bearer_token - Changed customer_id on customer_answer is nullable - Changed services.bearer_pwd length to 312 (the size of the token) - Dropped services.api_token and service.api_token_date
2021-12-19 15:57:24 +01:00
* Perform any actions required after the model boots.
2021-08-23 20:21:05 +02:00
*
* @return void
*/
protected static function booted(): void
2021-03-14 10:45:42 +01:00
{
2023-06-26 14:38:26 +02:00
static::creating(function (self $model) {
$model->hashcode = Str::uuid();
});
2023-06-26 14:38:26 +02:00
static::deleting(function (self $model) {
CustomerOptInHistory::query()->whereHas('customer', function (Builder $q) use ($model) {
$q->where('id', $model->customer->id);
})->whereHas('brand', function (Builder $q) use ($model) {
$q->where('id', $model->brand->id);
Version 3.0.0 This removes api authentification with token in favor of signature method. Refactorize code and clean up. It add more unit test plus code coverage in test/converage. - Added config.api.timestampTimeout - Added Helper::signRequest This calculates the request signature - Added more unit tests and extend code coverage. - Added rate limiter for web routes - Added RemoveSpaces Sanitizer on api requests - Added stress test suite - Changed AuthServiceProvider api-token to api-hmac - Changed CustomerSeeder to have at least of customer activated (for unit tests) - Changed used transation for saveing multiple models - Changed, in ServiceController generateApiToken to generateServicePassword - Changed, timestamp has to be send in request with a DateTime format, it also has to a UTC value - Composer added brianium/paratest - Composer update - Fixed case where removing single optin could be the wrong one (not for the good service) - Fixed customer delete - Fixed customer would not be resolved by brand. - Fixed Really check that request is json checking body content, preventing sending malformed requests - Remove ApiError::needAuth - Removed api_token - Removed ApiController::authenticate - Removed config.api.tokenTimeout - Removed Header check or usage because of HAProxy doing weird stuff - Removed lock files from gitignore - Removed memcached dependency - Removed unused questions resources api routes - Removed workspace file Migrations - Added unique index on services.bearer_token - Changed customer_id on customer_answer is nullable - Changed services.bearer_pwd length to 312 (the size of the token) - Dropped services.api_token and service.api_token_date
2021-12-19 15:57:24 +01:00
})->delete();
2021-03-14 10:45:42 +01:00
});
}
Version 3.0.0 This removes api authentification with token in favor of signature method. Refactorize code and clean up. It add more unit test plus code coverage in test/converage. - Added config.api.timestampTimeout - Added Helper::signRequest This calculates the request signature - Added more unit tests and extend code coverage. - Added rate limiter for web routes - Added RemoveSpaces Sanitizer on api requests - Added stress test suite - Changed AuthServiceProvider api-token to api-hmac - Changed CustomerSeeder to have at least of customer activated (for unit tests) - Changed used transation for saveing multiple models - Changed, in ServiceController generateApiToken to generateServicePassword - Changed, timestamp has to be send in request with a DateTime format, it also has to a UTC value - Composer added brianium/paratest - Composer update - Fixed case where removing single optin could be the wrong one (not for the good service) - Fixed customer delete - Fixed customer would not be resolved by brand. - Fixed Really check that request is json checking body content, preventing sending malformed requests - Remove ApiError::needAuth - Removed api_token - Removed ApiController::authenticate - Removed config.api.tokenTimeout - Removed Header check or usage because of HAProxy doing weird stuff - Removed lock files from gitignore - Removed memcached dependency - Removed unused questions resources api routes - Removed workspace file Migrations - Added unique index on services.bearer_token - Changed customer_id on customer_answer is nullable - Changed services.bearer_pwd length to 312 (the size of the token) - Dropped services.api_token and service.api_token_date
2021-12-19 15:57:24 +01:00
// * RELATIONS
/**
* Customer belongs to relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function customer(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Customer::class);
}
/**
* Brand belongs to relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function brand(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Brand::class);
}
// * METHODS
/**
* Get Login.
*
* @return \App\Models\CustomerLogin
*/
public function login(): \App\Models\CustomerLogin
{
return $this->customer->login($this->brand);
}
2021-03-14 10:45:42 +01:00
/**
* Get customer info from email.
2021-03-14 10:45:42 +01:00
*
2023-06-23 17:18:59 +02:00
* @param \App\Models\Brand $brand
* @param string $email
2021-03-14 10:45:42 +01:00
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function getFromEmail(Brand $brand, string $email): \Illuminate\Database\Eloquent\Builder
2021-03-14 10:45:42 +01:00
{
2023-06-26 14:38:26 +02:00
return CustomerInfo::query()->whereHas('brand', function (Builder $q) use ($brand) {
2021-03-14 10:45:42 +01:00
$q->where('id', $brand->id);
2023-06-26 14:38:26 +02:00
})->whereHas('customer', function (Builder $q) use ($email) {
2021-03-14 10:45:42 +01:00
$q->where('email', $email);
});
}
/**
* Get customer info from hashcode.
*
* @param \App\Models\Brand $brand
* @param string $hashcode
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function getFromHashcode(Brand $brand, string $hashcode): \Illuminate\Database\Eloquent\Builder
{
return CustomerInfo::query()->whereHas('brand', function (Builder $q) use ($brand) {
$q->where('id', $brand->id);
})->where('hashcode', $hashcode);
}
}