Initial scaffold : Laravel 12 + PostgreSQL + auth + domaine métier (étapes 1-5)
- Laravel 12 sur PHP 8.5, Breeze (Blade/Tailwind/Alpine.js) - Docker Compose dev (PostgreSQL 18 + Redis) et prod (stack complète + nginx) - Migrations et models : lieux, sections, dépôts, source_types/fields, sources, relevés - Colonne JSONB data sur releves avec colonnes générées indexées (nom, prenom, date_evenement) - Index GIN pour la recherche fulltext - Enums : UserRole, SourceStatus (avec transitions), CalendarType, FieldType - RoleMiddleware (alias `role`) + helpers isAdmin/isSectionManager sur User - CRUD Lieux (arbre hiérarchique, calcul nom_long en cascade) - CRUD admin : Sections (+ gestion membres), Dépôts, Types de sources (+ champs dynamiques, drag & drop) - CRUD Sources : visibilité filtrée par rôle, assignation membres, workflow de statut Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Depot extends Model
|
||||
{
|
||||
protected $fillable = ['nom', 'description', 'adresse_postale', 'url'];
|
||||
|
||||
public function sources(): HasMany
|
||||
{
|
||||
return $this->hasMany(Source::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Lieu extends Model
|
||||
{
|
||||
protected $table = 'lieux';
|
||||
|
||||
protected $fillable = ['nom', 'code', 'lieu_parent_id', 'nom_long', 'latitude', 'longitude', 'note'];
|
||||
|
||||
public function parent(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Lieu::class, 'lieu_parent_id');
|
||||
}
|
||||
|
||||
public function enfants(): HasMany
|
||||
{
|
||||
return $this->hasMany(Lieu::class, 'lieu_parent_id');
|
||||
}
|
||||
|
||||
public function sections(): HasMany
|
||||
{
|
||||
return $this->hasMany(Section::class);
|
||||
}
|
||||
|
||||
public function calculerNomLong(): string
|
||||
{
|
||||
$noms = [$this->nom];
|
||||
$parent = $this->parent;
|
||||
while ($parent) {
|
||||
$noms[] = $parent->nom;
|
||||
$parent = $parent->parent;
|
||||
}
|
||||
return implode(', ', $noms);
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
$recalc = function (Lieu $lieu) {
|
||||
$lieu->nom_long = $lieu->calculerNomLong();
|
||||
};
|
||||
|
||||
static::creating($recalc);
|
||||
static::updating($recalc);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Releve extends Model
|
||||
{
|
||||
protected $fillable = ['source_id', 'data', 'created_by', 'updated_by'];
|
||||
|
||||
protected $casts = [
|
||||
'data' => 'array',
|
||||
];
|
||||
|
||||
// Colonnes générées (read-only) : nom, prenom, date_evenement
|
||||
protected $appends = [];
|
||||
|
||||
public function source(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Source::class);
|
||||
}
|
||||
|
||||
public function createur(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'created_by');
|
||||
}
|
||||
|
||||
public function modificateur(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'updated_by');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
class Section extends Model
|
||||
{
|
||||
protected $fillable = ['nom', 'lieu_id', 'adresse', 'email_contact', 'url'];
|
||||
|
||||
public function lieu(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Lieu::class);
|
||||
}
|
||||
|
||||
public function membres(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'section_user')
|
||||
->withPivot('role_in_section');
|
||||
}
|
||||
|
||||
public function responsables(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'section_user')
|
||||
->wherePivot('role_in_section', 'section_manager')
|
||||
->withPivot('role_in_section');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\SourceStatus;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Source extends Model
|
||||
{
|
||||
protected $fillable = ['nom', 'description', 'source_type_id', 'depot_id', 'cote', 'auteur', 'status'];
|
||||
|
||||
protected $casts = [
|
||||
'status' => SourceStatus::class,
|
||||
];
|
||||
|
||||
public function sourceType(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(SourceType::class);
|
||||
}
|
||||
|
||||
public function depot(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Depot::class);
|
||||
}
|
||||
|
||||
public function membres(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'source_user');
|
||||
}
|
||||
|
||||
public function releves(): HasMany
|
||||
{
|
||||
return $this->hasMany(Releve::class);
|
||||
}
|
||||
|
||||
public function isVisibleBy(User $user): bool
|
||||
{
|
||||
if ($user->isAdmin() || $user->isSectionManager()) {
|
||||
return true;
|
||||
}
|
||||
if ($this->status === SourceStatus::Termine) {
|
||||
return true;
|
||||
}
|
||||
return $this->membres()->where('user_id', $user->id)->exists();
|
||||
}
|
||||
|
||||
public function canTransitionTo(SourceStatus $new, User $user): bool
|
||||
{
|
||||
if (! in_array($new, $this->status->transitions())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return match ($new) {
|
||||
SourceStatus::AValider => $user->isAdmin() || $user->isSectionManager()
|
||||
|| $this->membres()->where('user_id', $user->id)->exists(),
|
||||
SourceStatus::Termine, SourceStatus::EnCours => $user->isAdmin() || $user->isSectionManager(),
|
||||
default => false,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class SourceType extends Model
|
||||
{
|
||||
protected $fillable = ['nom', 'description'];
|
||||
|
||||
public function fields(): HasMany
|
||||
{
|
||||
return $this->hasMany(SourceTypeField::class)->orderBy('order');
|
||||
}
|
||||
|
||||
public function sources(): HasMany
|
||||
{
|
||||
return $this->hasMany(Source::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\FieldType;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class SourceTypeField extends Model
|
||||
{
|
||||
protected $fillable = ['source_type_id', 'name', 'label', 'type', 'required', 'order', 'options'];
|
||||
|
||||
protected $casts = [
|
||||
'type' => FieldType::class,
|
||||
'required' => 'boolean',
|
||||
'options' => 'array',
|
||||
];
|
||||
|
||||
public function sourceType(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(SourceType::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\UserRole;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
|
||||
protected $fillable = ['name', 'email', 'password', 'role'];
|
||||
|
||||
protected $hidden = ['password', 'remember_token'];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
'role' => UserRole::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function isAdmin(): bool
|
||||
{
|
||||
return $this->role === UserRole::Admin;
|
||||
}
|
||||
|
||||
public function isSectionManager(): bool
|
||||
{
|
||||
return $this->role === UserRole::SectionManager || $this->isAdmin();
|
||||
}
|
||||
|
||||
public function sections(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Section::class, 'section_user')
|
||||
->withPivot('role_in_section');
|
||||
}
|
||||
|
||||
public function sourcesAssignees(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Source::class, 'source_user');
|
||||
}
|
||||
|
||||
public function isMemberOfSection(Section $section): bool
|
||||
{
|
||||
return $this->sections()->where('section_id', $section->id)->exists();
|
||||
}
|
||||
|
||||
public function isManagerOfSection(Section $section): bool
|
||||
{
|
||||
return $this->isAdmin() || $this->sections()
|
||||
->where('section_id', $section->id)
|
||||
->wherePivot('role_in_section', 'section_manager')
|
||||
->exists();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user