Skip to content

Commit

Permalink
feat: conversations-with-latestMessage (#108)
Browse files Browse the repository at this point in the history
* feat: conversations-with-latestMessage
---------

Co-authored-by: Manikandan Sellasamy <[email protected]>
  • Loading branch information
manikandan5027 and Manikandan Sellasamy authored Oct 3, 2023
1 parent 64695ad commit fc4ce30
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
12 changes: 10 additions & 2 deletions datasheetapi/Controllers/ConversationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,19 @@ public async Task<ActionResult<GetConversationDto>> GetConversation(

}

/// <summary>
/// Get the list of conversation available under the reviewId
/// </summary>
/// <param name="reviewId">Unique Id for the review</param>
/// <param name="includeLatestMessage">Include Latest Message in the conversation.
/// The latest message will be non soft deleted message if at least one exists, else it will send last soft deleted message.</param>
/// <returns></returns>
[HttpGet(Name = "GetConversations")]
public async Task<ActionResult<List<GetConversationDto>>> GetConversations([NotEmptyGuid] Guid reviewId)
public async Task<ActionResult<List<GetConversationDto>>> GetConversations([NotEmptyGuid] Guid reviewId,
[FromQuery] bool includeLatestMessage = false)
{

var conversations = await _conversationService.GetConversations(reviewId);
var conversations = await _conversationService.GetConversations(reviewId, includeLatestMessage);

var userIds = conversations.SelectMany(conversation =>
conversation.Participants.Select(p => p.UserId)).ToList();
Expand Down
31 changes: 29 additions & 2 deletions datasheetapi/Repositories/ConversationRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,34 @@ public async Task<Conversation> CreateConversation(Conversation conversation)
public async Task<List<Conversation>> GetConversations(Guid reviewId)
{
return await _context.Conversations
.Include(p => p.Participants)
.Where(c => c.TagDataReviewId == reviewId).ToListAsync();
.Include(c => c.Participants)
.Where(c => c.TagDataReviewId == reviewId).ToListAsync();
}

public async Task<List<Conversation>> GetConversationsWithLatestMessage(Guid reviewId, bool includeSoftDeletedMessage)
{
var conversationWithLatestMessage = await _context.Conversations
.Include(c => c.Participants)
.Where(c => c.TagDataReviewId == reviewId)
.Select(c => new
{
Conversation = c,
LatestMessage = c.Messages
.OrderByDescending(m => m.CreatedDate)
.FirstOrDefault(m => m.SoftDeleted == includeSoftDeletedMessage)
?? c.Messages
.OrderByDescending(m => m.CreatedDate)
.First()
})
.ToListAsync();

return conversationWithLatestMessage
.Select(c =>
{
c.Conversation.Messages =
new List<Message> { c.LatestMessage };
return c.Conversation;
})
.ToList();
}
}
1 change: 1 addition & 0 deletions datasheetapi/Repositories/IConversationRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public interface IConversationRepository
{
Task<Conversation> CreateConversation(Conversation conversation);
Task<Conversation?> GetConversation(Guid conversationId);
Task<List<Conversation>> GetConversationsWithLatestMessage(Guid reviewId, bool includeSoftDeletedMessage);
Task<List<Conversation>> GetConversations(Guid reviewId);

Task<Message> AddMessage(Message message);
Expand Down
9 changes: 6 additions & 3 deletions datasheetapi/Services/ConversationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ public async Task<Conversation> GetConversation(Guid conversationId)
throw new NotFoundException($"Unable to find conversation for the conversationId - {conversationId} not found");
}

public async Task<List<Conversation>> GetConversations(Guid reviewId)
public async Task<List<Conversation>> GetConversations(Guid reviewId, bool includeLatestMessage)
{
var conversations = await _conversationRepository.GetConversations(reviewId);
return conversations;
if (includeLatestMessage)
{
return await _conversationRepository.GetConversationsWithLatestMessage(reviewId, false);
}
return await _conversationRepository.GetConversations(reviewId);
}

public async Task<Message> AddMessage(Guid conversationId, Message message)
Expand Down
2 changes: 1 addition & 1 deletion datasheetapi/Services/IConversationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public interface IConversationService
{
Task<Conversation> CreateConversation(Conversation conversation);
Task<Conversation> GetConversation(Guid conversationId);
Task<List<Conversation>> GetConversations(Guid reviewId);
Task<List<Conversation>> GetConversations(Guid reviewId, bool includeLatestMessage);

Task<Message> AddMessage(Guid conversationId, Message message);
Task<Message> GetMessage(Guid messageId);
Expand Down
21 changes: 19 additions & 2 deletions tests/Services/ConversationServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public async Task GetConversations_ThrowsWhenFetchingConversationsThrowsExceptio

_conversationRepositoryMock.Setup(x => x.GetConversations(conversation.TagDataReviewId)).ThrowsAsync(new ArgumentNullException());

await Assert.ThrowsAsync<ArgumentNullException>(() => _conversatiosnService.GetConversations(conversation.TagDataReviewId));
await Assert.ThrowsAsync<ArgumentNullException>(() =>
_conversatiosnService.GetConversations(conversation.TagDataReviewId, false));
}

[Fact]
Expand All @@ -143,14 +144,30 @@ public async Task GetConversations_RunsOkayWithCorrectInput()

_conversationRepositoryMock.Setup(x => x.GetConversations(conversation.TagDataReviewId)).ReturnsAsync(new List<Conversation> { conversation });

var result = await _conversatiosnService.GetConversations(conversation.TagDataReviewId);
var result = await _conversatiosnService.GetConversations(conversation.TagDataReviewId, false);

Assert.NotNull(result);
Assert.Single(result);
Assert.Equal(conversation.Id, result[0].Id);
_conversationRepositoryMock.Verify(x => x.GetConversations(conversation.TagDataReviewId), Times.Once);
}

[Fact]
public async Task GetConversationsWithLatestMessage_RunsOkay()
{
var conversation = SetUpConversation();

_conversationRepositoryMock.Setup(x => x.GetConversationsWithLatestMessage(conversation.TagDataReviewId, false))
.ReturnsAsync(new List<Conversation> { conversation });

var result = await _conversatiosnService.GetConversations(conversation.TagDataReviewId, true);

Assert.NotNull(result);
Assert.Single(result);
Assert.Equal(conversation.Id, result[0].Id);
_conversationRepositoryMock.Verify(x => x.GetConversationsWithLatestMessage(conversation.TagDataReviewId, false), Times.Once);
}

[Fact]
public async Task AddMessage_ThrowsWhenUnableToFetchConversation()
{
Expand Down

0 comments on commit fc4ce30

Please sign in to comment.