diff --git a/src/app/modules/shared/components/details-fields/details-fields.component.spec.ts b/src/app/modules/shared/components/details-fields/details-fields.component.spec.ts index 92f95babc..cfb9c1570 100644 --- a/src/app/modules/shared/components/details-fields/details-fields.component.spec.ts +++ b/src/app/modules/shared/components/details-fields/details-fields.component.spec.ts @@ -576,6 +576,37 @@ describe('DetailsFieldsComponent', () => { const foundActivity = component.findInactiveActivity(state.activities.data); expect(foundActivity).toEqual(expectedActivity); }); + + const datesParams = [ + { + case: 'should return true when the start time entry is greater than the end time', + entryDates: { + start_date: '2021-04-21', + end_date: '2021-04-21', + start_hour: '20:00', + end_hour: '08:00', + }, + expected_result: true, + }, + { + case: 'should return false when the start time entry is not greater than the end time', + entryDates: { + start_date: '2021-04-21', + end_date: '2021-04-21', + start_hour: '19:00', + end_hour: '20:00', + }, + expected_result: false, + }, + ]; + datesParams.forEach((param) => { + it(`${param.case}`, () => { + component.entryForm.setValue({ ...formValues, ...param.entryDates }); + const result = component.isStartTimeEntryAfterEndedEntry(); + + expect(result).toBe(param.expected_result); + }); + }); /* TODO As part of https://github.com/ioet/time-tracker-ui/issues/424 a new parameter was added to the details-field-component, and now these couple of tests are failing. A solution to this error might be generate a Test Wrapper Component. More details here: diff --git a/src/app/modules/shared/components/details-fields/details-fields.component.ts b/src/app/modules/shared/components/details-fields/details-fields.component.ts index a1d920e81..f16f9e7c3 100644 --- a/src/app/modules/shared/components/details-fields/details-fields.component.ts +++ b/src/app/modules/shared/components/details-fields/details-fields.component.ts @@ -244,6 +244,12 @@ export class DetailsFieldsComponent implements OnChanges, OnInit { this.closeModal?.nativeElement?.click(); } + isStartTimeEntryAfterEndedEntry(): boolean { + const startDate = moment(`${this.start_date.value} ${this.start_hour.value}`); + const endDate = moment(`${this.end_date.value} ${this.end_hour.value}`); + return startDate >= endDate; + } + dateToSubmit(date, hour) { const entryFormDate = this.entryForm.value[date]; const updatedHour = this.entryForm.value[hour]; @@ -264,6 +270,11 @@ export class DetailsFieldsComponent implements OnChanges, OnInit { const startDateToSubmit = this.dateToSubmit('start_date', 'start_hour'); const endDateToSubmit = this.dateToSubmit('end_date', 'end_hour'); + if (this.isStartTimeEntryAfterEndedEntry()) { + this.toastrService.error('You must end the time entry after it started'); + return; + } + const entry = { project_id: this.entryForm.value.project_id, activity_id: this.entryForm.value.activity_id,