ch some
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled

This commit is contained in:
2026-09-04 19:33:26 +05:00
parent 28509eb4ca
commit d1a6fcaadc
102 changed files with 2089 additions and 1957 deletions

View File

@@ -10,8 +10,10 @@ import (
"online-back/internal/identity/rbac"
"online-back/internal/identity/user"
"online-back/internal/shared"
"online-back/pkg/client/postgresql"
"online-back/pkg/logging"
"online-back/pkg/repository/genericrepo"
"github.com/jackc/pgx/v4"
"golang.org/x/crypto/bcrypt"
@@ -55,6 +57,31 @@ const userBranchesJSON = `COALESCE((
) b
), '[]'::json)`
const userPermissionsJSON = `COALESCE((
SELECT json_agg(jsonb_build_object(
'id', p.id,
'code', COALESCE(p.code, ''),
'name', p.name,
'guardName', p.guard_name,
'module', COALESCE(p.module, '')
) ORDER BY p.module, p.id)
FROM permissions p
WHERE NOT EXISTS (
SELECT 1 FROM permission_user pu
WHERE pu.user_id = u.id AND pu.permission_id = p.id AND pu.effect = 'deny'
)
AND (
EXISTS (
SELECT 1 FROM permission_user pu
WHERE pu.user_id = u.id AND pu.permission_id = p.id AND pu.effect = 'allow'
)
OR EXISTS (
SELECT 1 FROM permission_role pr
WHERE pr.role_id = r.id AND pr.permission_id = p.id
)
)
), '[]'::json)`
func (r *repository) List(ctx context.Context, filter user.ListFilter) (user.ListUserDTO, error) {
fromClause, whereClause, args := buildFilteredListQuery(filter, listVisibleRoles...)
return r.paginateUsers(ctx, filter, fromClause, whereClause, args)
@@ -181,7 +208,7 @@ func appendNameSearch(conditions []string, args []interface{}, term string) ([]s
if term == "" {
return conditions, args
}
args = append(args, likePattern(term))
args = append(args, shared.LikePattern(term))
n := len(args)
conditions = append(conditions, fmt.Sprintf(
`(u.username ILIKE $%d OR u.first_name ILIKE $%d OR u.last_name ILIKE $%d OR CONCAT_WS(' ', u.first_name, u.last_name) ILIKE $%d)`,
@@ -190,13 +217,6 @@ func appendNameSearch(conditions []string, args []interface{}, term string) ([]s
return conditions, args
}
func likePattern(term string) string {
term = strings.ReplaceAll(term, `\`, `\\`)
term = strings.ReplaceAll(term, `%`, `\%`)
term = strings.ReplaceAll(term, `_`, `\_`)
return "%" + term + "%"
}
func (r *repository) GetByRole(ctx context.Context, role string, filter user.ListFilter) (user.ListUserDTO, error) {
fromClause, whereClause, args := buildFilteredListQuery(filter, role)
if len(filter.BranchIds) == 0 {
@@ -211,8 +231,8 @@ func (r *repository) GetByRole(ctx context.Context, role string, filter user.Lis
}
func (r *repository) GetByID(ctx context.Context, id int64) (user.UserDTO, error) {
query := fmt.Sprintf(`SELECT %s, %s%s WHERE u.id = $1`, userColumns, userBranchesJSON, userFromClause)
item, err := scanStaffUser(r.client.QueryRow(ctx, query, id))
query := fmt.Sprintf(`SELECT %s, %s, %s%s WHERE u.id = $1`, userColumns, userBranchesJSON, userPermissionsJSON, userFromClause)
item, err := scanUserDetail(r.client.QueryRow(ctx, query, id))
if errors.Is(err, pgx.ErrNoRows) {
return user.UserDTO{}, err
}
@@ -306,14 +326,16 @@ func (r *repository) Update(ctx context.Context, id int64, dto user.UpdateUserDT
}
func (r *repository) Delete(ctx context.Context, id int64) error {
tag, err := r.client.Exec(ctx, `DELETE FROM users WHERE id = $1`, id)
if err != nil {
return err
return genericrepo.Delete(ctx, r.client, "users", id)
}
func (r *repository) RoleGuardName(ctx context.Context, code string) (string, error) {
var guardName string
err := r.client.QueryRow(ctx, `SELECT guard_name FROM roles WHERE code = $1`, code).Scan(&guardName)
if errors.Is(err, pgx.ErrNoRows) {
return "", nil
}
if tag.RowsAffected() == 0 {
return pgx.ErrNoRows
}
return nil
return guardName, err
}
func assignRoleByCode(ctx context.Context, tx pgx.Tx, userID int64, roleCode string) error {
@@ -344,11 +366,7 @@ func roleIDByCode(ctx context.Context, tx pgx.Tx, roleCode string) *int64 {
return &id
}
type scannable interface {
Scan(dest ...interface{}) error
}
func scanUser(row scannable) (user.UserDTO, error) {
func scanUser(row genericrepo.Scanner) (user.UserDTO, error) {
var item user.UserDTO
if err := row.Scan(
&item.Id, &item.Email, &item.Name, &item.FirstName, &item.LastName, &item.Username, &item.Phone, &item.Avatar,
@@ -359,7 +377,7 @@ func scanUser(row scannable) (user.UserDTO, error) {
return item, nil
}
func scanUserWithRoleID(row scannable) (user.UserDTO, error) {
func scanUserWithRoleID(row genericrepo.Scanner) (user.UserDTO, error) {
var item user.UserDTO
var roleID sql.NullInt64
var roleCode sql.NullString
@@ -373,7 +391,7 @@ func scanUserWithRoleID(row scannable) (user.UserDTO, error) {
return item, nil
}
func scanStaffUser(row scannable) (user.UserDTO, error) {
func scanStaffUser(row genericrepo.Scanner) (user.UserDTO, error) {
var item user.UserDTO
var roleID sql.NullInt64
var roleCode sql.NullString
@@ -385,16 +403,50 @@ func scanStaffUser(row scannable) (user.UserDTO, error) {
return user.UserDTO{}, err
}
applyJoinedRole(&item, roleID, roleCode)
if len(branchesJSON) == 0 || string(branchesJSON) == "null" {
item.Branches = []user.StaffBranchDTO{}
return item, nil
}
if err := json.Unmarshal(branchesJSON, &item.Branches); err != nil {
if err := unmarshalJSON(branchesJSON, &item.Branches); err != nil {
return user.UserDTO{}, err
}
if item.Branches == nil {
item.Branches = []user.StaffBranchDTO{}
}
return item, nil
}
func scanUserDetail(row genericrepo.Scanner) (user.UserDTO, error) {
var item user.UserDTO
var roleID sql.NullInt64
var roleCode sql.NullString
var branchesJSON []byte
var permissionsJSON []byte
if err := row.Scan(
&item.Id, &item.Email, &item.Name, &item.FirstName, &item.LastName, &item.Username, &item.Phone, &item.Avatar,
&item.Active, &item.CreatedAt, &item.UpdatedAt, &roleID, &roleCode, &branchesJSON, &permissionsJSON,
); err != nil {
return user.UserDTO{}, err
}
applyJoinedRole(&item, roleID, roleCode)
if err := unmarshalJSON(branchesJSON, &item.Branches); err != nil {
return user.UserDTO{}, err
}
if err := unmarshalJSON(permissionsJSON, &item.Permissions); err != nil {
return user.UserDTO{}, err
}
if item.Branches == nil {
item.Branches = []user.StaffBranchDTO{}
}
if item.Permissions == nil {
item.Permissions = []user.UserPermissionDTO{}
}
return item, nil
}
func unmarshalJSON(raw []byte, dest interface{}) error {
if len(raw) == 0 || string(raw) == "null" {
return nil
}
return json.Unmarshal(raw, dest)
}
func applyJoinedRole(item *user.UserDTO, roleID sql.NullInt64, roleCode sql.NullString) {
if roleCode.Valid {
item.Role = roleCode.String