Skip to content

Commit 5d55cc1

Browse files
committed
User system user for new contacts
1 parent 6e38a60 commit 5d55cc1

File tree

5 files changed

+51
-16
lines changed

5 files changed

+51
-16
lines changed

Diff for: backends/rapidpro/backend.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ type backend struct {
5858
dyLogWriter *DynamoLogWriter // all logs being written to dynamo
5959
writerWG *sync.WaitGroup
6060

61-
db *sqlx.DB
62-
rp *redis.Pool
63-
dynamo *dynamo.Service
64-
s3 *s3x.Service
65-
cw *cwatch.Service
61+
db *sqlx.DB
62+
rp *redis.Pool
63+
dynamo *dynamo.Service
64+
s3 *s3x.Service
65+
cw *cwatch.Service
66+
systemUserID UserID
6667

6768
channelsByUUID *cache.Local[courier.ChannelUUID, *Channel]
6869
channelsByAddr *cache.Local[courier.ChannelAddress, *Channel]
@@ -232,6 +233,12 @@ func (b *backend) Start() error {
232233
b.dyLogWriter = NewDynamoLogWriter(b.dynamo, b.writerWG)
233234
b.dyLogWriter.Start()
234235

236+
// store the system user id
237+
b.systemUserID, err = getSystemUserID(ctx, b.db)
238+
if err != nil {
239+
return err
240+
}
241+
235242
// register and start our spool flushers
236243
courier.RegisterFlusher(path.Join(b.config.SpoolDir, "msgs"), b.flushMsgFile)
237244
courier.RegisterFlusher(path.Join(b.config.SpoolDir, "statuses"), b.flushStatusFile)

Diff for: backends/rapidpro/contact.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ type Contact struct {
5252
CreatedOn_ time.Time `db:"created_on"`
5353
ModifiedOn_ time.Time `db:"modified_on"`
5454

55-
CreatedBy_ int `db:"created_by_id"`
56-
ModifiedBy_ int `db:"modified_by_id"`
55+
CreatedBy_ UserID `db:"created_by_id"`
56+
ModifiedBy_ UserID `db:"modified_by_id"`
5757

5858
IsNew_ bool
5959
}
6060

6161
// UUID returns the UUID for this contact
6262
func (c *Contact) UUID() courier.ContactUUID { return c.UUID_ }
6363

64-
const insertContactSQL = `
64+
const sqlInsertContact = `
6565
INSERT INTO
6666
contacts_contact(org_id, is_active, status, uuid, created_on, modified_on, created_by_id, modified_by_id, name, ticket_count)
6767
VALUES(:org_id, TRUE, 'A', :uuid, :created_on, :modified_on, :created_by_id, :modified_by_id, :name, 0)
@@ -70,7 +70,7 @@ RETURNING id
7070

7171
// insertContact inserts the passed in contact, the id field will be populated with the result on success
7272
func insertContact(tx *sqlx.Tx, contact *Contact) error {
73-
rows, err := tx.NamedQuery(insertContactSQL, contact)
73+
rows, err := tx.NamedQuery(sqlInsertContact, contact)
7474
if err != nil {
7575
return err
7676
}
@@ -134,7 +134,9 @@ func contactForURN(ctx context.Context, b *backend, org OrgID, channel *Channel,
134134
contact.OrgID_ = org
135135
contact.UUID_ = courier.ContactUUID(uuids.NewV4())
136136
contact.CreatedOn_ = time.Now()
137+
contact.CreatedBy_ = b.systemUserID
137138
contact.ModifiedOn_ = time.Now()
139+
contact.ModifiedBy_ = b.systemUserID
138140
contact.IsNew_ = true
139141

140142
// if we aren't an anonymous org, we want to look up a name if possible and set it
@@ -166,10 +168,6 @@ func contactForURN(ctx context.Context, b *backend, org OrgID, channel *Channel,
166168
}
167169
}
168170

169-
// TODO: Set these to a system user
170-
contact.CreatedBy_ = 1
171-
contact.ModifiedBy_ = 1
172-
173171
// insert it
174172
tx, err := b.db.BeginTxx(ctx, nil)
175173
if err != nil {

Diff for: backends/rapidpro/schema.sql

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
DROP TABLE IF EXISTS users_user CASCADE;
2+
CREATE TABLE users_user (
3+
id serial primary key,
4+
username character varying(254) NOT NULL,
5+
first_name character varying(150) NOT NULL
6+
);
7+
18
DROP TABLE IF EXISTS orgs_org CASCADE;
29
CREATE TABLE orgs_org (
310
id serial primary key,
@@ -36,8 +43,8 @@ CREATE TABLE contacts_contact (
3643
uuid character varying(36) NOT NULL,
3744
name character varying(128),
3845
language character varying(3),
39-
created_by_id integer NOT NULL,
40-
modified_by_id integer NOT NULL,
46+
created_by_id integer references users_user(id) NOT NULL,
47+
modified_by_id integer references users_user(id) NOT NULL,
4148
org_id integer references orgs_org(id) on delete cascade
4249
);
4350

@@ -89,7 +96,7 @@ CREATE TABLE msgs_msg (
8996
--broadcast_id integer REFERENCES msgs_broadcast(id) ON DELETE CASCADE,
9097
--flow_id integer REFERENCES flows_flow(id) ON DELETE CASCADE,
9198
--ticket_id integer REFERENCES tickets_ticket(id) ON DELETE CASCADE,
92-
--created_by_id integer REFERENCES auth_user(id) ON DELETE CASCADE,
99+
created_by_id integer REFERENCES users_user(id),
93100
text text NOT NULL,
94101
attachments character varying(255)[] NULL,
95102
quick_replies character varying(64)[] NULL,

Diff for: backends/rapidpro/testdata.sql

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
DELETE FROM users_user;
2+
INSERT INTO users_user("id", "username", "first_name") VALUES(1, 'system', 'System');
3+
14
/* Org with id 1 */
25
DELETE FROM orgs_org;
36
INSERT INTO orgs_org("id", "name", "language", "is_anon", "config")

Diff for: backends/rapidpro/user.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package rapidpro
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/jmoiron/sqlx"
8+
)
9+
10+
type UserID int
11+
12+
// gets the system user to use for contact audit fields
13+
func getSystemUserID(ctx context.Context, db *sqlx.DB) (UserID, error) {
14+
var id UserID
15+
err := db.GetContext(ctx, &id, "SELECT id FROM users_user WHERE username = 'system'")
16+
if err != nil {
17+
return 0, fmt.Errorf("error looking up system user: %w", err)
18+
}
19+
return id, nil
20+
}

0 commit comments

Comments
 (0)