Skip to content

Commit

Permalink
Merge branch 'feat/router-contact-fields' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
paulobernardoaf committed Jan 27, 2025
2 parents d935426 + e136061 commit 6332a60
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
4 changes: 4 additions & 0 deletions WENI-CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.49.2
----------
* Fix template field to save null when empty

1.49.1
----------
* Send created_on on wenichats message forward
Expand Down
9 changes: 5 additions & 4 deletions core/models/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ type Msg struct {
URNAuth null.String `db:"urn_auth" json:"urn_auth,omitempty"`
OrgID OrgID `db:"org_id" json:"org_id"`
TopupID TopupID `db:"topup_id" json:"-"`
Template string `db:"template" json:"template"`
Template null.String `db:"template" json:"template"`

SessionID SessionID `json:"session_id,omitempty"`
SessionStatus SessionStatus `json:"session_status,omitempty"`
Expand Down Expand Up @@ -179,7 +179,7 @@ func (m *Msg) TopupID() TopupID { return m.m.TopupID }
func (m *Msg) ContactID() ContactID { return m.m.ContactID }
func (m *Msg) ContactURNID() *URNID { return m.m.ContactURNID }
func (m *Msg) IsResend() bool { return m.m.IsResend }
func (m *Msg) Template() string { return m.m.Template }
func (m *Msg) Template() null.String { return m.m.Template }

func (m *Msg) SetTopup(topupID TopupID) { m.m.TopupID = topupID }

Expand Down Expand Up @@ -383,6 +383,7 @@ func newOutgoingMsg(rt *runtime.Runtime, org *Org, channel *Channel, contactID C
m.OrgID = org.ID()
m.TopupID = NilTopupID
m.CreatedOn = createdOn
m.Template = null.NullString

msg.SetChannel(channel)
msg.SetURN(out.URN())
Expand Down Expand Up @@ -436,7 +437,7 @@ func newOutgoingMsg(rt *runtime.Runtime, org *Org, channel *Channel, contactID C
}
if out.Templating() != nil {
metadata["templating"] = out.Templating()
m.Template = out.Templating().Template().Name
m.Template = null.String(out.Templating().Template().Name)
}
if out.Topic() != flows.NilMsgTopic {
metadata["topic"] = string(out.Topic())
Expand Down Expand Up @@ -661,7 +662,7 @@ func newOutgoingMsgWpp(rt *runtime.Runtime, org *Org, channel *Channel, contactI
}
if msgWpp.Templating() != nil {
metadata["templating"] = msgWpp.Templating()
m.Template = msgWpp.Templating().Template().Name
m.Template = null.String(msgWpp.Templating().Template().Name)
}

m.Metadata = null.NewMap(metadata)
Expand Down
4 changes: 2 additions & 2 deletions core/models/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func TestMarshalMsg(t *testing.T) {
"session_id": %d,
"session_status": "W",
"status": "Q",
"template": "",
"template": null,
"text": "Hi there",
"tps_cost": 2,
"urn": "tel:+250700000001?id=10000",
Expand Down Expand Up @@ -333,7 +333,7 @@ func TestMarshalMsg(t *testing.T) {
"session_id": %d,
"session_status": "W",
"status": "Q",
"template": "",
"template": null,
"text": "Hi there",
"tps_cost": 1,
"urn": "tel:+250700000001?id=10000",
Expand Down
50 changes: 36 additions & 14 deletions core/tasks/handler/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ func handleMsgEvent(ctx context.Context, rt *runtime.Runtime, event *MsgEvent) e
return fmt.Errorf("no project uuid found")
}

err = requestToRouter(event, rt.Config, projectUUID)
err = requestToRouter(event, rt.Config, contact, projectUUID)
if err != nil {
return errors.Wrap(err, "unable to send message to router")
}
Expand Down Expand Up @@ -918,23 +918,25 @@ type StopEvent struct {
OccurredOn time.Time `json:"occurred_on"`
}

func requestToRouter(event *MsgEvent, rtConfig *runtime.Config, projectUUID uuids.UUID) error {
func requestToRouter(event *MsgEvent, rtConfig *runtime.Config, contact *flows.Contact, projectUUID uuids.UUID) error {
httpClient, httpRetries, _ := goflow.HTTP(rtConfig)

body := struct {
ProjectUUID uuids.UUID `json:"project_uuid"`
ContactURN urns.URN `json:"contact_urn"`
Text string `json:"text"`
Attachments []utils.Attachment `json:"attachments"`
Metadata json.RawMessage `json:"metadata"`
MsgEvent MsgEvent `json:"msg_event"`
ProjectUUID uuids.UUID `json:"project_uuid"`
ContactURN urns.URN `json:"contact_urn"`
Text string `json:"text"`
Attachments []utils.Attachment `json:"attachments"`
Metadata json.RawMessage `json:"metadata"`
MsgEvent MsgEvent `json:"msg_event"`
ContactFields map[string]interface{} `json:"contact_fields"`
}{
ProjectUUID: projectUUID,
ContactURN: event.URN.Identity(),
Text: event.Text,
Attachments: event.Attachments,
Metadata: event.Metadata,
MsgEvent: *event,
ProjectUUID: projectUUID,
ContactURN: event.URN.Identity(),
Text: event.Text,
Attachments: event.Attachments,
Metadata: event.Metadata,
MsgEvent: *event,
ContactFields: mapContactFields(contact),
}

var b io.Reader
Expand Down Expand Up @@ -964,6 +966,26 @@ func requestToRouter(event *MsgEvent, rtConfig *runtime.Config, projectUUID uuid
return nil
}

func mapContactFields(contact *flows.Contact) map[string]interface{} {
if len(contact.Fields()) == 0 {
return nil
}

contactFields := make(map[string]interface{})

for key, field := range contact.Fields() {
contactFields[key] = struct {
Value interface{} `json:"value"`
Type string `json:"type"`
}{
Value: field.QueryValue(),
Type: string(field.Type()),

Check failure on line 982 in core/tasks/handler/worker.go

View workflow job for this annotation

GitHub Actions / Test (12)

field.Type undefined (type *flows.FieldValue has no field or method Type)

Check failure on line 982 in core/tasks/handler/worker.go

View workflow job for this annotation

GitHub Actions / Test (12)

field.Type undefined (type *flows.FieldValue has no field or method Type)

Check failure on line 982 in core/tasks/handler/worker.go

View workflow job for this annotation

GitHub Actions / Test (12)

field.Type undefined (type *flows.FieldValue has no field or method Type)

Check failure on line 982 in core/tasks/handler/worker.go

View workflow job for this annotation

GitHub Actions / Test (12)

field.Type undefined (type *flows.FieldValue has no field or method Type)

Check failure on line 982 in core/tasks/handler/worker.go

View workflow job for this annotation

GitHub Actions / Test (13)

field.Type undefined (type *flows.FieldValue has no field or method Type)

Check failure on line 982 in core/tasks/handler/worker.go

View workflow job for this annotation

GitHub Actions / Test (13)

field.Type undefined (type *flows.FieldValue has no field or method Type)

Check failure on line 982 in core/tasks/handler/worker.go

View workflow job for this annotation

GitHub Actions / Test (13)

field.Type undefined (type *flows.FieldValue has no field or method Type)

Check failure on line 982 in core/tasks/handler/worker.go

View workflow job for this annotation

GitHub Actions / Test (13)

field.Type undefined (type *flows.FieldValue has no field or method Type)

Check failure on line 982 in core/tasks/handler/worker.go

View workflow job for this annotation

GitHub Actions / Test (12)

field.Type undefined (type *flows.FieldValue has no field or method Type)

Check failure on line 982 in core/tasks/handler/worker.go

View workflow job for this annotation

GitHub Actions / Test (12)

field.Type undefined (type *flows.FieldValue has no field or method Type)

Check failure on line 982 in core/tasks/handler/worker.go

View workflow job for this annotation

GitHub Actions / Test (12)

field.Type undefined (type *flows.FieldValue has no field or method Type)

Check failure on line 982 in core/tasks/handler/worker.go

View workflow job for this annotation

GitHub Actions / Test (12)

field.Type undefined (type *flows.FieldValue has no field or method Type)

Check failure on line 982 in core/tasks/handler/worker.go

View workflow job for this annotation

GitHub Actions / Test (13)

field.Type undefined (type *flows.FieldValue has no field or method Type)

Check failure on line 982 in core/tasks/handler/worker.go

View workflow job for this annotation

GitHub Actions / Test (13)

field.Type undefined (type *flows.FieldValue has no field or method Type)

Check failure on line 982 in core/tasks/handler/worker.go

View workflow job for this annotation

GitHub Actions / Test (13)

field.Type undefined (type *flows.FieldValue has no field or method Type)

Check failure on line 982 in core/tasks/handler/worker.go

View workflow job for this annotation

GitHub Actions / Test (13)

field.Type undefined (type *flows.FieldValue has no field or method Type)
}
}

return contactFields
}

// creates a new event task for the passed in timeout event
func newTimedTask(eventType string, orgID models.OrgID, contactID models.ContactID, sessionID models.SessionID, runID models.FlowRunID, eventTime time.Time) *queue.Task {
event := &TimedEvent{
Expand Down

0 comments on commit 6332a60

Please sign in to comment.