Skip to content

Commit b2a5093

Browse files
author
Juan Gabriel Guzman
committed
feat: #204 Adding service method to query time entries by time range
1 parent 652a5f5 commit b2a5093

File tree

3 files changed

+70
-34
lines changed

3 files changed

+70
-34
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface TimeEntriesTimeRange {
2+
start_date: Date;
3+
end_date: Date;
4+
}
Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
1-
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2-
import { TestBed, inject } from '@angular/core/testing';
1+
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
2+
import {TestBed, inject} from '@angular/core/testing';
33

4-
import { EntryService } from './entry.service';
5-
import { NewEntry } from '../../shared/models';
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';
68

79
describe('EntryService', () => {
810
let service: EntryService;
911
let httpMock: HttpTestingController;
12+
let datePipe: DatePipe;
1013

1114
beforeEach(() => {
12-
TestBed.configureTestingModule({ imports: [HttpClientTestingModule] });
15+
TestBed.configureTestingModule({imports: [HttpClientTestingModule], providers: [DatePipe]});
1316
service = TestBed.inject(EntryService);
1417
httpMock = TestBed.inject(HttpTestingController);
18+
datePipe = TestBed.inject(DatePipe);
19+
service.baseUrl = 'time-entries';
20+
1521
});
1622

1723
it('services are ready to be used', inject(
18-
[HttpClientTestingModule, EntryService],
19-
(httpClient: HttpClientTestingModule, entryService: EntryService) => {
24+
[HttpClientTestingModule, EntryService, DatePipe],
25+
(httpClient: HttpClientTestingModule, entryService: EntryService, datePipes: DatePipe) => {
2026
expect(entryService).toBeTruthy();
2127
expect(httpClient).toBeTruthy();
28+
expect(datePipes).toBeTruthy();
2229
}
2330
));
2431

2532
it('create entry using POST from baseUrl', () => {
26-
service.baseUrl = 'time-entries';
27-
const entry: NewEntry[] = [{ project_id: '1', start_date: new Date().toISOString() }];
33+
const entry: NewEntry[] = [{project_id: '1', start_date: new Date().toISOString()}];
2834

2935
service.createEntry(entry).subscribe((response) => {
3036
expect(response.length).toBe(1);
@@ -35,58 +41,70 @@ describe('EntryService', () => {
3541
createEntryRequest.flush(entry);
3642
});
3743

38-
3944
it('loads an activeEntry with /running', () => {
40-
service.baseUrl = 'time-entries';
41-
4245
service.loadActiveEntry().subscribe((response) => {
43-
const loadEntryRequest = httpMock.expectOne(`${service.baseUrl}/running`);
44-
expect(loadEntryRequest.request.method).toBe('GET');
4546
});
47+
48+
const loadEntryRequest = httpMock.expectOne(`${service.baseUrl}/running`);
49+
expect(loadEntryRequest.request.method).toBe('GET');
4650
});
4751

4852
it('loads summary with get /summary', () => {
49-
service.baseUrl = 'time-entries';
50-
5153
service.summary().subscribe((response) => {
52-
const loadEntryRequest = httpMock.expectOne(`${service.baseUrl}/summary`);
53-
expect(loadEntryRequest.request.method).toBe('GET');
5454
});
55+
56+
const loadEntryRequest = httpMock.expectOne(`${service.baseUrl}/summary`);
57+
expect(loadEntryRequest.request.method).toBe('GET');
5558
});
5659

5760
it('loads all Entries', () => {
58-
service.baseUrl = 'time-entries';
5961
service.loadEntries(new Date().getMonth).subscribe((response) => {
60-
const loadEntryRequest = httpMock.expectOne(`${service.baseUrl}`);
61-
expect(loadEntryRequest.request.method).toBe('GET');
6262
});
63+
64+
const loadEntryRequest = httpMock.expectOne(`${service.baseUrl}`);
65+
expect(loadEntryRequest.request.method).toBe('GET');
66+
6367
});
6468

6569
it('update an entry using PUT', () => {
66-
service.baseUrl = 'time-entries';
67-
6870
const updatedEntry = {foo: 'bar', id: 'id'};
71+
6972
service.updateActiveEntry(updatedEntry).subscribe((response) => {
70-
const updateEntryRequest = httpMock.expectOne(`${service.baseUrl}/id`);
71-
expect(updateEntryRequest.request.method).toBe('PUT');
73+
7274
});
75+
76+
const updateEntryRequest = httpMock.expectOne(`${service.baseUrl}/id`);
77+
expect(updateEntryRequest.request.method).toBe('PUT');
7378
});
7479

7580
it('delete an entry using DELETE', () => {
76-
service.baseUrl = 'time-entries';
7781
const entry = 'entryId';
7882
service.deleteEntry(entry).subscribe((response) => {
79-
const updateEntryRequest = httpMock.expectOne(`${service.baseUrl}/${entry}`);
80-
expect(updateEntryRequest.request.method).toBe('DELETE');
8183
});
84+
const updateEntryRequest = httpMock.expectOne(`${service.baseUrl}/${entry}`);
85+
expect(updateEntryRequest.request.method).toBe('DELETE');
8286
});
8387

8488
it('stops an entry using POST', () => {
85-
service.baseUrl = 'time-entries';
86-
8789
service.stopEntryRunning('id').subscribe((response) => {
88-
const updateEntryRequest = httpMock.expectOne(`${service.baseUrl}/id/stop`);
89-
expect(updateEntryRequest.request.method).toBe('POST');
9090
});
91+
92+
const updateEntryRequest = httpMock.expectOne(`${service.baseUrl}/id/stop`);
93+
expect(updateEntryRequest.request.method).toBe('POST');
94+
});
95+
96+
it('when getting time entries for report, time range should be sent', () => {
97+
const startDateValue = new Date();
98+
const endDateValue = new Date();
99+
const pipe: DatePipe = new DatePipe('en');
100+
const timeRange: TimeEntriesTimeRange = {start_date: startDateValue, end_date: endDateValue};
101+
102+
service.loadEntriesByTimeRange(timeRange).subscribe();
103+
104+
const loadEntryRequest = httpMock.expectOne(req => req.method === 'GET' && req.url === service.baseUrl);
105+
106+
expect(loadEntryRequest.request.params.get('start_date')).toBe(pipe.transform(startDateValue,
107+
EntryService.TIME_ENTRIES_DATE_TIME_FORMAT));
108+
expect(loadEntryRequest.request.params.get('end_date')).toBe(pipe.transform(endDateValue, EntryService.TIME_ENTRIES_DATE_TIME_FORMAT));
91109
});
92110
});

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ import { HttpClient } from '@angular/common/http';
44

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

810
@Injectable({
911
providedIn: 'root',
1012
})
1113
export class EntryService {
12-
baseUrl = `${environment.timeTrackerApiUrl}/time-entries`;
1314

14-
constructor(private http: HttpClient) {}
15+
constructor(private http: HttpClient, private datePipe: DatePipe) {}
16+
static TIME_ENTRIES_DATE_TIME_FORMAT = 'yyyy-MM-ddThh:mm:ssZZZZZ';
17+
baseUrl = `${environment.timeTrackerApiUrl}/time-entries`;
1518

1619
loadActiveEntry(): Observable<any> {
1720
return this.http.get(`${this.baseUrl}/running`);
@@ -45,4 +48,15 @@ export class EntryService {
4548
return this.http.get<TimeEntriesSummary>(summaryUrl);
4649
}
4750

51+
loadEntriesByTimeRange(range: TimeEntriesTimeRange): Observable<any> {
52+
53+
return this.http.get(this.baseUrl,
54+
{
55+
params: {
56+
start_date: this.datePipe.transform(range.start_date, EntryService.TIME_ENTRIES_DATE_TIME_FORMAT),
57+
end_date: this.datePipe.transform(range.end_date, EntryService.TIME_ENTRIES_DATE_TIME_FORMAT)
58+
}
59+
}
60+
);
61+
}
4862
}

0 commit comments

Comments
 (0)