Skip to content

fetch-rewards/receipt-rule-interview

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Receipt Rule Engine

A flexible rule engine for processing receipts and calculating point rewards based on configurable rules.

Overview

This receipt rule engine allows you to define and apply rules to receipts to calculate point rewards. The engine supports two types of rules:

  • Store Name Rules: Award points when a receipt is from a specific store
  • Item Match Rules: Award points based on matching item IDs and applying a rate to the item price

Rule Types

Store Name Rule

Awards a fixed number of points when the store name contains the specified value.

Rule Type: RuleTypeStoreName

JSON Format:

{
  "value": "Target",
  "points": 100
}

Fields:

  • value (string, required): The store name or substring to match against
  • points (int, required): Number of points to award (must be positive)

Matching Behavior:

  • Uses substring matching (case-sensitive)
  • Example: "value": "Target" matches store names "Target", "Target Store", "Super Target"

Item Match Rule

Awards points based on matching item IDs and applying a rate to the item price.

Rule Type: RuleTypeItemMatch

JSON Format:

{
  "ids": ["111", "222", "333"],
  "rate": 0.1
}

Fields:

  • ids (array of strings, required): List of item IDs to match
  • rate (float, required): Multiplier rate to apply to the item price (must be positive)

Point Calculation:

  1. Convert item price to points: $1.00 = 1000 points
  2. Apply the rate: points * rate
  3. Floor the result: math.Floor(points * rate)

Example:

  • Item price: $12.25
  • Rate: 0.1 (10%)
  • Calculation: 12.25 * 1000 * 0.1 = 1225 points

Usage

import (
    "receipt-rule-engine/model"
    "receipt-rule-engine/processor"
)

// Create a new processor
p := processor.NewProcessor()

// Add rules
p.AddRule(model.RuleTypeStoreName, `{"value":"Target","points":100}`)
p.AddRule(model.RuleTypeItemMatch, `{"ids":["111","222"],"rate":0.1}`)

// Process a receipt
receipt := model.Receipt{
    StoreName: "Target",
    Items: []model.Item{
        {ID: "111", Price: 12.25},
        {ID: "333", Price: 5.00},
    },
}

points, err := p.Process(receipt)
// Returns: 1325 points (100 for store + 1225 for item "111")

Running Tests

go test ./tests/... -v

About

A simple project for technical interviews.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages