Salon Appointment Scheduler - Build a Salon Appointment Scheduler

Tell us what’s happening:

Last test not passing after several tries. Please guide how to resolve the issue.
After an appointment is successfully added, you should output the message I have put you down for a <service> at <time>, <name>. For example, if the user chooses cut as the service, 10:30 is entered for the time, and their name is Fabio in the database the output would be I have put you down for a cut at 10:30, Fabio. Make sure your script finishes running after completing any of the tasks above, or else the tests won’t pass

Your code so far

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36

Challenge Information:

Salon Appointment Scheduler - Build a Salon Appointment Scheduler

Here is My Code :
#!/bin/bash

PSQL=“psql --username=freecodecamp --dbname=salon --no-align --tuples-only -c”

echo -e “\n~~~~~ MY SALON ~~~~~\n”
echo -e “Welcome to My Salon, how can I help you?\n”

main_menu() {
echo -e “Services offered:”
SERVICES=“$($PSQL “SELECT service_id, name FROM services”)”
echo “$SERVICES” | while IFS=“|” read SERVICE_ID SERVICE_NAME; do
echo “$SERVICE_ID) $SERVICE_NAME”
done

read SERVICE_ID_SELECTED
if [[ $SERVICE_ID_SELECTED =~ [1]+$ ]]; then
SERVICE_NAME=$($PSQL “SELECT name FROM services WHERE service_id=$SERVICE_ID_SELECTED”)
if [[ -z $SERVICE_NAME ]]; then
echo -e “\nI could not find that service. What would you like today?\n”
main_menu
else
echo -e “\nWhat’s your phone number?”
read CUSTOMER_PHONE

  if [[ $CUSTOMER_PHONE =~ ^[0-9]+(-[0-9]+)+$ ]]; then
    # check number in db
    CUSTOMER_ID=$($PSQL "SELECT customer_id FROM customers WHERE phone='$CUSTOMER_PHONE'")
    # if not found than ask for name
    if [[ -z $CUSTOMER_ID ]]; then
      echo -e "\nI don't have a record for that phone number, what's your name?"
      IFS=$"\n\t\r" read CUSTOMER_NAME
      ADD_CUSTOMER_RESULT=$($PSQL "INSERT INTO customers(name, phone) VALUES('$CUSTOMER_NAME', '$CUSTOMER_PHONE') RETURNING customer_id")
      CUSTOMER_ID=$($PSQL "SELECT customer_id FROM customers WHERE phone='$CUSTOMER_PHONE'")
    else
      CUSTOMER_NAME=$($PSQL "SELECT name FROM customers WHERE phone='$CUSTOMER_PHONE'")
    fi
    
    echo -e "\nWhat time would you like your $SERVICE_NAME, $CUSTOMER_NAME?"
    read SERVICE_TIME
    if [[ ! $SERVICE_TIME =~ ^[0-9]{1,2}:[0-9]{2}$ ]]; then
      echo -e "\nInvalid time format. Please enter the time in HH:MM format."
    fi
    BOOKING_QUERY="INSERT INTO appointments(customer_id, service_id, time) VALUES($CUSTOMER_ID, $SERVICE_ID_SELECTED, '$SERVICE_TIME')"
    BOOKING_RESULT=$($PSQL "$BOOKING_QUERY" > /dev/null 2>&1)
    
    if [[ $? -eq 0 ]]; then
      echo -e "\nI have put you down for a $SERVICE_NAME at $SERVICE_TIME, $CUSTOMER_NAME."
    else
      echo -e "\nFailed to add appointment."
    fi
  fi
fi

else
echo -e “\nError: Invalid service ID. Please enter a valid service ID.”
main_menu
fi
}

main_menu


  1. 0-9 ↩︎