Refund & Exchange & Split Order Script
This script is designed to send emails to clients when they request a refund or exchange. it also send emasils to clients whose orders has been split, and will arrrive at different times. It first makes a SQL query to Linnworks to fetch data about the requested refund or exchange. Once it has the necessary data, the script sends an email to the client using a professional email template. The script is written in Python and uses the SMTPlib library to send emails.
- Clone the repository and install the dependencies
- Running the script
The database is created automatically when you run the code, if the database doesn’t already exist. It is used to store order data and retrieve processed order data on demand. The database used in this script is SQLite.
The RETURN_TABLE_QUERY also creates the table if does not already exist.
The Linnapi class is designed to interact with the Linnworks API for fetching and processing split, exchange and refund orders data.
- Token Management: This method is responsible for obtaining a new authentication token to interact with the Linnworks API. It ensures that a valid token is always available.
The method is used to fetch the access token and add it to the headers. If the token has expired and has not been refreshed, it is fetched again using the get_data_from_api method
- Data Retrieval: This method fetches data from the Linnworks API using a POST request and returns the JSON response. It also handles token refresh if the current token is invalid. it mostly acts as a helper function to fetch data
- insert_data_into_db: This method inserts data into an SQLite database using the provided cursor. It is used to store refund and exchange data. For more info on the sqlite python documentation visit this LINK
- process_data - The process_data function is a part of a data pipeline that extracts, transforms, and loads (ETL) data into a SQLite database.
- Extracts Data: It uses a custom script query (data_query) to retrieve data from an external source via an API. The API URL is constructed using the base URL (self.apiurl) and the endpoint (‘/Dashboards/ExecuteCustomScriptQuery’).
- Transforms Data: The retrieved data is transformed into a pandas DataFrame for easier manipulation. It then iterates over each row of the DataFrame, checking if the record already exists in the database. If not, it inserts the new record into the database.
- Loads Data: The transformed data is loaded into a SQLite database (towerlondonRefunds.db). The function creates a table in the database using the provided SQL query (table_query), and inserts data into the specified table (table_name).
- Email Notifications: After loading the data into the database, it retrieves all unprocessed records (where processed = 0) from the table and triggers email notifications for each record using the provided email function (email_func).
the code snippet shows how the function works.
- refund_data, exchange_data, and split_order - Each of these methods processes a specific type of order (refunds, exchanges, and split orders respectively) by calling the process_data function with parameters specific to each order type. These parameters include the SQL queries for creating the table and retrieving data, the table name, column names, and a function for sending emails specific to the order type (send_refund_mail, send_exchange_mail, or send_split_mail).
This Python module provides a set of helper methods for sending emails. It is designed to streamline the process of sending various types of emails, including refund notifications and order exchange notifications.
- send_email_template - This function sends an email using a provided template file and dynamic data. It handles the process of replacing placeholders in the template with the dynamic values and sending the email. Parameters are:
- template_file (str): Path to the template file.
- name (str): Recipient's name.
- email (str): Recipient's email address.
- subject (str): Email subject.
- bcc (str): Bcc email address.
- data (dict): Dictionary containing dynamic data for the template.
- caller: function calling the send_email_template function.
The code snippet shoes how data is filled to the template File, and then an email is sent to the client.
create_email_data - This function is designed to prepare the data needed for sending an email. It takes a dictionary of row data (row_dict) and a template file path (template_file) as inputs.
- update_order_status - This function is used update the OrderId status in the Database after an e-mail has been sent. processed is changed from 0 to 1.
- prepare_email - this function is a utility function used to prepare and send an email using data from a SQLite database and a specified template file. Here’s a brief description of its functionality and parameters:
- Functionality: The function first calls the create_email_data function to prepare the email data using the provided row data and template file. It then sends the email using the send_email function, and updates the order status in the database using the update_order_status function. If any error occurs during this process, it logs the error using main_log.error.
code snippet:
- send_refund_email - The function is used to send an email notification based on a refund or out-of-stock reason.
- send_exchange_mail - The function is used to send an email notification for order exchange.
- send_split_mail - The function is used to send an email notification for an order that has been Split
They are different but serve the same purpose.
Example of the send_exchange_mail
The Configuration Module serves as a centralized location for various essential parameters and credentials. It includes:
File Paths: Paths to files that the application needs to access. API Keys: Keys required to authenticate with various APIs. Email Credentials: Credentials needed to send emails from the application. Logfile Paths: Locations of log files for application debugging and record-keeping. Function Configurations: Settings and parameters for various functions within the application.
The template files are stored in the templates folder. The template files are in html format and contain placeholders for dynamic values. The placeholders are replaced with dynamic values before the email is sent. The template files are passed to the send_email_template function as parameters. The function then replaces the placeholders with the dynamic values and sends the email to the client.
The Test suite in this script has been written with Pytest
To run the Tests run
This script run as a PM2 process on the server. the process is scheduled to run as specified below. I use the Schedule Library to schedule the process.
This is a general overview of the Refund, Exchange & Split Order Mail. With the Information provided you will be able to run the script seamlessly.