Home > database >  how to set remaining values to last slots
how to set remaining values to last slots

Time:05-24

Divide all the total seating capacities during the selected start and end time equally among the slots created. But, if some capacities are exceeding the rounding values then distribute/divide the slot seating capacities to the last slots. Example: User has selected the start time as tomorrow at start time 11:00 and end time as 11:45. And the total seating capacity as 6. So, the 15 mins slots will be created as:

Timing Seating Capacity
11:00 - 11:15 1
11:15 - 11:30 1
11:30- 11:45 2
11:45 - 12:00 2

I need the above result. I have done it using the below code, but no success.

    const timePair = timePairs(
      calculate(
        '11:00',
        '12:00'
      )
    );
    console.log(timePair);
    if (timePair.length === values.capacity) {
      console.log(
        map(timePair, (element) => extend({}, element, { capacity: 1 }))
      );
    } else if (values.capacity < timePair.length) {
      console.log(
        map(timePair, (element) =>
          extend({}, element, { capacity: 1 })
        ).slice(0, values.capacity)
      );
    } else {
      if (timePair.length === 1) {
        console.log(
          map(timePair, (element) =>
            extend({}, element, { capacity: values.capacity })
          )
        );
      } else if (timePair.length < values.capacity) {
        console.log(
          map(timePair, (element) =>
            extend({}, element, {
              capacity: Math.round(values.capacity / timePair.length),
            })
          )
        );
      } else {
        const balance = Math.round(values.capacity / timePair.length);
        console.log(balance);
      }
    }

const calculate = (openTime: any, closeTime: any) => {
    const x = {
      slotInterval: 15,
      openTime: openTime,
      closeTime: closeTime,
    };

    const startTime = moment(x.openTime, "HH:mm");
    const endTime = moment(x.closeTime, "HH:mm");

    const allTimes = [];

    while (startTime <= endTime) {
      allTimes.push(startTime.format("HH:mm"));
      startTime.add(x.slotInterval, "minutes");
    }

    return allTimes;
};

const timePairs = (times: any) => {
    const result = [];
    for (let i = 0, n = times.length; i   1 < n; i  = 1) {
      result.push({ start: times[i], end: times[i   1] });
    }
    return result;
};

The output for the above code is,

[{"start": "11:00", "end": "11:15", "capacity": 2}, {"start": "11:15", "end": "11:30", "capacity": 2}, {"start": "11:30", "end": "11:45", "capacity": 2}, {"start": "11:45", "end": "12:00", "capacity": 2}]

I want the output as (if capacity is 6),

[{"start": "11:00", "end": "11:15", "capacity": 1}, {"start": "11:15", "end": "11:30", "capacity": 1}, {"start": "11:30", "end": "11:45", "capacity": 2}, {"start": "11:45", "end": "12:00", "capacity": 2}]

I want the output as (if capacity is 7),

[{"start": "11:00", "end": "11:15", "capacity": 1}, {"start": "11:15", "end": "11:30", "capacity": 1}, {"start": "11:30", "end": "11:45", "capacity": 2}, {"start": "11:45", "end": "12:00", "capacity": 3}]

CodePudding user response:

Finally I find out the answer. For those whose searching for the same, can refer the below code.

console.log(addCapacity(timePair, values.capacity, timePair.length));

const addCapacity = (timePair: any, input: any, num: any) => {
    const extra = input % num;
    const eachTime = Math.floor(input / num);

    const assignment = timePair
      .map((e: any) => {
        if (timePair.indexOf(e) < extra) {
          return eachTime   1;
        } else {
          return eachTime;
        }
      })
      .reverse();

    assignment.forEach((element: any, index: number) => {
      timePair[index].capacity = element;
    });

    return timePair;
};
  • Related