diff --git a/apier/v1/accounts.go b/apier/v1/accounts.go index e16b50f46..07f2096fa 100644 --- a/apier/v1/accounts.go +++ b/apier/v1/accounts.go @@ -24,7 +24,6 @@ import ( "strings" "time" - "github.com/cgrates/cgrates/cache" "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/guardian" "github.com/cgrates/cgrates/utils" @@ -46,27 +45,29 @@ func (self *ApierV1) GetAccountActionPlan(attrs AttrAcntAction, reply *[]*Accoun if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account"}); len(missing) != 0 { return utils.NewErrMandatoryIeMissing(strings.Join(missing, ","), "") } - accountATs := make([]*AccountActionTiming, 0) // needs to be initialized if remains empty - allAPs, err := self.RatingDb.GetAllActionPlans() - if err != nil { + acntID := utils.AccountKey(attrs.Tenant, attrs.Account) + acntAPids, err := self.RatingDb.GetAccountActionPlans(acntID, false, utils.NonTransactional) + if err != nil && err != utils.ErrNotFound { return utils.NewErrServerError(err) } - accID := utils.AccountKey(attrs.Tenant, attrs.Account) - for _, ap := range allAPs { - if ap == nil { - continue + var acntAPs []*engine.ActionPlan + for _, apID := range acntAPids { + if ap, err := self.RatingDb.GetActionPlan(apID, false, utils.NonTransactional); err != nil { + return err + } else if ap != nil { + acntAPs = append(acntAPs, ap) } - if _, exists := ap.AccountIDs[accID]; exists { - for _, at := range ap.ActionTimings { - accountATs = append(accountATs, &AccountActionTiming{ - ActionPlanId: ap.Id, - Uuid: at.Uuid, - ActionsId: at.ActionsID, - NextExecTime: at.GetNextStartTime(time.Now()), - }) - } + } + accountATs := make([]*AccountActionTiming, 0) // needs to be initialized if remains empty + for _, ap := range acntAPs { + for _, at := range ap.ActionTimings { + accountATs = append(accountATs, &AccountActionTiming{ + ActionPlanId: ap.Id, + Uuid: at.Uuid, + ActionsId: at.ActionsID, + NextExecTime: at.GetNextStartTime(time.Now()), + }) } - } *reply = accountATs return nil @@ -172,7 +173,6 @@ func (self *ApierV1) SetAccount(attr utils.AttrSetAccount, reply *string) (err e } } if len(attr.ActionPlanId) != 0 { - _, err := guardian.Guardian.Guard(func() (interface{}, error) { var ap *engine.ActionPlan ap, err := self.RatingDb.GetActionPlan(attr.ActionPlanId, false, utils.NonTransactional) @@ -203,25 +203,31 @@ func (self *ApierV1) SetAccount(attr utils.AttrSetAccount, reply *string) (err e } } // clean previous action plans - actionPlansMap, err := self.RatingDb.GetAllActionPlans() - if err != nil { - if err == utils.ErrNotFound { // if no action plans just continue - return 0, nil - } + acntAPids, err := self.RatingDb.GetAccountActionPlans(accID, false, utils.NonTransactional) + if err != nil && err != utils.ErrNotFound { return 0, err } - for actionPlanID, ap := range actionPlansMap { - if actionPlanID == attr.ActionPlanId { - // don't remove it if it's the current one - continue - } - if _, exists := ap.AccountIDs[accID]; exists { + for _, apID := range acntAPids { + if apID != attr.ActionPlanId { + ap, err := self.RatingDb.GetActionPlan(apID, false, utils.NonTransactional) + if err != nil { + return 0, err + } delete(ap.AccountIDs, accID) - // clean from cache - cache.RemKey(utils.ACTION_PLAN_PREFIX+actionPlanID, true, utils.NonTransactional) + if err = self.RatingDb.SetActionPlan(apID, ap, true, utils.NonTransactional); err != nil { + return 0, err + } + if err = self.RatingDb.CacheDataFromDB(utils.ACTION_PLAN_PREFIX, []string{ap.Id}, true); err != nil { + return 0, err + } } } - + if err = self.RatingDb.SetAccountActionPlans(accID, []string{attr.ActionPlanId}, false); err != nil { + return 0, err + } + if err = self.RatingDb.CacheDataFromDB(utils.AccountActionPlansPrefix, []string{accID}, true); err != nil { + return 0, err + } return 0, nil }, 0, utils.ACTION_PLAN_PREFIX) if err != nil { diff --git a/apier/v1/apier.go b/apier/v1/apier.go index 8d5bca27d..33d008a1d 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -667,12 +667,12 @@ func (self *ApierV1) SetActionPlan(attrs AttrSetActionPlan, reply *string) (err } type AttrGetActionPlan struct { - Id string + ID string } func (self *ApierV1) GetActionPlan(attr AttrGetActionPlan, reply *[]*engine.ActionPlan) error { var result []*engine.ActionPlan - if attr.Id == "" || attr.Id == "*" { + if attr.ID == "" || attr.ID == "*" { aplsMap, err := self.RatingDb.GetAllActionPlans() if err != nil { return err @@ -681,7 +681,7 @@ func (self *ApierV1) GetActionPlan(attr AttrGetActionPlan, reply *[]*engine.Acti result = append(result, apls) } } else { - apls, err := self.RatingDb.GetActionPlan(attr.Id, false, utils.NonTransactional) + apls, err := self.RatingDb.GetActionPlan(attr.ID, false, utils.NonTransactional) if err != nil { return err } diff --git a/engine/storage_mongo_datadb.go b/engine/storage_mongo_datadb.go index 3c83f5ef0..3f2d6c3b8 100644 --- a/engine/storage_mongo_datadb.go +++ b/engine/storage_mongo_datadb.go @@ -1473,11 +1473,11 @@ func (ms *MongoStorage) SetActionTriggers(key string, atrs ActionTriggers, trans session, col := ms.conn(colAtr) defer session.Close() if len(atrs) == 0 { - err = col.Remove(bson.M{"key": key}) // delete the key - if err != mgo.ErrNotFound { - return err + err = col.Remove(bson.M{"key": key}) + if err == mgo.ErrNotFound { // Overwrite not found since it is not really mandatory here to be returned + err = nil } - return nil + return } _, err = col.Upsert(bson.M{"key": key}, &struct { Key string