All files / src/app/core/models/custom-field-definition custom-field-definition.mapper.ts

100% Statements 13/13
100% Branches 3/3
100% Functions 5/5
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40      103x   2x 5x                       2x 6x   5x 8x 1x   7x 4x   7x         5x     2x      
import { CustomFieldDefinitionsData } from './custom-field-definition.interface';
import { CustomFieldDefinitions } from './custom-field-definition.model';
 
export class CustomFieldDefinitionMapper {
  static fromData(data: CustomFieldDefinitionsData[] = []): CustomFieldDefinitions {
    const entities = data.reduce<CustomFieldDefinitions['entities']>(
      (acc, entry) => ({
        ...acc,
        [entry.name]: {
          description: entry.description,
          displayName: entry.displayName,
          name: entry.name,
          type: entry.type,
        },
      }),
      {}
    );
 
    const scopes = data
      .sort((a, b) => a.position - b.position)
      .reduce<CustomFieldDefinitions['scopes']>((acc, entry) => {
        entry.scopes.forEach(scope => {
          if (!scope.isVisible) {
            return;
          }
          if (!acc[scope.name]) {
            acc[scope.name] = [];
          }
          acc[scope.name].push({
            name: entry.name,
            editable: scope.isEditable,
          });
        });
        return acc;
      }, {});
 
    return { entities, scopes };
  }
}