blob: 7b0cca76399b66186ec5618969807b2b66fa0cc0 [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
import { BaseSandbox } from '@shared/sandbox/base.sandbox';
import { Injectable } from '@angular/core';
import * as internalPersonActions from '@shared/store/actions/persons/internal-person.action';
import { FormGroupState, SetValueAction, ResetAction, MarkAsTouchedAction } from 'ngrx-forms';
import { InternalPerson, CommunicationsData, CommunicationType } from '@shared/models';
import { Observable } from 'rxjs';
import * as store from '@shared/store';
import { Store, ActionsSubject } from '@ngrx/store';
import * as internalPersonDetailsFormReducer from '@shared/store/reducers/persons/internal-person/internal-person-details-form.reducer';
import { ofType } from '@ngrx/effects';
import { UtilService } from '@shared/utility';
import { takeUntil, take, map } from 'rxjs/operators';
import { Router } from '@angular/router';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { SafetyQueryDialogComponent } from '@shared/components/dialogs/safety-query-dialog/safety-query-dialog.component';
import * as internalCommunicationsDataDetailsFormReducer from '@shared/store/reducers/persons/internal-person/communications-data-details-form.reducer';
import * as communicationTypesActions from '@shared/store/actions/admin/communication-types.action';
@Injectable()
export class InternalPersonDetailsSandBox extends BaseSandbox {
public formState$: Observable<FormGroupState<InternalPerson>> = this.appState$.select(store.getInternalPersonDetails);
public currentFormState: FormGroupState<InternalPerson>;
public salutations$ = this.appState$.select(store.getSalutationsData);
public isCommunicationsDataDetailViewVisible: boolean = false;
public communicationsDataDetailsFormState$: Observable<FormGroupState<CommunicationsData>> = this.appState$.select(store.getInternalPersonCommunicationsDataDetails);
public communicationsDataDetailsCurrentFormState: FormGroupState<CommunicationsData>;
public communicationsDataList$: Observable<Array<CommunicationsData>> = this.appState$.select(store.getInternalCommunicationsDataData);
public communicationsDataListLoading$: Observable<boolean> = this.appState$.select(store.getInternalCommunicationsDataLoading);
private _currentInternalPerson: InternalPerson = null;
private _internalPersonContactId: string;
private _communicationTypes: Array<CommunicationType> = new Array<CommunicationType>();
/**
* InternalPerson Sandbox constructor
*/
constructor(
public appState$: Store<store.State>,
public utilService: UtilService,
protected actionsSubject: ActionsSubject,
protected router: Router,
protected modalService: NgbModal
) {
super(appState$);
}
/**
* Loads internal person from the server
*/
public loadInternalPerson(id: string): void {
this._internalPersonContactId = id;
this.appState$.dispatch(internalPersonActions.loadInternalPersonDetail({ payload: id }));
this.actionsSubject
.pipe(
ofType(internalPersonActions.loadInternalPersonDetailSuccess),
map((action: internalPersonActions.ILoadInternalPersonDetailsSuccess) => action.payload),
take(1),
takeUntil(this._endSubscriptions$)
)
.subscribe((payload: InternalPerson) => {
this._currentInternalPerson = payload;
});
}
public persistInternalPerson(): void {
if (this.currentFormState.isValid) {
let isUidUnique: boolean = null;
let isUserrefUnique: boolean = null;
const isNew: boolean = this.currentFormState.value.contactId === null;
if (this.currentFormState.value.uid) {
this.appState$.dispatch(internalPersonActions.loadInternalPersonsForUid({payload: {uid: this.currentFormState.value.uid}}));
}
if (this.currentFormState.value.userRef) {
this.appState$.dispatch(internalPersonActions.loadInternalPersonsForUserref({payload: {userRef: this.currentFormState.value.userRef}}));
}
if (!this.currentFormState.value.uid && !this.currentFormState.value.userRef) {
this.appState$.dispatch(internalPersonActions.persistInternalPersonDetail({payload: this.currentFormState.value}));
}
this.actionsSubject
.pipe(
ofType(internalPersonActions.loadInternalPersonsForUidSuccess),
map((action: internalPersonActions.ILoadInternalPersonsForUidSuccess) => action.payload),
take(1),
takeUntil(this._endSubscriptions$)
)
.subscribe((payload: Array<InternalPerson>) => {
isUidUnique = payload.length == 0 || (!isNew && payload.length == 1 && payload[0].uid === this._currentInternalPerson.uid);
if (!isUidUnique) {
this.utilService.displayNotification('InternalError.UniqueUid', 'error');
} else {
if ((isUidUnique && this.currentFormState.value.userRef && isUserrefUnique) || (isUidUnique && !this.currentFormState.value.userRef)) {
this.appState$.dispatch(internalPersonActions.persistInternalPersonDetail({payload: this.currentFormState.value}));
}
}
});
this.actionsSubject
.pipe(
ofType(internalPersonActions.loadInternalPersonsForUserrefSuccess),
map((action: internalPersonActions.ILoadInternalPersonsForUserrefSuccess) => action.payload),
take(1),
takeUntil(this._endSubscriptions$)
)
.subscribe((payload: Array<InternalPerson>) => {
isUserrefUnique = payload.length == 0 || (!isNew && payload.length == 1 && payload[0].userRef === this._currentInternalPerson.userRef);
if (!isUserrefUnique) {
this.utilService.displayNotification('InternalError.UniqueUserref', 'error');
} else {
if ((isUserrefUnique && this.currentFormState.value.uid && isUidUnique) || (isUserrefUnique && !this.currentFormState.value.uid)) {
this.appState$.dispatch(internalPersonActions.persistInternalPersonDetail({payload: this.currentFormState.value}));
}
}
});
this.actionsSubject
.pipe(
ofType(internalPersonActions.persistInternalPersonDetailSuccess),
take(1),
takeUntil(this._endSubscriptions$)
)
.subscribe(() => {
this.clearInternalPerson();
this.router.navigateByUrl(`/overview`);
});
} else {
this.utilService.displayNotification('MandatoryFieldsNotFilled', 'error');
}
}
clearInternalPerson(): void {
this.appState$.dispatch(
new SetValueAction(
internalPersonDetailsFormReducer.FORM_ID,
internalPersonDetailsFormReducer.INITIAL_STATE.value
)
);
this.appState$.dispatch(new ResetAction(internalPersonDetailsFormReducer.FORM_ID));
this.appState$.dispatch(new MarkAsTouchedAction(internalPersonDetailsFormReducer.FORM_ID));
}
/**
* Subscribes to events
*/
public registerInternalPersonEvents(): void {
// subscribes to formState
this.formState$.pipe(takeUntil(this._endSubscriptions$)).subscribe(
(formState: FormGroupState<InternalPerson>) =>
(this.currentFormState = formState)
);
}
public loadCommunicationsData(internalPersonId: string): void {
this.actionsSubject
.pipe(
ofType(communicationTypesActions.loadCommunicationTypesSuccess),
map((action: communicationTypesActions.ILoadCommunicationTypesSuccess) => action.payload),
take(1),
takeUntil(this._endSubscriptions$)
)
.subscribe((payload: Array<CommunicationType>) => {
this._communicationTypes = payload;
this.appState$.dispatch(internalPersonActions.loadInternalPersonDetailCommunicationsData({ payload: internalPersonId }));
});
this.actionsSubject
.pipe(
ofType(internalPersonActions.loadInternalPersonDetailCommunicationsDataSuccess),
map((action: internalPersonActions.ILoadInternalPersonCommunicationsDataSuccess) => action.payload),
take(1),
takeUntil(this._endSubscriptions$)
)
.subscribe((communicationsData: Array<CommunicationsData>) => {
if (this._communicationTypes) {
for (let i = 0; i < this._communicationTypes.length; i++) {
const ct = this._communicationTypes[i];
const existingCommunicationsData: CommunicationsData = communicationsData.find(cd => cd.communicationTypeId == ct.id);
ct.isDisabled = existingCommunicationsData ? true : false;
}
}
});
}
public loadCommunicationsDataDetails(communicationsDataId: string): void {
this.appState$.dispatch(
internalPersonActions.loadInternalPersonDetailCommunicationsDataDetails({ payload_contactId: this._internalPersonContactId, payload_communicationsId: communicationsDataId })
);
}
public persistCommunicationsData(): void {
if (this.communicationsDataDetailsCurrentFormState.isValid) {
const newCommunicationsData = new CommunicationsData(this.communicationsDataDetailsCurrentFormState.value);
newCommunicationsData.contactId = newCommunicationsData.contactId !== null ? newCommunicationsData.contactId : this._internalPersonContactId;
this.appState$.dispatch(
internalPersonActions.persistCommunicationsDataDetail({payload: newCommunicationsData})
);
this.actionsSubject
.pipe(
ofType(internalPersonActions.persistCommunicationsDataDetailSuccess),
take(1),
takeUntil(this._endSubscriptions$)
)
.subscribe(() => {
this.closeCommunicationsDataDetail();
});
} else {
this.utilService.displayNotification(
'MandatoryFieldsNotFilled',
'error'
);
}
}
public deleteCommunicationsData(communicationsData: CommunicationsData): void {
const modalRef = this.modalService.open(SafetyQueryDialogComponent);
modalRef.componentInstance.title = 'ConfirmDialog.Action.delete';
modalRef.componentInstance.body = 'ConfirmDialog.Deletion';
modalRef.result.then(() => {
this.appState$.dispatch(internalPersonActions.deleteCommunicationsData({ payload: communicationsData }));
this.closeCommunicationsDataDetail();
}, () => {
});
}
public closeCommunicationsDataDetail(): void {
this.loadCommunicationsData(this._internalPersonContactId);
this.clearCommunicationsData();
this.isCommunicationsDataDetailViewVisible = false;
}
public clearCommunicationsData(): void {
this.appState$.dispatch(
new SetValueAction(
internalCommunicationsDataDetailsFormReducer.FORM_ID,
internalCommunicationsDataDetailsFormReducer.INITIAL_STATE.value
)
);
this.appState$.dispatch(new ResetAction(internalCommunicationsDataDetailsFormReducer.FORM_ID));
}
/**
* Subscribes to communicationsData events
*/
public registerCommunicationsDataEvents(): void {
// subscribes to formState
this.communicationsDataDetailsFormState$.pipe(takeUntil(this._endSubscriptions$)).subscribe(
(formState: FormGroupState<CommunicationsData>) =>
(this.communicationsDataDetailsCurrentFormState = formState)
);
}
}