From 7d97dde354d200da694ae162cb6cc39e5754fcea Mon Sep 17 00:00:00 2001 From: Fedor Date: Wed, 30 Oct 2024 16:50:48 +0700 Subject: [PATCH] task-132985 minor fixes --- .../Commands/DispatchResumeLiftJobCommand.php | 19 +++-- .../Commands/DispatchVacancyLiftJobCommand.php | 16 ++-- app/Events/MessageSended.php | 20 +++++ app/Http/Controllers/EmployerController.php | 2 +- app/Http/Controllers/WorkerController.php | 5 +- app/Listeners/MessageAutoresponder.php | 70 ++++++++++++++++ app/Models/Message.php | 6 +- app/Observers/MessageObserver.php | 88 -------------------- app/Providers/EventServiceProvider.php | 4 +- public/css/style_may2024.css | 2 + .../views/employers/vacancy_autolift.blade.php | 2 +- resources/views/workers/resume_autolift.blade.php | 1 - 12 files changed, 127 insertions(+), 108 deletions(-) create mode 100644 app/Events/MessageSended.php create mode 100644 app/Listeners/MessageAutoresponder.php delete mode 100644 app/Observers/MessageObserver.php diff --git a/app/Console/Commands/DispatchResumeLiftJobCommand.php b/app/Console/Commands/DispatchResumeLiftJobCommand.php index 9fdeb09..9ae6992 100644 --- a/app/Console/Commands/DispatchResumeLiftJobCommand.php +++ b/app/Console/Commands/DispatchResumeLiftJobCommand.php @@ -16,25 +16,30 @@ class DispatchResumeLiftJobCommand extends Command { $now = now()->timezone('Europe/Moscow')->format('H:i'); - $employers = WorkerAutoliftOption::query() + $workers = WorkerAutoliftOption::query() ->where(function ($query) use ($now) { $query->where('times_per_day', 1) ->where('time_send_first', $now); }) ->orWhere(function ($query) use ($now) { $query->where('times_per_day', 2) - ->where('time_send_first', $now) - ->where('time_send_second', $now); + ->where(function ($query) use ($now) { + $query->where('time_send_first', $now) + ->orWhere('time_send_second', $now); + }); }) ->orWhere(function ($query) use ($now) { $query->where('times_per_day', 3) - ->where('time_send_first', $now) - ->where('time_send_second', $now) - ->where('time_send_third', $now); + ->where(function ($query) use ($now) { + $query->where('time_send_first', $now) + ->orWhere('time_send_second', $now) + ->orWhere('time_send_third', $now); + }); + }) ->get(); - LiftVacancyJob::dispatch($employers->pluck('employer_id')->toArray()); + LiftVacancyJob::dispatch($workers->pluck('worker_id')->toArray()); return Command::SUCCESS; } diff --git a/app/Console/Commands/DispatchVacancyLiftJobCommand.php b/app/Console/Commands/DispatchVacancyLiftJobCommand.php index bc9b1eb..617f49d 100644 --- a/app/Console/Commands/DispatchVacancyLiftJobCommand.php +++ b/app/Console/Commands/DispatchVacancyLiftJobCommand.php @@ -5,6 +5,7 @@ namespace App\Console\Commands; use App\Jobs\LiftVacancyJob; use App\Jobs\SendVacancyToTelegramJob; use App\Models\EmployerAutoliftOption; +use App\Models\WorkerAutoliftOption; use Illuminate\Console\Command; class DispatchVacancyLiftJobCommand extends Command @@ -22,14 +23,19 @@ class DispatchVacancyLiftJobCommand extends Command }) ->orWhere(function ($query) use ($now) { $query->where('times_per_day', 2) - ->where('time_send_first', $now) - ->where('time_send_second', $now); + ->where(function ($query) use ($now) { + $query->where('time_send_first', $now) + ->orWhere('time_send_second', $now); + }); }) ->orWhere(function ($query) use ($now) { $query->where('times_per_day', 3) - ->where('time_send_first', $now) - ->where('time_send_second', $now) - ->where('time_send_third', $now); + ->where(function ($query) use ($now) { + $query->where('time_send_first', $now) + ->orWhere('time_send_second', $now) + ->orWhere('time_send_third', $now); + }); + }) ->orWhere('time_send_tg', $now) ->get(); diff --git a/app/Events/MessageSended.php b/app/Events/MessageSended.php new file mode 100644 index 0000000..5832990 --- /dev/null +++ b/app/Events/MessageSended.php @@ -0,0 +1,20 @@ +message = $message; + } +} diff --git a/app/Http/Controllers/EmployerController.php b/app/Http/Controllers/EmployerController.php index 67ef335..df63ffd 100644 --- a/app/Http/Controllers/EmployerController.php +++ b/app/Http/Controllers/EmployerController.php @@ -811,7 +811,7 @@ class EmployerController extends Controller 'employer_id' => $employer->id, ], [ - 'is_enabled' => $request->get('is_enabled'), + 'is_enabled' => $request->get('is_enabled') === 'on', 'times_per_day' => $request->get('times_per_day'), 'days_repeat' => $request->get('days_repeat'), 'time_send_first' => $request->get('time_send_first'), diff --git a/app/Http/Controllers/WorkerController.php b/app/Http/Controllers/WorkerController.php index ad3f3be..4c000f4 100644 --- a/app/Http/Controllers/WorkerController.php +++ b/app/Http/Controllers/WorkerController.php @@ -1023,8 +1023,7 @@ class WorkerController extends Controller $chat = Chat::where('user_id', $user1) ->where('to_user_id', $user2) ->where('is_removed', 0) - ->first() - ; + ->first(); if ($chat->id){ return redirect()->route('worker.dialog', ['chat' => $chat->id])->withErrors($validator); @@ -1034,8 +1033,8 @@ class WorkerController extends Controller } else { $new_message = Message::add_message($request, $user1, $user2, $request->all(), file_store_path: 'messages'); + //dd('new message', $new_message); return redirect()->route('worker.dialog', ['chat' => $new_message->chat_id_from]); - } } diff --git a/app/Listeners/MessageAutoresponder.php b/app/Listeners/MessageAutoresponder.php new file mode 100644 index 0000000..7c97781 --- /dev/null +++ b/app/Listeners/MessageAutoresponder.php @@ -0,0 +1,70 @@ +message; + + /** @var User $user */ + $user = User::find($message->to_user_id); + + if ($user === null || !$user->autoresponder) { + return; + } + + $recentAutoresponderMessage = Message::query() + ->where('user_id', $message->to_user_id) + ->where('to_user_id', $message->user_id) + ->where('text', $user->autoresponder_message) + ->where('created_at', '>', now()->subDays(4)) + ->orWhereExists(function (Builder $query) use ($message) { + $query->from('messages', 'm2') + ->where('m2.created_at', '>', now()->subMinutes(5)) + ->where('m2.chat_id_from', $message->chat_id_to); + }) + ->orderBy('id', 'desc') + ->first(); + + if ($recentAutoresponderMessage !== null) { + return; + } + + Message::add_message( + request: new Request(), + user_id: $message->to_user_id, + to_user_id: $message->user_id, + message_params: [ + 'text' => $user->autoresponder_message, + 'flag_new' => 1 + ] + ); + //dump('after listener'); + } +} diff --git a/app/Models/Message.php b/app/Models/Message.php index 46b5bfb..bd03349 100644 --- a/app/Models/Message.php +++ b/app/Models/Message.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Events\MessageSended; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Http\Request; @@ -81,15 +82,18 @@ class Message extends Model $message_params['chat_id_to'] = $chat_to->id; } + //dump('before creation '.$message_params['text']); $new_message = Message::create($message_params); + //dump('after creation '.$new_message->id, [$new_message->id , $chat_form->last_message_id, $chat_to->last_message_id]); - if (!empty($new_message->id)) { + if ($new_message->id > $chat_form->last_message_id) { $chat_form->update(['last_message_date' => date("Y-m-d H:i:s"), 'last_message_id' => $new_message->id]); if (!$is_admin_chat) { $chat_to->update(['last_message_date' => date("Y-m-d H:i:s"), 'last_message_id' => $new_message->id]); } } + MessageSended::dispatch($new_message); return $new_message ?? false; } diff --git a/app/Observers/MessageObserver.php b/app/Observers/MessageObserver.php deleted file mode 100644 index c0f6813..0000000 --- a/app/Observers/MessageObserver.php +++ /dev/null @@ -1,88 +0,0 @@ -to_user_id); - - if ($user === null || !$user->autoresponder) { - return; - } - - $recentAutoresponderMessage = Message::query() - ->where('user_id', $message->to_user_id) - ->where('to_user_id', $message->user_id) - ->where('text', $user->autoresponder_message) - ->where('created_at', '>', now()->subDays(4)) - ->orderBy('id', 'desc') - ->first(); - - if ($recentAutoresponderMessage !== null) { - return; - } - - Message::add_message( - request: new Request(), - user_id: $message->to_user_id, - to_user_id: $message->user_id, - message_params: [ - 'text' => $user->autoresponder_message, - 'flag_new' => 1 - ] - ); - } - - /** - * Handle the Message "updated" event. - * - * @param \App\Models\Message $message - * @return void - */ - public function updated(Message $message) - { - // - } - - /** - * Handle the Message "deleted" event. - * - * @param \App\Models\Message $message - * @return void - */ - public function deleted(Message $message) - { - // - } - - /** - * Handle the Message "restored" event. - * - * @param \App\Models\Message $message - * @return void - */ - public function restored(Message $message) - { - // - } - - /** - * Handle the Message "force deleted" event. - * - * @param \App\Models\Message $message - * @return void - */ - public function forceDeleted(Message $message) - { - // - } -} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 4d512c9..38b825e 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -2,6 +2,8 @@ namespace App\Providers; +use App\Events\MessageSended; +use App\Listeners\MessageAutoresponder; use App\Models\Message; use App\Observers\MessageObserver; use Illuminate\Auth\Events\Registered; @@ -29,7 +31,7 @@ class EventServiceProvider extends ServiceProvider */ public function boot() { - Message::observe(MessageObserver::class); + Event::listen(MessageSended::class, MessageAutoresponder::class); } /** diff --git a/public/css/style_may2024.css b/public/css/style_may2024.css index 3720ef0..aea7f9a 100644 --- a/public/css/style_may2024.css +++ b/public/css/style_may2024.css @@ -9766,3 +9766,5 @@ nav .drop-down.closed { .cabinet__inputs-item:has(.input-picker:disabled) { display: none; } + +.select select {display: none;} diff --git a/resources/views/employers/vacancy_autolift.blade.php b/resources/views/employers/vacancy_autolift.blade.php index cf88b4c..610d887 100644 --- a/resources/views/employers/vacancy_autolift.blade.php +++ b/resources/views/employers/vacancy_autolift.blade.php @@ -3,7 +3,7 @@