Skip to content

Commit

Permalink
refactor: msg status and service
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Dec 29, 2021
1 parent 795308f commit 014897d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
11 changes: 5 additions & 6 deletions app/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ type Message struct {
Status Status `json:"status"` // Status is the status of the message.
}

type Status string
type Status uint8

const (
StatusUnsent Status = "unsent"
StatusPending Status = "pending"
StatusPartial Status = "partial"
StatusSent Status = "sent"
StatusCreated Status = iota
StatusSent
StatusFailed
)

func NewMessage(subject, from string, to map[string]bool, text string) *Message {
Expand All @@ -34,7 +33,7 @@ func NewMessage(subject, from string, to map[string]bool, text string) *Message
From: from,
To: to,
Text: text,
Status: StatusPending,
Status: StatusCreated,
}
}

Expand Down
40 changes: 17 additions & 23 deletions service/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (m *Message) Create(subject, from string, to map[string]bool, text string)
return nil, err
}

return msg, err
return msg, nil
}

func (m *Message) CreateAttachment(msg *app.Message, name string, data []byte) (*app.Attachment, error) {
Expand Down Expand Up @@ -59,42 +59,36 @@ func (m *Message) SendBridges(msg *app.Message, bridges []app.Bridge) error {
return app.ErrBridgesNotFound
}

var errs []error
sentCount := 0
for _, bridge := range bridges {
emsg := bridge.EndpointMessage(msg)
if !emsg.IsEmpty() {
for _, name := range bridge.Endpoints {
endpoint, err := m.endpointREPO.Get(name)
if err != nil {
return err
}
err = endpoint.Send(emsg)
if err != nil {
errs = append(errs, err)
} else {
sentCount++
}
}
if emsg.IsEmpty() {
continue
}
}

for _, err := range errs {
log.Println("service.Message.SendBridges:", err)
for _, name := range bridge.Endpoints {
endpoint, err := m.endpointREPO.Get(name)
if err != nil {
return err
}

err = endpoint.Send(emsg)
if err != nil {
log.Println("service.Message.SendBridges:", err)
} else {
sentCount++
}
}
}

if sentCount == 0 {
if err := m.UpdateStatus(msg, app.StatusUnsent); err != nil {
if err := m.UpdateStatus(msg, app.StatusFailed); err != nil {
return err
}

return app.ErrEndpointSendFailed
}

if len(errs) > 0 {
return m.UpdateStatus(msg, app.StatusPartial)
}

return m.UpdateStatus(msg, app.StatusSent)
}

Expand Down

0 comments on commit 014897d

Please sign in to comment.