Refactor ShiftForm to streamline field definitions and implement unique code generation for shifts based on their names. Update ShiftFactory to utilize the new code generation method.

This commit is contained in:
Mekan1206
2026-08-02 20:17:16 +05:00
parent e3ec83686c
commit 28cbef8eb5
4 changed files with 99 additions and 23 deletions

View File

@@ -14,15 +14,6 @@ class ShiftForm
{
return $schema
->components([
TextInput::make('name')
->label(__('hr.fields.name'))
->required()
->maxLength(255),
TextInput::make('code')
->label(__('hr.fields.code'))
->required()
->maxLength(50)
->unique(ignoreRecord: true),
TimePicker::make('starts_at')
->label(__('hr.fields.starts_at'))
->required()
@@ -31,10 +22,16 @@ class ShiftForm
->label(__('hr.fields.ends_at'))
->required()
->seconds(false),
TextInput::make('name')
->label(__('hr.fields.name'))
->required()
->maxLength(255),
Textarea::make('description')
->label(__('hr.fields.description'))
->rows(3)
->columnSpanFull(),
->rows(3),
Toggle::make('is_active')
->label(__('hr.fields.active'))
->default(true)

View File

@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
class Shift extends Model
{
@@ -34,6 +35,38 @@ class Shift extends Model
];
}
protected static function booted(): void
{
static::saving(function (Shift $shift): void {
if (blank($shift->code) && filled($shift->name)) {
$shift->code = static::generateUniqueCodeFromName(
$shift->name,
$shift->id,
);
}
});
}
public static function generateUniqueCodeFromName(string $name, ?int $ignoreId = null): string
{
$base = Str::upper(Str::slug($name));
$query = static::query()
->when($ignoreId, fn ($query) => $query->where('id', '!=', $ignoreId));
if (! $query->clone()->where('code', $base)->exists()) {
return $base;
}
$maxSuffix = $query->clone()
->where('code', 'like', "{$base}-%")
->pluck('code')
->map(fn (string $code): int => (int) Str::after($code, "{$base}-"))
->max();
return "{$base}-".(($maxSuffix ?? 0) + 1);
}
public function employees(): HasMany
{
return $this->hasMany(Employee::class);

View File

@@ -27,14 +27,16 @@ class ShiftFactory extends Factory
default => ['starts_at' => '22:00:00', 'ends_at' => '06:00:00'],
};
return [
'name' => match ($code) {
$name = match ($code) {
'A' => 'Morning Shift',
'B' => 'Afternoon Shift',
'C' => 'Evening Shift',
default => 'Night Shift',
},
'code' => $code.'-'.fake()->unique()->numerify('###'),
};
return [
'name' => $name,
'code' => Shift::generateUniqueCodeFromName($name),
'starts_at' => $times['starts_at'],
'ends_at' => $times['ends_at'],
'description' => fake()->optional()->sentence(),

View File

@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
use App\Models\Shift;
it('generates an uppercase slug code from name', function (): void {
expect(Shift::generateUniqueCodeFromName('Morning Shift'))
->toBe('MORNING-SHIFT');
});
it('generates code on save when code is blank', function (): void {
$shift = Shift::factory()->create([
'name' => 'Morning Shift',
'code' => '',
]);
expect($shift->code)->toBe('MORNING-SHIFT');
});
it('does not overwrite an existing code on update', function (): void {
$shift = Shift::factory()->create([
'name' => 'Morning Shift',
'code' => 'MS',
]);
$shift->update(['name' => 'Early Shift']);
expect($shift->fresh()->code)->toBe('MS');
});
it('appends a suffix when the generated code already exists', function (): void {
Shift::factory()->create([
'name' => 'Morning Team',
'code' => 'MORNING',
]);
$shift = Shift::factory()->create([
'name' => 'Morning',
'code' => '',
]);
expect($shift->code)->toBe('MORNING-1');
});