All files / app/src/features/measure/lib normalize-object-filters-to-string.ts

3.33% Statements 2/60
100% Branches 0/0
0% Functions 0/1
3.33% Lines 2/60

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 59 60 61 62 63 64 651x       1x                                                                                                                        
import { MEASURE_PIPES_ITEMS_PER_PAGE, MeasureFormValues } from '@/features/measure'
import { getDateFromDays, getDateRange, getIdFromIRI, getRangeFilter, removeEmptyProperties } from '@/shared/helpers'
import { LockPriceEnum, PipeStatusEnum } from '@/entities/pipe'
 
export const normalizeObjectFiltersToString = (filters: MeasureFormValues): string => {
  const isTonPrice = filters.priceType === LockPriceEnum.TON
  const isPriceExists = filters?.price?.filter(Boolean)?.length
  const filtersValue = {
    connection: getIdFromIRI(filters.connection as string),
    diameter: getIdFromIRI(filters.diameter as string),
    side: getIdFromIRI(filters.side as string),
    strengthGroup: getIdFromIRI(filters.strengthGroup as string),
    ...(filters?.status?.length && {
      status: filters?.status?.map(({ id }) => id),
    }),
    ...(filters?.state?.length && {
      state: filters?.state?.map(({ id }) => id),
    }),
    batch: getIdFromIRI(filters.batch as string),
    manufacturer: filters?.manufacturer?.map(id => getIdFromIRI(id)),
    execution: getIdFromIRI(filters.execution as string),
    specialTypeAlloy: getIdFromIRI(filters.specialTypeAlloy as string),
    affiliation: getIdFromIRI(filters.affiliation as string),
    index: getIdFromIRI(filters.index as string),
    greenWellOption: getIdFromIRI(filters.greenWellOption as string),
    'exists[package]': filters.package,
    ...(filters?.rack?.length && {
      'row.rack': filters?.rack?.map(({ id }) => getIdFromIRI(id)),
    }),
    ...(filters?.row?.length && {
      row: filters?.row?.map(({ id }) => id),
    }),
    'warehouseContract.contract': getIdFromIRI(filters.contract as string),
    'warehouseContract.warehouse': getIdFromIRI(filters.warehouse as string),
    'warehouseContract.contract.client': getIdFromIRI(filters.client as string),
    ...(isPriceExists && {
      lockPrice: filters.priceType,
      priceCurrency: getIdFromIRI(filters?.priceCurrency as string),
    }),
    ...getRangeFilter(filters?.length as string[], 'length'),
    ...getRangeFilter(filters?.price as string[], isTonPrice ? 'priceTon' : 'priceMeter'),
    ...getDateRange(filters?.age as string[], 'manufactureDate[before]', 'manufactureDate[after]'),
    ...(filters?.status?.some(status => status.id === PipeStatusEnum.ARCHIVED) && {
      showArchived: true,
    }),
    ...getDateFromDays(
      filters?.inspectionDate as string[],
      'inspectionDate[strictly_after]',
      'inspectionDate[strictly_before]'
    ),
    itemsPerPage: MEASURE_PIPES_ITEMS_PER_PAGE,
  }
  const actualFilters = removeEmptyProperties(filtersValue)
 
  const queryParams = Object.entries(actualFilters)
    .map(([key, value]) => {
      if (Array.isArray(value)) {
        return value.map(v => `${key}[]=${v}`).join('&')
      }
      return `${key}=${value}`
    })
    .join('&')
  return queryParams
}