diff --git a/src/app/modules/time-clock/components/entry-fields/entry-fields.component.spec.ts b/src/app/modules/time-clock/components/entry-fields/entry-fields.component.spec.ts index aa588c670..33e0419c9 100644 --- a/src/app/modules/time-clock/components/entry-fields/entry-fields.component.spec.ts +++ b/src/app/modules/time-clock/components/entry-fields/entry-fields.component.spec.ts @@ -1,4 +1,4 @@ -import { Subscription } from 'rxjs'; +import { Subscription, of, Observable } from 'rxjs'; import { LoadActiveEntry, EntryActionTypes, UpdateEntry } from './../../store/entry.actions'; import { ActivityManagementActionTypes } from './../../../activities-management/store/activity-management.actions'; import {waitForAsync, ComponentFixture, TestBed} from '@angular/core/testing'; @@ -15,6 +15,8 @@ import { formatDate } from '@angular/common'; import { NgxMaterialTimepickerModule } from 'ngx-material-timepicker'; import * as moment from 'moment'; import { DATE_FORMAT_YEAR } from 'src/environments/environment'; +import { FeatureManagerService } from './../../../shared/feature-toggles/feature-toggle-manager.service'; + describe('EntryFieldsComponent', () => { type Merged = TechnologyState & ProjectState; @@ -24,6 +26,7 @@ describe('EntryFieldsComponent', () => { let mockTechnologySelector; let mockProjectsSelector; let entryForm; + let featureManagerService: FeatureManagerService; const actionSub: ActionsSubject = new ActionsSubject(); const toastrServiceStub = { error: (message?: string, title?: string, override?: Partial) => { }, @@ -114,6 +117,7 @@ describe('EntryFieldsComponent', () => { entryForm = TestBed.inject(FormBuilder); mockTechnologySelector = store.overrideSelector(allTechnologies, state.technologies); mockProjectsSelector = store.overrideSelector(getCustomerProjects, state.projects); + featureManagerService = TestBed.inject(FeatureManagerService); })); beforeEach(() => { @@ -425,4 +429,46 @@ describe('EntryFieldsComponent', () => { expect(component.loadActiveEntrySubscription.unsubscribe).toHaveBeenCalled(); expect(component.actionSetDateSubscription.unsubscribe).toHaveBeenCalled(); }); + + it('when feature-toggle "update-entries" enable for the user, the updateEntry function is executes to update the entries', () => { + spyOn(featureManagerService, 'isToggleEnabledForUser').and.returnValue(of(true)); + + const mockEntry = { ...entry, + start_date : moment().format(DATE_FORMAT_YEAR), + start_hour : moment().format('HH:mm'), + update_last_entry_if_overlap: true + }; + component.newData = mockEntry; + component.isFeatureToggleActivated(); + const expected = { update_last_entry_if_overlap: true }; + expect(component.newData.update_last_entry_if_overlap).toEqual(expected.update_last_entry_if_overlap); + }); + + it('when FT "update-entries" disable for the user,the UpdateCurrentOrLastEntry function is called to update the entries', () => { + spyOn(featureManagerService, 'isToggleEnabledForUser').and.returnValue(of(false)); + + const mockEntry = { ...entry, + start_date : moment().format(DATE_FORMAT_YEAR), + start_hour : moment().format('HH:mm'), + update_last_entry_if_overlap: false + }; + component.newData = mockEntry; + component.isFeatureToggleActivated(); + const expected = { update_last_entry_if_overlap: false }; + expect(component.newData.update_last_entry_if_overlap).toEqual(expected.update_last_entry_if_overlap); + }); + + const toggleValues = [true, false]; + toggleValues.map((toggleValue) => { + it(`when FeatureToggle is ${toggleValue} should return ${toggleValue}`, () => { + spyOn(featureManagerService, 'isToggleEnabledForUser').and.returnValue(of(toggleValue)); + + const isFeatureToggleActivated: Observable = component.isFeatureToggleActivated(); + + expect(featureManagerService.isToggleEnabledForUser).toHaveBeenCalled(); + isFeatureToggleActivated.subscribe((value) => expect(value).toEqual(toggleValue)); + }); + }); }); + + diff --git a/src/app/modules/time-clock/components/entry-fields/entry-fields.component.ts b/src/app/modules/time-clock/components/entry-fields/entry-fields.component.ts index a5481281e..29d14c643 100644 --- a/src/app/modules/time-clock/components/entry-fields/entry-fields.component.ts +++ b/src/app/modules/time-clock/components/entry-fields/entry-fields.component.ts @@ -1,6 +1,6 @@ import { ActivityManagementActionTypes } from './../../../activities-management/store/activity-management.actions'; -import { EntryActionTypes, LoadActiveEntry } from './../../store/entry.actions'; -import { filter } from 'rxjs/operators'; +import { EntryActionTypes, LoadActiveEntry, UpdateCurrentOrLastEntry, UpdateEntry } from './../../store/entry.actions'; +import { filter, map } from 'rxjs/operators'; import { Component, OnDestroy, OnInit } from '@angular/core'; import { FormBuilder, FormGroup } from '@angular/forms'; import { Store, ActionsSubject, select } from '@ngrx/store'; @@ -15,7 +15,8 @@ import { ToastrService } from 'ngx-toastr'; import { formatDate } from '@angular/common'; import { getTimeEntriesDataSource } from '../../store/entry.selectors'; import { DATE_FORMAT } from 'src/environments/environment'; -import { Subscription } from 'rxjs'; +import { Subscription, Observable } from 'rxjs'; +import { FeatureManagerService } from './../../../shared/feature-toggles/feature-toggle-manager.service'; type Merged = TechnologyState & ProjectState & ActivityState; @@ -35,12 +36,15 @@ export class EntryFieldsComponent implements OnInit, OnDestroy { loadActivitiesSubscription: Subscription; loadActiveEntrySubscription: Subscription; actionSetDateSubscription: Subscription; + isEnableToggleSubscription: Subscription; + isFeatureToggleActive: boolean; constructor( private formBuilder: FormBuilder, private store: Store, private actionsSubject$: ActionsSubject, - private toastrService: ToastrService + private toastrService: ToastrService, + private featureManagerService: FeatureManagerService ) { this.entryForm = this.formBuilder.group({ description: '', @@ -60,6 +64,11 @@ export class EntryFieldsComponent implements OnInit, OnDestroy { this.activities = action.payload; this.store.dispatch(new LoadActiveEntry()); }); + + this.isEnableToggleSubscription = this.isFeatureToggleActivated().subscribe((flag) => { + this.isFeatureToggleActive = flag; + }); + this.loadActiveEntrySubscription = this.actionsSubject$ .pipe( filter( @@ -145,7 +154,12 @@ export class EntryFieldsComponent implements OnInit, OnDestroy { return; } this.entryForm.patchValue({ start_date: newHourEntered }); - this.store.dispatch(new entryActions.UpdateCurrentOrLastEntry({ ...this.newData, ...this.entryForm.value })); + if (this.isFeatureToggleActive) { + this.newData.update_last_entry_if_overlap = true; + this.store.dispatch(new entryActions.UpdateEntryRunning({ ...this.newData, ...this.entryForm.value })); + } else { + this.store.dispatch(new entryActions.UpdateCurrentOrLastEntry({ ...this.newData, ...this.entryForm.value })); + } this.showTimeInbuttons = false; } @@ -178,4 +192,9 @@ export class EntryFieldsComponent implements OnInit, OnDestroy { this.loadActiveEntrySubscription.unsubscribe(); this.actionSetDateSubscription.unsubscribe(); } + + isFeatureToggleActivated(): Observable { + return this.featureManagerService.isToggleEnabledForUser('update-entries') + .pipe(map((enabled: boolean) => enabled)); + } }