Skip to content

Commit 42e1df4

Browse files
authored
Merge pull request ioet#381 from ioet/346-fixing-report-time
closes ioet#346 fixing report time
2 parents 8394861 + c861258 commit 42e1df4

File tree

10 files changed

+81
-68
lines changed

10 files changed

+81
-68
lines changed

src/app/modules/reports/components/time-range-form/time-range-form.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {FormControl, FormGroup} from '@angular/forms';
33
import * as entryActions from '../../../time-clock/store/entry.actions';
44
import {Store} from '@ngrx/store';
55
import {EntryState} from '../../../time-clock/store/entry.reducer';
6+
import * as moment from 'moment';
67

78
@Component({
89
selector: 'app-time-range-form',
@@ -22,9 +23,8 @@ export class TimeRangeFormComponent {
2223

2324
onSubmit() {
2425
this.store.dispatch(new entryActions.LoadEntriesByTimeRange({
25-
start_date: this.startDate.value,
26-
end_date: this.endDate.value,
27-
user_id: '*',
26+
start_date: moment(this.startDate.value).startOf('day'),
27+
end_date: moment(this.endDate.value).endOf('day'),
2828
}));
2929
}
3030
}

src/app/modules/reports/components/time-range-form/time-range.component.spec.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {EntryState} from '../../../time-clock/store/entry.reducer';
55
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
66
import {InputDateComponent} from '../../../shared/components/input-date/input-date.component';
77
import * as entryActions from '../../../time-clock/store/entry.actions';
8+
import * as moment from 'moment';
89

910
describe('Reports Page', () => {
1011
describe('TimeRangeFormComponent', () => {
@@ -54,20 +55,17 @@ describe('Reports Page', () => {
5455
});
5556

5657
it('when submitting form a new LoadEntriesByTimeRange action is triggered', () => {
57-
58-
const startDateValue = new Date();
59-
const endDateValue = new Date();
60-
endDateValue.setMonth(1);
58+
const yesterday = moment(new Date()).subtract(1, 'days');
59+
const today = moment(new Date());
6160
spyOn(store, 'dispatch');
62-
component.reportForm.controls.startDate.setValue(startDateValue);
63-
component.reportForm.controls.endDate.setValue(endDateValue);
61+
component.reportForm.controls.startDate.setValue(yesterday);
62+
component.reportForm.controls.endDate.setValue(today);
6463

6564
component.onSubmit();
6665

6766
expect(store.dispatch).toHaveBeenCalledWith(new entryActions.LoadEntriesByTimeRange({
68-
start_date: startDateValue,
69-
end_date: endDateValue,
70-
user_id: '*',
67+
start_date: yesterday.startOf('day'),
68+
end_date: today.endOf('day')
7169
}));
7270
});
7371

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import {Moment} from 'moment';
2+
13
export interface TimeEntriesTimeRange {
2-
start_date: Date;
3-
end_date: Date;
4-
user_id: string;
4+
start_date: Moment;
5+
end_date: Moment;
56
}

src/app/modules/time-clock/services/entry.service.spec.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
2-
import {inject, TestBed} from '@angular/core/testing';
1+
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2+
import { inject, TestBed } from '@angular/core/testing';
33

4-
import {EntryService} from './entry.service';
5-
import {NewEntry} from '../../shared/models';
6-
import {DatePipe} from '@angular/common';
7-
import {TimeEntriesTimeRange} from '../models/time-entries-time-range';
4+
import { EntryService } from './entry.service';
5+
import { NewEntry } from '../../shared/models';
6+
import { DatePipe } from '@angular/common';
7+
import { TimeEntriesTimeRange } from '../models/time-entries-time-range';
8+
import * as moment from 'moment';
89

910
describe('EntryService', () => {
1011
let service: EntryService;
@@ -89,17 +90,18 @@ describe('EntryService', () => {
8990
});
9091

9192
it('when getting time entries for report, time range should be sent', () => {
92-
const startDateValue = new Date();
93-
const endDateValue = new Date();
93+
const yesterday = moment(new Date()).subtract(1, 'day');
94+
const today = moment(new Date());
9495
const pipe: DatePipe = new DatePipe('en');
95-
const timeRange: TimeEntriesTimeRange = {start_date: startDateValue, end_date: endDateValue, user_id: '*'};
96+
const timeRange: TimeEntriesTimeRange = {start_date: yesterday, end_date: today};
97+
const userId = '123';
9698

97-
service.loadEntriesByTimeRange(timeRange).subscribe();
99+
service.loadEntriesByTimeRange(timeRange, userId).subscribe();
98100

99101
const loadEntryRequest = httpMock.expectOne(req => req.method === 'GET' && req.url === service.baseUrl);
100-
101-
expect(loadEntryRequest.request.params.get('start_date')).toBe(pipe.transform(startDateValue,
102+
expect(loadEntryRequest.request.params.get('start_date')).toBe(pipe.transform(yesterday,
102103
EntryService.TIME_ENTRIES_DATE_TIME_FORMAT));
103-
expect(loadEntryRequest.request.params.get('end_date')).toBe(pipe.transform(endDateValue, EntryService.TIME_ENTRIES_DATE_TIME_FORMAT));
104+
expect(loadEntryRequest.request.params.get('end_date')).toBe(pipe.transform(today, EntryService.TIME_ENTRIES_DATE_TIME_FORMAT));
105+
expect(loadEntryRequest.request.params.get('user_id')).toEqual('123');
104106
});
105107
});

src/app/modules/time-clock/services/entry.service.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import {TimeEntriesSummary} from '../models/time.entry.summary';
2-
import {Injectable} from '@angular/core';
3-
import {HttpClient} from '@angular/common/http';
1+
import { TimeEntriesSummary } from '../models/time.entry.summary';
2+
import { Injectable } from '@angular/core';
3+
import { HttpClient } from '@angular/common/http';
44

5-
import {Observable} from 'rxjs';
6-
import {environment} from './../../../../environments/environment';
7-
import {TimeEntriesTimeRange} from '../models/time-entries-time-range';
8-
import {DatePipe} from '@angular/common';
5+
import { Observable } from 'rxjs';
6+
import { environment } from './../../../../environments/environment';
7+
import { TimeEntriesTimeRange } from '../models/time-entries-time-range';
8+
import { DatePipe } from '@angular/common';
99

1010
@Injectable({
1111
providedIn: 'root',
@@ -15,7 +15,7 @@ export class EntryService {
1515
constructor(private http: HttpClient, private datePipe: DatePipe) {
1616
}
1717

18-
static TIME_ENTRIES_DATE_TIME_FORMAT = 'yyyy-MM-ddThh:mm:ssZZZZZ';
18+
static TIME_ENTRIES_DATE_TIME_FORMAT = 'yyyy-MM-ddTHH:mm:ssZZZZZ';
1919
baseUrl = `${environment.timeTrackerApiUrl}/time-entries`;
2020

2121
loadActiveEntry(): Observable<any> {
@@ -50,13 +50,13 @@ export class EntryService {
5050
return this.http.get<TimeEntriesSummary>(summaryUrl);
5151
}
5252

53-
loadEntriesByTimeRange(range: TimeEntriesTimeRange): Observable<any> {
53+
loadEntriesByTimeRange(range: TimeEntriesTimeRange, userId: string): Observable<any> {
5454
return this.http.get(this.baseUrl,
5555
{
5656
params: {
5757
start_date: this.datePipe.transform(range.start_date, EntryService.TIME_ENTRIES_DATE_TIME_FORMAT),
5858
end_date: this.datePipe.transform(range.end_date, EntryService.TIME_ENTRIES_DATE_TIME_FORMAT),
59-
user_id: range.user_id
59+
user_id: userId
6060
}
6161
}
6262
);

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import * as actions from './entry.actions';
2+
import * as moment from 'moment';
3+
import { DatePipe } from '@angular/common';
4+
import { TimeEntriesTimeRange } from '../models/time-entries-time-range';
25

36
describe('Actions for Entries', () => {
47

@@ -83,8 +86,14 @@ describe('Actions for Entries', () => {
8386
});
8487

8588
it('LoadEntriesByTimeRange type is EntryActionTypes.LOAD_ENTRIES_BY_TIME_RANGE', () => {
86-
const action = new actions.LoadEntriesByTimeRange(null);
89+
const yesterday = moment(new Date()).subtract(1, 'day');
90+
const today = moment(new Date());
91+
const pipe: DatePipe = new DatePipe('en');
92+
const timeRange: TimeEntriesTimeRange = {start_date: yesterday, end_date: today};
93+
const action = new actions.LoadEntriesByTimeRange(timeRange);
8794
expect(action.type).toEqual(actions.EntryActionTypes.LOAD_ENTRIES_BY_TIME_RANGE);
95+
expect(action.timeRange).toEqual(timeRange);
96+
expect(action.userId).toEqual('*');
8897
});
8998

9099
it('LoadEntriesByTimeRangeSuccess type is EntryActionTypes.LOAD_ENTRIES_BY_TIME_RANGE_SUCCESS', () => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export class DefaultEntry implements Action {
167167

168168
export class LoadEntriesByTimeRange implements Action {
169169
public readonly type = EntryActionTypes.LOAD_ENTRIES_BY_TIME_RANGE;
170-
constructor(readonly timeRange: TimeEntriesTimeRange) {
170+
constructor(readonly timeRange: TimeEntriesTimeRange, readonly userId: string= '*') {
171171
}
172172
}
173173

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

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { INFO_SAVED_SUCCESSFULLY } from './../../shared/messages';
2-
import {TestBed} from '@angular/core/testing';
3-
import {provideMockActions} from '@ngrx/effects/testing';
4-
import {EntryEffects} from './entry.effects';
5-
import {Observable, of, throwError} from 'rxjs';
6-
import {HttpClientTestingModule} from '@angular/common/http/testing';
2+
import { TestBed } from '@angular/core/testing';
3+
import { provideMockActions } from '@ngrx/effects/testing';
4+
import { EntryEffects } from './entry.effects';
5+
import { Observable, of, throwError } from 'rxjs';
6+
import { HttpClientTestingModule } from '@angular/common/http/testing';
77
import { ToastrModule, ToastrService } from 'ngx-toastr';
8-
import {Action} from '@ngrx/store';
9-
import {DatePipe} from '@angular/common';
10-
import {EntryActionTypes} from './entry.actions';
11-
import {EntryService} from '../services/entry.service';
12-
import {TimeEntriesTimeRange} from '../models/time-entries-time-range';
8+
import { Action } from '@ngrx/store';
9+
import { DatePipe } from '@angular/common';
10+
import { EntryActionTypes } from './entry.actions';
11+
import { EntryService } from '../services/entry.service';
12+
import { TimeEntriesTimeRange } from '../models/time-entries-time-range';
13+
import * as moment from 'moment';
1314

1415
describe('TimeEntryActionEffects', () => {
1516

@@ -37,7 +38,7 @@ describe('TimeEntryActionEffects', () => {
3738
expect(effects).toBeTruthy();
3839
});
3940

40-
it('returns an action with type LOAD_ENTRIES_SUMMARY_SUCCESS when the service returns a value', () => {
41+
it('returns an action with type LOAD_ENTRIES_SUMMARY_SUCCESS when the service returns a value', () => {
4142
actions$ = of({type: EntryActionTypes.LOAD_ENTRIES_SUMMARY});
4243
const serviceSpy = spyOn(service, 'summary');
4344
serviceSpy.and.returnValue(of({}));
@@ -47,7 +48,7 @@ describe('TimeEntryActionEffects', () => {
4748
});
4849
});
4950

50-
it('returns an action with type LOAD_ENTRIES_SUMMARY_FAIL when the service fails', () => {
51+
it('returns an action with type LOAD_ENTRIES_SUMMARY_FAIL when the service fails', () => {
5152
actions$ = of({type: EntryActionTypes.LOAD_ENTRIES_SUMMARY});
5253
spyOn(service, 'summary').and.returnValue(throwError('any error'));
5354

@@ -56,9 +57,10 @@ describe('TimeEntryActionEffects', () => {
5657
});
5758
});
5859

59-
it('When the service returns a value, then LOAD_ENTRIES_BY_TIME_RANGE_SUCCESS should be triggered', () => {
60-
const timeRange: TimeEntriesTimeRange = {start_date: new Date(), end_date: new Date(), user_id: '*' };
61-
actions$ = of({type: EntryActionTypes.LOAD_ENTRIES_BY_TIME_RANGE, timeRange});
60+
it('When the service returns a value, then LOAD_ENTRIES_BY_TIME_RANGE_SUCCESS should be triggered', () => {
61+
const timeRange: TimeEntriesTimeRange = {start_date: moment(new Date()), end_date: moment(new Date())};
62+
const userId = '*';
63+
actions$ = of({type: EntryActionTypes.LOAD_ENTRIES_BY_TIME_RANGE, timeRange, userId});
6264
const serviceSpy = spyOn(service, 'loadEntriesByTimeRange');
6365
serviceSpy.and.returnValue(of([]));
6466

@@ -69,13 +71,14 @@ describe('TimeEntryActionEffects', () => {
6971
});
7072

7173
it('When the service fails, then LOAD_ENTRIES_BY_TIME_RANGE_FAIL should be triggered', async () => {
72-
const timeRange: TimeEntriesTimeRange = {start_date: new Date(), end_date: new Date(), user_id: '*'};
73-
actions$ = of({type: EntryActionTypes.LOAD_ENTRIES_BY_TIME_RANGE, timeRange});
74-
spyOn(service, 'loadEntriesByTimeRange').and.returnValue(throwError('any error'));
74+
const timeRange: TimeEntriesTimeRange = {start_date: moment(new Date()), end_date: moment(new Date())};
75+
const userId = '*';
76+
actions$ = of({type: EntryActionTypes.LOAD_ENTRIES_BY_TIME_RANGE, timeRange, userId});
77+
spyOn(service, 'loadEntriesByTimeRange').and.returnValue(throwError('any error'));
7578

76-
effects.loadEntriesByTimeRange$.subscribe(action => {
77-
expect(action.type).toEqual(EntryActionTypes.LOAD_ENTRIES_BY_TIME_RANGE_FAIL);
78-
});
79+
effects.loadEntriesByTimeRange$.subscribe(action => {
80+
expect(action.type).toEqual(EntryActionTypes.LOAD_ENTRIES_BY_TIME_RANGE_FAIL);
81+
});
7982
});
8083

8184
it('returns a LOAD_ACTIVE_ENTRY_SUCCESS when the entry that is running it is in the same day', async () => {
@@ -91,7 +94,7 @@ describe('TimeEntryActionEffects', () => {
9194

9295
it('returns a LOAD_ACTIVE_ENTRY_SUCCESS when the entry that is running it is in the same day', async () => {
9396
const startDateInPast = new Date();
94-
startDateInPast.setDate( startDateInPast.getDate() - 5);
97+
startDateInPast.setDate(startDateInPast.getDate() - 5);
9598
const activeEntry = {start_date: startDateInPast};
9699
actions$ = of({type: EntryActionTypes.LOAD_ACTIVE_ENTRY, activeEntry});
97100
const serviceSpy = spyOn(service, 'loadActiveEntry');

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ export class EntryEffects {
168168
@Effect()
169169
loadEntriesByTimeRange$: Observable<Action> = this.actions$.pipe(
170170
ofType(actions.EntryActionTypes.LOAD_ENTRIES_BY_TIME_RANGE),
171-
map((action: actions.LoadEntriesByTimeRange) => action.timeRange),
172-
mergeMap((timeRange) =>
173-
this.entryService.loadEntriesByTimeRange(timeRange).pipe(
171+
map((action: actions.LoadEntriesByTimeRange) => action),
172+
mergeMap((action) =>
173+
this.entryService.loadEntriesByTimeRange(action.timeRange, action.userId).pipe(
174174
map((response) => {
175175
return new actions.LoadEntriesByTimeRangeSuccess(response);
176176
}),

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {TimeDetails, TimeEntriesSummary} from '../models/time.entry.summary';
2-
import {Entry, NewEntry} from './../../shared/models';
1+
import { TimeDetails, TimeEntriesSummary } from '../models/time.entry.summary';
2+
import { Entry, NewEntry } from './../../shared/models';
33
import * as actions from './entry.actions';
4-
import {entryReducer, EntryState} from './entry.reducer';
4+
import { entryReducer, EntryState } from './entry.reducer';
55

66
describe('entryReducer', () => {
77

0 commit comments

Comments
 (0)