Use created_by instead of user_id

This commit is contained in:
Marcus Moore 2024-10-23 16:40:03 -07:00
parent 84f6638f50
commit 7238238d1f
No known key found for this signature in database
7 changed files with 12 additions and 10 deletions

View file

@ -18,9 +18,9 @@ class ReportTemplate extends Model
]; ];
protected $fillable = [ protected $fillable = [
'created_by',
'name', 'name',
'options', 'options',
'user_id',
]; ];
protected $rules = [ protected $rules = [
@ -35,7 +35,7 @@ class ReportTemplate extends Model
// Scope to current user // Scope to current user
static::addGlobalScope('current_user', function (Builder $builder) { static::addGlobalScope('current_user', function (Builder $builder) {
if (auth()->check()) { if (auth()->check()) {
$builder->where('user_id', auth()->id()); $builder->where('created_by', auth()->id());
} }
}); });
} }
@ -44,9 +44,9 @@ class ReportTemplate extends Model
* Establishes the report template -> creator relationship. * Establishes the report template -> creator relationship.
* *
*/ */
public function user(): BelongsTo public function creator(): BelongsTo
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class, 'created_by');
} }
/** /**

View file

@ -367,7 +367,7 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
*/ */
public function reportTemplates(): HasMany public function reportTemplates(): HasMany
{ {
return $this->hasMany(ReportTemplate::class); return $this->hasMany(ReportTemplate::class, 'created_by');
} }
/** /**

View file

@ -2,6 +2,7 @@
namespace Database\Factories; namespace Database\Factories;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
class ReportTemplateFactory extends Factory class ReportTemplateFactory extends Factory
@ -18,6 +19,7 @@ class ReportTemplateFactory extends Factory
'options' => [ 'options' => [
'id' => '1', 'id' => '1',
], ],
'created_by' => User::factory(),
]; ];
} }
} }

View file

@ -15,11 +15,11 @@ class CreateReportTemplatesTable extends Migration
{ {
Schema::create('report_templates', function (Blueprint $table) { Schema::create('report_templates', function (Blueprint $table) {
$table->id(); $table->id();
$table->integer('user_id')->nullable(); $table->integer('created_by')->nullable();
$table->string('name'); $table->string('name');
$table->json('options'); $table->json('options');
$table->timestamps(); $table->timestamps();
$table->index('user_id'); $table->index('created_by');
}); });
} }

View file

@ -35,7 +35,7 @@ class DeleteReportTemplateTest extends TestCase implements TestsPermissionsRequi
public function testCanDeleteAReportTemplate() public function testCanDeleteAReportTemplate()
{ {
$user = User::factory()->canViewReports()->create(); $user = User::factory()->canViewReports()->create();
$reportTemplate = ReportTemplate::factory()->for($user)->create(); $reportTemplate = ReportTemplate::factory()->for($user, 'creator')->create();
$this->actingAs($user) $this->actingAs($user)
->delete(route('report-templates.destroy', $reportTemplate)) ->delete(route('report-templates.destroy', $reportTemplate))

View file

@ -32,7 +32,7 @@ class EditReportTemplateTest extends TestCase implements TestsPermissionsRequire
public function testCanLoadEditReportTemplatePage() public function testCanLoadEditReportTemplatePage()
{ {
$user = User::factory()->canViewReports()->create(); $user = User::factory()->canViewReports()->create();
$reportTemplate = ReportTemplate::factory()->for($user)->create(); $reportTemplate = ReportTemplate::factory()->for($user, 'creator')->create();
$this->actingAs($user) $this->actingAs($user)
->get(route('report-templates.edit', $reportTemplate)) ->get(route('report-templates.edit', $reportTemplate))

View file

@ -28,7 +28,7 @@ class UpdateReportTemplateTest extends TestCase implements TestsPermissionsRequi
{ {
$user = User::factory()->canViewReports()->create(); $user = User::factory()->canViewReports()->create();
$reportTemplate = ReportTemplate::factory()->for($user)->create([ $reportTemplate = ReportTemplate::factory()->for($user, 'creator')->create([
'options' => [ 'options' => [
'id' => 1, 'id' => 1,
'category' => 1, 'category' => 1,