diff --git a/src/app/modules/time-clock/store/entry.actions.ts b/src/app/modules/time-clock/store/entry.actions.ts index 9116816e5..5783a76a2 100644 --- a/src/app/modules/time-clock/store/entry.actions.ts +++ b/src/app/modules/time-clock/store/entry.actions.ts @@ -52,7 +52,7 @@ export class LoadActiveEntry implements Action { export class LoadActiveEntrySuccess implements Action { readonly type = EntryActionTypes.LOAD_ACTIVE_ENTRY_SUCCESS; - constructor(readonly payload: NewEntry[]) {} + constructor(readonly payload) {} } export class LoadActiveEntryFail implements Action { diff --git a/src/app/modules/time-clock/store/entry.effects.spec.ts b/src/app/modules/time-clock/store/entry.effects.spec.ts index ea840229e..447b9e889 100644 --- a/src/app/modules/time-clock/store/entry.effects.spec.ts +++ b/src/app/modules/time-clock/store/entry.effects.spec.ts @@ -56,6 +56,30 @@ describe('TimeEntryActionEffects', () => { }); }); + it('returns a LOAD_ACTIVE_ENTRY_SUCCESS when the entry that is running it is in the same day', async () => { + const activeEntry = {start_date: new Date()}; + actions$ = of({type: EntryActionTypes.LOAD_ACTIVE_ENTRY, activeEntry}); + const serviceSpy = spyOn(service, 'loadActiveEntry'); + serviceSpy.and.returnValue(of(activeEntry)); + + effects.loadActiveEntry$.subscribe(action => { + expect(action.type).toEqual(EntryActionTypes.LOAD_ACTIVE_ENTRY_SUCCESS); + }); + }); + + it('returns a LOAD_ACTIVE_ENTRY_SUCCESS when the entry that is running it is in the same day', async () => { + const startDateInPast = new Date(); + startDateInPast.setDate( startDateInPast.getDate() - 5); + const activeEntry = {start_date: startDateInPast}; + actions$ = of({type: EntryActionTypes.LOAD_ACTIVE_ENTRY, activeEntry}); + const serviceSpy = spyOn(service, 'loadActiveEntry'); + serviceSpy.and.returnValue(of(activeEntry)); + + effects.loadActiveEntry$.subscribe(action => { + expect(action.type).toEqual(EntryActionTypes.UPDATE_ACTIVE_ENTRY); + }); + }); + // TODO Implement the remaining unit tests for the other effects. }); diff --git a/src/app/modules/time-clock/store/entry.effects.ts b/src/app/modules/time-clock/store/entry.effects.ts index f4218653f..51956b1bb 100644 --- a/src/app/modules/time-clock/store/entry.effects.ts +++ b/src/app/modules/time-clock/store/entry.effects.ts @@ -34,7 +34,21 @@ export class EntryEffects { mergeMap(() => this.entryService.loadActiveEntry().pipe( map((activeEntry) => { - return new actions.LoadActiveEntrySuccess(activeEntry); + if (activeEntry) { + const today = new Date(); + const entryStartDate = new Date (activeEntry.start_date); + const isSameDay = (today.getDate() === entryStartDate.getDate() + && today.getMonth() === entryStartDate.getMonth() + && today.getFullYear() === entryStartDate.getFullYear()); + if (isSameDay) { + return new actions.LoadActiveEntrySuccess(activeEntry); + } else { + const endDate = activeEntry.start_date; + endDate.setHours(23, 59, 59); + activeEntry.end_date = new Date(endDate.toISOString()); + return new actions.UpdateActiveEntry(activeEntry); + } + } }), catchError((error) => { return of(new actions.LoadActiveEntryFail(error));