Skip to content

Commit

Permalink
Merge pull request #75 from folded-ear/i-cooked-this-date
Browse files Browse the repository at this point in the history
add doneAt date to setStatus mutation
  • Loading branch information
switzerb authored Jun 8, 2024
2 parents 0a3aa2a + 9ee7155 commit 74a7146
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDate;
import java.util.List;

Expand Down Expand Up @@ -73,8 +74,8 @@ public Plan setGrant(Long planId, Long userId, AccessLevel accessLevel) {
return planService.setGrantOnPlan(planId, userId, accessLevel);
}

public PlanItem setStatus(Long id, PlanItemStatus status) {
return planService.setItemStatus(id, status);
public PlanItem setStatus(Long id, PlanItemStatus status, Instant doneAt) {
return planService.setItemStatus(id, status, doneAt);
}

public Plan revokeGrant(Long planId, Long userId) {
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/brennaswitzer/cookbook/services/PlanService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -388,30 +389,36 @@ private PlanMessage buildUpdateMessage(PlanItem item) {
}

public PlanItem setItemStatus(Long id, PlanItemStatus status) {
return setItemStatus(id, status, null);
}

public PlanItem setItemStatus(Long id, PlanItemStatus status, Instant doneAt) {
PlanItem item = getPlanItemById(id, AccessLevel.CHANGE);
item.setStatus(status);
if (item.getStatus().isForDelete()) {
recordRecipeHistories(item, item.getStatus());
recordRecipeHistories(item, item.getStatus(), doneAt);
item.moveToTrash();
}
return item;
}

private void recordRecipeHistories(PlanItem item,
PlanItemStatus status) {
PlanItemStatus status,
Instant doneAtOrNull) {
Instant doneAt = doneAtOrNull == null ? Instant.now() : doneAtOrNull;
if (Hibernate.unproxy(item.getIngredient()) instanceof Recipe r) {
var h = new PlannedRecipeHistory();
h.setRecipe(r);
h.setOwner(principalAccess.getUser());
h.setPlanItemId(item.getId());
h.setPlannedAt(item.getCreatedAt());
h.setDoneAt(Instant.now());
h.setDoneAt(doneAt);
h.setStatus(status);
recipeHistoryRepo.save(h);
}
if (item.hasChildren()) {
item.getChildView()
.forEach(it -> recordRecipeHistories(it, status));
.forEach(it -> recordRecipeHistories(it, status, doneAt));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/graphqls/planner.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ type PlannerMutation {
setGrant(planId: ID!, userId: ID!, accessLevel:AccessLevel): Plan!
"""Sets the status of the given item. This will always return the updated
item, though it may immediately moved to the trash (in the background)."""
setStatus(id: ID!, status: PlanItemStatus!): PlanItem!
setStatus(id: ID!, status: PlanItemStatus!, doneAt: DateTime): PlanItem!
"""Update a bucket w/in a plan, by setting or clearing its name and date."""
updateBucket(planId: ID!, bucketId: ID!, name: String, date: Date): PlanBucket!
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

import java.time.Instant;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -206,10 +208,11 @@ void setStatus() {
long id = 123L;
PlanItemStatus status = PlanItemStatus.ACQUIRED;
PlanItem item = mock(PlanItem.class);
when(planService.setItemStatus(id, status))
Instant date = Instant.now().minus(3, ChronoUnit.DAYS);
when(planService.setItemStatus(id, status, date))
.thenReturn(item);

PlanItem result = mutation.setStatus(id, status);
PlanItem result = mutation.setStatus(id, status, date);

assertSame(item, result);
}
Expand Down Expand Up @@ -241,5 +244,4 @@ void updateBucket() {

assertSame(mock, bucket);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,18 @@ void statusChangeEvents() {
assertEquals(PlanItemStatus.DELETED, h.getStatus());
// pizza got completed
h = byRecipe.get(box.pizza);
var pizzaHistory = byRecipe.get(box.pizza);
assertEquals(pizza.getId(), h.getPlanItemId());
assertNotNull(h.getPlannedAt());
assertEquals(PlanItemStatus.COMPLETED, h.getStatus());
// sauce got (implicitly) completed
h = byRecipe.get(box.pizzaSauce);
var sauceHistory = byRecipe.get(box.pizzaSauce);
assertEquals(sauce.getId(), h.getPlanItemId());
assertNotNull(h.getPlannedAt());
assertEquals(PlanItemStatus.COMPLETED, h.getStatus());
assertEquals(pizzaHistory.getDoneAt(), sauceHistory.getDoneAt());

// ignore tomatoes - not a recipe
assertEquals(3, recipeHistoryRepo.count());
}
Expand Down

0 comments on commit 74a7146

Please sign in to comment.