Skip to content

Commit

Permalink
Closes salesagility#395 - Include all not displayed fields in Record
Browse files Browse the repository at this point in the history
  • Loading branch information
mpuyosa91 committed Dec 6, 2023
1 parent 957b2eb commit b11036d
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 81 deletions.
9 changes: 5 additions & 4 deletions core/app/common/src/lib/metadata/metadata.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {BehaviorSubject, Observable} from 'rxjs';

export interface ViewFieldDefinition {
name?: string;
vardefBased?: boolean;
label?: string;
labelKey?: string;
dynamicLabelKey?: string;
Expand Down Expand Up @@ -63,7 +64,7 @@ export interface PanelCell extends ViewFieldDefinition {
}

export interface ViewFieldDefinitionMap {
[key: string]: ViewFieldDefinition
[key: string]: ViewFieldDefinition;
}

export interface TabDefinitions {
Expand All @@ -86,12 +87,12 @@ export interface LogicDefinition {
modes: Array<string>;
params: {
activeOnFields?: {
[key:string]: LogicRuleValues[];
}
[key: string]: LogicRuleValues[];
};
displayState?: boolean;
fieldDependencies: Array<string>;
asyncProcessHandler?: string;
}
};
}

export interface LogicRuleValues{
Expand Down
65 changes: 53 additions & 12 deletions core/app/core/src/lib/services/record/field/field.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,18 @@ export class FieldManager {

/**
* Build line item and add to record
* @param {object} itemDefinition
* @param {object }item
* @param {object} parentRecord
* @param {object} parentField
*
* @param {FieldDefinition} itemDefinition Item Definition
* @param {Record} parentRecord Parent Record
* @param {Field} parentField Parent Field
* @param {Record | null} item Item
*/
public addLineItem(
itemDefinition: FieldDefinition,
parentRecord: Record,
parentField: Field,
item: Record = null
) {
): void {
if (!item) {
item = {
id: '',
Expand All @@ -183,10 +184,11 @@ export class FieldManager {

/**
* Remove line item
* @param {object} parentField
* @param index
*
* @param {Field} parentField Parent Field
* @param {number} index Index
*/
public removeLineItem(parentField: Field, index: number) {
public removeLineItem(parentField: Field, index: number): void {
const item = parentField.items[index];

if (!item) {
Expand All @@ -204,13 +206,13 @@ export class FieldManager {

parentField.itemFormArray.clear();

parentField.items.forEach(item => {
const deleted = item && item.attributes && item.attributes.deleted;
if (!item || deleted) {
parentField.items.forEach(parentItem => {
const deleted = parentItem && parentItem.attributes && parentItem.attributes.deleted;
if (!parentItem || deleted) {
return;
}

parentField.itemFormArray.push(item.formGroup);
parentField.itemFormArray.push(parentItem.formGroup);
});

parentField.itemFormArray.updateValueAndValidity();
Expand Down Expand Up @@ -246,6 +248,45 @@ export class FieldManager {
}


/**
* Build and add vardef only field to record
*
* @param {object} record Record
* @param {object} viewField ViewFieldDefinition
* @param {object} language LanguageStore
* @returns {object}Field
*/
public addVardefOnlyField(record: Record, viewField: ViewFieldDefinition, language: LanguageStore = null): Field {

const field = this.fieldBuilder.buildField(record, viewField, language);

this.addVardefOnlyFieldToRecord(record, viewField.name, field);

return field;
}


/**
* Add field to record
*
* @param {object} record Record
* @param {string} name string
* @param {object} field Field
*/
public addVardefOnlyFieldToRecord(record: Record, name: string, field: Field): void {

if (!record || !name || !field) {
return;
}

if (!record.fields) {
record.fields = {};
}

record.fields[name] = field;
}


/**
* Is field initialized in record
*
Expand Down
12 changes: 12 additions & 0 deletions core/app/core/src/lib/services/record/record.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ export class RecordManager {
if (!viewField || !viewField.name) {
return;
}

if(record.fields[viewField.name]) {
return;
}

const isVardefBased = viewField?.vardefBased ?? false;

if (isVardefBased) {
this.fieldManager.addVardefOnlyField(record, viewField, this.language);
return;
}

this.fieldManager.addField(record, viewField, this.language);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,29 @@
* the words "Supercharged by SuiteCRM".
*/

import { isEmpty } from 'lodash-es';
import { BehaviorSubject, combineLatest, Observable, of, Subscription } from 'rxjs';
import { catchError, distinctUntilChanged, finalize, map, take, tap } from 'rxjs/operators';
import {Injectable} from '@angular/core';
import {BehaviorSubject, combineLatest, Observable, of, Subscription} from 'rxjs';
import { Params } from '@angular/router';
import {
BooleanMap,
deepClone,
Field,
FieldDefinitionMap,
FieldLogicMap,
FieldMetadata,
isVoid,
Record,
StatisticsMap,
StatisticsQueryMap,
SubPanelMeta,
ViewContext,
ViewFieldDefinition,
ViewMode
ViewFieldDefinitionMap,
ViewMode,
} from 'common';
import {catchError, distinctUntilChanged, finalize, map, take, tap} from 'rxjs/operators';
import {RecordViewData, RecordViewModel, RecordViewState} from './record-view.store.model';
import { RecordViewData, RecordViewModel, RecordViewState } from './record-view.store.model';
import {NavigationStore} from '../../../../store/navigation/navigation.store';
import {StateStore} from '../../../../store/state';
import {RecordSaveGQL} from '../../../../store/record/graphql/api.record.save';
Expand All @@ -61,7 +67,6 @@ import {LocalStorageService} from '../../../../services/local-storage/local-stor
import {SubpanelStoreFactory} from '../../../../containers/subpanel/store/subpanel/subpanel.store.factory';
import {ViewStore} from '../../../../store/view/view.store';
import {RecordFetchGQL} from '../../../../store/record/graphql/api.record.get';
import {Params} from '@angular/router';
import {StatisticsBatch} from '../../../../store/statistics/statistics-batch.service';
import {RecordStoreFactory} from '../../../../store/record/record.store.factory';
import {UserPreferenceStore} from '../../../../store/user-preference/user-preference.store';
Expand Down Expand Up @@ -170,9 +175,7 @@ export class RecordViewStore extends ViewStore implements StateStore {
this.subpanels$ = this.subpanelsState.asObservable();


this.viewContext$ = this.record$.pipe(map(() => {
return this.getViewContext();
}));
this.viewContext$ = this.record$.pipe(map(() => this.getViewContext()));
}

get widgets(): boolean {
Expand Down Expand Up @@ -394,6 +397,58 @@ export class RecordViewStore extends ViewStore implements StateStore {
return templates[this.getMode()] || '';
}

initValidators(record: Record): void {
if(!record || !Object.keys(record?.fields).length) {
return;
}

Object.keys(record.fields).forEach(fieldName => {
const field = record.fields[fieldName];
const formControl = field?.formControl ?? null;
if (!formControl) {
return;
}

this.resetValidators(field);

const validators = field?.validators ?? [];
const asyncValidators = field?.asyncValidators ?? [];

if (field?.formControl && validators.length) {
field.formControl.setValidators(validators);
}
if (field?.formControl && asyncValidators.length) {
field.formControl.setAsyncValidators(asyncValidators);
}
});

}

resetValidators(field: Field): void {
if (!field?.formControl) {
return;
}

field.formControl.clearValidators();
field.formControl.clearAsyncValidators();
}

resetValidatorsForAllFields(record: Record): void {
if(!record || !record?.fields?.length) {
return ;
}
Object.keys(record.fields).forEach(fieldName => {
const field = record.fields[fieldName];
const formControl = field?.formControl ?? null;

if (!formControl) {
return;
}

this.resetValidators(field);
});
}

/**
* Parse query params
*
Expand Down Expand Up @@ -587,33 +642,60 @@ export class RecordViewStore extends ViewStore implements StateStore {
*/
protected getViewFieldsObservable(): Observable<ViewFieldDefinition[]> {
return this.metadataStore.recordViewMetadata$.pipe(map((recordMetadata: RecordViewMetadata) => {
const fields: ViewFieldDefinition[] = [];
const fieldsMap: ViewFieldDefinitionMap = {};
recordMetadata.panels.forEach(panel => {
panel.rows.forEach(row => {
row.cols.forEach(col => {
fields.push(col);
const fieldName = col.name ?? col.fieldDefinition.name ?? '';
fieldsMap[fieldName] = col;
});
});
});

return fields;
Object.keys(recordMetadata.vardefs).forEach(fieldKey => {
const vardef = recordMetadata.vardefs[fieldKey] ?? null;
if (!vardef || isEmpty(vardef)) {
return;
}

// already defined. skip
if (fieldsMap[fieldKey]) {
return;
}

fieldsMap[fieldKey] = {
name: fieldKey,
vardefBased: true,
label: vardef.vname ?? '',
type: vardef.type ?? '',
display: vardef.display ?? '',
fieldDefinition: vardef,
metadata: vardef.metadata ?? {} as FieldMetadata,
logic: vardef.logic ?? {} as FieldLogicMap
} as ViewFieldDefinition;
});

return Object.values(fieldsMap);
}));
}

/**
* Build ui user preference key
* @param storageKey
*
* @param {string} storageKey Storage Key
* @protected
* @returns {string} Preference Key
*/
protected getPreferenceKey(storageKey: string): string {
return 'recordview-' + storageKey;
}

/**
* Save ui user preference
* @param module
* @param storageKey
* @param value
*
* @param {string} module Module
* @param {string} storageKey Storage Key
* @param {any} value Value
* @protected
*/
protected savePreference(module: string, storageKey: string, value: any): void {
Expand All @@ -622,60 +704,14 @@ export class RecordViewStore extends ViewStore implements StateStore {

/**
* Load ui user preference
* @param module
* @param storageKey
*
* @param {string} module Module
* @param {string} storageKey Storage Key
* @protected
* @returns {any} User Preference
*/
protected loadPreference(module: string, storageKey: string): any {
return this.preferences.getUi(module, this.getPreferenceKey(storageKey));
}

initValidators(record: Record): void {
if(!record || !Object.keys(record?.fields).length) {
return;
}

Object.keys(record.fields).forEach(fieldName => {
const field = record.fields[fieldName];
const formControl = field?.formControl ?? null;
if (!formControl) {
return;
}

this.resetValidators(field);

const validators = field?.validators ?? [];
const asyncValidators = field?.asyncValidators ?? [];

if (validators.length) {
field?.formControl?.setValidators(validators);
}
if (asyncValidators.length) {
field?.formControl?.setAsyncValidators(asyncValidators);
}
});

}

resetValidators(field) {
field?.formControl?.clearValidators();
field?.formControl?.clearAsyncValidators();
}

resetValidatorsForAllFields(record) {
if(!record || !record?.fields?.length) {
return ;
}
Object.keys(record.fields).forEach(fieldName => {
const field = record.fields[fieldName];
const formControl = field?.formControl ?? null;

if (!formControl) {
return;
}

this.resetValidators(field);
});
}

}

0 comments on commit b11036d

Please sign in to comment.