Skip to content

πŸ›οΈ Processes receipts and calculates reward points

Notifications You must be signed in to change notification settings

trushmi/fetch-rewards-receipt-processor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Fetch Rewards Receipt Processor

In this ReadMe:

About this project

Fetch Rewards is a popular consumer-engagement platform in the United States with over 17 million active users. The platform allows users to submit digital or scan printed receipts to earn points, which can later be redeemed for free gift cards or other prizes and rewards.

This project aims to develop a web service in alignment with the 'Receipt Processor' task outlined by Fetch Rewards. The service is designed to meet the requirements of the provided API documentation, facilitating the processing of receipts and the calculation of reward points. The original repository for the task can be found here

API Specification

Endpoint: Process Receipts

  • Path: /receipts/process
  • Method: POST
  • Payload: Receipt JSON
  • Response: JSON containing an id for the receipt.

Example Response:

{ "id": "7fb1377b-b223-49d9-a31a-5a02701dd310" }

Endpoint: Get Points

  • Path: /receipts/{id}/points
  • Method: GET
  • Response: A JSON object containing the number of points awarded.

Example Response:

{ "points": 32 }

Rules for getting points

The points are calculated based on the following rules:

  1. One point for every alphanumeric character in the retailer name.
  2. 50 points if the total is a round dollar amount with no cents.
  3. 25 points if the total is a multiple of 0.25.
  4. 5 points for every two items on the receipt.
  5. If the trimmed length of the item description is a multiple of 3, multiply the price by 0.2 and round up to the nearest integer. The result is the number of points earned.
  6. 6 points if the day in the purchase date is odd.
  7. 10 points if the time of purchase is after 2:00pm and before 4:00pm.

Example

{
  "retailer": "Target",
  "purchaseDate": "2022-01-01",
  "purchaseTime": "13:01",
  "items": [
    {
      "shortDescription": "Mountain Dew 12PK",
      "price": "6.49"
    },{
      "shortDescription": "Emils Cheese Pizza",
      "price": "12.25"
    },{
      "shortDescription": "Knorr Creamy Chicken",
      "price": "1.26"
    },{
      "shortDescription": "Doritos Nacho Cheese",
      "price": "3.35"
    },{
      "shortDescription": "   Klarbrunn 12-PK 12 FL OZ  ",
      "price": "12.00"
    }
  ],
  "total": "35.35"
}
Expected result: 28 points

Tech Stack

  • Backend: Python, Flask
  • Containerization: Docker

Code snippets:

POST request API in the server.py file:

This screenshot shows the implementation of a POST request API in the server.py file

Function to check if all items in the Receipt are valid from validations.py file:

This screenshot shows a function from the utils.py file that checks if all items in a receipt are valid.

Helper function to calculate points for a purchase hour:

This screenshot shows a helper function that calculates points based on the purchase hour.

Test of the function to check if point calculation functions work as expected using unittest framework:

This screenshot shows a unit test that verifies if the point calculation functions work as expected using the unittest framework.

Structure

  • requirements.txt: lists all the necessary Python packages and their versions required to run the Flask application.

  • Dockerfile: defines the setup for a Docker container to host the Flask application.

  • src folder:

    server.py: establishes a Flask web server and defines two routes to process receipts and retrieve points associated with a receipt. It imports necessary functions from utils.py and validations.py to calculate points based on receipt data and validate receipt structures, respectively. The file contains main functionality to receive POST requests at /receipts/process to process new receipts, and GET requests at /receipts/{id}/points to retrieve points associated with a given receipt ID.

    validations.py : contains functions to validate the receipt structure and its content, ensuring that the data provided adheres to specified conditions. For example, it checks the type and format of the retailer name, purchase date and time, total amount, and item structure in the receipt. It also defines a ValidationResult class to encapsulate the result of a validation check.

    utils.py: contains a collection of functions to calculate points based on different criteria provided in a receipt. Each function takes a receipt dictionary as input and returns points based on certain conditions like the retailer's name, purchase date and time, total amount, and item descriptions among others. The file also contains a main function get_total_receipt_points which aggregates points from all the criteria.

    test_utils.py: includes unit tests for the functions defined in utils.py. It uses the unittest framework to define test cases ensuring that the point calculation functions work as expected.

Setup and running instructions

  1. Prerequisites:
  • Ensure that you have Docker installed on your machine.
  • Clone the project repository.

2.Build Docker Image:

  • Navigate to the project directory where the Dockerfile and requirements.txt are located.
  • Run the following command to build the Docker image:
docker build -t my-receipts-app .
  1. Run Docker Container:
  • Once the image is built, run the following command to start the container:
docker run -d -p 8080:5000 my-receipts-app
  1. Accessing the Application:
  1. Stop and Remove Docker Container:
  • To stop the running container, first find the container ID with the following command:
docker ps

Then stop the container with:

docker stop <container-id>
  1. Viewing Logs:
  • To view the logs for the running container, use the following command:
docker logs <container-id>
  1. Run the tests
  • To run the tests within Docker, ensure you have built your Docker image and have a running Docker container. You can find the container ID using the following command:
docker ps
  • Use the docker exec command to run the tests in the specified container:
docker exec -it <container-id> python -m unittest test_utils.py

Demo

You can use Postman to check and try how endpoints work. Postman is a popular API testing and development tool that allows you to send HTTP requests to endpoints and receive responses.

Example of Endpoints: Get Points and Process Receipts with Postman:

github

About Author

My name is Iryna. I am a software engineer with a background in the media industry, leadership, and communications. I recently graduated from the Software Engineering Program at Hackbright Academy, where I honed my skills in full-stack development and fundamental computer science concepts.

My technical expertise lies in object-oriented programming languages, particularly in Python. Additionally, I work with JavaScript, HTML, CSS, and the React framework to create responsive and client-oriented user interfaces. My LinkedIn

About

πŸ›οΈ Processes receipts and calculates reward points

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published