using Common.Dtos.Conversation; using Common.Enums; using Hushian.Application.Constants; using Hushian.Application.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace Hushian.WebApi.Controllers.v1 { [Route("api/v1/[controller]")] [ApiController] public class ChatController : ControllerBase { private readonly ChatService _chatService; private readonly ExperService _experService; public ChatController(ChatService chatService, ExperService experService) { _chatService = chatService; _experService = experService; } [HttpPost("MyChats")] [Authorize(Roles = "Company,Exper")] public async Task MyChats([FromBody] ConversationStatus status) { if (User.IsInRole("Exper")) { string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First(); int ExperID = Convert.ToInt32(strExperID); var response = await _chatService.GeChatsByExperID(ExperID, status); return Ok(response); } else if (User.IsInRole("Company")) { string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First(); int CompanyID = Convert.ToInt32(strCompanyID); var response = await _chatService.GetChatsByCompanyID(CompanyID, status); return Ok(response); } return Forbid(); } [HttpGet("ChatsAwaitingOurResponse")] [Authorize(Roles = "Company,Exper")] public async Task ChatsAwaitingOurResponse() { int CompanyID = 0; if (User.IsInRole("Exper")) { string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First(); int ExperID = Convert.ToInt32(strExperID); CompanyID = await _experService.GetCompanyIDExper(ExperID); } else if (User.IsInRole("Company")) { string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First(); CompanyID = Convert.ToInt32(strCompanyID); } var response = await _chatService.ChatsAwaitingOurResponse(CompanyID); return Ok(response); } [HttpPost("NewChatFromCurrentUser")] public async Task NewChatFromCurrentUser(ADD_ConversationDto conversation) { conversation.UserID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => Convert.ToInt32(s.Value)).First(); var response = await _chatService.NewChat(conversation); return response.Success ? Ok(response.Value) : BadRequest(response.Errors); } [HttpPost("ADDChatResponse")] [Authorize(Roles = "Company,User,Exper")] public async Task ADDChatResponse([FromBody] ADD_ConversationResponseDto ConversationItem) { int? ExperID = null; if (User.IsInRole("Exper")) { string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First(); ExperID = Convert.ToInt32(strExperID); ConversationItem.Type = ConversationType.EU; } else if (User.IsInRole("User")) { ConversationItem.Type = ConversationType.UE; } else if (User.IsInRole("Company")) { ConversationItem.Type = ConversationType.CU; } else return Unauthorized(); var Response = await _chatService.ADDChatResponse(ConversationItem, ExperID); return Response.Success ? Ok(Response.Value) : BadRequest(Response.Errors); } [HttpGet("ChatIsFinish/{ChatID}")] [Authorize(Roles = "Company,Exper")] public async Task ChatIsFinish(int ChatID) { return await _chatService.FinishChat(ChatID) ? NoContent() : BadRequest(new List { "خطا در بروزرسانی وضعیت" }); } [HttpPut("MarkAsReadChatItem/{ConversationItemID}")] [Authorize(Roles = "Company,User,Exper")] public async Task MarkAsReadChatItem(int ConversationItemID) { int? ExperID = null; ConversationType Type = ConversationType.UE; if (User.IsInRole("Exper")) { string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First(); ExperID = Convert.ToInt32(strExperID); Type = ConversationType.EU; } else if (User.IsInRole("User")) { Type = ConversationType.UE; } else if (User.IsInRole("Company")) { Type = ConversationType.CU; } else return Unauthorized(); return await _chatService.MarkAsReadChatItem(ConversationItemID, Type, ExperID) ? NoContent() : BadRequest(new List() { "خطا در بروزرسانی گفتگو" }); } } }