Skip to content

Commit bf62fa9

Browse files
committed
#178: implement shouldShowHeimatTimeWhenProjectIsNotMappedInKeeptime
1 parent 336a2f8 commit bf62fa9

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

src/main/java/de/doubleslash/keeptime/controller/HeimatController.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ public HeimatController(HeimatAPI heimatAPI, ExternalProjectsMappingsRepository
4444
}
4545

4646
public List<Mapping> getTableRows(final LocalDate currentReportDate, final List<Work> currentWorkItems) {
47-
// TODO check if external projects are available for the currentDay
4847
final List<HeimatTask> heimatTasks = heimatAPI.getMyTasks(currentReportDate);
49-
5048
final List<HeimatTime> heimatTimes = heimatAPI.getMyTimes(currentReportDate);
5149
final List<ExternalProjectMapping> mappedProjects = externalProjectsMappingsRepository.findByExternalSystemId(
5250
ExternalSystem.Heimat);
@@ -63,20 +61,20 @@ public List<Mapping> getTableRows(final LocalDate currentReportDate, final List<
6361
String heimatNotes = "";
6462
long heimatTimeSeconds = 0;
6563
boolean isMappedInHeimat = false;
66-
final Optional<ExternalProjectMapping> heimatMappings = mappedProjects.stream()
64+
final Optional<ExternalProjectMapping> optHeimatMapping = mappedProjects.stream()
6765
.filter(mp -> mp.getProject().getId()
6866
== project.getId())
6967
.findAny();
7068
List<HeimatTime> optionalAlreadyBookedTimes = new ArrayList<>();
7169
Optional<Mapping> optionalExistingMapping = Optional.empty();
72-
if (heimatMappings.isPresent()) {
70+
if (optHeimatMapping.isPresent()) {
7371
isMappedInHeimat = true;
7472
optionalExistingMapping = list.stream()
75-
.filter(mapping -> mapping.heimatTaskId == heimatMappings.get()
73+
.filter(mapping -> mapping.heimatTaskId == optHeimatMapping.get()
7674
.getExternalTaskId())
7775
.findAny();
7876
optionalAlreadyBookedTimes = heimatTimes.stream()
79-
.filter(heimatTime -> heimatMappings.stream()
77+
.filter(heimatTime -> optHeimatMapping.stream()
8078
.anyMatch(
8179
hm -> heimatTime.taskId()
8280
== hm.getExternalTaskId()))
@@ -105,11 +103,11 @@ public List<Mapping> getTableRows(final LocalDate currentReportDate, final List<
105103
String canBeSynced;
106104
if (!isMappedInHeimat) {
107105
canBeSynced = "Not mapped in Heimat";
108-
} else if (heimatTasks.stream().noneMatch(ht -> ht.id() == heimatMappings.get().getExternalTaskId())) {
106+
} else if (heimatTasks.stream().noneMatch(ht -> ht.id() == optHeimatMapping.get().getExternalTaskId())) {
109107
canBeSynced = "Heimat Task is no longer available.";
110108
isMappedInHeimat = false;
111109
} else {
112-
final ExternalProjectMapping externalProjectMapping = heimatMappings.get();
110+
final ExternalProjectMapping externalProjectMapping = optHeimatMapping.get();
113111
canBeSynced = "Sync to " + externalProjectMapping.getExternalTaskName() + "("
114112
+ externalProjectMapping.getExternalProjectName() + ")";
115113
}
@@ -118,7 +116,7 @@ public List<Mapping> getTableRows(final LocalDate currentReportDate, final List<
118116
final Mapping existingMapping = optionalExistingMapping.get();
119117
final ArrayList<Project> projects = new ArrayList<>(existingMapping.projects());
120118
projects.add(project);
121-
final Mapping mapping = new Mapping(isMappedInHeimat ? heimatMappings.get().getExternalTaskId() : -1,
119+
final Mapping mapping = new Mapping(isMappedInHeimat ? optHeimatMapping.get().getExternalTaskId() : -1,
122120
isMappedInHeimat, canBeSynced, existingMapping.existingTimes(), projects,
123121
existingMapping.heimatNotes(),
124122
existingMapping.keeptimeNotes() + ". " + keeptimeNotes,
@@ -128,13 +126,27 @@ public List<Mapping> getTableRows(final LocalDate currentReportDate, final List<
128126
list.add(mapping);
129127
} else {
130128
final List<Project> projects = Collections.singletonList(project);
131-
final Mapping mapping = new Mapping(isMappedInHeimat ? heimatMappings.get().getExternalTaskId() : -1,
129+
final Mapping mapping = new Mapping(isMappedInHeimat ? optHeimatMapping.get().getExternalTaskId() : -1,
132130
isMappedInHeimat, canBeSynced, optionalAlreadyBookedTimes, projects, heimatNotes, keeptimeNotes,
133131
heimatTimeSeconds, projectWorkSeconds);
134132
list.add(mapping);
135133
}
136134
}
137-
135+
final List<Long> mappedIds = mappedProjects.stream().map(ExternalProjectMapping::getExternalTaskId).toList();
136+
final Map<Long, List<HeimatTime>> notMappedExistingTimes = heimatTimes.stream().filter(ht -> !mappedIds.contains(ht.taskId())).collect(
137+
Collectors.groupingBy(HeimatTime::taskId));
138+
notMappedExistingTimes.forEach((id, times)->{
139+
String heimatNotes = times.stream()
140+
.map(HeimatTime::note)
141+
.collect(Collectors.joining(". "));
142+
long heimatTimeSeconds = times.stream()
143+
.reduce(0L, (subtotal, element) -> subtotal
144+
+ element.durationInMinutes() * 60L, Long::sum);
145+
final Mapping mapping = new Mapping(id,
146+
false, "Not mapped in KeepTime", times, new ArrayList<>(0), heimatNotes, "",
147+
heimatTimeSeconds, 0);
148+
list.add(mapping);
149+
});
138150
return list;
139151
}
140152

src/test/java/de/doubleslash/keeptime/controller/HeimatControllerTest.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,39 @@ void shouldCombineAllWorksAndProjectsWhenMultipleProjectsAreMappedToSame() {
148148
() -> assertThat(mapping.projects(), Matchers.containsInAnyOrder(workProject1, workProject2)));
149149
}
150150

151-
// shouldShowHeimatTimeWhenProjectIsNotMappedInKeeptime? (to know absolute time for the day)
151+
// shouldDisableShouldBeSyncedWhenAlreadyPresentInHeimat
152152

153+
@Test
154+
void shouldShowHeimatTimeWhenProjectIsNotMappedInKeeptime() {
155+
// ARRANGE
156+
final HeimatTime existingTime1 = new HeimatTime(project1To1Mapping.getExternalTaskId(), now.toLocalDate(), null,
157+
null, 60, "Existing note 1", 12);
158+
when(mockedHeimatAPI.getMyTimes(now.toLocalDate())).thenReturn(Arrays.asList(existingTime1));
159+
// there could be more than 1 time for a task in heimat (e.g. when manually saved with start,end feature)
160+
final HeimatTime existingTime2 = new HeimatTime(project1To1Mapping.getExternalTaskId(), now.toLocalDate(), null,
161+
null, 30, "Existing note 2", 13);
162+
when(mockedHeimatAPI.getMyTimes(now.toLocalDate())).thenReturn(Arrays.asList(existingTime1, existingTime2));
163+
164+
// ACT
165+
final List<HeimatController.Mapping> tableRows = heimatController.getTableRows(now.toLocalDate(), workItems);
166+
final HeimatController.Mapping mapping = tableRows.get(0);
167+
168+
// ASSERT
169+
assertAll(() -> assertFalse(mapping.canBeSynced()),
170+
() -> assertThat(mapping.syncMessage(), Matchers.containsString("Not mapped in KeepTime")),
171+
() -> assertThat(mapping.keeptimeSeconds(), Matchers.is(0L)),
172+
() -> assertThat(mapping.keeptimeNotes(), Matchers.is("")),
173+
() -> assertThat(mapping.projects().size(), Matchers.is(0)),
174+
() -> assertThat(mapping.heimatNotes(), Matchers.is("Existing note 1. Existing note 2")),
175+
() -> assertThat(mapping.heimatSeconds(), Matchers.is((60 + 30) * 60L)),
176+
() -> assertThat(mapping.existingTimes(), Matchers.containsInAnyOrder(existingTime1, existingTime2))
177+
//
178+
);
179+
}
153180
// Save
154181
// shouldSaveTimes
155182
// shouldDeleteExistingTimesBeforeSavingWhenTimesAlreadyExist
156183
// shouldContinueOnErrorAndReturnErrorsWhenErrorsOccurred
157-
// shouldOnlyUpdateHeimatWhenSomethingHasChanged
184+
// shouldOnlyUpdateHeimatWhenSomethingHasChanged (not needed - user should decide)
158185

159186
}

0 commit comments

Comments
 (0)