Skip to content

Commit

Permalink
allow null names
Browse files Browse the repository at this point in the history
  • Loading branch information
nicpottier committed Aug 23, 2017
1 parent cb47efb commit a2e73e8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
16 changes: 12 additions & 4 deletions backends/rapidpro/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (ts *MsgTestSuite) TestMsgUnmarshal() {
ts.Equal(msg.ChannelUUID_.String(), "f3ad3eb6-d00d-4dc3-92e9-9f34f32940ba")
ts.Equal(msg.ChannelID_, courier.NewChannelID(11))
ts.Equal([]string{"https://foo.bar/image.jpg"}, msg.Attachments())
ts.Equal(msg.ExternalID_, "")
ts.Equal(msg.ExternalID(), "")
}

func (ts *MsgTestSuite) TestCheckMsgExists() {
Expand Down Expand Up @@ -165,11 +165,19 @@ func (ts *MsgTestSuite) TestContact() {
ts.Equal(contact.UUID, contact2.UUID)
ts.Equal(contact.ID, contact2.ID)
ts.Equal(knChannel.OrgID(), contact2.OrgID)
ts.Equal("Ryan Lewis", contact2.Name)
ts.Equal("Ryan Lewis", contact2.Name.String)
ts.True(contact2.ModifiedOn.After(now))
ts.True(contact2.CreatedOn.After(now))
ts.True(contact2.ModifiedOn.Before(now2))
ts.True(contact2.CreatedOn.Before(now2))

// load a contact by URN instead (this one is in our testdata)
contact, err = contactForURN(ts.b.db, knChannel.OrgID(), knChannel.ID(), courier.NewTelURNForCountry("+12067799192", "US"), "")
ts.NoError(err)
ts.NotNil(contact)

ts.Equal("", contact.Name.String)
ts.Equal("a984069d-0008-4d8c-a772-b14a8a6acccc", contact.UUID)
}

func (ts *MsgTestSuite) TestContactURN() {
Expand Down Expand Up @@ -522,7 +530,7 @@ func (ts *MsgTestSuite) TestWriteMsg() {
ts.Equal(MsgIncoming, m.Direction_)
ts.Equal(courier.MsgPending, m.Status_)
ts.Equal(courier.DefaultPriority, m.Priority_)
ts.Equal("ext123", m.ExternalID_)
ts.Equal("ext123", m.ExternalID())
ts.Equal("test123", m.Text_)
ts.Equal(0, len(m.Attachments()))
ts.Equal(1, m.MessageCount_)
Expand All @@ -534,7 +542,7 @@ func (ts *MsgTestSuite) TestWriteMsg() {
ts.NotNil(m.QueuedOn_)

contact, err := contactForURN(ts.b.db, m.OrgID_, m.ChannelID_, urn, "")
ts.Equal("test contact", contact.Name)
ts.Equal("test contact", contact.Name.String)
ts.Equal(m.OrgID_, contact.OrgID)
ts.Equal(m.ContactID_, contact.ID)
ts.NotNil(contact.UUID)
Expand Down
14 changes: 9 additions & 5 deletions backends/rapidpro/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@ func contactForURN(db *sqlx.DB, org OrgID, channelID courier.ChannelID, urn cour
// didn't find it, we need to create it instead
contact.OrgID = org
contact.UUID = uuid.NewV4().String()
contact.Name = name
contact.CreatedOn = time.Now()
contact.ModifiedOn = time.Now()

// TODO: don't set name for anonymous orgs
if name != "" {
contact.Name = null.StringFrom(name)
}

// TODO: Set these to a system user
contact.CreatedBy = 1
contact.ModifiedBy = 1
Expand All @@ -91,10 +95,10 @@ func contactForURN(db *sqlx.DB, org OrgID, channelID courier.ChannelID, urn cour

// DBContact is our struct for a contact in the database
type DBContact struct {
OrgID OrgID `db:"org_id"`
ID ContactID `db:"id"`
UUID string `db:"uuid"`
Name string `db:"name"`
OrgID OrgID `db:"org_id"`
ID ContactID `db:"id"`
UUID string `db:"uuid"`
Name null.String `db:"name"`

URNID ContactURNID `db:"urn_id"`

Expand Down
7 changes: 4 additions & 3 deletions backends/rapidpro/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/nyaruka/courier/queue"
"github.com/nyaruka/courier/utils"
"github.com/sirupsen/logrus"
null "gopkg.in/guregu/null.v3"
filetype "gopkg.in/h2non/filetype.v1"
)

Expand Down Expand Up @@ -330,7 +331,7 @@ type DBMsg struct {
URN_ courier.URN `json:"urn"`
Text_ string `json:"text" db:"text"`
Attachments_ pq.StringArray `json:"attachments" db:"attachments"`
ExternalID_ string `json:"external_id" db:"external_id"`
ExternalID_ null.String `json:"external_id" db:"external_id"`

ChannelID_ courier.ChannelID `json:"channel_id" db:"channel_id"`
ContactID_ ContactID `json:"contact_id" db:"contact_id"`
Expand Down Expand Up @@ -358,7 +359,7 @@ func (m *DBMsg) ID() courier.MsgID { return m.ID_ }
func (m *DBMsg) UUID() courier.MsgUUID { return m.UUID_ }
func (m *DBMsg) Text() string { return m.Text_ }
func (m *DBMsg) Attachments() []string { return []string(m.Attachments_) }
func (m *DBMsg) ExternalID() string { return m.ExternalID_ }
func (m *DBMsg) ExternalID() string { return m.ExternalID_.String }
func (m *DBMsg) URN() courier.URN { return m.URN_ }
func (m *DBMsg) ContactName() string { return m.ContactName_ }
func (m *DBMsg) Priority() courier.MsgPriority { return m.Priority_ }
Expand All @@ -378,7 +379,7 @@ func (m *DBMsg) WithContactName(name string) courier.Msg { m.ContactName_ = name
func (m *DBMsg) WithReceivedOn(date time.Time) courier.Msg { m.SentOn_ = date; return m }

// WithExternalID can be used to set the external id on a msg in a chained call
func (m *DBMsg) WithExternalID(id string) courier.Msg { m.ExternalID_ = id; return m }
func (m *DBMsg) WithExternalID(id string) courier.Msg { m.ExternalID_ = null.StringFrom(id); return m }

// WithID can be used to set the id on a msg in a chained call
func (m *DBMsg) WithID(id courier.MsgID) courier.Msg { m.ID_ = id; return m }
Expand Down
2 changes: 1 addition & 1 deletion backends/rapidpro/testdata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ INSERT INTO channels_channel("id", "schemes", "is_active", "created_on", "modifi
INSERT INTO channels_channel("id", "schemes", "is_active", "created_on", "modified_on", "uuid", "channel_type", "address", "org_id", "country", "config")
VALUES('13', '{"telegram"}', 'Y', NOW(), NOW(), 'dbc126ed-66bc-4e28-b67b-81dc3327c98a', 'TG', 'courierbot', 1, NULL, NULL);

/* Contact with id 100 */
/* Contacts with ids 100, 101 */
DELETE FROM contacts_contact;
INSERT INTO contacts_contact("id", "is_active", "created_on", "modified_on", "uuid", "is_blocked", "is_test", "is_stopped", "language", "created_by_id", "modified_by_id", "org_id")
VALUES(100, True, now(), now(), 'a984069d-0008-4d8c-a772-b14a8a6acccc', False, False, False, 'eng', 1, 1, 1);
Expand Down

0 comments on commit a2e73e8

Please sign in to comment.