From cc70a3d90428109699d926fe097ad1d164374cd1 Mon Sep 17 00:00:00 2001 From: Ulises Santana Date: Mon, 18 Dec 2023 10:17:37 +0000 Subject: [PATCH] =?UTF-8?q?fix(current):=20=F0=9F=90=9B=20handle=20negativ?= =?UTF-8?q?e=20entry=20duration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/entities/duration.ts | 6 +----- .../repositories/time-entry.repository.implementation.ts | 9 ++++++++- .../cases/continue-with-last-time-entry.case.test.ts | 2 +- test/builders/toggl-time-entry.builder.ts | 9 +++++++-- test/core/duration.test.ts | 7 +------ test/core/time-entry.test.ts | 7 +------ .../time-entry.repository.implementation.test.ts | 7 ++++++- 7 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/core/entities/duration.ts b/src/core/entities/duration.ts index b0f8b4c..54e90ea 100644 --- a/src/core/entities/duration.ts +++ b/src/core/entities/duration.ts @@ -1,7 +1,7 @@ import {TimeHelper} from "./time-helper"; export class Duration { - constructor(private duration: number = -1) { + constructor(private duration: number = 0) { } private static isDuration(duration: Duration | number): duration is Duration { @@ -9,10 +9,6 @@ export class Duration { } get value(): number { - if (this.duration < 0) { - return Math.floor(Date.now() / 1000) + this.duration; - } - return this.duration; } diff --git a/src/infrastructure/repositories/time-entry.repository.implementation.ts b/src/infrastructure/repositories/time-entry.repository.implementation.ts index 1475e23..0390339 100644 --- a/src/infrastructure/repositories/time-entry.repository.implementation.ts +++ b/src/infrastructure/repositories/time-entry.repository.implementation.ts @@ -8,10 +8,17 @@ export class TimeEntryRepositoryImplementation implements TimeEntryRepository { constructor(private readonly api: TogglApi, private readonly time = new TimeHelper()) { } + static calcDuration(entry: TogglTimeEntry): number { + const start = new Date(entry.start!) + const stop = entry.stop ? new Date(entry.stop) : new Date() + const duration = stop.getTime() - start.getTime() + return Math.floor(duration / 1000) + } + static mapToTimeEntry(entry: TogglTimeEntry, project: Nullable): TimeEntry { return new TimeEntry({ description: entry.description, - duration: new Duration(entry.duration), + duration: entry.start && entry.stop ? new Duration(entry.duration) : new Duration(TimeEntryRepositoryImplementation.calcDuration(entry)), id: entry.id, project: project ? new Project(project.id, project.name) : new NullProject(), }) diff --git a/test/application/cases/continue-with-last-time-entry.case.test.ts b/test/application/cases/continue-with-last-time-entry.case.test.ts index ed8e510..45baad6 100644 --- a/test/application/cases/continue-with-last-time-entry.case.test.ts +++ b/test/application/cases/continue-with-last-time-entry.case.test.ts @@ -27,7 +27,7 @@ describe('ContinueWithLastTimeEntryUseCase', () => { it('should continue with last time entry when there is a last entry', async () => { const lastEntry = buildTimeEntry({ description: "Test Description", duration: 60}); - const newEntry = new TimeEntry({...lastEntry, duration: new Duration(-1)}) + const newEntry = new TimeEntry({...lastEntry, duration: new Duration(0)}) const continueTime = new Date(); mockTimeEntryRepository.getLastEntry.returns(Promise.resolve(lastEntry)); mockTimeEntryRepository.createEntry.returns(Promise.resolve(newEntry)); diff --git a/test/builders/toggl-time-entry.builder.ts b/test/builders/toggl-time-entry.builder.ts index e3bf78d..b92972a 100644 --- a/test/builders/toggl-time-entry.builder.ts +++ b/test/builders/toggl-time-entry.builder.ts @@ -2,10 +2,15 @@ import {TogglTimeEntry} from "../../src/infrastructure/types"; export function buildTogglTimeEntry(params = {} as Partial): TogglTimeEntry { + const duration = params.duration ?? 1800 + const start = new Date(Date.now() - (duration * 1000)) + const stop = new Date() return { description: params.description ?? "Dummy time entry", - duration: params.duration ?? 1800, + duration, id: params.id ?? Math.floor(Math.random() * 10**16), - project_id: params.project_id ?? Math.floor(Math.random() * 10**16) + project_id: params.project_id ?? Math.floor(Math.random() * 10**16), + start: start.toISOString(), + stop: stop.toISOString() } } diff --git a/test/core/duration.test.ts b/test/core/duration.test.ts index 75f3778..da970d7 100644 --- a/test/core/duration.test.ts +++ b/test/core/duration.test.ts @@ -5,7 +5,7 @@ import {Duration} from "../../src/core"; describe('Duration', () => { it('should initialize with the default duration if no value is provided', () => { const duration = new Duration(); - expect(duration.value).to.be.closeTo(Date.now() / 1000, 2); + expect(duration.value).to.be.equal(0); }); it('should initialize with the provided value', () => { @@ -40,9 +40,4 @@ describe('Duration', () => { expect(duration.toString()).to.equal('01h 01m 01s'); }); - it('should handle negative durations correctly', () => { - const duration = new Duration(-100); - expect(duration.value).to.be.closeTo(Date.now() / 1000, 101); - }); - }); diff --git a/test/core/time-entry.test.ts b/test/core/time-entry.test.ts index 6633885..7d01c82 100644 --- a/test/core/time-entry.test.ts +++ b/test/core/time-entry.test.ts @@ -17,11 +17,6 @@ describe('TimeEntry', () => { it('should return the correct string representation', () => { const timeEntry = new TimeEntry({description: "Prueba de descripciĆ³n", duration: new Duration(3600), id: 1, project}) - let expectedStr = `01h 00m 00s - ${timeEntry.description} (${project.name})`; - expect(timeEntry.toString()).to.equal(expectedStr); - - timeEntry.duration.value = (Math.floor(Date.now() / 1000) - 3665) * -1; - expectedStr = `01h 01m 05s - ${timeEntry.description} (${project.name})`; - expect(timeEntry.toString()).to.equal(expectedStr); + expect(timeEntry.toString()).to.equal(`01h 00m 00s - ${timeEntry.description} (${project.name})`); }); }); diff --git a/test/infrastructure/repositories/time-entry.repository.implementation.test.ts b/test/infrastructure/repositories/time-entry.repository.implementation.test.ts index 5f83069..97372be 100644 --- a/test/infrastructure/repositories/time-entry.repository.implementation.test.ts +++ b/test/infrastructure/repositories/time-entry.repository.implementation.test.ts @@ -29,7 +29,12 @@ describe('TimeEntryRepositoryImplementation', () => { const result = await repository.createEntry(entry, date); expect(result).to.be.deep.equal(entry); - sinon.assert.calledOnceWithExactly(apiMock.createTimeEntry, mockTogglEntry, date); + sinon.assert.calledOnceWithExactly(apiMock.createTimeEntry, { + description: mockTogglEntry.description, + duration: mockTogglEntry.duration, + id: mockTogglEntry.id, + project_id: mockTogglEntry.project_id + }, date); }); it('should retrieve the current time entry when it exists', async () => {