diff --git a/app/Console/Commands/CreateAdminChat.php b/app/Console/Commands/CreateAdminChat.php new file mode 100644 index 0000000..18a116c --- /dev/null +++ b/app/Console/Commands/CreateAdminChat.php @@ -0,0 +1,64 @@ +id)->get(); + $chats = Chat::where('is_admin_chat', 1) + ->get(); + + foreach ($chats as $chat) { + $chat->delete(); + } + + $loopCount = $users->count(); + + if ($superAdmin){ + foreach ($users as $user){ + Chat::firstOrCreate([ + 'user_id' => $user->id, + 'to_user_id' => $superAdmin->id, + 'is_admin_chat' => 1, + 'is_removed' => 0, + ]); + + } + + $this->info("Admin chats created for {$loopCount} users." ); + + return Command::SUCCESS; + } + + $this->error("Admin is missing!" ); + + } + +} diff --git a/app/Console/Commands/RemoveOldUsers.php b/app/Console/Commands/RemoveOldUsers.php new file mode 100644 index 0000000..d27913b --- /dev/null +++ b/app/Console/Commands/RemoveOldUsers.php @@ -0,0 +1,105 @@ +where('created_at', '<', $date) + ->pluck('id'); + + if ($users->isEmpty()) { + $this->info('No users to delete.'); + return 0; + } + + $this->info('Found ' . $users->count() . ' users to delete.'); + + $userRelatedTables = [ + 'answers', + 'chats', + 'employers', + 'group_users', + 'group_works', +// 'like_vacancy', +// 'like_worker', + 'media', + 'messages', + 'messages_requests', + 'static_workers', + 'workers', + ]; + + $workerEmployerRelatedTables = [ + 'ad_employers' => 'employer_id', + 'answers' => 'employer_id', + 'employers_mains' => 'employer_id', + 'employer_autolift_options' => 'employer_id', + 'flots' => 'employer_id', + 'place_works' => 'worker_id', + 'prev_company' => 'worker_id', + 'response_works' => 'worker_id', + 'sertifications' => 'worker_id', + 'title_workers' => 'worker_id', + 'worker_autolift_options' => 'worker_id', + ]; + + try { + DB::beginTransaction(); + + foreach ($userRelatedTables as $table) { + DB::table($table)->whereIn('user_id', $users)->delete(); + $this->info("Removed related records from table: $table"); + } + + $workerIds = DB::table('workers')->whereIn('user_id', $users)->pluck('id'); + $employerIds = DB::table('employers')->whereIn('user_id', $users)->pluck('id'); + + foreach ($workerEmployerRelatedTables as $table => $column) { + DB::table($table)->whereIn($column, str_contains($column, 'worker') ? $workerIds : $employerIds)->delete(); + $this->info("Removed related records for table: $table"); + } + foreach (['like_vacancy', 'like_worker'] as $table) { + DB::table($table)->whereIn('user_id', $users->map(fn($id) => (string)$id))->delete(); + DB::table($table)->whereIn('user_id', $users->map(fn($id) => (string)$id))->delete(); + $this->info("Removed related records from table: $table (VARCHAR user_id)"); + } + DB::table('workers')->whereIn('id', $workerIds)->delete(); + DB::table('employers')->whereIn('id', $employerIds)->delete(); + DB::table('users')->whereIn('id', $users)->delete(); + + DB::commit(); + $this->info('All related data and users have been removed successfully.'); + } catch (\Exception $e) { + DB::rollBack(); + $this->error('An error occurred: ' . $e->getMessage()); + } + + return 0; + } + +} diff --git a/app/Http/Controllers/EmployerController.php b/app/Http/Controllers/EmployerController.php index c8ecb0b..c8f18f8 100644 --- a/app/Http/Controllers/EmployerController.php +++ b/app/Http/Controllers/EmployerController.php @@ -350,12 +350,12 @@ class EmployerController extends Controller //Страницы сообщений список public function messages($type_message) { $user_id = Auth()->user()->id; - + $superAdmin =User_Model::superAdmin(); $chats = Chat::get_user_chats($user_id); + $admin_chat = Chat::get_user_admin_chat($user_id); $user_type = 'employer'; - $admin_chat = false; - return view('employers.messages', compact('chats', 'admin_chat', 'user_id', 'user_type')); + return view('employers.messages', compact('chats', 'admin_chat', 'user_id', 'user_type','superAdmin')); } // Диалог между пользователями @@ -366,6 +366,7 @@ class EmployerController extends Controller } else { $ad_employer = 0; } + $superAdmin =User_Model::superAdmin(); $sender = User_Model::query()->with('workers')->with('employers')->where('id', $chat->user_id)->first(); $companion = User_Model::query()->with('workers')->with('employers')->where('id', $chat->to_user_id)->first(); @@ -374,7 +375,7 @@ class EmployerController extends Controller Message::where('user_id', '=', $chat->to_user_id)->where('to_user_id', '=', $chat->user_id)->update(['flag_new' => 0]); - return view('employers.dialog', compact('companion', 'sender', 'ad_employer', 'Messages')); + return view('employers.dialog', compact('companion', 'sender', 'ad_employer', 'Messages','chat','superAdmin')); } public function pin_chat(Request $request){ @@ -610,13 +611,20 @@ class EmployerController extends Controller //Установка уведомлений сохранение public function save_subscribe(Request $request) { -// dd($request->all()); + $msg = $request->validate([ - 'subscribe_email' => 'required|email|min:5|max:255', - ]); + 'email' => 'required|email|min:5|max:255', + ]); + $user= Auth::user(); + + User_Model::updateOrCreate( + ['id' => $user->id], + ['subscribe_email' => $request->email, + 'subscribe' => request()->has('email_msg') && request('email_msg') === 'on' ? 1 : 0 + ] + ); return redirect()->route('employer.subscribe')->with('Вы успешно подписались на рассылку'); } - //Сбросить форму с паролем public function password_reset() { $email = Auth()->user()->email; diff --git a/app/Http/Controllers/WorkerController.php b/app/Http/Controllers/WorkerController.php index 4507a42..c0aa0cb 100644 --- a/app/Http/Controllers/WorkerController.php +++ b/app/Http/Controllers/WorkerController.php @@ -772,7 +772,11 @@ class WorkerController extends Controller 'Job_title')); } - + public function pin_chat(Request $request){ + $chat_id = $request->get('id'); + $is_fixed = $request->get('is_fixed'); + Chat::pin_chat($chat_id, $is_fixed); + } //Переписка public function dialog(Chat $chat, Request $request) { // Получение параметров. @@ -781,7 +785,7 @@ class WorkerController extends Controller } else { $ad_employer = 0; } - + $supeAdmin=User::superAdmin(); $sender = User_Model::query()->with('workers')->with('employers')->where('id', $chat->user_id)->first(); $companion = User_Model::query()->with('workers')->with('employers')->where('id', $chat->to_user_id)->first(); @@ -789,7 +793,7 @@ class WorkerController extends Controller Message::where('chat_id_to', '=', $chat->id)->update(['flag_new' => 0]); - return view('workers.dialog', compact('companion', 'sender', 'chat', 'Messages', 'ad_employer')); + return view('workers.dialog', compact('companion', 'sender', 'chat', 'Messages', 'ad_employer','supeAdmin')); } // Даунылоады @@ -889,7 +893,30 @@ class WorkerController extends Controller return redirect()->route('worker.additional_documents')->with('success', 'Успешно сохранено!'); } + public function subscribe() { + + $user= Auth::user(); + return view('workers.subcribe',compact('user')); + } + + //Установка уведомлений сохранение + public function save_subscribe(Request $request) { + $msg = $request->validate([ + 'email' => 'required|email|min:5|max:255', + ]); + + $user= Auth::user(); + + User_Model::updateOrCreate( + ['id' => $user->id], + ['subscribe_email' => $request->email, + 'subscribe' => request()->has('email_msg') && request('email_msg') === 'on' ? 1 : 0 + ] + ); + return redirect()->route('worker.subscribe')->with('Вы успешно подписались на рассылку'); + + } public function delete_add_diplom(Request $request, Worker $worker) { $infoblok_id = $request->get('infoblok_id'); diff --git a/app/Mail/SendMessage.php b/app/Mail/SendMessage.php new file mode 100644 index 0000000..37f4bb1 --- /dev/null +++ b/app/Mail/SendMessage.php @@ -0,0 +1,72 @@ +messageText = $message->text; + $this->userName = $user->name; + $this->link = route('employer.dialog', ['chat' => $message->chat_id_from]); + + } + + /** + * Get the message envelope. + * + * @return \Illuminate\Mail\Mailables\Envelope + */ + public function envelope() + { + return new Envelope( + subject: 'У вас новое личное сообщение!', + ); + } + + /** + * Get the message content definition. + * + * @return \Illuminate\Mail\Mailables\Content + */ + public function content() + { + + return new Content( + view: 'emails.message', + with: [ + 'text' => $this->messageText, + 'userName' => $this->userName, + 'link' => $this->link, + ], + ); + } + + /** + * Get the attachments for the message. + * + * @return array + */ + public function attachments() + { + return []; + } +} diff --git a/app/Mail/SendVacancyMessage.php b/app/Mail/SendVacancyMessage.php new file mode 100644 index 0000000..0fe2788 --- /dev/null +++ b/app/Mail/SendVacancyMessage.php @@ -0,0 +1,75 @@ +messageText = $message->text; + $this->userName = $user->name; + $this->vacancy = $vacancy->name; + $this->link = route('employer.dialog', ['chat' => $message->chat_id_from]); + + } + + /** + * Get the message envelope. + * + * @return \Illuminate\Mail\Mailables\Envelope + */ + public function envelope() + { + return new Envelope( + subject: 'Отклик на вашу вакансию!', + ); + } + + /** + * Get the message content definition. + * + * @return \Illuminate\Mail\Mailables\Content + */ + public function content() + { + + return new Content( + view: 'emails.message_vacancy', + with: [ + 'text' => $this->messageText, + 'userName' => $this->userName, + 'link' => $this->link, + 'vacancy' => $this->vacancy, + ], + ); + } + + /** + * Get the attachments for the message. + * + * @return array + */ + public function attachments() + { + return []; + } +} diff --git a/app/Models/Chat.php b/app/Models/Chat.php index 51b843b..e6199ce 100644 --- a/app/Models/Chat.php +++ b/app/Models/Chat.php @@ -49,6 +49,7 @@ class Chat extends Model public static function pin_chat(int $chat_id, $fixed) { + return self::where('id', '=', $chat_id) ->update([ 'is_fixed' => !empty($fixed) ? 1 : 0, @@ -88,24 +89,17 @@ class Chat extends Model { return Chat::query() ->with('last_message') - ->withCount(['admin_chat_unread_messages' => function ($query) use($user_id) { - $query->whereNull('chat_id_from')->where('to_user_id', '=', $user_id)->where('flag_new', '=', 1); + ->withCount(['unread_messages' => function ($query) use($user_id) { + $query->where('to_user_id', '=', $user_id)->where('flag_new', '=', 1); }]) - ->where('to_user_id', '=', $user_id) + ->where('user_id', '=', $user_id) ->where('is_admin_chat', 1) ->first() ; } public static function get_chat_messages(Chat $chat){ - if ($chat->is_admin_chat){ - return Message::query() - ->where('chat_id_to', $chat->id) - ->where('to_user_id', $chat->to_user_id) - ->orderBy('created_at') - ->get() - ; - } else { + return Message::query() ->where(function ($query) use ($chat) { $query->where('chat_id_from', $chat->id)->orWhere('chat_id_to', $chat->id); @@ -123,7 +117,6 @@ class Chat extends Model ->OrderBy('created_at') ->get() ; - } } } diff --git a/app/Models/Message.php b/app/Models/Message.php index bd03349..b548388 100644 --- a/app/Models/Message.php +++ b/app/Models/Message.php @@ -56,6 +56,14 @@ class Message extends Model string $file_store_path = '/', bool $is_admin_chat = false ) { + + $superAdmin =User::superAdmin(); + + if ($superAdmin && $superAdmin->id == $to_user_id ){ + + $is_admin_chat=true; + } + $message_params['user_id'] = $user_id; $message_params['to_user_id'] = $to_user_id; $files = $request->file(); @@ -66,21 +74,20 @@ class Message extends Model } $chat_form = Chat::firstOrCreate([ - 'user_id' => $is_admin_chat ? 0 : $user_id, + 'user_id' => $user_id, 'to_user_id' => $to_user_id, 'is_removed' => 0, 'is_admin_chat' => $is_admin_chat ? 1 : 0, ]); - $message_params[$is_admin_chat ? 'chat_id_to' : 'chat_id_from'] = $chat_form->id; + $message_params['chat_id_from'] = $chat_form->id; + - if (!$is_admin_chat) { $chat_to = Chat::firstOrCreate([ 'user_id' => $to_user_id, 'to_user_id' => $user_id, - 'is_removed' => 0 + 'is_removed' => 0, ]); $message_params['chat_id_to'] = $chat_to->id; - } //dump('before creation '.$message_params['text']); $new_message = Message::create($message_params); @@ -89,9 +96,7 @@ class Message extends Model 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]); - } + $chat_to->update(['last_message_date' => date("Y-m-d H:i:s"), 'last_message_id' => $new_message->id]); } MessageSended::dispatch($new_message); diff --git a/app/Models/User.php b/app/Models/User.php index 3fcab18..a1da1a6 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -169,6 +169,14 @@ class User extends Authenticatable implements MustVerifyEmail } /** + * @param $query + * @return mixed + */ + public function scopeSuperAdmin($query) { + return $query->where('email', '=', config('admin.email'))->first(); + } + + /** * @throws JsonException */ public function getJobAttribute(): ?string diff --git a/app/Observers/MessageObserver.php b/app/Observers/MessageObserver.php new file mode 100644 index 0000000..c4299c8 --- /dev/null +++ b/app/Observers/MessageObserver.php @@ -0,0 +1,88 @@ +to_user_id; + $fromUserId = $message->user_id; + $toUser =User::query()->where('id',$toUserId)->first(); + $fromUser =User::query()->where('id',$fromUserId)->first(); + + + if ($toUser && !empty($toUser->subscribe_email) && $toUser->subscribe==1 ){ + + if ( (int) $message->ad_employer_id === 0 ){ + + Mail::to($toUser->subscribe_email)->send(new SendMessage($message,$fromUser)); + + }else{ + $vacancy=Ad_employer::find($message->ad_employer_id); + Mail::to($toUser->subscribe_email)->send(new SendVacancyMessage($message,$fromUser,$vacancy)); + + } + + } + } + + /** + * 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/Observers/UserObserver.php b/app/Observers/UserObserver.php new file mode 100644 index 0000000..3f1cf2a --- /dev/null +++ b/app/Observers/UserObserver.php @@ -0,0 +1,74 @@ + $user->id, + 'to_user_id' => $superAdmin->id, + 'is_admin_chat' => 1, + 'is_removed' => 0, + ]); + } + + } + + /** + * Handle the User "updated" event. + * + * @param \App\Models\User $user + * @return void + */ + public function updated(User $user) + { + // + } + + /** + * Handle the User "deleted" event. + * + * @param \App\Models\User $user + * @return void + */ + public function deleted(User $user) + { + // + } + + /** + * Handle the User "restored" event. + * + * @param \App\Models\User $user + * @return void + */ + public function restored(User $user) + { + // + } + + /** + * Handle the User "force deleted" event. + * + * @param \App\Models\User $user + * @return void + */ + public function forceDeleted(User $user) + { + // + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ae3ba48..99e7f13 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,10 @@ namespace App\Providers; +use App\Models\Message; +use App\Models\User; +use App\Observers\MessageObserver; +use App\Observers\UserObserver; use Carbon\Carbon; use Illuminate\Auth\Notifications\VerifyEmail; use Illuminate\Notifications\Messages\MailMessage; @@ -36,6 +40,11 @@ class AppServiceProvider extends ServiceProvider ->subject('Подтвердите ваш адрес электронной почты!') ->view('emails.send_verify', ['url' => $verifyUrl]); }); + + Message::observe(MessageObserver::class); + User::observe(UserObserver::class); + + } } diff --git a/config/admin.php b/config/admin.php new file mode 100644 index 0000000..5f8f1eb --- /dev/null +++ b/config/admin.php @@ -0,0 +1,4 @@ + env('EMAIL_ADMIN'), +]; diff --git a/resources/views/chats/chats_list.blade.php b/resources/views/chats/chats_list.blade.php index 2808a64..f9ed4bd 100644 --- a/resources/views/chats/chats_list.blade.php +++ b/resources/views/chats/chats_list.blade.php @@ -10,7 +10,7 @@ Администратор сайта
У вас новое личное сообщение!
+ ++ Сообщение от {{$userName}}: +
+ + + {{ $text }} + + + + Ответить на сообщение + +Отклик на вашу вакансию!
+ ++ {{$userName}} откликнулся на вакансию "{{$vacancy}}" +
+ + + {{ $text }} + + + + Ответить на сообщение + +