All files / src/components/common/Filter/utils createDateFilter.ts

16.66% Statements 4/24
0% Branches 0/7
0% Functions 0/4
16.66% Lines 4/24

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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117                                        540x                                                         540x                                                         540x                                               540x                            
import { format, sub } from "date-fns";
import { replaceValues, withAppendedKey } from "@services/filtering";
import moment from "moment";
import { PLATFORM_TIMEZONE } from "@utils/timezones";
 
type TModifier =
  | "is on or after"
  | "is on or before"
  | "is between"
  | "in the last";
 
export type TDate = {
  modifier: TModifier;
  count: number;
  dayMonthYear: string;
  startDate: number;
  endDate: number;
  date: number;
};
 
const createDateFilter = ({
  modifier,
  count,
  dayMonthYear,
  startDate,
  endDate,
  date,
}: TDate) => {
  switch (modifier) {
    case "is on or after":
      return getFromDate(modifier, date);
    case "is on or before":
      return getFromDate(modifier, date);
    case "is between":
      return getBetween(startDate, endDate);
    case "in the last":
    default:
      return getInTheLast(count, dayMonthYear);
  }
};
 
export default createDateFilter;
 
/**
 * Computes the date filter and the API query
 * @function
 * @param  {string} modifier - Modifier that determine the output of the filter
 * @param  {Date} date - The date that is the start/end of the filter
 */
const getFromDate = (
  modifier: Extract<TModifier, "is on or after" | "is on or before">,
  date: number,
) => {
  const name = [modifier, format(date, "dd/MM/yy")];
 
  const momentDate = moment(date).tz(PLATFORM_TIMEZONE);
  const queryDate =
    modifier === "is on or before"
      ? momentDate.endOf("day")
      : momentDate.startOf("day");
 
  const query = replaceValues(
    withAppendedKey.created[modifier],
    queryDate.unix(),
  );
 
  return {
    name,
    query,
  };
};
 
/**
 * Computes the date filter and the API query
 * @function
 * @param  {Date} startDate - The date that is the start of the filter
 * @param  {Date} endDate - The date that is the end of the filter
 */
const getBetween = (startDate: number, endDate: number) => {
  const name = [
    format(startDate, "dd/MM/yyyy"),
    "-",
    format(endDate, "dd/MM/yyyy"),
  ];
 
  const queryStartDate = moment(startDate).tz(PLATFORM_TIMEZONE).startOf("day").unix();
  const queryEndDate = moment(endDate).tz(PLATFORM_TIMEZONE).endOf("day").unix();
  const query = replaceValues(
    withAppendedKey.created["is between"],
    queryStartDate,
    queryEndDate,
  );
 
  return { name, query };
};
 
/**
 * Computes the date filter and the API query
 * @function
 * @param  {number} count - The subtraction amount in the specified unit of measure
 * @param  {string} dayMonthYear - The unit of measure of the subtraction (day, month, year, ...)
 */
const getInTheLast = (count: number, dayMonthYear: string) => {
  const name = ["last", count, dayMonthYear];
 
  const result = sub(new Date(), { [dayMonthYear]: count });
  const queryDate = moment(result).tz(PLATFORM_TIMEZONE).startOf("day").unix();
  const query = replaceValues(
    withAppendedKey.created["in the last"],
    queryDate,
  );
  return {
    name,
    query,
  };
};