Skip to content

Commit

Permalink
fix: Save who created admin and teacher accounts (#63)
Browse files Browse the repository at this point in the history
* feat(db): Add created by column in users table

* fix(accounts): Save who created teachers and admin accounts
  • Loading branch information
PedroChaparro authored Oct 5, 2023
1 parent e2c0ebd commit 313d795
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 7 deletions.
11 changes: 10 additions & 1 deletion sql/migrations/20230920232901_init.down.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
-- ## Triggers
DROP TRIGGER IF EXISTS set_created_by ON users;
DROP FUNCTION IF EXISTS update_created_by();

-- ## Indexes
DROP INDEX IF EXISTS idx_class_users;

Expand All @@ -12,6 +16,10 @@ DROP INDEX IF EXISTS idx_users_fullname;
-- ## Views
DROP VIEW IF EXISTS courses_with_color;

DROP VIEW IF EXISTS courses_has_users_with_course;

DROP VIEW IF EXISTS users_with_creator;

-- ## Tables
DROP TABLE IF EXISTS grade_has_criteria;

Expand Down Expand Up @@ -49,4 +57,5 @@ DROP TYPE IF EXISTS SUBMISSION_STATUS;
DROP TYPE IF EXISTS USER_ROLES;

-- ## Extensions
DROP EXTENSION IF EXISTS "uuid-ossp";
DROP EXTENSION IF EXISTS "uuid-ossp";
DROP EXTENSION IF EXISTS "citext";
38 changes: 38 additions & 0 deletions sql/migrations/20230920232901_init.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CREATE TABLE IF NOT EXISTS users (
"email" CITEXT NOT NULL UNIQUE,
"full_name" VARCHAR NOT NULL,
"password_hash" VARCHAR NOT NULL,
"created_by" UUID NULL REFERENCES users(id),
"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

Expand Down Expand Up @@ -132,6 +133,22 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_grade_criteria ON grade_has_criteria(grade
CREATE INDEX IF NOT EXISTS idx_users_fullname ON users(full_name);

-- ## Views
--- ### Users
CREATE
OR REPLACE VIEW users_with_creator AS
SELECT
users.id,
users.role,
users.institutional_id,
users.email,
users.full_name,
users.created_by,
creator.full_name AS creator_full_name,
users.created_at
FROM
users
LEFT JOIN users AS creator ON users.created_by = creator.id;

--- ### courses
CREATE
OR REPLACE VIEW courses_with_color AS
Expand Down Expand Up @@ -160,6 +177,27 @@ FROM
INNER JOIN courses ON courses_has_users.course_id = courses.id
INNER JOIN colors ON courses.color_id = colors.id;

-- ## Triggers
--- ### Update created_by on users
CREATE
OR REPLACE FUNCTION update_created_by()
RETURNS TRIGGER
LANGUAGE PLPGSQL
AS $$
BEGIN
IF NEW.created_by IS NULL THEN
NEW.created_by := NEW.id;
END IF;

RETURN NEW;
END $$
;

CREATE OR REPLACE TRIGGER set_created_by
BEFORE INSERT ON users
FOR EACH ROW
EXECUTE PROCEDURE update_created_by();

-- ## Data
-- ### Colors
INSERT INTO
Expand Down
1 change: 1 addition & 0 deletions src/accounts/domain/dtos/register_user_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ type RegisterUserDTO struct {
Email string
InstitutionalId string
Password string
CreatedBy string
}
1 change: 1 addition & 0 deletions src/accounts/domain/entities/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ type User struct {
InstitutionalId string
PasswordHash string
CreatedAt string
CreatedBy string
}
7 changes: 7 additions & 0 deletions src/accounts/infrastructure/http/http_controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func (controller *AccountsController) HandleRegisterStudent(c *gin.Context) {
}

func (controller *AccountsController) HandleRegisterAdmin(c *gin.Context) {
adminUUID := c.GetString("session_uuid")

// Parse request body
var request requests.RegisterAdminRequest
if err := c.ShouldBindJSON(&request); err != nil {
Expand All @@ -62,6 +64,7 @@ func (controller *AccountsController) HandleRegisterAdmin(c *gin.Context) {

// Register admin
dto := request.ToDTO()
dto.CreatedBy = adminUUID
err := controller.UseCases.RegisterAdmin(*dto)
if err != nil {
c.Error(err)
Expand All @@ -72,6 +75,8 @@ func (controller *AccountsController) HandleRegisterAdmin(c *gin.Context) {
}

func (controller *AccountsController) HandleRegisterTeacher(c *gin.Context) {
adminUUID := c.GetString("session_uuid")

// Parse request body
var request requests.RegisterTeacherRequest
if err := c.ShouldBindJSON(&request); err != nil {
Expand All @@ -92,6 +97,7 @@ func (controller *AccountsController) HandleRegisterTeacher(c *gin.Context) {

// Register teacher
dto := request.ToDTO()
dto.CreatedBy = adminUUID
err := controller.UseCases.RegisterTeacher(*dto)
if err != nil {
c.Error(err)
Expand All @@ -114,6 +120,7 @@ func (controller *AccountsController) HandleGetAdmins(c *gin.Context) {
"uuid": admin.UUID,
"full_name": admin.FullName,
"created_at": admin.CreatedAt,
"created_by": admin.CreatedBy,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (repository *AccountsPostgresRepository) SaveAdmin(dto dtos.RegisterUserDTO
defer cancel()

query := `
INSERT INTO users (role, email, full_name, password_hash)
VALUES ($1, $2, $3, $4)
INSERT INTO users (role, email, full_name, password_hash, created_by)
VALUES ($1, $2, $3, $4, $5)
`

_, err := repository.Connection.ExecContext(
Expand All @@ -69,6 +69,7 @@ func (repository *AccountsPostgresRepository) SaveAdmin(dto dtos.RegisterUserDTO
dto.Email,
dto.FullName,
dto.Password,
dto.CreatedBy,
)
if err != nil {
return err
Expand All @@ -83,8 +84,8 @@ func (repository *AccountsPostgresRepository) SaveTeacher(dto dtos.RegisterUserD
defer cancel()

query := `
INSERT INTO users (role, email, full_name, password_hash)
VALUES ($1, $2, $3, $4)
INSERT INTO users (role, email, full_name, password_hash, created_by)
VALUES ($1, $2, $3, $4, $5)
`

_, err := repository.Connection.ExecContext(
Expand All @@ -94,6 +95,7 @@ func (repository *AccountsPostgresRepository) SaveTeacher(dto dtos.RegisterUserD
dto.Email,
dto.FullName,
dto.Password,
dto.CreatedBy,
)
if err != nil {
return err
Expand Down Expand Up @@ -221,8 +223,8 @@ func (repository *AccountsPostgresRepository) GetAdmins() ([]*entities.User, err
defer cancel()

query := `
SELECT id, institutional_id, email, full_name, created_at
FROM users
SELECT id, institutional_id, email, full_name, created_at, creator_full_name
FROM users_with_creator
WHERE role = 'admin'
`

Expand All @@ -241,6 +243,7 @@ func (repository *AccountsPostgresRepository) GetAdmins() ([]*entities.User, err
&admin.Email,
&admin.FullName,
&admin.CreatedAt,
&admin.CreatedBy,
)

if err != nil {
Expand Down

0 comments on commit 313d795

Please sign in to comment.