Skip to content

Commit

Permalink
Merge pull request #75 from weni-ai/revert-fix-wenichats-historymsg
Browse files Browse the repository at this point in the history
Revert "Merge pull request #70 from weni-ai/fix/wenichats-send-history"
  • Loading branch information
rasoro authored Jan 31, 2024
2 parents 9505ed4 + 1ba8eb5 commit 7661ee6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 106 deletions.
50 changes: 0 additions & 50 deletions core/models/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,56 +710,6 @@ func SelectContactMessages(ctx context.Context, db Queryer, contactID int, after
return msgs, nil
}

const selectMsgByFlowrunUUID = `
SELECT
id,
broadcast_id,
uuid,
text,
created_on,
direction,
status,
visibility,
msg_count,
error_count,
next_attempt,
external_id,
attachments,
metadata,
channel_id,
contact_id,
contact_urn_id,
org_id,
topup_id
FROM public.msgs_msg
JOIN (
SELECT (jsonb_array_elements(events)->'msg'->>'uuid')::uuid AS msg_uuid
FROM public.flows_flowrun
WHERE "uuid" = $1
) AS flow_events
ON flow_events.msg_uuid = public.msgs_msg."uuid";
`

// SelectMessagesByFlowRun loads the given messages for the passed in contact, created after the passed in time
func SelectMessagesByFlowRun(ctx context.Context, db Queryer, flowrunUUID string) ([]*Msg, error) {
rows, err := db.QueryxContext(ctx, selectMsgByFlowrunUUID, flowrunUUID)
if err != nil {
return nil, errors.Wrapf(err, "error querying msgs for flowrun UUID %v", flowrunUUID)
}
defer rows.Close()

msgs := make([]*Msg, 0)
for rows.Next() {
msg := &Msg{}
err = rows.StructScan(&msg.m)
if err != nil {
return nil, errors.Wrapf(err, "error scanning msg row")
}
msgs = append(msgs, msg)
}
return msgs, nil
}

// NormalizeAttachment will turn any relative URL in the passed in attachment and normalize it to
// include the full host for attachment domains
func NormalizeAttachment(cfg *runtime.Config, attachment utils.Attachment) utils.Attachment {
Expand Down
32 changes: 0 additions & 32 deletions core/models/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import (
"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/test"
"github.com/nyaruka/goflow/utils"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/core/runner"
"github.com/nyaruka/mailroom/runtime"
"github.com/nyaruka/mailroom/testsuite"
"github.com/nyaruka/mailroom/testsuite/testdata"
Expand Down Expand Up @@ -625,33 +623,3 @@ func TestSelectContactMessages(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 3, len(msgs))
}

func TestSelectMsgByFlowrunUUID(t *testing.T) {
ctx, rt, db, _ := testsuite.Get()
defer testsuite.Reset(testsuite.ResetAll)
defer uuids.SetGenerator(uuids.DefaultGenerator)
uuids.SetGenerator(uuids.NewSeededGenerator(12345))

oa, err := models.GetOrgAssetsWithRefresh(ctx, rt, testdata.Org1.ID, models.RefreshFlows)
assert.NoError(t, err)

rt.Config.DB = "postgres://mailroom_test:temba@localhost/mailroom_test?sslmode=disable&Timezone=UTC"

contactIDs := []models.ContactID{testdata.Cathy.ID}
start := models.NewFlowStart(models.OrgID(1), models.StartTypeManual, models.FlowTypeMessaging, testdata.SingleMessage.ID, true, true).
WithContactIDs(contactIDs)
batch := start.CreateBatch(contactIDs, true, len(contactIDs))

sessions, err := runner.StartFlowBatch(ctx, rt, batch)
assert.NoError(t, err)

csession := sessions[0]

fcs, err := csession.FlowSession(rt.Config, oa.SessionAssets(), oa.Env())
assert.NoError(t, err)

fruuid := string(fcs.Runs()[0].UUID())
msgs, err := models.SelectMessagesByFlowRun(ctx, db, fruuid)
assert.NoError(t, err)
assert.Equal(t, 1, len(msgs))
}
27 changes: 5 additions & 22 deletions services/tickets/wenichats/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -157,29 +156,13 @@ func (s *service) Open(session flows.Session, topic *flows.Topic, body string, a
return nil, errors.Wrap(err, "failed to create wenichats room webhook")
}

// get messages for history
after := session.Runs()[0].CreatedOn()
cx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
msgs := make([]*models.Msg, 0)
for _, fr := range session.Runs() {
fruuid := string(fr.UUID())
frmsgs, err := models.SelectMessagesByFlowRun(cx, db, fruuid)
if err != nil {
return nil, errors.Wrap(err, "failed to get history messages")
}
msgs = append(msgs, frmsgs...)
}

sort.Slice(msgs, func(i, j int) bool {
return msgs[i].CreatedOn().Before(msgs[j].CreatedOn())
})

if len(msgs) > 0 {
after := msgs[0].CreatedOn()
contactID := int(session.Contact().ID())
msgs, err = models.SelectContactMessages(cx, db, contactID, after)
if err != nil {
return nil, errors.Wrap(err, "failed to get history messages")
}
msgs, err := models.SelectContactMessages(cx, db, int(contact.ID()), after)
if err != nil {
return nil, errors.Wrap(err, "failed to get history messages")
}

//send history
Expand Down
19 changes: 17 additions & 2 deletions services/tickets/wenichats/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
"github.com/jmoiron/sqlx"
"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/gocommon/httpx"
"github.com/nyaruka/gocommon/uuids"
Expand All @@ -24,7 +26,7 @@ import (
)

func TestOpenAndForward(t *testing.T) {
ctx, rt, db, _ := testsuite.Get()
ctx, rt, _, _ := testsuite.Get()
testsuite.Reset(testsuite.ResetData | testsuite.ResetStorage)

defer dates.SetNowSource(dates.DefaultNowSource)
Expand Down Expand Up @@ -258,7 +260,20 @@ func TestOpenAndForward(t *testing.T) {
)
assert.EqualError(t, err, "missing project_auth or sector_uuid")

wenichats.SetDB(db)
mockDB, mock, _ := sqlmock.New()
defer mockDB.Close()
sqlxDB := sqlx.NewDb(mockDB, "sqlmock")

rows := sqlmock.NewRows([]string{"id", "uuid", "text", "high_priority", "created_on", "modified_on", "sent_on", "queued_on", "direction", "status", "visibility", "msg_type", "msg_count", "error_count", "next_attempt", "external_id", "attachments", "metadata", "broadcast_id", "channel_id", "contact_id", "contact_urn_id", "org_id", "topup_id"})

after, err := time.Parse("2006-01-02T15:04:05", "2019-10-07T15:21:30")
assert.NoError(t, err)

mock.ExpectQuery("SELECT").
WithArgs(1234567, after).
WillReturnRows(rows)

wenichats.SetDB(sqlxDB)

svc, err := wenichats.NewService(
rt.Config,
Expand Down

0 comments on commit 7661ee6

Please sign in to comment.