A flexible rule engine for processing receipts and calculating point rewards based on configurable rules.
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
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 againstpoints(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"
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 matchrate(float, required): Multiplier rate to apply to the item price (must be positive)
Point Calculation:
- Convert item price to points:
$1.00 = 1000 points - Apply the rate:
points * rate - Floor the result:
math.Floor(points * rate)
Example:
- Item price:
$12.25 - Rate:
0.1(10%) - Calculation:
12.25 * 1000 * 0.1 = 1225 points
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")go test ./tests/... -v