ldc-crm/app/Models/Brand.php

236 lines
6.7 KiB
PHP
Raw Permalink Normal View History

2020-12-01 17:02:10 +01:00
<?php
namespace App\Models;
2025-08-04 14:50:03 +02:00
use App\Enums\Users\RoleEnum;
2023-10-06 15:00:42 +02:00
use App\Traits\Models\ActivityLog;
2020-12-01 17:02:10 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2025-08-04 14:50:03 +02:00
use Illuminate\Support\Facades\Cache;
use Uwil\LaravelCommon\Helpers\FileHelper;
2020-12-01 17:02:10 +01:00
/**
* @property integer $id Brand id.
* @property string $name Brand name.
* @property string $logo Brand logo.
* @property string $url Brand url link.
* @property string $url_claim Brand associative url claim.
* @property string $domain Brand domain.
* @property-read \Illuminate\Support\Carbon $created_at Brand created date.
* @property-read \Illuminate\Support\Carbon $updated_at Brand updated date.
*
* @property-read \Illuminate\Database\Eloquent\Collection<App\Models\Service>
* $services Service HasMany relation.
* @property-read \Illuminate\Database\Eloquent\Collection<App\Models\Question>
* $questions Question HasMany relation.
* @property-read \Illuminate\Database\Eloquent\Collection<App\Models\CustomerInfo>
* $customerInfos CustomerInfo HasMany relation.
* @property-read \Illuminate\Database\Eloquent\Collection<App\Models\ServicesStat>
* $servicesStats ServicesStat HasMany relation.
* @property-read \Illuminate\Database\Eloquent\Collection<App\Models\SingleOptin>
* $singleOptins SingleOptin HasMany relation.
*/
2020-12-01 17:02:10 +01:00
class Brand extends Model
{
2023-10-06 15:00:42 +02:00
use ActivityLog;
2020-12-01 17:02:10 +01:00
use HasFactory;
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 = [
2023-09-04 13:08:34 +02:00
'name',
'logo',
'url',
'url_claim',
'domain',
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
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $model) {
$model->logo = FileHelper::storeFile($model, $model->logo, true);
});
static::created(function () {
2025-08-04 14:50:03 +02:00
self::resetCache();
});
static::updating(function (self $model) {
$model->logo = FileHelper::storeFile($model, $model->logo, true);
});
static::updated(function (self $model) {
2025-08-04 14:50:03 +02:00
self::resetCache();
FileHelper::removeOldFile($model, 'logo');
});
static::deleted(function (self $model) {
2025-08-04 14:50:03 +02:00
self::resetCache();
FileHelper::removeOldFile($model, 'logo');
2025-08-04 14:50:03 +02:00
});
}
// * METHODS
/**
* Account attribute accessor.
*
* @return integer
*/
public function getAccountsAttribute(): int
{
return $this->customerInfos()->count();
}
/**
* Total optins attribute accessor.
*
* @return integer
*/
public function getTotalOptinsAttribute(): int
{
return $this->customerInfos()->where('opt_in', 1)->count() + $this->singleOptins()->count();
}
/**
* Get all brands associated to the auth user.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function getUserAssociatedBrands(): \Illuminate\Database\Eloquent\Collection
{
/** @var \App\Models\User $userModel */
$userModel = auth('backend')->user();
$brandModels = self::getBrands();
if ($userModel->role->equals(RoleEnum::marketing)) {
$brandIds = $userModel->brands->modelKeys();
2025-08-04 14:50:03 +02:00
$brandModels = $brandModels->filter(function ($brand) use ($brandIds) {
return in_array($brand->getKey(), $brandIds);
})->values();
}
return $brandModels;
}
/**
* Reset all cache elements related to this model.
*
* @return void
*/
private static function resetCache(): void
{
Cache::forget('brands');
self::getBrands();
}
/**
* Get all brands.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function getBrands(): \Illuminate\Database\Eloquent\Collection
{
return Cache::remember('brands', 86400, function () {
return self::query()->orderBy('name', 'ASC')->get();
});
}
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
/**
* Service has many relation.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function services(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Service::class);
2020-12-01 17:02:10 +01:00
}
/**
* Question has many relation.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function questions(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Question::class);
}
/**
* CustomerInfo has many relation.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function customerInfos(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(CustomerInfo::class);
2020-12-01 17:02:10 +01:00
}
/**
* ServiceStat has many relation.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function servicesStats(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(ServiceStat::class);
2020-12-01 17:02:10 +01:00
}
2020-12-02 10:08:27 +01:00
/**
* SingleOptin has many relation.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function singleOptins(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(SingleOptin::class);
2020-12-02 10:08:27 +01:00
}
/**
* Users many to many relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function users(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(Brand::class, 'brand_users', 'brand_id', 'user_id');
}
/**
* Claim has many relation.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function claims(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Claim::class);
}
/**
* ClaimSubject has many relation.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function claimSubjects(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(ClaimSubject::class);
}
/**
* ClaimMailTemplate has many relation.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function claimMailTemplates(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(ClaimMailTemplate::class);
}
2020-12-01 17:02:10 +01:00
}