Skip to content

Commit

Permalink
Updating pricing strategy with filter functions and stragety chain ap…
Browse files Browse the repository at this point in the history
…proach + request to obj datatype change
  • Loading branch information
rajagopal28 committed Jul 30, 2021
1 parent bbc73b6 commit af9ac93
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pricing_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package pricingengine
// inputs that are used to provide pricing for a given user.
type GeneratePricingRequest struct {
DateOfBirth string `json:"date_of_birth"`
InsuranceGroup string `json:"insurance_group"`
InsuranceGroup int `json:"insurance_group"`
LicenseHeldSince string `json:"license_held_since"`
}

Expand All @@ -22,6 +22,6 @@ type GeneratePricingResponse struct {
// PricingItem - contains the pricing data generated for partucular group based on the request passed
type PricingItem struct {
Premium float64 `json:"premium"`
Currency string `json:"premium"`
Currency string `json:"currency"`
FareGroup string `json:"fare_group"`
}
80 changes: 76 additions & 4 deletions service/app/pricing_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package app

import (
"log"
"time"
"errors"

"pricingengine"
"pricingengine/service/model"
Expand All @@ -11,16 +13,86 @@ import (
type Strategy struct{}

// Chained functional response that keeps the ball rolling with the
type StrartegyChain func(*pricingengine.GeneratePricingResponse) (*pricingengine.GeneratePricingResponse)
type StrartegyChain func(*pricingengine.PricingItem) (*pricingengine.PricingItem, error)

// GeneratePricing will calculate how much a 'risk' be priced or if they should
// be denied.
func (s *Strategy) ApplyBasePricing(input *pricingengine.GeneratePricingRequest, config *models.BaseRate, fn StrartegyChain) (*pricingengine.GeneratePricingResponse, error) {
var result pricingengine.GeneratePricingResponse = pricingengine.GeneratePricingResponse{}
func (s *Strategy) ApplyBasePricing(input *pricingengine.GeneratePricingRequest, config *models.RangeConfig, fn StrartegyChain) (*pricingengine.PricingItem, error) {
var result pricingengine.PricingItem = pricingengine.PricingItem{}
// for the current BaseRate and GeneratePricingRequest calculate outcome rate
// just check the base price and
result.Premium = config.Value
result.Currency = "£"
result.FareGroup = config.Label
if fn != nil {
log.Println("Found a chain function, Passing on the result for further computation")
return fn(&result), nil
return fn(&result)
}
return &result, nil // return nil, errors.New("not implemented")
}

func (s *Strategy) ApplySubsecuentFactorsToPricing(input *pricingengine.GeneratePricingRequest, previousPricingItem *pricingengine.PricingItem, config *models.RangeConfig, fn StrartegyChain) (*pricingengine.PricingItem, error) {
var result pricingengine.PricingItem = pricingengine.PricingItem{}
// for the current BaseRate and GeneratePricingRequest calculate outcome rate
// just check the base price and
result.Premium = previousPricingItem.Premium * config.Value
result.Currency = previousPricingItem.Currency
result.FareGroup = previousPricingItem.FareGroup + " " + config.Label
if fn != nil {
log.Println("Found a chain function, Passing on the result for further computation")
return fn(&result)
}
return &result, nil // return nil, errors.New("not implemented")
}

func (s *Strategy) FindMatchingDriverAgeFactor(input *pricingengine.GeneratePricingRequest, allDriverAgeFactords []models.RangeConfig) (*models.RangeConfig, error) {
date_of_birth := input.DateOfBirth
parse_dob_t,_ := time.Parse("2006-01-02", date_of_birth)
now := time.Now()
age := int(now.Sub(parse_dob_t).Hours()/(24*30*12))
for i:= 0; i < len(allDriverAgeFactords); i++ {
current := allDriverAgeFactords[i]
if (current.Start < age && current.End >= age) {
if (current.IsEligible) {
return &current, nil
} else {
return &current, errors.New("Not Eligible")
}
}
}
return nil, errors.New("not found")
}

func (s *Strategy) FindMatchingInsuranceGroupFactor(input *pricingengine.GeneratePricingRequest, allInsuranceGroupFactors []models.RangeConfig) (*models.RangeConfig, error) {
insurance_group := input.InsuranceGroup
for i:= 0; i < len(allInsuranceGroupFactors); i++ {
current := allInsuranceGroupFactors[i]
if (current.Start < insurance_group && current.End >= insurance_group) {
if (current.IsEligible) {
return &current, nil
} else {
return &current, errors.New("Not Eligible")
}
}
}
return nil, errors.New("not found")
}


func (s *Strategy) FindMatchingLicenceValidityFactor(input *pricingengine.GeneratePricingRequest, allLicenceValidtyFactors []models.RangeConfig) (*models.RangeConfig, error) {
date_of_birth := input.LicenseHeldSince
parse_date_t,_ := time.Parse("2006-01-02", date_of_birth)
now := time.Now()
licence_length := int(now.Sub(parse_date_t).Hours()/(24*30*12))
for i:= 0; i < len(allLicenceValidtyFactors); i++ {
current := allLicenceValidtyFactors[i]
if (current.Start < licence_length && current.End >= licence_length) {
if (current.IsEligible) {
return &current, nil
} else {
return &current, errors.New("Not Eligible")
}
}
}
return nil, errors.New("not found")
}

0 comments on commit af9ac93

Please sign in to comment.