chat implemented

This commit is contained in:
andrei-mihnea-cerbu
2024-04-08 11:41:48 +03:00
parent 48eb2adcbd
commit 90604e48ae
101 changed files with 1493 additions and 116 deletions
@@ -0,0 +1,150 @@
using Application.Services.Database;
using Application.Services.Database.MongoDB;
using Core.Entities;
namespace Application.Endpoints.Chats;
public class ChatHandler
{
private readonly IChatMongoDbService _chatMongoDbService;
private readonly IPatientRepository _patientRepository;
private readonly IDoctorRepository _doctorRepository;
public ChatHandler(IChatMongoDbService chatMongoDbService, IPatientRepository patientRepository,
IDoctorRepository doctorRepository)
{
_chatMongoDbService = chatMongoDbService;
_patientRepository = patientRepository;
_doctorRepository = doctorRepository;
}
public async Task<BaseResponse> HandleSendMessage(SendMessageDto sendMessageDto)
{
var validation = new SendMessageValidator();
var validationResult = await validation.ValidateAsync(sendMessageDto);
if (!validationResult.IsValid)
{
var firstError = validationResult.Errors.FirstOrDefault();
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
var errorMessage = firstError.ErrorMessage;
return new BaseResponse
{
StatusCode = errorCode,
Message = errorMessage,
Data = null
};
}
if (!await CheckForUsersExistence(sendMessageDto.Sender, sendMessageDto.Receiver))
{
return new BaseResponse
{
StatusCode = HttpStatusCodes.BadRequest,
Message = "Users can't be found in the system.",
Data = null
};
}
var chatId = ChatIdentifier.GenerateChatId(sendMessageDto.Sender, sendMessageDto.Receiver);
var criteria = new List<(string, string)>();
criteria.Add(("_id", chatId));
var documents = await _chatMongoDbService.FindAsync<Chat>(criteria);
if (!documents.Any())
{
return new BaseResponse()
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Access to medical history not found.",
Data = null
};
}
var chat = documents[0].Messages;
chat.Add(new Message(sendMessageDto.Sender, sendMessageDto.Message));
var newChat = new Chat();
newChat.SetId(chatId);
newChat.SetMessages(chat);
await _chatMongoDbService.ModifyAsync("_id", chatId, newChat);
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = null,
Data = null
};
}
public async Task<BaseResponse> HandleGetConversation(GetConversationDto getConversationDto)
{
var validation = new GetConversationValidator();
var validationResult = await validation.ValidateAsync(getConversationDto);
if (!validationResult.IsValid)
{
var firstError = validationResult.Errors.FirstOrDefault();
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
var errorMessage = firstError.ErrorMessage;
return new BaseResponse
{
StatusCode = errorCode,
Message = errorMessage,
Data = null
};
}
if (!await CheckForUsersExistence(getConversationDto.IdUser1, getConversationDto.IdUser2))
{
return new BaseResponse
{
StatusCode = HttpStatusCodes.BadRequest,
Message = "Users can't be found in the system.",
Data = null
};
}
var chatId = ChatIdentifier.GenerateChatId(getConversationDto.IdUser1, getConversationDto.IdUser2);
var criteria = new List<(string, string)>();
criteria.Add(("_id", chatId));
Chat? chat = null;
var documents = await _chatMongoDbService.FindAsync<Chat>(criteria);
if (!documents.Any())
{
chat = new Chat();
chat.SetId(chatId);
await _chatMongoDbService.AddAsync(chat);
}
else
{
chat = documents[0];
}
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Fetching messages.",
Data = chat
};
}
private async Task<bool> CheckForUsersExistence(Guid idUser1, Guid idUser2)
{
var firstCheck = await _patientRepository.GetByIdAsync(idUser1) != null &&
await _doctorRepository.GetByIdAsync(idUser2) != null;
var secondCheck = await _patientRepository.GetByIdAsync(idUser2) != null &&
await _doctorRepository.GetByIdAsync(idUser1) != null;
return firstCheck || secondCheck;
}
}
@@ -0,0 +1,18 @@
namespace Application.Endpoints.Chats;
public class ChatIdentifier
{
public static string GenerateChatId(Guid id1, Guid id2)
{
// Convert GUIDs to strings
string strId1 = id1.ToString();
string strId2 = id2.ToString();
// Sort the GUID strings
string firstId = strId1.CompareTo(strId2) < 0 ? strId1 : strId2;
string secondId = strId1.CompareTo(strId2) < 0 ? strId2 : strId1;
// Combine them to get a symmetric string
return firstId + "-" + secondId;
}
}
@@ -0,0 +1,7 @@
namespace Application.Endpoints.Chats;
public class GetConversationDto
{
public Guid IdUser1 { get; set; }
public Guid IdUser2 { get; set; }
}
@@ -0,0 +1,15 @@
using FluentValidation;
namespace Application.Endpoints.Chats;
public class GetConversationValidator : AbstractValidator<GetConversationDto>
{
public GetConversationValidator()
{
RuleFor(x => x.IdUser1)
.NotEmpty().WithMessage("IdUser1 is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x.IdUser2)
.NotEmpty().WithMessage("IdUser2 is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
}
}
@@ -0,0 +1,8 @@
namespace Application.Endpoints.Chats;
public class SendMessageDto
{
public Guid Sender { get; set; }
public Guid Receiver { get; set; }
public string Message { get; set; }
}
@@ -0,0 +1,18 @@
using FluentValidation;
namespace Application.Endpoints.Chats;
public class SendMessageValidator : AbstractValidator<SendMessageDto>
{
public SendMessageValidator()
{
RuleFor(x => x.Sender)
.NotEmpty().WithMessage("Sender Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x.Receiver)
.NotEmpty().WithMessage("Receiver Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x.Message)
.NotEmpty().WithMessage("Message is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
}
}
@@ -1,6 +1,5 @@
using Application.Services.Database;
using Application.Services.Database.MongoDB;
using Core.Entities;
namespace Application.Endpoints.MedicalHistories.ManageAuthorization;
@@ -113,7 +112,6 @@ public class MedicalHistoryManageAuthorizationHandler
}
var authorisations = documents[0].Authorisation;
Console.WriteLine(authorisations);
if (!authorisations.Contains(infoDto.DoctorId.ToString()))
{
return new BaseResponse()