All files / utils/report-filter report-filter.ts

10.52% Statements 2/19
0% Branches 0/1
0% Functions 0/4
10.52% Lines 2/19

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58  1x           1x                                                                                                    
import { MetadataFilter } from '@zeedhi/common';
import { I18n, IDictionary } from '@zeedhi/core';
import { IDynamicFilterItem } from '../../components';
 
/**
 * Class to translate a TekRestDatasource dynamicFilter to the ZhReports format
 */
export class ReportFilter {
  public filter: IDictionary<IDynamicFilterItem[]> = {};
 
  private messages: IDictionary<string> = {
    CONTAINS: 'TEKGRID_CONTAINS',
    NOT_CONTAINS: 'TEKGRID_NOT_CONTAINS',
    EQUALS: 'TEKGRID_EQUALS',
    NOT_EQUAL: 'TEKGRID_NOT_EQUALS',
    GREATER_THAN: 'TEKGRID_GREATER_THAN',
    LESS_THAN: 'TEKGRID_LESS_THAN',
    GREATER_THAN_EQUALS: 'TEKGRID_GREATER_THAN_EQUALS',
    LESS_THAN_EQUALS: 'TEKGRID_LESS_THAN_EQUALS',
    IN: 'TEKGRID_IN',
    NOT_IN: 'TEKGRID_NOT_IN',
    BETWEEN: 'TEKGRID_BETWEEN',
    AND: 'TEKGRID_AND',
    OR: 'TEKGRID_OR',
  };
 
  constructor(filter: IDictionary) {
    this.filter = filter;
  }
 
  public build(): MetadataFilter {
    return Object.keys(this.filter).map((key) => {
      const items = this.filter[key];
 
      const words: string[] = [];
      items.forEach((item, index) => {
        const operation = this.messages[item.operation];
        words.push(I18n.translate(operation));
 
        words.push(item.value);
 
        const isLast = index === items.length - 1;
        Iif (!isLast) {
          const next = items[index + 1];
          const relation = this.messages[next.relation];
          words.push(I18n.translate(relation));
        }
      });
 
      const value = words.join(' ').trim();
      return {
        value,
        label: key,
      };
    });
  }
}