Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to use new users_user table #20

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions core/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ type User struct {

const sqlSelectUser = `
SELECT row_to_json(r) FROM (
SELECT u.id, u.email, TRIM(CONCAT(u.first_name, ' ', u.last_name)) AS name, s.avatar
FROM auth_user u
INNER JOIN orgs_usersettings s ON s.user_id = u.id
WHERE u.id = $1 AND u.is_active
SELECT id, email, TRIM(CONCAT(first_name, ' ', last_name)) AS name, avatar
FROM users_user
WHERE id = $1 AND is_active
) r`

func LoadUser(ctx context.Context, rt *runtime.Runtime, id UserID) (*User, error) {
Expand Down
14 changes: 4 additions & 10 deletions testsuite/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,14 @@ CREATE TABLE contacts_contacturn (
UNIQUE (org_id, identity)
);

DROP TABLE IF EXISTS auth_user CASCADE;
CREATE TABLE auth_user (
DROP TABLE IF EXISTS users_user CASCADE;
CREATE TABLE users_user (
id serial primary key,
email character varying(254) NOT NULL,
first_name character varying(150),
last_name character varying(150),
is_active boolean NOT NULL,
is_staff boolean NOT NULL
);

DROP TABLE IF EXISTS orgs_usersettings CASCADE;
CREATE TABLE orgs_usersettings (
id serial primary key,
user_id integer NOT NULL REFERENCES auth_user(id) ON DELETE CASCADE,
is_staff boolean NOT NULL,
avatar character varying(100) NULL
);

Expand All @@ -94,7 +88,7 @@ CREATE TABLE msgs_msg (
broadcast_id integer REFERENCES msgs_broadcast(id) ON DELETE CASCADE,
flow_id integer REFERENCES flows_flow(id) ON DELETE CASCADE,
ticket_id integer REFERENCES tickets_ticket(id) ON DELETE CASCADE,
created_by_id integer REFERENCES auth_user(id) ON DELETE CASCADE,
created_by_id integer REFERENCES users_user(id) ON DELETE CASCADE,
text text NOT NULL,
attachments character varying(255)[] NULL,
quick_replies character varying(64)[] NULL,
Expand Down
8 changes: 2 additions & 6 deletions testsuite/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,10 @@ func InsertURN(rt *runtime.Runtime, orgID models.OrgID, contactID models.Contact

func InsertUser(rt *runtime.Runtime, email, firstName, lastName, avatar string) models.UserID {
row := rt.DB.QueryRow(
`INSERT INTO auth_user(email, first_name, last_name, is_active, is_staff)
VALUES($1, $2, $3, TRUE, FALSE) RETURNING id`, email, firstName, lastName,
`INSERT INTO users_user(email, first_name, last_name, is_active, is_staff, avatar)
VALUES($1, $2, $3, TRUE, FALSE, $4) RETURNING id`, email, firstName, lastName, avatar,
)
var id models.UserID
must(row.Scan(&id))

_, err := rt.DB.Exec(`INSERT INTO orgs_usersettings(user_id, avatar) VALUES($1, $2)`, id, avatar)
noError(err)

return id
}