From 5a1994a9437dbfb63d5cf40741b81e05e832099d Mon Sep 17 00:00:00 2001 From: DanB Date: Wed, 16 Jul 2014 09:36:15 +0200 Subject: [PATCH] Apier.AddTriggeredAction receiving now BalanceWeight and ExpiryTime --- apier/apier.go | 47 +++++++++++++++++++++++----------------- engine/action_trigger.go | 24 ++++++++++---------- engine/balances.go | 4 ++-- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/apier/apier.go b/apier/apier.go index 8a08420d5..ccdcef247 100644 --- a/apier/apier.go +++ b/apier/apier.go @@ -419,36 +419,43 @@ func (self *ApierV1) SetActionPlan(attrs AttrSetActionPlan, reply *string) error } type AttrAddActionTrigger struct { - Tenant string - Account string - Direction string - BalanceType string - ThresholdType string - ThresholdValue float64 - DestinationId string - Weight float64 - ActionsId string + Tenant string + Account string + Direction string + BalanceType string + ThresholdType string + ThresholdValue float64 + DestinationId string + BalanceWeight float64 + BalanceExpiryTime string + Weight float64 + ActionsId string } func (self *ApierV1) AddTriggeredAction(attr AttrAddActionTrigger, reply *string) error { if attr.Direction == "" { attr.Direction = engine.OUTBOUND } - + balExpiryTime, err := utils.ParseTimeDetectLayout(attr.BalanceExpiryTime) + if err != nil { + return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) + } at := &engine.ActionTrigger{ - Id: utils.GenUUID(), - BalanceType: attr.BalanceType, - Direction: attr.Direction, - ThresholdType: attr.ThresholdType, - ThresholdValue: attr.ThresholdValue, - DestinationId: attr.DestinationId, - Weight: attr.Weight, - ActionsId: attr.ActionsId, - Executed: false, + Id: utils.GenUUID(), + BalanceType: attr.BalanceType, + Direction: attr.Direction, + ThresholdType: attr.ThresholdType, + ThresholdValue: attr.ThresholdValue, + DestinationId: attr.DestinationId, + BalanceWeight: attr.BalanceWeight, + BalanceExpiryTime: balExpiryTime, + Weight: attr.Weight, + ActionsId: attr.ActionsId, + Executed: false, } tag := utils.BalanceKey(attr.Tenant, attr.Account, attr.Direction) - _, err := engine.AccLock.Guard(tag, func() (float64, error) { + _, err = engine.AccLock.Guard(tag, func() (float64, error) { userBalance, err := self.AccountDb.GetAccount(tag) if err != nil { return 0, err diff --git a/engine/action_trigger.go b/engine/action_trigger.go index 716423ad5..5b9b2f6e6 100644 --- a/engine/action_trigger.go +++ b/engine/action_trigger.go @@ -28,18 +28,18 @@ import ( ) type ActionTrigger struct { - Id string // uniquely identify the trigger - BalanceType string - Direction string - ThresholdType string //*min_counter, *max_counter, *min_balance, *max_balance - ThresholdValue float64 - Recurrent bool // reset eexcuted flag each run - DestinationId string - BalanceWeight float64 - BalanceExpirationDate time.Time - Weight float64 - ActionsId string - Executed bool + Id string // uniquely identify the trigger + BalanceType string + Direction string + ThresholdType string //*min_counter, *max_counter, *min_balance, *max_balance + ThresholdValue float64 + Recurrent bool // reset eexcuted flag each run + DestinationId string + BalanceWeight float64 + BalanceExpiryTime time.Time + Weight float64 + ActionsId string + Executed bool } func (at *ActionTrigger) Execute(ub *Account) (err error) { diff --git a/engine/balances.go b/engine/balances.go index 0fb946792..ec1ac8f67 100644 --- a/engine/balances.go +++ b/engine/balances.go @@ -76,8 +76,8 @@ func (b *Balance) MatchDestination(destinationId string) bool { func (b *Balance) MatchActionTrigger(at *ActionTrigger) bool { matchesExpirationDate := true - if !at.BalanceExpirationDate.IsZero() { - matchesExpirationDate = (at.BalanceExpirationDate.Equal(b.ExpirationDate)) + if !at.BalanceExpiryTime.IsZero() { + matchesExpirationDate = (at.BalanceExpiryTime.Equal(b.ExpirationDate)) } matchesWeight := true if at.BalanceWeight > 0 {