Laravel-Backpack / community-forum

A workspace to discuss improvement and feature ideas, before they're actually implemented.
28 stars 0 forks source link

Issues on "backpack/calendar-operation" How can prevent overlap slots in each time #1032

Closed NeguinhoAdmin closed 2 weeks ago

NeguinhoAdmin commented 2 weeks ago

Issues "Calendar Operation backpack/calendar-operation"

Currently, I am trying to restrict timeslot, so same time no more than one slot can be booked, I am looking for organic or systematic way.

If no than, I need to understand where I could change calendar behaviour. So, I do customise it.

this is what i want to stop it/fix it(not more than two slots):

image

Thank you

jcastroa87 commented 2 weeks ago

Hello @NeguinhoAdmin

Did you try using a validation restriction? Something like combine date + time need to be unique.

Cheers.

ShariqAyaz commented 2 weeks ago

No I havnt, I was thinking to put constrain on database "unique together" but it is huge probability that it will accept it anyway because of minutes , seconds , or milli-second.

ShariqAyaz commented 2 weeks ago

Hi,

At least unique together works for me. So, same slot cannot be issued to someone. image

Now i need to think how to give appropriate message instead of that 500 error.

Thank you.

jcastroa87 commented 2 weeks ago

Hello @ShariqAyaz

Can you share the request validation you write, because if you write correctly there is a beautiful message, not an SQL error.

Cheers.

ShariqAyaz commented 2 weeks ago

I believe you asking this?


    public function rules()
    {
        return [
            'branch_id' => 'required|exists:branches,id',
            'title' => 'nullable|string',
            'date_of_appointment' => 'nullable|date',
            'start' => 'nullable|date',
            'end' => 'nullable|date',
            'vehicle_registration' => 'required|string',
            'vehicle_chassis' => 'nullable|string',
            'vehicle_color' => 'nullable|string',
            'all_day' => 'nullable|boolean',
            'customer_name' => 'required|string',
            'customer_contact' => 'required|string',
            'customer_email' => 'required|email',
            'notes' => 'nullable|string',
            'background_color' => 'nullable|string',
            'text_color' => 'nullable|string',
            'start' => [
                'nullable',
                'date',
                function ($attribute, $value, $fail) {
                    $end = request('end');
                    if ($value && $end) {
                        $exists = \App\Models\MOTBooking::where('start', $value)
                            ->where('end', $end)
                            ->exists();
                        if ($exists) {
                            $fail('The combination of start and end times must be unique.');
                        }
                    }
                },
            ],
        ];
    }

I have recently added this.


'start' => [
                'nullable',
                'date',
                function ($attribute, $value, $fail) {
                    $end = request('end');
                    if ($value && $end) {
                        $exists = \App\Models\MOTBooking::where('start', $value)
                            ->where('end', $end)
                            ->exists();
                        if ($exists) {
                            $fail('The combination of start and end times must be unique.');
                        }
                    }
                },
            ],
ShariqAyaz commented 2 weeks ago

Hello @ShariqAyaz

Can you share the request validation you write, because if you write correctly there is a beautiful message, not an SQL error.

Cheers.

Done ...! Cheers

jcastroa87 commented 2 weeks ago

in your code you have "start" rule declare two times, i think laravel ignore the second declaration.

Try to fix it please.

Cheers.

jcastroa87 commented 2 weeks ago

Did you try it?

Cheers.

NeguinhoAdmin commented 2 weeks ago

yes, it is tested working fine now.

jcastroa87 commented 2 weeks ago

Im glad the issue is solve. I will close the issue, but please feel free to re-open or create a new one if needed.

Cheers.