2
\$\begingroup\$

The website I'm developing allows customers to book an appointment. They can also cancel their appointment with the following rules:

  • if the appointment is cancelled between 48 hours and 1 hour in advance, the customer has to pay 50%.

  • if the appointment is cancelled below 1 hour in advance, the customer should pay 100 %

  • if the appointment is cancelled above 48 hours in advance, the customer has to pay 0%.

My function to decide this:

decidePercentageToBePayed(cancelledReservation: Reservation) {
    const reservationYear = parseInt(cancelledReservation.getYear(), 10);
    const reservationMonth = parseInt(cancelledReservation.getMonth(), 10);
    const reservationDay = parseInt(cancelledReservation.getDay(), 10);
    const reservationHour = Utils.getTimeSlotHour(Utils.getStartSlot(cancelledReservation.period));
    const reservationMinute = Utils.getTimeSlotMinute(Utils.getStartSlot(cancelledReservation.period));
    const currentYear = parseInt(Utils.getCurrentYear(), 10);
    const currentMonth = parseInt(Utils.getCurrentMonth(), 10);
    const currentDay = parseInt(Utils.getCurrentDay(), 10);
    const currentHour = parseInt(Utils.getCurrentHour(), 10);
    const currentMinute = parseInt(Utils.getCurrentMinute(), 10);

    if (reservationYear > currentYear) {
      if (reservationMonth === 1 && currentMonth === 12) {
        if (currentDay === 30 && reservationDay === 1 || currentDay === 31 && reservationDay === 2) {
          if (currentHour > reservationHour) {
            return 50;
          }
          return 0;
        } else if (currentDay === 31 && reservationDay === 1) {
          return 50;
        }
        return 0;
      }
      return 0;
    } else if (reservationMonth - currentMonth > 1) {
      return 0;
    } else if (reservationMonth - currentMonth === 1) {
      if (currentMonth === 2 && Utils.isLeapYear()) {
        if (currentDay === 27 && reservationDay === 1 || currentDay === 28 && reservationDay === 2) {
          if (currentHour > reservationHour) {
            return 50;
          }
          return 0;
        } else if (currentDay === 28 && reservationDay === 1) {
          return 50;
        }
        return 0;
      } else if (currentMonth === 2 && !Utils.isLeapYear()) {
        if (currentDay === 28 && reservationDay === 1 || currentDay === 29 && reservationDay === 2) {
          if (currentHour > reservationHour) {
            return 50;
          }
          return 0;
        } else if (currentDay === 29 && reservationDay === 1) {
          return 50;
        }
        return 0;
      } else if (Utils.has31Days(currentMonth)) {
        if (currentDay === 30 && reservationDay === 1 || currentDay === 31 && reservationDay === 2) {
          if (currentHour > reservationHour) {
            return 50;
          }
          return 0;
        }  else if (currentDay === 31 && reservationDay === 1) {
          return 50;
        }
        return 0;
      } else {
        if (currentDay === 29 && reservationDay === 1 || currentDay === 30 && reservationDay === 2) {
          if (currentHour > reservationHour) {
            return 50;
          }
          return 0;
        } else if (currentDay === 30 && reservationDay === 1) {
          return 50;
        }
        return 0;
      }
    } else {
      if (reservationDay - currentDay > 2) {
        return 0;
      } else if (reservationDay - currentDay === 2) {
        if (currentHour > reservationHour) {
          return 50;
        }
        return 0;
      } else if (reservationDay - currentDay === 1) {
        return 50;
      } else {
        if (reservationHour - currentHour === 0) {
          return 100;
        } else if (reservationHour - currentHour === 1) {
          if (reservationMinute < currentMinute) {
            return 100;
          }
          return 50;
        }
        return 50;
      }
    }
  }
```
\$\endgroup\$
  • 3
    \$\begingroup\$ Just use the Date object eg to set to time const time = new Date(year, month, day, hour, min) (Note month starts at 0) subtract the current time from reservation time and divide by number ms in an hour eg hours = (resTime.getTime() - curTime.getTime()) / (60 * 60 * 1000) then just test the hours if (hours < 1) { return 100 } if (hours < 48) {return 50} return 0; Details for Date at developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… \$\endgroup\$ – Blindman67 Jul 26 '19 at 13:00
  • \$\begingroup\$ @Blindman67 Had the same thought, will you create an answer out of your comment? \$\endgroup\$ – konijn Jul 26 '19 at 13:36
  • \$\begingroup\$ @konijn A little to much ambiguity (why all the parseInts are there extra characters?) in the question for me to bother at the moment. All yours if you want it. \$\endgroup\$ – Blindman67 Jul 26 '19 at 14:14
  • \$\begingroup\$ @Blindman67 Utils returns date info as string because I almost always need them as string, except for this function and maybe one other. There are no extra characters, they are just the numbers as strings. \$\endgroup\$ – Bas Velden Jul 29 '19 at 11:39
  • 1
    \$\begingroup\$ @BasVelden You can coerce a string into a number using the unary operator + before the string. Eg typeof +"10" === "number" is true or the better option is via the Number object eg const day = Number(Utils.getCurrentDay()) However I would never return a number as a string (especially if a number in its abstract form, eg year) To convert explicitly from number to string is seldom needed, JS will do it for you \$\endgroup\$ – Blindman67 Jul 29 '19 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.