mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-16 21:59:53 +05:00
ApierV1 TPActions - remove checking for existing, allow duplicates
This commit is contained in:
@@ -21,7 +21,6 @@ package apier
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
@@ -35,33 +34,11 @@ func (self *ApierV1) SetTPActions(attrs utils.TPActions, reply *string) error {
|
||||
if action.BalanceType != "" { // Add some inter-dependent parameters - if balanceType then we are not talking about simply calling actions
|
||||
requiredFields = append(requiredFields, "Direction", "Units")
|
||||
}
|
||||
if missing := utils.MissingStructFields(&action, requiredFields); len(missing) != 0 {
|
||||
if missing := utils.MissingStructFields(action, requiredFields); len(missing) != 0 {
|
||||
return fmt.Errorf("%s:Action:%s:%v", utils.ERR_MANDATORY_IE_MISSING, action.Identifier, missing)
|
||||
}
|
||||
}
|
||||
if exists, err := self.StorDb.ExistsTPActions(attrs.TPid, attrs.ActionsId); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else if exists {
|
||||
return errors.New(utils.ERR_DUPLICATE)
|
||||
}
|
||||
acts := make([]*engine.Action, len(attrs.Actions))
|
||||
for idx, act := range attrs.Actions {
|
||||
acts[idx] = &engine.Action{
|
||||
ActionType: act.Identifier,
|
||||
BalanceId: act.BalanceType,
|
||||
Direction: act.Direction,
|
||||
ExpirationString: act.ExpiryTime,
|
||||
ExtraParameters: act.ExtraParameters,
|
||||
Balance: &engine.Balance{
|
||||
Value: act.Units,
|
||||
DestinationId: act.DestinationId,
|
||||
RateSubject: act.RatingSubject,
|
||||
Weight: act.BalanceWeight,
|
||||
},
|
||||
Weight: act.Weight,
|
||||
}
|
||||
}
|
||||
if err := self.StorDb.SetTPActions(attrs.TPid, map[string][]*engine.Action{attrs.ActionsId: acts}); err != nil {
|
||||
if err := self.StorDb.SetTPActions(attrs.TPid, map[string][]*utils.TPAction{attrs.ActionsId: attrs.Actions}); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
}
|
||||
*reply = "OK"
|
||||
@@ -106,3 +83,17 @@ func (self *ApierV1) GetTPActionIds(attrs AttrGetTPActionIds, reply *[]string) e
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
// Removes specific Actions on Tariff plan
|
||||
func (self *ApierV1) RemTPActions(attrs AttrGetTPActions, reply *string) error {
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "ActionsId"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if err := self.StorDb.RemTPData(utils.TBL_TP_ACTIONS, attrs.TPid, attrs.ActionsId); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else {
|
||||
*reply = "OK"
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ type LoadStorage interface {
|
||||
GetTPRatingProfile(string, string) (*utils.TPRatingProfile, error)
|
||||
GetTPRatingProfileIds(*utils.AttrTPRatingProfileIds) ([]string, error)
|
||||
ExistsTPActions(string, string) (bool, error)
|
||||
SetTPActions(string, map[string][]*Action) error
|
||||
SetTPActions(string, map[string][]*utils.TPAction) error
|
||||
GetTPActions(string, string) (*utils.TPActions, error)
|
||||
GetTPActionIds(string) ([]string, error)
|
||||
ExistsTPActionTimings(string, string) (bool, error)
|
||||
|
||||
@@ -551,23 +551,24 @@ func (self *SQLStorage) ExistsTPActions(tpid, actsId string) (bool, error) {
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func (self *SQLStorage) SetTPActions(tpid string, acts map[string][]*Action) error {
|
||||
func (self *SQLStorage) SetTPActions(tpid string, acts map[string][]*utils.TPAction) error {
|
||||
if len(acts) == 0 {
|
||||
return nil //Nothing to set
|
||||
}
|
||||
qry := fmt.Sprintf("INSERT INTO %s (tpid,tag,action,balance_type,direction,units,expiry_time,destination_tag,rating_subject,balance_weight,extra_parameters,weight) VALUES ", utils.TBL_TP_ACTIONS)
|
||||
vals := ""
|
||||
i := 0
|
||||
for actId, actRows := range acts {
|
||||
for _, act := range actRows {
|
||||
if i != 0 { //Consecutive values after the first will be prefixed with "," as separator
|
||||
qry += ","
|
||||
vals += ","
|
||||
}
|
||||
qry += fmt.Sprintf("('%s','%s','%s','%s','%s',%f,'%s','%s','%s',%f,'%s',%f)",
|
||||
tpid, actId, act.ActionType, act.BalanceId, act.Direction, act.Balance.Value, act.ExpirationString,
|
||||
act.Balance.DestinationId, act.Balance.RateSubject, act.Balance.Weight, act.ExtraParameters, act.Weight)
|
||||
vals += fmt.Sprintf("('%s','%s','%s','%s','%s',%f,'%s','%s','%s',%f,'%s',%f)",
|
||||
tpid, actId, act.Identifier, act.BalanceType, act.Direction, act.Units, act.ExpiryTime,
|
||||
act.DestinationId, act.RatingSubject, act.BalanceWeight, act.ExtraParameters, act.Weight)
|
||||
i++
|
||||
}
|
||||
}
|
||||
qry := fmt.Sprintf("INSERT INTO %s (tpid,tag,action,balance_type,direction,units,expiry_time,destination_tag,rating_subject,balance_weight,extra_parameters,weight) VALUES %s ON DUPLICATE KEY UPDATE action=values(action),balance_type=values(balance_type),direction=values(direction),units=values(units),expiry_time=values(expiry_time),destination_tag=values(destination_tag),rating_subject=values(rating_subject),balance_weight=values(balance_weight),extra_parameters=values(extra_parameters),weight=values(weight)", utils.TBL_TP_ACTIONS, vals)
|
||||
if _, err := self.Db.Exec(qry); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -589,7 +590,7 @@ func (self *SQLStorage) GetTPActions(tpid, actsId string) (*utils.TPActions, err
|
||||
if err = rows.Scan(&action, &balanceId, &dir, &units, &expTime, &destId, &rateSubject, &balanceWeight, &extraParameters, &weight); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
acts.Actions = append(acts.Actions, &utils.Action{action, balanceId, dir, units, expTime, destId, rateSubject, balanceWeight, extraParameters, weight})
|
||||
acts.Actions = append(acts.Actions, &utils.TPAction{action, balanceId, dir, units, expTime, destId, rateSubject, balanceWeight, extraParameters, weight})
|
||||
}
|
||||
if i == 0 {
|
||||
return nil, nil
|
||||
|
||||
@@ -308,21 +308,19 @@ func (self *TPCSVImporter) importActions(fn string) error {
|
||||
}
|
||||
continue
|
||||
}
|
||||
act := &Action{
|
||||
ActionType: actionType,
|
||||
BalanceId: balanceType,
|
||||
act := &utils.TPAction{
|
||||
Identifier: actionType,
|
||||
BalanceType: balanceType,
|
||||
Direction: direction,
|
||||
ExpirationString: record[5],
|
||||
Units: units,
|
||||
ExpiryTime: record[5],
|
||||
DestinationId: destTag,
|
||||
RatingSubject: rateSubject,
|
||||
BalanceWeight: balanceWeight,
|
||||
ExtraParameters: record[9],
|
||||
Balance: &Balance{
|
||||
Value: units,
|
||||
DestinationId: destTag,
|
||||
RateSubject: rateSubject,
|
||||
Weight: balanceWeight,
|
||||
},
|
||||
Weight: weight,
|
||||
Weight: weight,
|
||||
}
|
||||
if err := self.StorDb.SetTPActions(self.TPid, map[string][]*Action{actId: []*Action{act}}); err != nil {
|
||||
if err := self.StorDb.SetTPActions(self.TPid, map[string][]*utils.TPAction{actId: []*utils.TPAction{act}}); err != nil {
|
||||
if self.Verbose {
|
||||
log.Printf("Ignoring line %d, storDb operational error: <%s> ", lineNr, err.Error())
|
||||
}
|
||||
|
||||
@@ -148,10 +148,10 @@ type AttrTPRatingProfileIds struct {
|
||||
type TPActions struct {
|
||||
TPid string // Tariff plan id
|
||||
ActionsId string // Actions id
|
||||
Actions []*Action // Set of actions this Actions profile will perform
|
||||
Actions []*TPAction // Set of actions this Actions profile will perform
|
||||
}
|
||||
|
||||
type Action struct {
|
||||
type TPAction struct {
|
||||
Identifier string // Identifier mapped in the code
|
||||
BalanceType string // Type of balance the action will operate on
|
||||
Direction string // Balance direction
|
||||
|
||||
Reference in New Issue
Block a user