Skip to content

Commit

Permalink
refactor(review): add received route and update ReceivedAt To Reviewa…
Browse files Browse the repository at this point in the history
…bleAt In Review Table
  • Loading branch information
jvondermarck committed Jun 4, 2024
1 parent 5175a4f commit ca5dd3a
Show file tree
Hide file tree
Showing 12 changed files with 628 additions and 27 deletions.
37 changes: 37 additions & 0 deletions LivlReviews.Api/Controllers/RequestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,41 @@ public async Task<ActionResult<Request>> RejectRequest(int id, [FromBody] Messag

return Ok(request);
}

[HttpPost("{id}/received")]
[UserIdClaim]
public async Task<ActionResult<Request>> ReceivedRequest(int id)
{
var currentUserId = HttpContext.Items["UserId"] as string;
if(currentUserId is null) return Unauthorized();

var currentUser = await userManager.FindByIdAsync(currentUserId);
if(currentUser is null)
{
return Unauthorized();
}

Request request = repository.GetById(id);
if (request == null)
{
return NotFound();
}

if (!Domain.Entities.Request.Can(currentUser.Role, Operation.UPDATE))
{
return Forbid();
}

if(request.State != RequestState.Approved)
{
return BadRequest("Request must be approved before being received.");
}

request.State = RequestState.Received;
request.ReviewableAt = DateTime.Today.AddDays(7);

repository.Update(request);

return Ok(request);
}
}
11 changes: 10 additions & 1 deletion LivlReviews.Api/Controllers/ReviewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public async Task<ActionResult> CreateReview([FromBody] PostReviewRequest review
Title = reviewRequest.Title,
};

return Ok(reviewManager.CreateReview(review));
try
{
review = reviewManager.CreateReview(review);
}
catch (Exception e)
{
return BadRequest(e.Message);
}

return Ok(review);
}
}
20 changes: 10 additions & 10 deletions LivlReviews.Domain.Test/Entities/Review.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,44 @@ namespace LivlReviews.Domain.Test.Entities;
public class ReviewTest
{
[Fact]
public void Is_Request_Reviewable_When_Received_Over_Seven_Days_Ago()
public void Is_Request_Reviewable_When_Reviewable_Date_Reached()
{
// Arrange
var request = new Request
{
Id = 1,
State = RequestState.Received,
ReceivedAt = new DateTime(2024, 5, 20)
ReviewableAt = new DateTime(2024, 5, 20)
};

var fakeClock = new FakeClock(new DateTime(2024, 5, 28)); // 8 days after
var fakeClock = new FakeClock(new DateTime(2024, 5, 28));

var reviewInventory = new FakeReviewInventory(request, fakeClock);

// Act
var result = reviewInventory.IsReviewable(request.Id);
var result = reviewInventory.IsReviewableDateReached(request.Id);

// Assert
Assert.True(result);
}

[Fact]
public void Is_Request_Not_Reviewable_When_Received_Less_Than_Seven_Days_Ago()
public void Is_Request_Not_Reviewable_When_Reviewable_Date_Not_Reached()
{
// Arrange
var request = new Request
{
Id = 1,
State = RequestState.Received,
ReceivedAt = new DateTime(2024, 5, 23) // May 23, 2024
ReviewableAt = new DateTime(2024, 5, 23)
};

var fakeClock = new FakeClock(new DateTime(2024, 5, 28)); // 5 days after
var fakeClock = new FakeClock(new DateTime(2024, 5, 20)); // 3 days before the reviewable date

var reviewInventory = new FakeReviewInventory(request, fakeClock);

// Act
var result = reviewInventory.IsReviewable(request.Id);
var result = reviewInventory.IsReviewableDateReached(request.Id);

// Assert
Assert.False(result);
Expand All @@ -61,10 +61,10 @@ public void Set_Request_State_To_Completed_When_Review_Created()
{
Id = 1,
State = RequestState.Received,
ReceivedAt = new DateTime(2024, 5, 20)
ReviewableAt = new DateTime(2024, 5, 20)
};

var fakeClock = new FakeClock(new DateTime(2024, 5, 28)); // 8 days after
var fakeClock = new FakeClock(new DateTime(2024, 5, 28));

var reviewInventory = new FakeReviewInventory(request, fakeClock);
var requestInventory = new FakeRequestInventory(new List<ProductStock>(), new List<Request> { request });
Expand Down
9 changes: 7 additions & 2 deletions LivlReviews.Domain.Test/Fakes/FakeReviewInventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ namespace LivlReviews.Domain.Test.Stubs;

public class FakeReviewInventory(Request request, IClock clock) : IReviewInventory
{
public bool IsReviewable(int requestId)
public bool IsReviewableDateReached(int requestId)
{
return request.Id == requestId && request.State == RequestState.Received && request.ReceivedAt <= clock.Now.AddDays(-7);
return request.Id == requestId && clock.Now >= request.ReviewableAt;
}

public Review CreateReview(Review review)
{
return review;
}

public bool HasStatusReceived(int requestId)
{
return request.Id == requestId && request.State == RequestState.Received;
}
}
4 changes: 2 additions & 2 deletions LivlReviews.Domain/Domain_interfaces_input/IReviewManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace LivlReviews.Domain.Domain_interfaces_input;

public interface IReviewManager
{
public bool IsReviewable(int requestId);

public bool IsReviewableDateReached(int requestId);
public bool HasStatusReceived(int requestId);
public Review CreateReview(Review review);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace LivlReviews.Domain.Domain_interfaces_output;

public interface IReviewInventory
{
bool IsReviewable(int requestId);
bool IsReviewableDateReached(int requestId);
Review CreateReview(Review review);
bool HasStatusReceived(int requestId);
}
2 changes: 1 addition & 1 deletion LivlReviews.Domain/Entities/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Request : ICreatedDate, IUpdatedDate
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }

public DateTime? ReceivedAt { get; set; }
public DateTime? ReviewableAt { get; set; }

public static bool Can(Role role, Operation operation)
{
Expand Down
18 changes: 14 additions & 4 deletions LivlReviews.Domain/ReviewManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,26 @@ namespace LivlReviews.Domain;

public class ReviewManager(IReviewInventory reviewInventory, IStockManager stockManager) : IReviewManager
{
public bool IsReviewable(int requestId)
public bool IsReviewableDateReached(int requestId)
{
return reviewInventory.IsReviewable(requestId);
return reviewInventory.IsReviewableDateReached(requestId);
}

public bool HasStatusReceived(int requestId)
{
return reviewInventory.HasStatusReceived(requestId);
}

public Review CreateReview(Review review)
{
if(!IsReviewable(review.RequestId))
if(!IsReviewableDateReached(review.RequestId))
{
throw new Exception("The request is not reviewable yet, please wait until the reviewable date.");
}

if(!HasStatusReceived(review.RequestId))
{
throw new Exception("Request is not in a reviewable state. Cannot create review.");
throw new Exception("The request should be in the received state to create a review.");
}

var createdReview = reviewInventory.CreateReview(review);
Expand Down
Loading

0 comments on commit ca5dd3a

Please sign in to comment.