Automating Insulin Ordering: Creating the Wrapping Function (Part 3)
Tags: programming, productivity, automation, processoptimisation
Published: 2024-06-04 08:20:07

In the previous parts of this series, we discussed capturing the browser request to the GP's website and converting it into a cURL command. We also created a bash function to automate this request. Now, we'll take the next step by creating a wrapping function that orchestrates the entire automation process.

## Updating the order_insulin Function
To facilitate the wrapping function, we first need to update our order_insulin function to return an exit code. The function should return `0` if the prescription request is successful and `1` if it fails. I also renamed it, to actually capture the step of the process it does. Here’s the updated function:

```bash
function request_prescription() {
  insulin_order_response=$(<cURL Request>)

  if [ $? -eq 0 ]; then
      insulin_order_status=$(echo $insulin_order_response | jq -r '.status')
      insulin_order_message=$(echo $insulin_order_response | jq -r '.message')

      if [[ $insulin_order_status == "mail_sent" ]]; then
          echo "Ordered: $insulin_order_message"
          return 0
      else
          echo "Something went wrong, order via the website and update the curl request"
          return 1
      fi
  else
      echo "Something went wrong, order via the website and update the curl request"
      return 1
  fi
}
```

## Creating the Wrapping Function

Next, we need a wrapping function that triggers the prescription request and checks the exit code. If the prescription request is successful, it will proceed to the next steps: sending an email to the pharmacy (Step 4) and adding a task to Google Calendar (Step 5). These two steps will be implemented in future articles, but we will set up the structure for them now.


```bash
function order_insulin() {
  request_prescription
  exit_code=$?

  if [[ $exit_code -ne 0 ]]; then
    echo "Prescription request failed"
    return 1
  fi

  echo "Prescription requested successfully"

  send_pharmacy_email
  email_exit_code=$?

  if [[ $email_exit_code -ne 0 ]]; then
    echo "Failed to send email to pharmacy"
    return 1
  fi

  echo "Email sent successfully"

  add_calendar_task
  calendar_exit_code=$?

  if [[ $calendar_exit_code -ne 0 ]]; then
    echo "Calendar task added successfully"
    return 0
  else
    echo "Failed to add calendar task"
    return 1
  fi
}
```

Of course, these two functions do not yet exist, but they will soon. I added early returns in the cases that the other methods fail, as well as returns for the error or success states. This will allow me to add some error handling in the future, if I think it's needed.

## Explanation of the Wrapping Function
- **order_insulin Function**: This is the main wrapping function that coordinates the entire process. It calls the request_prescription function and checks its exit code to determine the next steps.
- **Exit Code Check**: The function checks if the request_prescription function returns 0 (success). If successful, it proceeds to the next steps.
- **Step 4 - Sending Email to Pharmacy**: The function send_pharmacy_email is called. The script checks the exit code of this function to determine if the email was sent successfully.
- **Step 5 - Adding Task to Google Calendar**: If the email was sent successfully, the function add_calendar_task is called. The script checks the exit code of this function to determine if the calendar task was added successfully.

## What’s Next?
In the next part of this series, we will focus on implementing the send_pharmacy_email function, which automates sending an email to the pharmacy to pre-order the insulin.

Stay tuned as we continue to streamline the insulin ordering process, one step at a time.

[Read part 4](https://dev.to/goudekettingrm/automating-insulin-ordering-sending-the-email-to-the-pharmacy-part-4-1bp)

Thank you for following along! If you have any questions or suggestions, feel free to leave a comment below. Don't forget to share this post with anyone who might find it useful.