mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-10-08 05:43:18 +01:00
- Rename the audit actor columns user_type/user_id to actor_type/actor_id - Add owner_organization_id and owner_user_id to the audits table as foreign keys with cascade on delete, so that the audits of an organization or user are deleted together with it. The indexes and foreign keys are created without blocking writes on the large table. - Fill the owner columns for new audits via CustomAuditable. Organizations and users no longer record their own deletion audit, since it would reference the already deleted owner.
94 lines
2.4 KiB
PHP
94 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Concerns\CustomAuditable;
|
|
use App\Models\Concerns\HasUuids;
|
|
use Database\Factories\ProjectMemberFactory;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Support\Carbon;
|
|
use OwenIt\Auditing\Contracts\Auditable as AuditableContract;
|
|
|
|
/**
|
|
* @property string $id
|
|
* @property int|null $billable_rate
|
|
* @property string $project_id Project ID
|
|
* @property string $member_id Member ID
|
|
* @property string $user_id User ID (legacy)
|
|
* @property Carbon|null $created_at
|
|
* @property Carbon|null $updated_at
|
|
* @property-read Project $project
|
|
* @property-read Member $member
|
|
* @property-read User $user
|
|
*
|
|
* @method static Builder<ProjectMember> whereBelongsToOrganization(Organization $organization)
|
|
* @method static ProjectMemberFactory factory()
|
|
*/
|
|
class ProjectMember extends Model implements AuditableContract
|
|
{
|
|
use CustomAuditable;
|
|
|
|
/** @use HasFactory<ProjectMemberFactory> */
|
|
use HasFactory;
|
|
|
|
use HasUuids;
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'billable_rate' => 'int',
|
|
];
|
|
|
|
/**
|
|
* @return BelongsTo<Project, $this>
|
|
*/
|
|
public function project(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Project::class, 'project_id');
|
|
}
|
|
|
|
/**
|
|
* @deprecated Use member relationship instead
|
|
*
|
|
* @return BelongsTo<User, $this>
|
|
*/
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<Member, $this>
|
|
*/
|
|
public function member(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Member::class, 'member_id');
|
|
}
|
|
|
|
/**
|
|
* @param Builder<ProjectMember> $builder
|
|
*/
|
|
public function scopeWhereBelongsToOrganization(Builder $builder, Organization $organization): void
|
|
{
|
|
$builder->whereHas('project', static function (Builder $query) use ($organization): void {
|
|
$query->whereBelongsTo($organization, 'organization');
|
|
});
|
|
}
|
|
|
|
public function getAuditOwnerOrganizationId(): ?string
|
|
{
|
|
/** @var string|null $organizationId */
|
|
$organizationId = Project::query()->whereKey($this->project_id)->value('organization_id');
|
|
|
|
return $organizationId;
|
|
}
|
|
}
|