Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed May 23, 2024
1 parent b9b07ba commit 568fd36
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 48 deletions.
8 changes: 3 additions & 5 deletions src/Database/Factories/DiningTableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ class DiningTableFactory extends Factory
public function definition(): array
{
return [
'table_name' => $this->faker->sentence(2),
'name' => $this->faker->sentence(2),
'min_capacity' => $this->faker->randomDigitNotNull(),
'max_capacity' => $this->faker->numberBetween(10, 99),
'extra_capacity' => $this->faker->numberBetween(1, 999),
'max_capacity' => $this->faker->numberBetween(10, 20),
'priority' => $this->faker->randomDigit(),
'is_joinable' => $this->faker->boolean(),
'table_status' => $this->faker->boolean(),
'is_enabled' => $this->faker->boolean(),
];
}
}
28 changes: 18 additions & 10 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ public function boot()
$model->relation['hasMany']['dining_areas'] = [DiningArea::class, 'delete' => true];
$model->relation['morphedByMany']['tables'] = [DiningTable::class, 'name' => 'locationable'];
});


Event::listen('igniter.reservation.statusAdded', function(Reservation $model, $statusHistory) {
if ($statusHistory->notify) {
$model->reloadRelations();
$model->mailSend('igniter.reservation::mail.reservation_update', 'customer');
}
});
}

public function registerMailTemplates(): array
Expand Down Expand Up @@ -222,32 +230,32 @@ public function registerLocationSettings()

protected function bindReservationEvent()
{
Event::listen('admin.statusHistory.beforeAddStatus', function($model, $object, $statusId, $previousStatus) {
if (!$object instanceof Reservation) {
Event::listen('admin.statusHistory.beforeAddStatus', function($statusHistory, $reservation, $statusId, $previousStatus) {
if (!$reservation instanceof Reservation) {
return;
}

if (StatusHistory::alreadyExists($object, $statusId)) {
if (StatusHistory::alreadyExists($reservation, $statusId)) {
return;
}

Event::fire('igniter.reservation.beforeAddStatus', [$model, $object, $statusId, $previousStatus], true);
Event::fire('igniter.reservation.beforeAddStatus', [$statusHistory, $reservation, $statusId, $previousStatus], true);
});

Event::listen('admin.statusHistory.added', function($model, $statusHistory) {
if (!$model instanceof Reservation) {
Event::listen('admin.statusHistory.added', function($reservation, $statusHistory) {
if (!$reservation instanceof Reservation) {
return;
}

Event::fire('igniter.reservation.statusAdded', [$model, $statusHistory], true);
Event::fire('igniter.reservation.statusAdded', [$reservation, $statusHistory], true);
});

Event::listen('admin.assignable.assigned', function($model, $assignableLog) {
if (!$model instanceof Reservation) {
Event::listen('admin.assignable.assigned', function($reservation, $assignableLog) {
if (!$reservation instanceof Reservation) {
return;
}

Event::fire('igniter.reservation.assigned', [$model, $assignableLog], true);
Event::fire('igniter.reservation.assigned', [$reservation, $assignableLog], true);
});
}

Expand Down
2 changes: 0 additions & 2 deletions src/Models/DiningTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class DiningTable extends \Igniter\Flame\Database\Model
'seat_layout' => 'array',
];

protected $guarded = [];

/**
* @var array Relations
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Observers/ReservationObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function saved(Reservation $reservation)

$reservation->reloadRelations('location');

if ($reservation->location->shouldAutoAllocateTable() && !$reservation->tables()->count()) {
if ($reservation->location?->shouldAutoAllocateTable() && !$reservation->tables()->count()) {
$reservation->assignTable();
}
}
Expand Down
12 changes: 2 additions & 10 deletions src/Requests/DiningAreaRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,14 @@ class DiningAreaRequest extends FormRequest
public function attributes()
{
return [
'table_name' => lang('igniter::admin.label_name'),
'min_capacity' => lang('igniter.reservation::default.tables.label_min_capacity'),
'max_capacity' => lang('igniter.reservation::default.tables.label_capacity'),
'extra_capacity' => lang('igniter.reservation::default.tables.label_extra_capacity'),
'priority' => lang('igniter.reservation::default.tables.label_priority'),
'is_joinable' => lang('igniter.reservation::default.tables.label_joinable'),
'table_status' => lang('igniter::admin.label_status'),
'locations' => lang('igniter::admin.label_location'),
'locations.*' => lang('igniter::admin.label_location'),
'name' => lang('igniter::admin.label_name'),
];
}

public function rules()
{
return [
['name', 'admin::lang.label_name', 'required|between:2,128'],
'name' => ['required', 'between:2,128'],
];
}
}
15 changes: 6 additions & 9 deletions src/Requests/DiningSectionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ class DiningSectionRequest extends FormRequest
public function attributes()
{
return [
'table_name' => lang('igniter::admin.label_name'),
'min_capacity' => lang('igniter.reservation::default.tables.label_min_capacity'),
'max_capacity' => lang('igniter.reservation::default.tables.label_capacity'),
'extra_capacity' => lang('igniter.reservation::default.tables.label_extra_capacity'),
'priority' => lang('igniter.reservation::default.tables.label_priority'),
'is_joinable' => lang('igniter.reservation::default.tables.label_joinable'),
'table_status' => lang('igniter::admin.label_status'),
'locations' => lang('igniter::admin.label_location'),
'locations.*' => lang('igniter::admin.label_location'),
'location_id' => lang('igniter::admin.label_location'),
'name' => lang('igniter::admin.label_name'),
'priority' => lang('igniter.reservation::default.dining_tables.label_priority'),
'description' => lang('igniter::admin.label_description'),
'is_enabled' => lang('igniter.reservation::default.dining_tables.label_is_enabled'),
];
}

Expand All @@ -28,6 +24,7 @@ public function rules()
'name' => ['required', 'string'],
'priority' => ['required', 'integer'],
'description' => ['string'],
'is_enabled' => ['is_enabled', 'boolean'],
'color' => ['nullable', 'string'],
];
}
Expand Down
16 changes: 8 additions & 8 deletions src/Requests/DiningTableRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class DiningTableRequest extends FormRequest
public function attributes()
{
return [
'table_name' => lang('igniter::admin.label_name'),
'name' => lang('igniter::admin.label_name'),
'shape' => lang('igniter.reservation::default.dining_tables.label_table_shape'),
'min_capacity' => lang('igniter.reservation::default.tables.label_min_capacity'),
'max_capacity' => lang('igniter.reservation::default.tables.label_capacity'),
'extra_capacity' => lang('igniter.reservation::default.tables.label_extra_capacity'),
'priority' => lang('igniter.reservation::default.tables.label_priority'),
'is_joinable' => lang('igniter.reservation::default.tables.label_joinable'),
'table_status' => lang('igniter::admin.label_status'),
'locations' => lang('igniter::admin.label_location'),
'locations.*' => lang('igniter::admin.label_location'),
'is_enabled' => lang('igniter::admin.label_status'),
'dining_area_id' => lang('igniter.reservation::default.dining_tables.label_dining_areas'),
'dining_section_id' => lang('igniter.reservation::default.dining_tables.column_section'),
];
}

Expand All @@ -28,9 +28,9 @@ public function rules()
'shape' => ['required', 'in:rectangle,round'],
'min_capacity' => ['required', 'integer', 'min:1', 'lte:max_capacity'],
'max_capacity' => ['required', 'integer', 'min:1', 'gte:min_capacity'],
'extra_capacity' => ['required', 'integer'],
'priority' => ['required', 'integer'],
'is_enabled' => ['required', 'boolean'],
'extra_capacity' => ['sometimes', 'integer'],
'priority' => ['sometimes', 'integer'],
'is_enabled' => ['sometimes', 'boolean'],
'dining_area_id' => ['required', 'integer'],
'dining_section_id' => ['nullable', 'integer'],
];
Expand Down
5 changes: 2 additions & 3 deletions tests/Requests/DiningTableRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
use Igniter\Reservation\Requests\DiningTableRequest;

it('has required rule for inputs', function() {
expect('required')->toBeIn(array_get((new DiningTableRequest)->rules(), 'table_name'))
expect('required')->toBeIn(array_get((new DiningTableRequest)->rules(), 'name'))
->and('required')->toBeIn(array_get((new DiningTableRequest)->rules(), 'min_capacity'))
->and('required')->toBeIn(array_get((new DiningTableRequest)->rules(), 'max_capacity'))
->and('required')->toBeIn(array_get((new DiningTableRequest)->rules(), 'extra_capacity'))
->and('required')->toBeIn(array_get((new DiningTableRequest)->rules(), 'priority'))
->and('required')->toBeIn(array_get((new DiningTableRequest)->rules(), 'is_joinable'))
->and('required')->toBeIn(array_get((new DiningTableRequest)->rules(), 'table_status'))
->and('required')->toBeIn(array_get((new DiningTableRequest)->rules(), 'is_enabled'))
->and('required')->toBeIn(array_get((new DiningTableRequest)->rules(), 'locations'));
});

Expand Down

0 comments on commit 568fd36

Please sign in to comment.