diff --git a/app/Http/Controllers/NotificationController.php b/app/Http/Controllers/NotificationController.php new file mode 100644 index 000000000..19cc477ea --- /dev/null +++ b/app/Http/Controllers/NotificationController.php @@ -0,0 +1,24 @@ +>>>>>> Stashed changes + +class NotificationController extends Controller +{ + public function markAsRead($id) + { + $notification = ProjectNotification::findOrFail($id); +<<<<<<< Updated upstream + $notification->markAsRead(); + +======= + $notification->update(['read' => true]); +>>>>>>> Stashed changes + return response()->json(['success' => true]); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 12ad6af40..8f18bfd68 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -20,6 +20,7 @@ use App\Notifications\ProjectInvitationNotification; use Illuminate\Support\Facades\Notification; +use App\Models\ProjectNotification; use PHPUnit\Event\Application\FinishedSubscriber; @@ -223,10 +224,12 @@ public function add_member(string $idProject) $users_relation = $project->users()->get(); $user = Auth::user(); +<<<<<<< Updated upstream if (!$project->userHasLevel($user, '1')) { return redirect()->back()->with('error', 'You do not have permission to add a member to the project.'); } +<<<<<<< Updated upstream return view('projects.add_member', compact('project', 'users_relation')); } @@ -248,11 +251,43 @@ public function add_member_project(ProjectAddMemberRequest $request, string $idP $level_member = $request->get('level_member', 1); // Default to level 1 if not specified $user = Auth::user(); +======= + /** + * Add a member to a project based on the submitted form data. + * + * @param \App\Http\Requests\ProjectAddMemberRequest $request The validated request object. + * @param string $idProject The ID of the project. + * @return \Illuminate\Http\RedirectResponse + * @throws \Illuminate\Database\Eloquent\ModelNotFoundException + */ +======= + return view('projects.add_member', compact('project', 'users_relation')); + } +>>>>>>> Stashed changes +public function add_member_project(ProjectAddMemberRequest $request, string $idProject) +{ + $request->validated(); + + // Carrega o projeto usando o id_project + $project = Project::findOrFail($idProject); + + $email_member = $request->get('email_member'); + $member_id = $this->findIdByEmail($email_member); + $name_member = User::findOrFail($member_id); + $user = Auth::user(); + $level_member = $request->get('level_member', 1); + + // Validações + if ($project->users()->wherePivot('id_user', $member_id)->exists()) { + return redirect()->back()->with('error', 'The user is already associated with the project.'); + } +>>>>>>> Stashed changes if ($project->users()->wherePivot('id_user', $member_id)->exists()) { return redirect()->back()->with('error', 'The user is already associated with the project.'); } +<<<<<<< Updated upstream if (!$project->userHasLevel($user, '1')) { return redirect()->back()->with('error', 'You do not have permission to add a member to the project.'); } @@ -265,6 +300,36 @@ public function add_member_project(ProjectAddMemberRequest $request, string $idP ]); Notification::send($name_member, new ProjectInvitationNotification($project, $token)); +======= + $token = Str::random(40); + $project->users()->attach($member_id, [ + 'level' => $level_member, + 'invitation_token' => $token, + 'status' => 'pending' + ]); + +<<<<<<< Updated upstream + // Criação da notificação usando id_project + ProjectNotification::create([ + 'user_id' => $member_id, + 'project_id' => $project->id_project, // Usando o nome correto do campo + 'type' => 'invitation', + 'message' => "Você foi convidado para o projeto: {$project->title}", +======= + ProjectNotification::create([ + 'user_id' => $member_id, // ID do usuário convidado + 'project_id' => $project->id, + 'type' => 'invitation', + 'message' => "Você foi adicionado ao projeto: {$project->title}", +>>>>>>> Stashed changes + 'read' => false + ]); + + Notification::send($name_member, new ProjectInvitationNotification($project, $token)); + + $activity = "Sent invitation to " . $name_member->username . " to join the project " . $project->title; + ActivityLogHelper::insertActivityLog($activity, 1, $project->id_project, $user->id); +>>>>>>> Stashed changes $activity = "Sent invitation to " . $name_member->username . " to join the project " . $project->title; ActivityLogHelper::insertActivityLog($activity, 1, $project->id, $user->id); diff --git a/app/Models/Project.php b/app/Models/Project.php index df9f4e25a..96522f2f1 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -30,6 +30,7 @@ class Project extends Model use HasFactory; protected $fillable = [ + 'id_project', 'id_user', 'title', 'description', diff --git a/app/Models/ProjectNotification.php b/app/Models/ProjectNotification.php new file mode 100644 index 000000000..6e9310df3 --- /dev/null +++ b/app/Models/ProjectNotification.php @@ -0,0 +1,60 @@ +>>>>>> Stashed changes +use Illuminate\Database\Eloquent\Model; + +class ProjectNotification extends Model +{ +<<<<<<< Updated upstream + protected $table = 'project_notifications'; // Tabela existente + + protected $fillable = [ + 'user_id', + 'project_id', + 'type', // Ex: 'invitation', 'update' + 'message', + 'read' // 0 = não lida, 1 = lida + ]; + + // Relação com o usuário +======= + protected $fillable = [ + 'user_id', + 'project_id', + 'type', + 'message', + 'read' + ]; + +>>>>>>> Stashed changes + public function user() + { + return $this->belongsTo(User::class); + } + +<<<<<<< Updated upstream + // Relação com o projeto + // app/Models/ProjectNotification.php + +// Especifique a relação com Project usando a chave correta +public function project() +{ + return $this->belongsTo(Project::class, 'project_id', 'id_project'); +} + + // Marcar como lida + public function markAsRead() + { + $this->update(['read' => true]); +======= + public function project() + { + return $this->belongsTo(Project::class); +>>>>>>> Stashed changes + } +} \ No newline at end of file diff --git a/app/Models/User.php b/app/Models/User.php index b90aed4a5..31ff4d6c5 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -56,6 +56,11 @@ public function projects() return $this->belongsToMany(Project::class, 'members', 'id_user', 'id_project'); } + public function notifications() +{ + return $this->hasMany(ProjectNotification::class)->latest(); +} + // Relacionamento com projetos com nível de acesso específico (usando o campo `level`) public function projectsWithLevels() { diff --git a/resources/views/layouts/navbars/auth/topnav.blade.php b/resources/views/layouts/navbars/auth/topnav.blade.php index 217b5d641..fa3e2b78f 100644 --- a/resources/views/layouts/navbars/auth/topnav.blade.php +++ b/resources/views/layouts/navbars/auth/topnav.blade.php @@ -46,86 +46,101 @@ class="nav-link text-white font-weight-bold px-0"> +