fix
This commit is contained in:
@@ -17,15 +17,102 @@ func NewPositionRepository(db *pgxpool.Pool) *PositionRepository {
|
||||
return &PositionRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *PositionRepository) GetAllPermissions(ctx context.Context) ([]models.Permission, error) {
|
||||
rows, err := r.db.Query(ctx, `
|
||||
SELECT id, code, COALESCE(description, ''), created_at
|
||||
FROM permissions
|
||||
ORDER BY code
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var result []models.Permission
|
||||
for rows.Next() {
|
||||
var p models.Permission
|
||||
if err := rows.Scan(&p.ID, &p.Code, &p.Description, &p.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, p)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func (r *PositionRepository) GetPermissionByCode(ctx context.Context, code string) (*models.Permission, error) {
|
||||
var p models.Permission
|
||||
err := r.db.QueryRow(ctx, `
|
||||
SELECT id, code, COALESCE(description, ''), created_at
|
||||
FROM permissions WHERE code = $1
|
||||
`, code).Scan(&p.ID, &p.Code, &p.Description, &p.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
func (r *PositionRepository) GetPositionPermissions(ctx context.Context, positionID uuid.UUID) ([]models.Permission, error) {
|
||||
rows, err := r.db.Query(ctx, `
|
||||
SELECT p.id, p.code, COALESCE(p.description, ''), p.created_at
|
||||
FROM position_permissions pp
|
||||
JOIN permissions p ON pp.permission_id = p.id
|
||||
WHERE pp.position_id = $1
|
||||
ORDER BY p.code
|
||||
`, positionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var result []models.Permission
|
||||
for rows.Next() {
|
||||
var p models.Permission
|
||||
if err := rows.Scan(&p.ID, &p.Code, &p.Description, &p.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, p)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func (r *PositionRepository) AddPermissionToPosition(ctx context.Context, positionID, permissionID uuid.UUID) error {
|
||||
_, err := r.db.Exec(ctx, `
|
||||
INSERT INTO position_permissions (position_id, permission_id)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT DO NOTHING
|
||||
`, positionID, permissionID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *PositionRepository) RemovePermissionFromPosition(ctx context.Context, positionID, permissionID uuid.UUID) error {
|
||||
_, err := r.db.Exec(ctx, `
|
||||
DELETE FROM position_permissions
|
||||
WHERE position_id = $1 AND permission_id = $2
|
||||
`, positionID, permissionID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *PositionRepository) GetPositionByCode(ctx context.Context, code string) (*models.Position, error) {
|
||||
var p models.Position
|
||||
err := r.db.QueryRow(ctx, `
|
||||
SELECT id, code, created_at, updated_at
|
||||
FROM positions WHERE code = $1
|
||||
`, code).Scan(&p.ID, &p.Code, &p.CreatedAt, &p.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
func (r *PositionRepository) GetUserPositions(ctx context.Context, userID uuid.UUID, locale string) ([]models.PositionLocalized, error) {
|
||||
rows, err := r.db.Query(ctx, `
|
||||
SELECT p.code,
|
||||
COALESCE(pt.name, p.code) AS name, pt.description
|
||||
SELECT p.id, p.code,
|
||||
COALESCE(pt.name, p.code) AS name,
|
||||
pt.description
|
||||
FROM user_positions up
|
||||
JOIN positions p ON up.position_id = p.id
|
||||
LEFT JOIN position_translations pt
|
||||
ON pt.position_id = p.id
|
||||
AND pt.locale = $2
|
||||
ON pt.position_id = p.id AND pt.locale = $2
|
||||
WHERE up.user_id = $1
|
||||
AND (up.valid_to IS NULL OR up.valid_to > now())
|
||||
ORDER BY name
|
||||
@@ -38,9 +125,18 @@ func (r *PositionRepository) GetUserPositions(ctx context.Context, userID uuid.U
|
||||
var result []models.PositionLocalized
|
||||
for rows.Next() {
|
||||
var p models.PositionLocalized
|
||||
if err := rows.Scan(&p.Code, &p.Name, &p.Description); err != nil {
|
||||
var positionID uuid.UUID
|
||||
if err := rows.Scan(&positionID, &p.Code, &p.Name, &p.Description); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
perms, err := r.GetPositionPermissions(ctx, positionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.Permissions = perms
|
||||
if p.Permissions == nil {
|
||||
p.Permissions = []models.Permission{}
|
||||
}
|
||||
result = append(result, p)
|
||||
}
|
||||
return result, rows.Err()
|
||||
@@ -48,27 +144,34 @@ func (r *PositionRepository) GetUserPositions(ctx context.Context, userID uuid.U
|
||||
|
||||
func (r *PositionRepository) GetAllPositions(ctx context.Context, locale string) ([]models.PositionLocalized, error) {
|
||||
rows, err := r.db.Query(ctx, `
|
||||
SELECT p.code,
|
||||
COALESCE(pt.name, p.code) AS name, pt.description
|
||||
SELECT p.id, p.code,
|
||||
COALESCE(pt.name, p.code) AS name,
|
||||
pt.description
|
||||
FROM positions p
|
||||
LEFT JOIN position_translations pt
|
||||
ON pt.position_id = p.id
|
||||
AND pt.locale = $1
|
||||
ON pt.position_id = p.id AND pt.locale = $1
|
||||
ORDER BY name
|
||||
`, strings.ToLower(locale))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
var result []models.PositionLocalized
|
||||
for rows.Next() {
|
||||
var p models.PositionLocalized
|
||||
if err := rows.Scan(&p.Code, &p.Name, &p.Description); err != nil {
|
||||
var positionID uuid.UUID
|
||||
if err := rows.Scan(&positionID, &p.Code, &p.Name, &p.Description); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
perms, err := r.GetPositionPermissions(ctx, positionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.Permissions = perms
|
||||
if p.Permissions == nil {
|
||||
p.Permissions = []models.Permission{}
|
||||
}
|
||||
result = append(result, p)
|
||||
}
|
||||
return result, rows.Err()
|
||||
@@ -85,8 +188,7 @@ func (r *PositionRepository) UpsertPositionForLocale(ctx context.Context, code,
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO positions (code)
|
||||
VALUES ($1)
|
||||
ON CONFLICT (code) DO UPDATE
|
||||
SET updated_at = now()
|
||||
ON CONFLICT (code) DO UPDATE SET updated_at = now()
|
||||
RETURNING id
|
||||
`, code).Scan(&positionID)
|
||||
if err != nil {
|
||||
@@ -97,10 +199,8 @@ func (r *PositionRepository) UpsertPositionForLocale(ctx context.Context, code,
|
||||
INSERT INTO position_translations (position_id, locale, name, description)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (position_id, locale) DO UPDATE
|
||||
SET name = EXCLUDED.name,
|
||||
description = EXCLUDED.description
|
||||
SET name = EXCLUDED.name, description = EXCLUDED.description
|
||||
`, positionID, strings.ToLower(locale), name, description)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -108,21 +208,66 @@ func (r *PositionRepository) UpsertPositionForLocale(ctx context.Context, code,
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (r *PositionRepository) UserHasPosition(ctx context.Context, userID uuid.UUID, code string) (bool, error) {
|
||||
var exists bool
|
||||
func (r *PositionRepository) AssignPosition(ctx context.Context, userID, positionID uuid.UUID, assignedBy *uuid.UUID, validFrom interface{}, validTo *interface{}) error {
|
||||
_, err := r.db.Exec(ctx, `
|
||||
INSERT INTO user_positions (user_id, position_id, assigned_by, valid_from, valid_to)
|
||||
VALUES ($1, $2, $3, COALESCE($4, now()), $5)
|
||||
`, userID, positionID, assignedBy, validFrom, validTo)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *PositionRepository) RevokePosition(ctx context.Context, userID, positionID uuid.UUID) error {
|
||||
_, err := r.db.Exec(ctx, `
|
||||
UPDATE user_positions
|
||||
SET valid_to = now()
|
||||
WHERE user_id = $1
|
||||
AND position_id = $2
|
||||
AND valid_to IS NULL
|
||||
`, userID, positionID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *PositionRepository) UserHasPermission(ctx context.Context, userID uuid.UUID, permissionCode string) (bool, string, error) {
|
||||
var positionCode string
|
||||
err := r.db.QueryRow(ctx, `
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM user_positions up
|
||||
JOIN positions p ON up.position_id = p.id
|
||||
WHERE up.user_id = $1
|
||||
AND p.code = $2
|
||||
AND (up.valid_to IS NULL OR up.valid_to > now())
|
||||
)
|
||||
`, userID, code).Scan(&exists)
|
||||
SELECT p.code
|
||||
FROM user_positions up
|
||||
JOIN positions p ON up.position_id = p.id
|
||||
JOIN position_permissions pp ON pp.position_id = p.id
|
||||
JOIN permissions perm ON pp.permission_id = perm.id
|
||||
WHERE up.user_id = $1
|
||||
AND perm.code = $2
|
||||
AND (up.valid_to IS NULL OR up.valid_to > now())
|
||||
LIMIT 1
|
||||
`, userID, permissionCode).Scan(&positionCode)
|
||||
if err != nil {
|
||||
return false, "", nil
|
||||
}
|
||||
return true, positionCode, nil
|
||||
}
|
||||
|
||||
func (r *PositionRepository) GetUserPermissions(ctx context.Context, userID uuid.UUID) ([]models.Permission, error) {
|
||||
rows, err := r.db.Query(ctx, `
|
||||
SELECT DISTINCT perm.id, perm.code, COALESCE(perm.description, ''), perm.created_at
|
||||
FROM user_positions up
|
||||
JOIN position_permissions pp ON pp.position_id = up.position_id
|
||||
JOIN permissions perm ON pp.permission_id = perm.id
|
||||
WHERE up.user_id = $1
|
||||
AND (up.valid_to IS NULL OR up.valid_to > now())
|
||||
ORDER BY perm.code
|
||||
`, userID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var result []models.Permission
|
||||
for rows.Next() {
|
||||
var p models.Permission
|
||||
if err := rows.Scan(&p.ID, &p.Code, &p.Description, &p.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, p)
|
||||
}
|
||||
return exists, nil
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
@@ -27,9 +27,9 @@ func (r *UserRepository) CreateUser(ctx context.Context, user models.User) (*mod
|
||||
err := r.db.QueryRow(ctx, `
|
||||
INSERT INTO users (email, password_hash, first_name, second_name, birth_date, sex)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING id, created_at, updated_at
|
||||
RETURNING id, is_admin, created_at, updated_at
|
||||
`, user.Email, user.PasswordHash, user.FirstName, user.SecondName, user.BirthDate, user.Sex).
|
||||
Scan(&user.ID, &user.CreatedAt, &user.UpdatedAt)
|
||||
Scan(&user.ID, &user.IsAdmin, &user.CreatedAt, &user.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create user: %w", err)
|
||||
}
|
||||
@@ -63,9 +63,15 @@ func (r *UserRepository) DeleteUser(ctx context.Context, id uuid.UUID) error {
|
||||
func (r *UserRepository) GetUserByEmail(ctx context.Context, email string) (*models.User, error) {
|
||||
var u models.User
|
||||
err := r.db.QueryRow(ctx, `
|
||||
SELECT id, email, password_hash, first_name, second_name, birth_date, sex, created_at, updated_at
|
||||
SELECT id, email, password_hash, first_name, second_name,
|
||||
birth_date, sex, is_admin, created_at, updated_at
|
||||
FROM users WHERE email = $1
|
||||
`, email).Scan(&u.ID, &u.Email, &u.PasswordHash, &u.FirstName, &u.SecondName, &u.BirthDate, &u.Sex, &u.CreatedAt, &u.UpdatedAt)
|
||||
`, email).Scan(
|
||||
&u.ID, &u.Email, &u.PasswordHash,
|
||||
&u.FirstName, &u.SecondName,
|
||||
&u.BirthDate, &u.Sex, &u.IsAdmin,
|
||||
&u.CreatedAt, &u.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -75,9 +81,15 @@ func (r *UserRepository) GetUserByEmail(ctx context.Context, email string) (*mod
|
||||
func (r *UserRepository) GetUserByID(ctx context.Context, id uuid.UUID) (*models.User, error) {
|
||||
var u models.User
|
||||
err := r.db.QueryRow(ctx, `
|
||||
SELECT id, email, password_hash, first_name, second_name, birth_date, sex, created_at, updated_at
|
||||
SELECT id, email, password_hash, first_name, second_name,
|
||||
birth_date, sex, is_admin, created_at, updated_at
|
||||
FROM users WHERE id = $1
|
||||
`, id).Scan(&u.ID, &u.Email, &u.PasswordHash, &u.FirstName, &u.SecondName, &u.BirthDate, &u.Sex, &u.CreatedAt, &u.UpdatedAt)
|
||||
`, id).Scan(
|
||||
&u.ID, &u.Email, &u.PasswordHash,
|
||||
&u.FirstName, &u.SecondName,
|
||||
&u.BirthDate, &u.Sex, &u.IsAdmin,
|
||||
&u.CreatedAt, &u.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user