Skip to content

Commit 759f2b1

Browse files
authored
Merge pull request #324 from ioet/322-clock-out-running-entry
closes: #324 Automate clock outs from the UI
2 parents 2eece4b + b202765 commit 759f2b1

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/app/modules/time-clock/store/entry.actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class LoadActiveEntry implements Action {
5252

5353
export class LoadActiveEntrySuccess implements Action {
5454
readonly type = EntryActionTypes.LOAD_ACTIVE_ENTRY_SUCCESS;
55-
constructor(readonly payload: NewEntry[]) {}
55+
constructor(readonly payload) {}
5656
}
5757

5858
export class LoadActiveEntryFail implements Action {

src/app/modules/time-clock/store/entry.effects.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,30 @@ describe('TimeEntryActionEffects', () => {
5656
});
5757
});
5858

59+
it('returns a LOAD_ACTIVE_ENTRY_SUCCESS when the entry that is running it is in the same day', async () => {
60+
const activeEntry = {start_date: new Date()};
61+
actions$ = of({type: EntryActionTypes.LOAD_ACTIVE_ENTRY, activeEntry});
62+
const serviceSpy = spyOn(service, 'loadActiveEntry');
63+
serviceSpy.and.returnValue(of(activeEntry));
64+
65+
effects.loadActiveEntry$.subscribe(action => {
66+
expect(action.type).toEqual(EntryActionTypes.LOAD_ACTIVE_ENTRY_SUCCESS);
67+
});
68+
});
69+
70+
it('returns a LOAD_ACTIVE_ENTRY_SUCCESS when the entry that is running it is in the same day', async () => {
71+
const startDateInPast = new Date();
72+
startDateInPast.setDate( startDateInPast.getDate() - 5);
73+
const activeEntry = {start_date: startDateInPast};
74+
actions$ = of({type: EntryActionTypes.LOAD_ACTIVE_ENTRY, activeEntry});
75+
const serviceSpy = spyOn(service, 'loadActiveEntry');
76+
serviceSpy.and.returnValue(of(activeEntry));
77+
78+
effects.loadActiveEntry$.subscribe(action => {
79+
expect(action.type).toEqual(EntryActionTypes.UPDATE_ACTIVE_ENTRY);
80+
});
81+
});
82+
5983
// TODO Implement the remaining unit tests for the other effects.
6084

6185
});

src/app/modules/time-clock/store/entry.effects.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,21 @@ export class EntryEffects {
3434
mergeMap(() =>
3535
this.entryService.loadActiveEntry().pipe(
3636
map((activeEntry) => {
37-
return new actions.LoadActiveEntrySuccess(activeEntry);
37+
if (activeEntry) {
38+
const today = new Date();
39+
const entryStartDate = new Date (activeEntry.start_date);
40+
const isSameDay = (today.getDate() === entryStartDate.getDate()
41+
&& today.getMonth() === entryStartDate.getMonth()
42+
&& today.getFullYear() === entryStartDate.getFullYear());
43+
if (isSameDay) {
44+
return new actions.LoadActiveEntrySuccess(activeEntry);
45+
} else {
46+
const endDate = activeEntry.start_date;
47+
endDate.setHours(23, 59, 59);
48+
activeEntry.end_date = new Date(endDate.toISOString());
49+
return new actions.UpdateActiveEntry(activeEntry);
50+
}
51+
}
3852
}),
3953
catchError((error) => {
4054
return of(new actions.LoadActiveEntryFail(error));

0 commit comments

Comments
 (0)