Skip to content

Commit 0d5f2a1

Browse files
committed
fix: #322 clock-out automatically
1 parent 2eece4b commit 0d5f2a1

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-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: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Entry } from './../../shared/models/entry.model';
12
import { INFO_SAVED_SUCCESSFULLY, INFO_DELETE_SUCCESSFULLY } from './../../shared/messages';
23
import { Injectable } from '@angular/core';
34
import { ofType, Actions, Effect } from '@ngrx/effects';
@@ -34,7 +35,21 @@ export class EntryEffects {
3435
mergeMap(() =>
3536
this.entryService.loadActiveEntry().pipe(
3637
map((activeEntry) => {
37-
return new actions.LoadActiveEntrySuccess(activeEntry);
38+
if (activeEntry) {
39+
const today = new Date();
40+
const entryStartDate = new Date (activeEntry.start_date);
41+
const isSameDay = (today.getDate() === entryStartDate.getDate()
42+
&& today.getMonth() === entryStartDate.getMonth()
43+
&& today.getFullYear() === entryStartDate.getFullYear());
44+
if (isSameDay) {
45+
return new actions.LoadActiveEntrySuccess(activeEntry);
46+
} else {
47+
const endDate = activeEntry.start_date;
48+
endDate.setHours(23, 59, 59);
49+
activeEntry.end_date = new Date(endDate.toISOString());
50+
return new actions.UpdateActiveEntry(activeEntry);
51+
}
52+
}
3853
}),
3954
catchError((error) => {
4055
return of(new actions.LoadActiveEntryFail(error));

0 commit comments

Comments
 (0)