Skip to content

Commit c861258

Browse files
author
Juan Gabriel Guzman
committed
refactor: #346 Extracting userId out from timeRange object
1 parent 778d55d commit c861258

File tree

10 files changed

+54
-45
lines changed

10 files changed

+54
-45
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export class TimeRangeFormComponent {
2525
this.store.dispatch(new entryActions.LoadEntriesByTimeRange({
2626
start_date: moment(this.startDate.value).startOf('day'),
2727
end_date: moment(this.endDate.value).endOf('day'),
28-
user_id: '*',
2928
}));
3029
}
3130
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ describe('Reports Page', () => {
6565

6666
expect(store.dispatch).toHaveBeenCalledWith(new entryActions.LoadEntriesByTimeRange({
6767
start_date: yesterday.startOf('day'),
68-
end_date: today.endOf('day'),
69-
user_id: '*',
68+
end_date: today.endOf('day')
7069
}));
7170
});
7271

src/app/modules/time-clock/models/time-entries-time-range.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ import {Moment} from 'moment';
33
export interface TimeEntriesTimeRange {
44
start_date: Moment;
55
end_date: Moment;
6-
user_id: string;
76
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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';
88
import * as moment from 'moment';
99

1010
describe('EntryService', () => {
@@ -93,14 +93,15 @@ describe('EntryService', () => {
9393
const yesterday = moment(new Date()).subtract(1, 'day');
9494
const today = moment(new Date());
9595
const pipe: DatePipe = new DatePipe('en');
96-
const timeRange: TimeEntriesTimeRange = {start_date: yesterday, end_date: today, user_id: '*'};
96+
const timeRange: TimeEntriesTimeRange = {start_date: yesterday, end_date: today};
97+
const userId = '123';
9798

98-
service.loadEntriesByTimeRange(timeRange).subscribe();
99+
service.loadEntriesByTimeRange(timeRange, userId).subscribe();
99100

100101
const loadEntryRequest = httpMock.expectOne(req => req.method === 'GET' && req.url === service.baseUrl);
101-
102102
expect(loadEntryRequest.request.params.get('start_date')).toBe(pipe.transform(yesterday,
103103
EntryService.TIME_ENTRIES_DATE_TIME_FORMAT));
104104
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');
105106
});
106107
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
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';
1313
import * as moment from 'moment';
1414

1515
describe('TimeEntryActionEffects', () => {
@@ -38,7 +38,7 @@ describe('TimeEntryActionEffects', () => {
3838
expect(effects).toBeTruthy();
3939
});
4040

41-
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', () => {
4242
actions$ = of({type: EntryActionTypes.LOAD_ENTRIES_SUMMARY});
4343
const serviceSpy = spyOn(service, 'summary');
4444
serviceSpy.and.returnValue(of({}));
@@ -48,7 +48,7 @@ describe('TimeEntryActionEffects', () => {
4848
});
4949
});
5050

51-
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', () => {
5252
actions$ = of({type: EntryActionTypes.LOAD_ENTRIES_SUMMARY});
5353
spyOn(service, 'summary').and.returnValue(throwError('any error'));
5454

@@ -57,9 +57,10 @@ describe('TimeEntryActionEffects', () => {
5757
});
5858
});
5959

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()), user_id: '*' };
62-
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});
6364
const serviceSpy = spyOn(service, 'loadEntriesByTimeRange');
6465
serviceSpy.and.returnValue(of([]));
6566

@@ -70,13 +71,14 @@ describe('TimeEntryActionEffects', () => {
7071
});
7172

7273
it('When the service fails, then LOAD_ENTRIES_BY_TIME_RANGE_FAIL should be triggered', async () => {
73-
const timeRange: TimeEntriesTimeRange = {start_date: moment(new Date()), end_date: moment(new Date()), user_id: '*'};
74-
actions$ = of({type: EntryActionTypes.LOAD_ENTRIES_BY_TIME_RANGE, timeRange});
75-
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'));
7678

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

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

9395
it('returns a LOAD_ACTIVE_ENTRY_SUCCESS when the entry that is running it is in the same day', async () => {
9496
const startDateInPast = new Date();
95-
startDateInPast.setDate( startDateInPast.getDate() - 5);
97+
startDateInPast.setDate(startDateInPast.getDate() - 5);
9698
const activeEntry = {start_date: startDateInPast};
9799
actions$ = of({type: EntryActionTypes.LOAD_ACTIVE_ENTRY, activeEntry});
98100
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)