++ fix: use months for assignment PDF link TTL instead of hours
This commit is contained in:
@@ -4,6 +4,8 @@ namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Assegnazione extends Model
|
||||
{
|
||||
@@ -17,6 +19,7 @@ class Assegnazione extends Model
|
||||
'returned_at',
|
||||
'counted_in_campaign',
|
||||
'campaign_id',
|
||||
'pdf_access_code',
|
||||
'note',
|
||||
'created_by',
|
||||
'returned_by',
|
||||
@@ -79,6 +82,39 @@ class Assegnazione extends Model
|
||||
return is_null($this->returned_at);
|
||||
}
|
||||
|
||||
public function ensurePdfAccessCode(): string
|
||||
{
|
||||
if ($this->pdf_access_code) {
|
||||
return $this->pdf_access_code;
|
||||
}
|
||||
|
||||
do {
|
||||
$code = strtoupper(Str::random(12));
|
||||
} while (static::query()->where('pdf_access_code', $code)->exists());
|
||||
|
||||
$this->forceFill(['pdf_access_code' => $code])->saveQuietly();
|
||||
|
||||
return $code;
|
||||
}
|
||||
|
||||
public function temporaryPdfViewerUrl(): ?string
|
||||
{
|
||||
if (! $this->is_aperta || ! $this->territorio?->pdf_path) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$months = max(1, (int) Setting::getValue('assignment_link_ttl_hours', 1));
|
||||
|
||||
return URL::temporarySignedRoute(
|
||||
'assignments.pdf.viewer',
|
||||
now()->addMonths($months),
|
||||
[
|
||||
'assignment' => $this->id,
|
||||
'code' => $this->ensurePdfAccessCode(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
// ─── Scopes ─────────────────────────────────────────────────
|
||||
|
||||
public function scopeAperte($query)
|
||||
|
||||
@@ -6,6 +6,8 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Setting extends Model
|
||||
{
|
||||
protected static ?self $cachedInstance = null;
|
||||
|
||||
protected $fillable = [
|
||||
'congregazione_nome',
|
||||
'logo_path',
|
||||
@@ -13,6 +15,7 @@ class Setting extends Model
|
||||
'giorni_giacenza_prioritari',
|
||||
'giorni_per_smarrito',
|
||||
'home_limit_list',
|
||||
'assignment_link_ttl_hours',
|
||||
'audit_retention_days',
|
||||
'setup_completed',
|
||||
];
|
||||
@@ -25,22 +28,41 @@ class Setting extends Model
|
||||
'giorni_giacenza_prioritari' => 'integer',
|
||||
'giorni_per_smarrito' => 'integer',
|
||||
'home_limit_list' => 'integer',
|
||||
'assignment_link_ttl_hours' => 'integer',
|
||||
'audit_retention_days' => 'integer',
|
||||
];
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saved(function (): void {
|
||||
static::$cachedInstance = null;
|
||||
});
|
||||
|
||||
static::deleted(function (): void {
|
||||
static::$cachedInstance = null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the singleton settings instance (first row).
|
||||
*/
|
||||
public static function instance(): static
|
||||
{
|
||||
return static::firstOrCreate([], [
|
||||
if (static::$cachedInstance instanceof static) {
|
||||
return static::$cachedInstance;
|
||||
}
|
||||
|
||||
static::$cachedInstance = static::firstOrCreate([], [
|
||||
'giorni_giacenza_da_assegnare' => 120,
|
||||
'giorni_giacenza_prioritari' => 180,
|
||||
'giorni_per_smarrito' => 120,
|
||||
'home_limit_list' => 10,
|
||||
'assignment_link_ttl_hours' => 1,
|
||||
'audit_retention_days' => 730,
|
||||
]);
|
||||
|
||||
return static::$cachedInstance;
|
||||
}
|
||||
|
||||
public static function isSetupComplete(): bool
|
||||
|
||||
@@ -107,11 +107,11 @@ class Territorio extends Model
|
||||
$ultima = $this->ultimaAssegnazione;
|
||||
|
||||
if ($ultima && $ultima->returned_at) {
|
||||
return Carbon::parse($ultima->returned_at)->diffInDays(now());
|
||||
return Carbon::parse($ultima->returned_at)->startOfDay()->diffInDays(today());
|
||||
}
|
||||
|
||||
if (!$ultima) {
|
||||
return $this->created_at->diffInDays(now());
|
||||
return $this->created_at->startOfDay()->diffInDays(today());
|
||||
}
|
||||
|
||||
// Currently assigned, no giacenza concept
|
||||
|
||||
Reference in New Issue
Block a user