Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
2.0 kB
1
Indexable
  const dataTable: Ref<{
    searchTerm: any,
    currentPage: Number,
    itemsPerPage: Number,
    totalPages: Number,
    filters: Record<string, any[]>
    sorter: { name: string, order: SortOrder }
  }> = ref({
    searchTerm: '',
    itemsPerPage: 20,
    currentPage: 1,
    totalPages: 1,
    filters: {},
    sorter: { ...defaultDataTableSorter },
  });


  const fetchOrders = () => {
    const { sorter, filters } = dataTable.value;

    let searchById = false;
    let search: string = dataTable.value.searchTerm;

    const filteredTags = filters.tags?.filter((tag) => tag !== TagsEnum[2] && tag !== TagsEnum[3]) ?? [];

    const filteredTagsParams = filteredTags.length === 1
      ? `filter.tickets.tags=$contains:${filteredTags.map((t) => TagsEnum[t]).join(',')}`
      : filteredTags.map((value, index) => `filter.tickets.tags=$${index === 0 ? 'contains' : 'or:$contains'}:${TagsEnum[value]}`).join('&');

    gltApi.get(`/order?${new URLSearchParams({
      search,
      ...(searchById ? { searchBy: 'id' } : {}),
      limit: dataTable.value.itemsPerPage.toString(),
      page: dataTable.value.currentPage.toString(),
      sortBy: `${sorter.name}:${sorter.order}`,
      ...(filters.status?.length ? {
        'filter.status': `$in:${filters.status.map((s) => StatusEnum[s]).join(',')}`,
      } : {}),
      ...(filters.event?.length ? {
        'filter.tickets.eventId': `$in:${filters.event.join(',')}`,
      } : {}),
      ...(filters.tags?.includes(TagsEnum[2]) && !filters.tags?.includes(TagsEnum[3]) ? { 'filter.signedByConsultant': '$eq: true' } : {}),
      ...(filters.tags?.includes(TagsEnum[3]) && !filters.tags?.includes(TagsEnum[2]) ? { 'filter.signedByConsultant': '$eq: false' } : {}),
    })}&${filteredTags.length && filteredTagsParams}`)
      .then((res) => {
        if (!res.data) throw new Error('Unable to fetch order list');
        orders.value = res.data.preparedOrders;
        dataTable.value.totalPages = res.data.meta.totalPages;
      });
  };
Leave a Comment