Files

234 lines
9.6 KiB
C#
Raw Permalink Normal View History

2025-07-28 17:41:14 +03:30
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;
2025-07-29 17:07:37 +03:30
public ChatController(ChatService chatService, ExperService experService)
{
_chatService = chatService;
_experService = experService;
}
2025-07-28 17:41:14 +03:30
[HttpPost("MyChats")]
[Authorize(Roles = "Company,Exper")]
public async Task<ActionResult> 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();
2025-07-30 23:49:51 +03:30
}
[HttpGet("User/LastOpenChatInCompany/{CompanyID}")]
[Authorize(Roles = "User")]
public async Task<ActionResult> GetLastOpenChatInCompany(int CompanyID)
{
string strUserID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
int UserID = Convert.ToInt32(strUserID);
var response = await _chatService.GetLastOpenChatInCompany(CompanyID,UserID);
return response.Success ? Ok(response.Value) : Accepted() ;
2025-08-02 19:00:53 +03:30
}
[HttpGet("User/Chat/{ChatID}")]
[Authorize(Roles = "User")]
public async Task<ActionResult> Getchat(int ChatID)
{
string strUserID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
int UserID = Convert.ToInt32(strUserID);
var response = await _chatService.GetChat(ChatID, UserID);
return response.Success ? Ok(response.Value) : Accepted();
2025-07-28 17:41:14 +03:30
}
[HttpGet("ChatsAwaitingOurResponse")]
[Authorize(Roles = "Company,Exper")]
public async Task<ActionResult> ChatsAwaitingOurResponse()
{
int CompanyID = 0;
2025-08-03 23:41:54 +03:30
int? ExperID = null;
2025-07-28 17:41:14 +03:30
if (User.IsInRole("Exper"))
{
string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
2025-08-03 23:41:54 +03:30
ExperID = Convert.ToInt32(strExperID);
2025-07-28 17:41:14 +03:30
2025-08-03 23:41:54 +03:30
CompanyID = await _experService.GetCompanyIDExper(ExperID.Value);
2025-07-28 17:41:14 +03:30
}
else if (User.IsInRole("Company"))
{
string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
CompanyID = Convert.ToInt32(strCompanyID);
}
2025-08-03 23:41:54 +03:30
var response = await _chatService.ChatsAwaitingOurResponse(CompanyID,ExperID);
2025-07-28 17:41:14 +03:30
return Ok(response);
}
2025-08-03 23:41:54 +03:30
[HttpPut("Attached-to/{toExperID}/{ChatID}")]
[Authorize(Roles = "Company,Exper")]
public async Task<ActionResult> Attachedto(int toExperID,int ChatID)
{
int CompanyID = 0;
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);
CompanyID = await _experService.GetCompanyIDExper(ExperID.Value);
}
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.Attachedto(ChatID, ExperID,toExperID,CompanyID);
return response ?NoContent(): BadRequest(new List<string> { "خطا در بروزرسانی وضعیت" });
}
2025-07-28 17:41:14 +03:30
[HttpPost("NewChatFromCurrentUser")]
public async Task<ActionResult> 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<ActionResult> 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);
}
2025-08-03 17:22:49 +03:30
[HttpPut("ChatIsFinish/{ChatID}")]
2025-07-29 00:00:16 +03:30
[Authorize(Roles = "Company,Exper")]
public async Task<ActionResult> ChatIsFinish(int ChatID)
{
2025-08-03 17:22:49 +03:30
int CompanyID = 0;
2025-08-03 23:01:44 +03:30
int? ExperID = null;
2025-08-03 17:22:49 +03:30
if (User.IsInRole("Exper"))
{
string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
2025-08-03 23:01:44 +03:30
ExperID = Convert.ToInt32(strExperID);
2025-08-03 17:22:49 +03:30
2025-08-03 23:01:44 +03:30
CompanyID = await _experService.GetCompanyIDExper(ExperID.Value);
2025-08-03 17:22:49 +03:30
}
else if (User.IsInRole("Company"))
{
string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
CompanyID = Convert.ToInt32(strCompanyID);
}
2025-08-03 23:01:44 +03:30
return await _chatService.FinishChat(ChatID, CompanyID, ExperID) ? NoContent()
2025-08-03 17:22:49 +03:30
: BadRequest(new List<string> { "خطا در بروزرسانی وضعیت" });
}
[HttpPut("OpenChat/{ChatID}")]
[Authorize(Roles = "Company,Exper")]
public async Task<ActionResult> OpenChat(int ChatID)
{
int CompanyID = 0;
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);
CompanyID = await _experService.GetCompanyIDExper(ExperID.GetValueOrDefault());
}
else if (User.IsInRole("Company"))
{
string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
CompanyID = Convert.ToInt32(strCompanyID);
}
return await _chatService.OpenChat(ChatID, CompanyID, ExperID) ? NoContent()
: BadRequest(new List<string> { "خطا در بروزرسانی وضعیت" });
}
[HttpPut("ChatIsFinishFromUser/{ChatID}")]
[Authorize(Roles = "User")]
public async Task<ActionResult> ChatIsFinishFromUser(int ChatID)
{
string strUserID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
int UserID = Convert.ToInt32(strUserID);
return await _chatService.FinishChatFromUser(ChatID, UserID) ? NoContent()
2025-07-29 00:00:16 +03:30
: BadRequest(new List<string> { "خطا در بروزرسانی وضعیت" });
}
2025-07-29 17:07:37 +03:30
[HttpPut("MarkAsReadChatItem/{ConversationItemID}")]
[Authorize(Roles = "Company,User,Exper")]
public async Task<ActionResult> MarkAsReadChatItem(int ConversationItemID)
{
2025-08-08 18:10:25 +03:30
int? ID = null;
2025-07-29 17:07:37 +03:30
ConversationType Type = ConversationType.UE;
if (User.IsInRole("Exper"))
{
string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
2025-08-08 18:10:25 +03:30
ID = Convert.ToInt32(strExperID);
2025-07-29 17:07:37 +03:30
Type = ConversationType.EU;
}
else if (User.IsInRole("User"))
{
Type = ConversationType.UE;
}
else if (User.IsInRole("Company"))
{
Type = ConversationType.CU;
}
else return Unauthorized();
2025-08-08 18:10:25 +03:30
return await _chatService.MarkAsReadChatItem(ConversationItemID, Type, ID) ? NoContent()
2025-07-29 17:07:37 +03:30
: BadRequest(new List<string>() { "خطا در بروزرسانی گفتگو" });
}
2025-07-28 17:41:14 +03:30
}
}