From 609e1213c334bd241a0852ac4a6957f5d50a74cb Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 18 Jun 2013 11:18:54 +0300 Subject: [PATCH] added AddTriggeredAction --- apier/tpdestinations.go | 48 +++++++++++++++++++++++++++++++++++++++++ docs/apicalls.rst | 19 +++++++++++++--- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/apier/tpdestinations.go b/apier/tpdestinations.go index 4ae67f56b..882ab9f94 100644 --- a/apier/tpdestinations.go +++ b/apier/tpdestinations.go @@ -177,3 +177,51 @@ func (self *Apier) SetRatingProfile(attr *AttrSetRatingProfile, reply *float64) err = self.StorDb.SetRatingProfile(subject) return err } + +type AttrActionTrigger struct { + Tenant string + Account string + Direction string + BalanceId string + ThresholdValue float64 + DestinationId string + Weight float64 + ActionsId string +} + +func (self *Apier) AddTriggeredAction(attr *AttrActionTrigger, reply *float64) error { + if attr.Direction == "" { + attr.Direction = rater.OUTBOUND + } + + at := &rater.ActionTrigger{ + Id: utils.GenUUID(), + BalanceId: attr.BalanceId, + Direction: attr.Direction, + ThresholdValue: attr.ThresholdValue, + DestinationId: attr.DestinationId, + Weight: attr.Weight, + ActionsId: attr.ActionsId, + Executed: false, + } + + tag := fmt.Sprintf("%s:%s:%s", attr.Direction, attr.Tenant, attr.Account) + var dbErr error + rater.AccLock.Guard(tag, func() (float64, error) { + userBalance, err := self.StorDb.GetUserBalance(tag) + if err != nil { + dbErr = err + return 0, err + } + + userBalance.ActionTriggers = append(userBalance.ActionTriggers, at) + + if err = self.StorDb.SetUserBalance(userBalance); err != nil { + dbErr = err + return 0, err + } + return 0, nil + }) + + return dbErr +} diff --git a/docs/apicalls.rst b/docs/apicalls.rst index 85f7cb22a..f01b6c904 100644 --- a/docs/apicalls.rst +++ b/docs/apicalls.rst @@ -408,12 +408,25 @@ Executes specified action on a user account. Example ExecuteAction(attr \*AttrExecuteAction, reply \*float64) -AddTimedAction -++++++++++++++ - AddTriggeredAction ++++++++++++++++++ +:: + + type AttrActionTrigger struct { + Tenant string + Account string + Direction string + BalanceId string + ThresholdValue float64 + DestinationId string + Weight float64 + ActionsId string + } + +Example + AddTriggeredAction(attr \*AttrActionTrigger, reply \*float64) + SetRatingProfile ++++++++++++++++