Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: #204 Adding service method to query time entries by time range
  • Loading branch information
Juan Gabriel Guzman committed May 29, 2020
commit b2a5093de0d4722673115cf02e7f5d90f94f423d
4 changes: 4 additions & 0 deletions src/app/modules/time-clock/models/time-entries-time-range.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface TimeEntriesTimeRange {
start_date: Date;
end_date: Date;
}
82 changes: 50 additions & 32 deletions src/app/modules/time-clock/services/entry.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { TestBed, inject } from '@angular/core/testing';
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
import {TestBed, inject} from '@angular/core/testing';

import { EntryService } from './entry.service';
import { NewEntry } from '../../shared/models';
import {EntryService} from './entry.service';
import {NewEntry} from '../../shared/models';
import {DatePipe} from '@angular/common';
import {TimeEntriesTimeRange} from '../models/time-entries-time-range';

describe('EntryService', () => {
let service: EntryService;
let httpMock: HttpTestingController;
let datePipe: DatePipe;

beforeEach(() => {
TestBed.configureTestingModule({ imports: [HttpClientTestingModule] });
TestBed.configureTestingModule({imports: [HttpClientTestingModule], providers: [DatePipe]});
service = TestBed.inject(EntryService);
httpMock = TestBed.inject(HttpTestingController);
datePipe = TestBed.inject(DatePipe);
service.baseUrl = 'time-entries';

});

it('services are ready to be used', inject(
[HttpClientTestingModule, EntryService],
(httpClient: HttpClientTestingModule, entryService: EntryService) => {
[HttpClientTestingModule, EntryService, DatePipe],
(httpClient: HttpClientTestingModule, entryService: EntryService, datePipes: DatePipe) => {
expect(entryService).toBeTruthy();
expect(httpClient).toBeTruthy();
expect(datePipes).toBeTruthy();
}
));

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

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


it('loads an activeEntry with /running', () => {
service.baseUrl = 'time-entries';

service.loadActiveEntry().subscribe((response) => {
const loadEntryRequest = httpMock.expectOne(`${service.baseUrl}/running`);
expect(loadEntryRequest.request.method).toBe('GET');
});

const loadEntryRequest = httpMock.expectOne(`${service.baseUrl}/running`);
expect(loadEntryRequest.request.method).toBe('GET');
});

it('loads summary with get /summary', () => {
service.baseUrl = 'time-entries';

service.summary().subscribe((response) => {
const loadEntryRequest = httpMock.expectOne(`${service.baseUrl}/summary`);
expect(loadEntryRequest.request.method).toBe('GET');
});

const loadEntryRequest = httpMock.expectOne(`${service.baseUrl}/summary`);
expect(loadEntryRequest.request.method).toBe('GET');
});

it('loads all Entries', () => {
service.baseUrl = 'time-entries';
service.loadEntries(new Date().getMonth).subscribe((response) => {
const loadEntryRequest = httpMock.expectOne(`${service.baseUrl}`);
expect(loadEntryRequest.request.method).toBe('GET');
});

const loadEntryRequest = httpMock.expectOne(`${service.baseUrl}`);
expect(loadEntryRequest.request.method).toBe('GET');

});

it('update an entry using PUT', () => {
service.baseUrl = 'time-entries';

const updatedEntry = {foo: 'bar', id: 'id'};

service.updateActiveEntry(updatedEntry).subscribe((response) => {
const updateEntryRequest = httpMock.expectOne(`${service.baseUrl}/id`);
expect(updateEntryRequest.request.method).toBe('PUT');

});

const updateEntryRequest = httpMock.expectOne(`${service.baseUrl}/id`);
expect(updateEntryRequest.request.method).toBe('PUT');
});

it('delete an entry using DELETE', () => {
service.baseUrl = 'time-entries';
const entry = 'entryId';
service.deleteEntry(entry).subscribe((response) => {
const updateEntryRequest = httpMock.expectOne(`${service.baseUrl}/${entry}`);
expect(updateEntryRequest.request.method).toBe('DELETE');
});
const updateEntryRequest = httpMock.expectOne(`${service.baseUrl}/${entry}`);
expect(updateEntryRequest.request.method).toBe('DELETE');
});

it('stops an entry using POST', () => {
service.baseUrl = 'time-entries';

service.stopEntryRunning('id').subscribe((response) => {
const updateEntryRequest = httpMock.expectOne(`${service.baseUrl}/id/stop`);
expect(updateEntryRequest.request.method).toBe('POST');
});

const updateEntryRequest = httpMock.expectOne(`${service.baseUrl}/id/stop`);
expect(updateEntryRequest.request.method).toBe('POST');
});

it('when getting time entries for report, time range should be sent', () => {
const startDateValue = new Date();
const endDateValue = new Date();
const pipe: DatePipe = new DatePipe('en');
const timeRange: TimeEntriesTimeRange = {start_date: startDateValue, end_date: endDateValue};

service.loadEntriesByTimeRange(timeRange).subscribe();

const loadEntryRequest = httpMock.expectOne(req => req.method === 'GET' && req.url === service.baseUrl);

expect(loadEntryRequest.request.params.get('start_date')).toBe(pipe.transform(startDateValue,
EntryService.TIME_ENTRIES_DATE_TIME_FORMAT));
expect(loadEntryRequest.request.params.get('end_date')).toBe(pipe.transform(endDateValue, EntryService.TIME_ENTRIES_DATE_TIME_FORMAT));
});
});
18 changes: 16 additions & 2 deletions src/app/modules/time-clock/services/entry.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import { HttpClient } from '@angular/common/http';

import { Observable } from 'rxjs';
import { environment } from './../../../../environments/environment';
import {TimeEntriesTimeRange} from '../models/time-entries-time-range';
import {DatePipe} from '@angular/common';

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

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

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

loadEntriesByTimeRange(range: TimeEntriesTimeRange): Observable<any> {

return this.http.get(this.baseUrl,
{
params: {
start_date: this.datePipe.transform(range.start_date, EntryService.TIME_ENTRIES_DATE_TIME_FORMAT),
end_date: this.datePipe.transform(range.end_date, EntryService.TIME_ENTRIES_DATE_TIME_FORMAT)
}
}
);
}
}