Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.5 kB
2
Indexable
Never
export const getRandomIntegerFromInterval = (min: number, max: number) => {
  return Math.round(Math.random() * (max - min + 1) + min);
};

export function getPlanningDeals(
  ordersMin: number,
  ordersMax: number,
  averageBaseOrderTotal: any,
  ourSideOrders: any[],
  previousPlanningDeals?: any[],
) {
  const planningDeals: { count: number, total: number }[] = [];
  const ourSideOrdersCopy = [...ourSideOrders];
  let iteration = 0;
  let ordersPricesBaseLength = ourSideOrdersCopy.length;
  while (ordersPricesBaseLength > 0) {
    let ordersDealsAmount = 0;
    iteration++;
    let total = 0;

    if (ordersPricesBaseLength === 1) {
      break;
    } else if (
      previousPlanningDeals &&
      previousPlanningDeals[iteration - 1] &&
      previousPlanningDeals[iteration - 1].count < ordersPricesBaseLength
    ) {
      ordersDealsAmount = previousPlanningDeals[iteration - 1].count;
    } else if (ordersPricesBaseLength >= ordersMin && ordersPricesBaseLength < ordersMax) {
      ordersDealsAmount = getRandomIntegerFromInterval(ordersMin, ordersPricesBaseLength);
    } else {
      ordersDealsAmount = getRandomIntegerFromInterval(ordersMin, ordersMax);
    }
    let virtualOrder = 0;
    if (iteration !== 1) {
      virtualOrder = 1;
      total += averageBaseOrderTotal;
    }
    ourSideOrdersCopy.splice(0, ordersDealsAmount - virtualOrder);
    planningDeals.push({ count: ordersDealsAmount, total: total });
  }

  return planningDeals;
}