Skip to content

Commit

Permalink
♻️ refactor: added timezone for message and event #14
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jan 14, 2024
1 parent c3acda4 commit a35e397
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 71 deletions.
134 changes: 66 additions & 68 deletions pkg/ami/ami_action.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ami

import (
"fmt"

"github.com/pnguyen215/voipkit/pkg/ami/config"
)

Expand Down Expand Up @@ -30,74 +28,74 @@ func (a *AMIAction) SetTimeout(timeout int) *AMIAction {
return a
}

func (c *AMIAction) Revoke(a *AMI, d *AMIDictionary, e *AMIMessage, deadlock bool) (*AMIResponse, error) {
D().Info("Ami revoking action (state mutex opened lock~unlock): '%v'", e.String())
var response AMIResponse
var _err error
if IsStringEmpty(c.Name) {
response.Message = fmt.Sprintf(config.AmiErrorFieldRequired, "name")
response.IsSuccess = false
_err = fmt.Errorf(response.Message)
return &response, _err
}
a.Action(e)
all := a.AllEvents()
if deadlock {
defer a.Close()
}
for {
select {
case message := <-all:
message.SetTimeFormat(e.TimeFormat)
message.SetPhonePrefix(e.PhonePrefix)
message.SetRegion(e.Region)
message.AddFieldDateReceivedAt()
if message.IsResponse() {
response.event = message
response.IsSuccess = true
response.Json = message.JsonTranslator(d)
goto on_success
}
case err := <-a.Error():
a.Close()
_err = err
response.event = nil
response.IsSuccess = false
response.Message = _err.Error()
goto on_failed
}
}
// func (c *AMIAction) Revoke(a *AMI, d *AMIDictionary, e *AMIMessage, deadlock bool) (*AMIResponse, error) {
// D().Info("Ami revoking action (state mutex opened lock~unlock): '%v'", e.String())
// var response AMIResponse
// var _err error
// if IsStringEmpty(c.Name) {
// response.Message = fmt.Sprintf(config.AmiErrorFieldRequired, "name")
// response.IsSuccess = false
// _err = fmt.Errorf(response.Message)
// return &response, _err
// }
// a.Action(e)
// all := a.AllEvents()
// if deadlock {
// defer a.Close()
// }
// for {
// select {
// case message := <-all:
// message.SetTimeFormat(e.TimeFormat)
// message.SetPhonePrefix(e.PhonePrefix)
// message.SetRegion(e.Region)
// message.AddFieldDateReceivedAt()
// if message.IsResponse() {
// response.event = message
// response.IsSuccess = true
// response.Json = message.JsonTranslator(d)
// goto on_success
// }
// case err := <-a.Error():
// a.Close()
// _err = err
// response.event = nil
// response.IsSuccess = false
// response.Message = _err.Error()
// goto on_failed
// }
// }

on_success:
return &response, nil
on_failed:
return &response, _err
}
// on_success:
// return &response, nil
// on_failed:
// return &response, _err
// }

func (c *AMIAction) Run(a *AMI) (*AMIResponse, error) {
action := WithMessage(config.AmiActionCommand)
action.AddField(config.AmiActionCommand, c.Name)
return c.Revoke(a, NewDictionary(), action, false)
}
// func (c *AMIAction) Run(a *AMI) (*AMIResponse, error) {
// action := WithMessage(config.AmiActionCommand)
// action.AddField(config.AmiActionCommand, c.Name)
// return c.Revoke(a, NewDictionary(), action, false)
// }

func (c *AMIAction) WithRunX(a *AMI, dictionaries map[string]string) (*AMIResponse, error) {
action := WithMessage(config.AmiActionCommand)
action.AddField(config.AmiActionCommand, c.Name)
d := NewDictionary()
d.AddKeysTranslator(dictionaries)
return c.Revoke(a, d, action, false)
}
// func (c *AMIAction) WithRunX(a *AMI, dictionaries map[string]string) (*AMIResponse, error) {
// action := WithMessage(config.AmiActionCommand)
// action.AddField(config.AmiActionCommand, c.Name)
// d := NewDictionary()
// d.AddKeysTranslator(dictionaries)
// return c.Revoke(a, d, action, false)
// }

func (c *AMIAction) WithRunV(a *AMI, command map[string]string) (*AMIResponse, error) {
action := WithMessage(c.Name)
action.AddFields(command)
return c.Revoke(a, NewDictionary(), action, false)
}
// func (c *AMIAction) WithRunV(a *AMI, command map[string]string) (*AMIResponse, error) {
// action := WithMessage(c.Name)
// action.AddFields(command)
// return c.Revoke(a, NewDictionary(), action, false)
// }

func (c *AMIAction) WithRunXV(a *AMI, command, dictionaries map[string]string) (*AMIResponse, error) {
action := WithMessage(c.Name)
action.AddFields(command)
d := NewDictionary()
d.AddKeysTranslator(dictionaries)
return c.Revoke(a, d, action, false)
}
// func (c *AMIAction) WithRunXV(a *AMI, command, dictionaries map[string]string) (*AMIResponse, error) {
// action := WithMessage(c.Name)
// action.AddFields(command)
// d := NewDictionary()
// d.AddKeysTranslator(dictionaries)
// return c.Revoke(a, d, action, false)
// }
5 changes: 5 additions & 0 deletions pkg/ami/ami_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func (m *AMIEvent) SetRegion(value string) *AMIEvent {
return m
}

func (m *AMIEvent) SetTimezone(value string) *AMIEvent {
m.Timezone = value
return m
}

func (e *AMIEvent) OpenFullEvents(c *AMI) {
all := c.AllEvents()
defer c.Close()
Expand Down
20 changes: 17 additions & 3 deletions pkg/ami/ami_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func (m *AMIMessage) SetRegion(value string) *AMIMessage {
return m
}

func (m *AMIMessage) SetTimezone(value string) *AMIMessage {
m.Timezone = value
return m
}

func ofMessage(header textproto.MIMEHeader) *AMIMessage {
m := &AMIMessage{}
m.header = header
Expand Down Expand Up @@ -206,10 +211,18 @@ func (k *AMIMessage) AddActionId() {

// Added Date Received at generated
func (k *AMIMessage) AddFieldDateReceivedAt() {
if len(k.TimeFormat) > 0 {
k.AddField(config.AmiFieldDateReceivedAt, time.Now().Format(k.TimeFormat))
if !IsStringEmpty(k.TimeFormat) {
if IsStringEmpty(k.Timezone) {
k.AddField(config.AmiFieldDateReceivedAt, time.Now().Format(k.TimeFormat))
} else {
k.AddField(config.AmiFieldDateReceivedAt, AdjustTimezone(time.Now(), k.Timezone).Format(k.TimeFormat))
}
} else {
k.AddField(config.AmiFieldDateReceivedAt, time.Now().String())
if IsStringEmpty(k.Timezone) {
k.AddField(config.AmiFieldDateReceivedAt, time.Now().String())
} else {
k.AddField(config.AmiFieldDateReceivedAt, AdjustTimezone(time.Now(), k.Timezone).String())
}
}
}

Expand Down Expand Up @@ -413,6 +426,7 @@ func (k *AMIMessage) apply(e *AMIEvent) *AMIMessage {
k.SetTimeFormat(e.TimeFormat).
SetPhonePrefix(e.PhonePrefix).
SetRegion(e.Region).
SetTimezone(e.Timezone).
AddFieldDateReceivedAt()
return k
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/ami/ami_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ type AMIMessage struct {
TimeFormat string `json:"time_format,omitempty"`
PhonePrefix []string `json:"phone_prefix,omitempty"`
Region string `json:"region,omitempty"`
Timezone string `json:"timezone"`
}

type AMIEvent struct {
TimeFormat string `json:"time_format,omitempty"`
PhonePrefix []string `json:"phone_prefix,omitempty"`
Region string `json:"region,omitempty"`
Timezone string `json:"timezone"`
}

type AMIDictionary struct {
Expand Down

0 comments on commit a35e397

Please sign in to comment.