All files / src/utils queryString.ts

65.9% Statements 29/44
51.61% Branches 16/31
87.5% Functions 7/8
65.85% Lines 27/41

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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95        8682x 4341x         532x 4341x   4341x 4337x   3018x       532x 4341x 4341x   4341x   4341x                                           532x 8x 8x   8x 8x 8x         8x 8x 8x 8x 8x     8x   8x 8x                             8x                    
import Cookies from "js-cookie";
import { safeParse } from "@utils/index";
import qs from "query-string";
 
export const getSearchParams = (url: string) => qs.parse(new URL(url).search);
const getPathName = (url: string) => qs.parse(new URL(url).pathname);
 
/*
 Parse the search params to get rig of filters and keep the rest then stringify it
*/
const removedFilterKeyAndStringifySearchParams = (url: string) => {
  const searchParams = getSearchParams(url);
 
  return Object.keys(searchParams)
    .filter((x) => !x.includes("filter"))
    .reduce((acc, v) => {
      return `${acc}${acc.length ? "&" : ""}${v}=${searchParams[v]}`;
    }, "");
};
 
export const parsedQueryString = (url: string, output: string) => {
  const searchParams = getSearchParams(url);
  const pathname = getPathName(url);
  const withoutFilterSearchParams =
    removedFilterKeyAndStringifySearchParams(url);
 
  Iif (searchParams?.["?filter"]) {
    const currentStr = searchParams["?filter"];
 
    output = `${currentStr};${output}`;
 
    const f = Object.keys(pathname).find((x) => x.includes("products"));
 
    if (f) {
      const pathArr = f.split("/").filter(Boolean);
 
      if (pathArr.length > 1) {
        //if at some point are more than one paths, it means that we are not in the main product page and is very likely than we are inside the route of a single element where the second path is the id of the element
        return;
      }
 
      return `/${f.replaceAll("/", "")}?filter=${encodeURIComponent(
        output,
      )}&${withoutFilterSearchParams}`;
    }
  }
};
 
export const getGivecashToWebData = () => {
  const user = Cookies.get("user");
  const parsedUser = user ? safeParse(user) : null;
 
  let params = new URLSearchParams(document.location.search);
  const next = params.get("next");
  Iif (next) {
    params = new URLSearchParams(
      new URLSearchParams(next?.split("?")[1] || ""),
    );
  }
  const merchantId = parseInt(params.get("merchant") || "");
  const enterpriseId = parseInt(params.get("provider") || "");
  const acquirerId = parseInt(params.get("acquirer") || "");
  const merchantName = params.get("merchantName") || "";
  const redirectRoute = encodeURIComponent(
    document.location.pathname + document.location.search,
  );
  const fromMobile = merchantId && enterpriseId && acquirerId;
 
  let toPath = "";
  Iif (fromMobile) {
    const selectedAccount = parsedUser?.id;
 
    if (selectedAccount === merchantId) {
      toPath = `${parsedUser.role}/manage-money`;
    }
    if ([enterpriseId, acquirerId].includes(selectedAccount)) {
      toPath = `${
        parsedUser.role
      }/manage-money?merchant=${merchantId}&merchantName=${encodeURIComponent(
        merchantName,
      )}`;
    }
  }
 
  return {
    toPath,
    fromMobile,
    redirectRoute,
    merchantId,
    enterpriseId,
    acquirerId,
    merchantName,
  };
};