Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand All @@ -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<IndividualConfig>) => { },
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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<boolean> = component.isFeatureToggleActivated();

expect(featureManagerService.isToggleEnabledForUser).toHaveBeenCalled();
isFeatureToggleActivated.subscribe((value) => expect(value).toEqual(toggleValue));
});
});
});


Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -15,7 +15,10 @@ 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';
import { AzureAdB2CService } from 'src/app/modules/login/services/azure.ad.b2c.service';


type Merged = TechnologyState & ProjectState & ActivityState;

Expand All @@ -35,12 +38,16 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
loadActivitiesSubscription: Subscription;
loadActiveEntrySubscription: Subscription;
actionSetDateSubscription: Subscription;
isEnableToggleSubscription: Subscription;
isFeatureToggleActive: boolean;

constructor(
private formBuilder: FormBuilder,
private store: Store<Merged>,
private actionsSubject$: ActionsSubject,
private toastrService: ToastrService
private toastrService: ToastrService,
private azureService: AzureAdB2CService,
private featureManagerService: FeatureManagerService
) {
this.entryForm = this.formBuilder.group({
description: '',
Expand All @@ -60,6 +67,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(
Expand Down Expand Up @@ -145,10 +157,20 @@ 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.owner_id = this.getOwnerId();
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;
}

getOwnerId(){
return this.azureService.getUserId();
}

private getLastEntry() {
const lastEntry$ = this.store.pipe(select(getTimeEntriesDataSource));
lastEntry$.subscribe((entry) => {
Expand Down Expand Up @@ -178,4 +200,9 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
this.loadActiveEntrySubscription.unsubscribe();
this.actionSetDateSubscription.unsubscribe();
}

isFeatureToggleActivated(): Observable<boolean> {
return this.featureManagerService.isToggleEnabledForUser('update-entries')
.pipe(map((enabled: boolean) => enabled));
}
}