using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Threading.Tasks; using System.Security.Claims; using vendorApproverBackend.Services; using vendorApproverBackend.Entities; using vendorApproverBackend.Exceptions; using vendorApproverBackend.Responses; using vendorApproverBackend.Requests; using vendorApproverBackend.Interfaces; using Microsoft.AspNetCore.Authorization; using System.Linq; namespace YourProject.Controllers { [ApiController] [Route("api/v1/[controller]")] public class NotificationController : ControllerBase { private readonly NotificationService _notificationService; private readonly UserService _userService; private readonly INotificationService _notificationSenderService; public NotificationController(NotificationService notificationService, UserService userService, INotificationService notificationSenderService) { _notificationService = notificationService; _userService = userService; _notificationSenderService = notificationSenderService; } // Encapsulate the required user authentication logic private async Task GetAuthenticatedUserAsync() { var username = User.FindFirstValue(ClaimTypes.Name); if (string.IsNullOrEmpty(username)) { throw new UnauthorizedException("Username claim not found in token."); } var user = await _userService.FindByUsernameAsync(username); if (user == null) { throw new NotFoundException("User not found."); } return user; } // Corresponds to /all_my_notification [HttpGet("all_my_notification")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,MANAGER,RENWE_APPROVER,PERFERMANCE_MANAGER")] public async Task>> AllMyNotification( [FromQuery] int page = 0, [FromQuery] int size = 3) { try { var user = await GetAuthenticatedUserAsync(); var result = await _notificationService.AllNotificationByUserAsync(page, size, user); return Ok(result); } catch (UnauthorizedException ex) { return Unauthorized(ex.Message); } catch (NotFoundException ex) { return NotFound(ex.Message); } } // Corresponds to /all_my_notification_waiting (Note: Logic is identical to the first endpoint as per Spring code) [HttpGet("all_my_notification_waiting")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,MANAGER,RENWE_APPROVER,PERFERMANCE_MANAGER")] public async Task>> AllMyNotificationWaiting( [FromQuery] int page = 0, [FromQuery] int size = 3) { try { var user = await GetAuthenticatedUserAsync(); var result = await _notificationService.AllNotificationByUserAsync(page, size, user); return Ok(result); } catch (UnauthorizedException ex) { return Unauthorized(ex.Message); } catch (NotFoundException ex) { return NotFound(ex.Message); } } // Corresponds to /my_unread_notifications_count [HttpGet("my_unread_notifications_count")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,MANAGER,RENWE_APPROVER,PERFERMANCE_MANAGER")] public async Task> MyUnreadNotificationsCount() { try { var user = await GetAuthenticatedUserAsync(); var result = await _notificationService.AllUnreadNotificationByUserAsync(user); return Ok(result); } catch (UnauthorizedException ex) { return Unauthorized(ex.Message); } catch (NotFoundException ex) { return NotFound(ex.Message); } } // Corresponds to /get_notification_by_id/{notifId} [HttpGet("get_notification_by_id/{notifId}")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,MANAGER,RENWE_APPROVER,PERFERMANCE_MANAGER")] public async Task> GetNotificationById(long notifId) { try { var user = await GetAuthenticatedUserAsync(); var result = await _notificationService.FindByIdAsync(notifId); // Simple access check: ensure the authenticated user is the receiver or sender if (result.ReciverId != user.Id && result.SenderId != user.Id) { return Forbid(); } return Ok(result); } catch (UnauthorizedException ex) { return Unauthorized(ex.Message); } catch (NotFoundException) { return NotFound("not notif found with id"); } } // Corresponds to /read_my_waiting_notification [HttpPatch("read_my_waiting_notification")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,MANAGER,RENWE_APPROVER,PERFERMANCE_MANAGER")] public async Task>> ReadMyWaitingNotification( [FromQuery] List notificationid) { try { var user = await GetAuthenticatedUserAsync(); var result = await _notificationService.ReadMyWaitingNotificationAsync(notificationid, user); return Ok(result); } catch (UnauthorizedException ex) { return Unauthorized(ex.Message); } catch (NotFoundException ex) { return NotFound(ex.Message); } catch (BadRequestException ex) { return BadRequest(ex.Message); } } // Corresponds to /read_notification/{notifId} [HttpGet("read_notification/{notifId}")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,MANAGER,RENWE_APPROVER,PERFERMANCE_MANAGER")] public async Task> ReadNotification(long notifId) { try { var user = await GetAuthenticatedUserAsync(); await _notificationService.ReadNotificationAsync(notifId, user); // Get the new unread count as the response var result = await _notificationService.AllUnreadNotificationByUserAsync(user); return Ok(result); } catch (UnauthorizedException ex) { return Unauthorized(ex.Message); } catch (NotFoundException) { return NotFound("not notif found with id"); } } [HttpPost("send-to-user")] public async Task SendToUser([FromBody] SendNotificationToUsersRequest request) { var notification = new NotificationModel { SenderId = request.SenderId, ReciverId = request.UserId, Subject = request.Subject, Content = request.Content, ActionId = request.ActionId, LogUrl = request.LogoUrl, }; await _notificationSenderService.SendNotificationToUser(request.UserId, notification); return Ok(new { message = "Notification sent successfully" }); } [HttpPost("send-dynamic-notifications")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,MANAGER,RENWE_APPROVER,PERFERMANCE_MANAGER")] public async Task SendDynamicNotification([FromBody] DynamicNotificationRequest request) { var username = User.FindFirstValue(ClaimTypes.Name); if (string.IsNullOrEmpty(username)) { return Unauthorized("Username claim not found in token."); } var user = await _userService.FindByUsernameAsync(username); if (user == null) { return NotFound("User not found."); } try { await _notificationService.SendDynamicNotificationsAsync(request, user.Id); return Ok(new { Message = "Notification sent successfully." }); } catch (Exception ex) { return StatusCode(406, new { Message = "Failed to send notification.", Details = ex.Message }); } } } }