Salon Appointment Scheduler does what's asked but does not pass tests

Hi guys! I am working on the Salon Appointment Scheduler project, but I can’t seem to pass these tests even if it seems like my code works. If someone can help, I’d greatly appreciate it.

Thank you!

Tests I can’t pass:

  • You should display a numbered list of the services you offer before the first prompt for input, each with the format #) <service>. For example, 1) cut, where 1 is the service_id

  • If you pick a service that doesn’t exist, you should be shown the same list of services again

  • If a phone number entered doesn’t exist, you should get the customers name and enter it, and the phone number, into the customers table

  • You can create a row in the appointments table by running your script and entering 1, 555-555-5555, Fabio, 10:30 at each request for input if that phone number isn’t in the customers table. The row should have the customer_id for that customer, and the service_id for the service entered

  • You can create another row in the appointments table by running your script and entering 2, 555-555-5555, 11am at each request for input if that phone number is already in the customers table. The row should have the customer_id for that customer, and the service_id for the service entered

  • 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

My salon.sh code:

#!/bin/bash

PSQL="psql -X --username=freecodecamp --dbname=salon --tuples-only -c"l

# truncate old tables
$PSQL "TRUNCATE TABLES appointments,customers"

echo -e "\n~~~~~ Gummy's Hair Salon ~~~~~\n"

MAIN_MENU() {
  # user can choose a service
  if [[ $1 ]]
    then echo -e "\n$1"
  fi

  # display prompt for user choice of service
  echo "\nHow can we help you?"

  # display list of services in format <service_id>) <service>
  AVAILABLE_SERVICES=$($PSQL "SELECT service_id,name FROM services")

  if [[ -z $AVAILABLE_SERVICES ]]
    then
      # if no services availabe, send to main menu with message
      MAIN_MENU "Sorry, we do not have any services available right now."
    else
      # display available services
      echo -e "\nHere are the services we currently have available:"
      echo "$AVAILABLE_SERVICES" | while read SERVICE_ID BAR SERVICE_NAME
      do
        echo "$SERVICE_ID) $SERVICE_NAME"
      done

      echo -e "\nEnter the number of the service you would like."
      read SERVICE_ID_SELECTED

      SERVICE_NAME_SELECTED=$($PSQL "SELECT name FROM services WHERE $SERVICE_ID_SELECTED = service_id")

      if [[ -z $SERVICE_ID_SELECTED ]]
        then
          # if input is not an existing service_id
          MAIN_MENU "Please enter a valid service number."
        else
          # otherwise, start scheduling the appointment
          SCHEDULE_APPOINTMENT $SERVICE_ID_SELECTED $SERVICE_NAME_SELECTED
      fi
  fi

  # read input
  read MAIN_MENU_SELECTION

  # if not valid service_id, send to MAIN_MENU with error message
}

# function for scheduling appointment will take service_id and name as arguments
SCHEDULE_APPOINTMENT() {
  SERVICE_ID=$1
  SERVICE_NAME=$2

  echo You have requested service number $1, which is the $2 service.
  
  # prompt for phone number
  echo -e "\nPlease enter your phone number."
  read CUSTOMER_PHONE

  # check if customer already exists in table
  CUSTOMER_NAME=$($PSQL "SELECT name FROM customers WHERE phone='$CUSTOMER_PHONE'")

  if [[ -z $CUSTOMER_NAME ]]
    then
      # if customer doesn't exist in the table, 
      # get name
      echo -e "\nI see you are a new customer--what's your name?"
      read CUSTOMER_NAME

      # insert the new customer
      INSERT_CUSTOMER_RESULT=$($PSQL "INSERT INTO customers(phone,name) VALUES('$CUSTOMER_PHONE','$CUSTOMER_NAME')")
  fi

  # get customer id for scheduling
  CUSTOMER_ID=$($PSQL "SELECT customer_id FROM customers WHERE phone='$CUSTOMER_PHONE'")

  echo -e "\nWhen would you like to schedule your appointment?"
  read SERVICE_TIME

  # insert appointment
  INSERT_APPOINTMENT_RESULT=$($PSQL "INSERT INTO appointments(customer_id,service_id,time) VALUES($CUSTOMER_ID,$SERVICE_ID,'$SERVICE_TIME')")

  SERVICE_FORMATTED=$(echo "$SERVICE_NAME" | sed -e 's/\(.*\)/\L\1/')

  MAIN_MENU "I have put you down for a $SERVICE_FORMATTED appointment at $SERVICE_TIME, $(echo $CUSTOMER_NAME | sed -E 's/^ *| *$//g')."

  exit 0
}

MAIN_MENU

Make sure you read the last instruction carefully:

Make sure your script finishes running after completing any of the tasks above, or else the tests won’t pass

Oops, by the way, please disregard that “truncate” line in the beginning–it wasn’t in my code last time I ran it. :sob:

EDIT: Shoot, more was wrong with my code than I thought–I originally named my SERVICE_ID_SELECTED and SERVICE_NAME_SELECTED variables differently, and when I switched them to what was required for the tests, I accidentally used one in place of the other. :sob: :sob: :sob: