Skip to content

Commit

Permalink
♻️ refactor: updated codebase #14
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Sep 20, 2023
1 parent f2b29ee commit 1427ea2
Show file tree
Hide file tree
Showing 20 changed files with 418 additions and 600 deletions.
3 changes: 1 addition & 2 deletions pkg/ami/ami_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/pnguyen215/gobase-voip-core/pkg/ami/config"
"github.com/pnguyen215/gobase-voip-core/pkg/ami/utils"
)

type AMICallbackService interface {
Expand Down Expand Up @@ -72,7 +71,7 @@ func (a *AMICallbackHandler) AppendIgnoredEvents(values ...string) *AMICallbackH
}

func (a *AMICallbackHandler) Json() string {
return utils.ToJson(a)
return ToJson(a)
}

func (h *AMICallbackHandler) Send() (AMIResultRaw, error) {
Expand Down
39 changes: 19 additions & 20 deletions pkg/ami/ami_cdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/pnguyen215/gobase-voip-core/pkg/ami/config"
"github.com/pnguyen215/gobase-voip-core/pkg/ami/utils"
)

func NewAMICdr() *AMICdr {
Expand All @@ -19,27 +18,27 @@ func NewAMICdr() *AMICdr {
}

func (r *AMICdr) SetEvent(value string) *AMICdr {
r.Event = utils.TrimAllSpace(value)
r.Event = TrimAllSpace(value)
return r
}

func (r *AMICdr) SetAccountCode(value string) *AMICdr {
r.AccountCode = utils.TrimAllSpace(value)
r.AccountCode = TrimAllSpace(value)
return r
}

func (r *AMICdr) SetSource(value string) *AMICdr {
r.Source = utils.TrimAllSpace(value)
r.Source = TrimAllSpace(value)
return r
}

func (r *AMICdr) SetDestination(value string) *AMICdr {
r.Destination = utils.TrimAllSpace(value)
r.Destination = TrimAllSpace(value)
return r
}

func (r *AMICdr) SetDestinationContext(value string) *AMICdr {
r.DestinationContext = utils.TrimAllSpace(value)
r.DestinationContext = TrimAllSpace(value)
return r
}

Expand All @@ -49,22 +48,22 @@ func (r *AMICdr) SetCallerId(value string) *AMICdr {
}

func (r *AMICdr) SetChannel(value string) *AMICdr {
r.Channel = utils.TrimAllSpace(value)
r.Channel = TrimAllSpace(value)
return r
}

func (r *AMICdr) SetDestinationChannel(value string) *AMICdr {
r.DestinationChannel = utils.TrimAllSpace(value)
r.DestinationChannel = TrimAllSpace(value)
return r
}

func (r *AMICdr) SetLastApplication(value string) *AMICdr {
r.LastApplication = utils.TrimAllSpace(value)
r.LastApplication = TrimAllSpace(value)
return r
}

func (r *AMICdr) SetLastData(value string) *AMICdr {
r.LastData = utils.TrimAllSpace(value)
r.LastData = TrimAllSpace(value)
return r
}

Expand Down Expand Up @@ -162,12 +161,12 @@ func (r *AMICdr) SetBillableSecondWith(value string) *AMICdr {
}

func (r *AMICdr) SetDisposition(value string) *AMICdr {
r.Disposition = utils.TrimAllSpace(value)
r.Disposition = TrimAllSpace(value)
return r
}

func (r *AMICdr) SetAmaFlag(value string) *AMICdr {
r.AmaFlags = utils.TrimAllSpace(value)
r.AmaFlags = TrimAllSpace(value)
return r
}

Expand Down Expand Up @@ -200,7 +199,7 @@ func (r *AMICdr) SetPrivilege(value string) *AMICdr {
}

func (r *AMICdr) SetDirection(value string) *AMICdr {
r.Direction = utils.TrimAllSpace(value)
r.Direction = TrimAllSpace(value)
return r
}

Expand All @@ -210,22 +209,22 @@ func (r *AMICdr) SetFlowCall(value string) *AMICdr {
}

func (r *AMICdr) SetTypeDirection(value string) *AMICdr {
r.TypeDirection = utils.TrimAllSpace(value)
r.TypeDirection = TrimAllSpace(value)
return r
}

func (r *AMICdr) SetUserExten(value string) *AMICdr {
r.UserExtension = utils.TrimAllSpace(value)
r.UserExtension = TrimAllSpace(value)
return r
}

func (r *AMICdr) SetPhoneNumber(value string) *AMICdr {
r.PhoneNumber = utils.TrimAllSpace(value)
r.PhoneNumber = TrimAllSpace(value)
return r
}

func (r *AMICdr) SetExtenSplitterSymbol(value string) *AMICdr {
r.ExtenSplitterSymbol = utils.TrimAllSpace(value)
r.ExtenSplitterSymbol = TrimAllSpace(value)
return r
}

Expand All @@ -235,7 +234,7 @@ func (r *AMICdr) SetPlaybackUrl(value string) *AMICdr {
}

func (r *AMICdr) Json() string {
return utils.ToJson(r)
return ToJson(r)
}

func (r *AMICdr) IsCdrNoAnswer() bool {
Expand Down Expand Up @@ -346,7 +345,7 @@ func ParseCdr(e *AMIMessage, d *AMIDictionary) *AMICdr {
// detect outbound, inbound
// if the field destination is phone number, so mark this cdr belong to outbound, otherwise mark as inbound
form := "flow_call_from_'%v'_to_'%v'"
phone := utils.RemovePrefix(r.Destination, e.PhonePrefix...)
phone := RemovePrefix(r.Destination, e.PhonePrefix...)
if IsPhoneNumberAbsolute(phone, e.Region) {
flow := fmt.Sprintf(form, r.Channel, phone)
r.SetFlowCall(flow)
Expand Down Expand Up @@ -386,7 +385,7 @@ func ParseCdr(e *AMIMessage, d *AMIDictionary) *AMICdr {
r.SetPhoneNumber(r.Source)
}
if !inCase {
log.Printf("ParseCdr, CDR exception case = %v", utils.ToJson(r))
log.Printf("ParseCdr, CDR exception case = %v", ToJson(r))
}
}
return r
Expand Down
9 changes: 4 additions & 5 deletions pkg/ami/ami_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/pnguyen215/gobase-voip-core/pkg/ami/config"
"github.com/pnguyen215/gobase-voip-core/pkg/ami/utils"
)

func NewChannel() *AMIChannel {
Expand All @@ -18,7 +17,7 @@ func NewChannel() *AMIChannel {

func (c *AMIChannel) SetChannelProtocol(protocol string) *AMIChannel {
if ok := config.AmiChannelProtocols[protocol]; !ok {
msg := fmt.Sprintf(config.AmiErrorProtocolMessage, strings.Join(utils.Keys(config.AmiChannelProtocols), ","))
msg := fmt.Sprintf(config.AmiErrorProtocolMessage, strings.Join(Keys(config.AmiChannelProtocols), ","))
log.Panic(config.AmiErrorInvalidProtocol, "\n", msg)
}
c.ChannelProtocol = protocol
Expand Down Expand Up @@ -66,10 +65,10 @@ func (c *AMIChannel) ValidWith(channelProtocol string, regex string, digitsExten
if len(digitsExten) == 0 {
return false
}
if utils.IsEmptyAbsolute(extension) {
if IsEmptyAbsolute(extension) {
return false
}
if utils.IsEmptyAbsolute(regex) {
if IsEmptyAbsolute(regex) {
return false
}
c.SetChannelProtocol(channelProtocol)
Expand Down Expand Up @@ -103,7 +102,7 @@ func (c *AMIChannel) ValidSIPDefaultWith(digitsExten []interface{}, extension st
// Return as form [email protected]
func (c *AMIChannel) JoinHostChannel(protocol, ip string) string {
c.SetChannelProtocol(protocol)
host, _, _ := utils.IPDecode(ip)
host, _, _ := IPDecode(ip)
form := "%v@%v"

if len(host) > 0 {
Expand Down
19 changes: 9 additions & 10 deletions pkg/ami/ami_chanspy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

"github.com/pnguyen215/gobase-voip-core/pkg/ami/config"
"github.com/pnguyen215/gobase-voip-core/pkg/ami/utils"
)

func NewAMIPayloadChanspy() *AMIPayloadChanspy {
Expand All @@ -18,20 +17,20 @@ func NewAMIPayloadChanspy() *AMIPayloadChanspy {
func (s *AMIPayloadChanspy) SetJoin(value string) *AMIPayloadChanspy {
ok := config.AmiChanspy[value]
if !ok {
msg := fmt.Sprintf(config.AmiErrorChanspyMessage, strings.Join(utils.Keys(config.AmiChanspy), ","))
msg := fmt.Sprintf(config.AmiErrorChanspyMessage, strings.Join(Keys(config.AmiChanspy), ","))
log.Panic(config.AmiErrorInvalidChanspy, "\n", msg)
}
s.Join = utils.TrimAllSpace(value)
s.Join = TrimAllSpace(value)
return s
}

func (s *AMIPayloadChanspy) SetSourceExten(value string) *AMIPayloadChanspy {
s.SourceExten = utils.TrimAllSpace(value)
s.SourceExten = TrimAllSpace(value)
return s
}

func (s *AMIPayloadChanspy) SetCurrentExten(value string) *AMIPayloadChanspy {
s.CurrentExten = utils.TrimAllSpace(value)
s.CurrentExten = TrimAllSpace(value)
return s
}

Expand All @@ -47,10 +46,10 @@ func (s *AMIPayloadChanspy) SetAllowDebug(value bool) *AMIPayloadChanspy {
}

func (s *AMIPayloadChanspy) CommandChanspy(channelExten string) string {
if utils.IsEmptyAbsolute(s.Join) {
if IsEmptyAbsolute(s.Join) {
return ""
}
if utils.IsEmptyAbsolute(channelExten) {
if IsEmptyAbsolute(channelExten) {
return ""
}
if strings.EqualFold(s.Join, config.AmiChanspySpy) {
Expand All @@ -69,13 +68,13 @@ func (s *AMIPayloadChanspy) CommandChanspy(channelExten string) string {
func Chanspy(ctx context.Context, s AMISocket, ch AMIPayloadChanspy) (AMIResultRawLevel, error) {
ok := config.AmiChanspy[ch.Join]
if !ok {
msg := fmt.Sprintf(config.AmiErrorChanspyMessage, strings.Join(utils.Keys(config.AmiChanspy), ","))
msg := fmt.Sprintf(config.AmiErrorChanspyMessage, strings.Join(Keys(config.AmiChanspy), ","))
log.Panic(config.AmiErrorInvalidChanspy, "\n", msg)
}
if utils.IsEmptyAbsolute(ch.SourceExten) {
if IsEmptyAbsolute(ch.SourceExten) {
return AMIResultRawLevel{}, fmt.Errorf("Source exten is required")
}
if utils.IsEmptyAbsolute(ch.CurrentExten) {
if IsEmptyAbsolute(ch.CurrentExten) {
return AMIResultRawLevel{}, fmt.Errorf("Current exten is required")
}
sourceValid, err := HasSIPPeerStatus(ctx, s, ch.SourceExten)
Expand Down
31 changes: 15 additions & 16 deletions pkg/ami/ami_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ami

import (
"github.com/pnguyen215/gobase-voip-core/pkg/ami/config"
"github.com/pnguyen215/gobase-voip-core/pkg/ami/utils"
)

var chargingEvents *map[string]string = &map[string]string{}
Expand All @@ -16,28 +15,28 @@ func (e *AMIEvent) SnapChargingEvent() *map[string]string {

_merged := make(map[string]string)

_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassCommands))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassSecurities))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassCalls))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassSystems))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassUsers))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassDialPlans))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassAgents))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassAgis))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassAocs))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassCallDetailRecords))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassChannelEventLoggings))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClasses))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassReports))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassDualToneMultiFrequencies))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassCommands))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassSecurities))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassCalls))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassSystems))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassUsers))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassDialPlans))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassAgents))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassAgis))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassAocs))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassCallDetailRecords))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassChannelEventLoggings))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClasses))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassReports))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassDualToneMultiFrequencies))

chargingEvents = &_merged
return chargingEvents
}

// SnapChargingEventWith
func (e *AMIEvent) SnapChargingEventWith(ls map[string][]string) *map[string]string {
chargingEvents := utils.MergeMaps(*chargingEvents, e.SwapCharging(ls))
chargingEvents := MergeMaps(*chargingEvents, e.SwapCharging(ls))
return &chargingEvents
}

Expand Down
Loading

0 comments on commit 1427ea2

Please sign in to comment.