added soft deleted
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:
guwandyk committed 2026-09-05 17:58:23 +05:00
1 parent d1a6fcaadc
commit e0d03730de
36 files changed
+327 -217

No files matched your search

+14 -22
View File
@@ -52,7 +52,7 @@ const userBranchesJSON = `COALESCE((
SELECT DISTINCT ON (b.id) b.id, b.unique_code, b.name
FROM branch_user bu
JOIN branches b ON b.id = bu.branch_id
WHERE bu.user_id = u.id
WHERE bu.user_id = u.id AND bu.deleted_at IS NULL AND b.deleted_at IS NULL
ORDER BY b.id
) b
), '[]'::json)`
@@ -66,18 +66,19 @@ const userPermissionsJSON = `COALESCE((
'module', COALESCE(p.module, '')
) ORDER BY p.module, p.id)
FROM permissions p
WHERE NOT EXISTS (
WHERE p.deleted_at IS NULL
AND NOT EXISTS (
SELECT 1 FROM permission_user pu
WHERE pu.user_id = u.id AND pu.permission_id = p.id AND pu.effect = 'deny'
WHERE pu.user_id = u.id AND pu.permission_id = p.id AND pu.effect = 'deny' AND pu.deleted_at IS NULL
)
AND (
EXISTS (
SELECT 1 FROM permission_user pu
WHERE pu.user_id = u.id AND pu.permission_id = p.id AND pu.effect = 'allow'
WHERE pu.user_id = u.id AND pu.permission_id = p.id AND pu.effect = 'allow' AND pu.deleted_at IS NULL
)
OR EXISTS (
SELECT 1 FROM permission_role pr
WHERE pr.role_id = r.id AND pr.permission_id = p.id
WHERE pr.role_id = r.id AND pr.permission_id = p.id AND pr.deleted_at IS NULL
)
)
), '[]'::json)`
@@ -126,12 +127,12 @@ func (r *repository) ListStaff(ctx context.Context, filter user.ListFilter) (use
}
func buildStaffWhere(filter user.ListFilter) (whereClause string, args []interface{}) {
var conditions []string
conditions := []string{"u.deleted_at IS NULL"}
if len(filter.BranchIds) > 0 {
args = append(args, filter.BranchIds)
conditions = append(conditions, fmt.Sprintf(`EXISTS (SELECT 1 FROM branch_user bu WHERE bu.user_id = u.id AND bu.branch_id = ANY($%d))`, len(args)))
conditions = append(conditions, fmt.Sprintf(`EXISTS (SELECT 1 FROM branch_user bu WHERE bu.user_id = u.id AND bu.branch_id = ANY($%d) AND bu.deleted_at IS NULL)`, len(args)))
} else {
conditions = append(conditions, `EXISTS (SELECT 1 FROM branch_user bu WHERE bu.user_id = u.id)`)
conditions = append(conditions, `EXISTS (SELECT 1 FROM branch_user bu WHERE bu.user_id = u.id AND bu.deleted_at IS NULL)`)
}
conditions, args = appendNameSearch(conditions, args, filter.Search)
return " WHERE " + strings.Join(conditions, " AND "), args
@@ -177,7 +178,7 @@ var listVisibleRoles = []string{"user"}
func buildFilteredListQuery(filter user.ListFilter, roles ...string) (fromClause, whereClause string, args []interface{}) {
fromClause = userFromClause
var conditions []string
conditions := []string{"u.deleted_at IS NULL"}
conditions, args = appendNameSearch(conditions, args, filter.Search)
if len(roles) > 0 {
placeholders := make([]string, len(roles))
@@ -197,10 +198,6 @@ func buildFilteredListQuery(filter user.ListFilter, roles ...string) (fromClause
conditions = append(conditions, rbac.UserInBranchSQL("u.id", len(args)))
}
if len(conditions) == 0 {
return fromClause, "", args
}
return fromClause, " WHERE " + strings.Join(conditions, " AND "), args
}
@@ -220,18 +217,13 @@ func appendNameSearch(conditions []string, args []interface{}, term string) ([]s
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 {
excludeStaff := `NOT EXISTS (SELECT 1 FROM branch_user bu WHERE bu.user_id = u.id)`
if whereClause == "" {
whereClause = " WHERE " + excludeStaff
} else {
whereClause += " AND " + excludeStaff
}
whereClause += ` AND NOT EXISTS (SELECT 1 FROM branch_user bu WHERE bu.user_id = u.id AND bu.deleted_at IS NULL)`
}
return r.paginateUsers(ctx, filter, fromClause, whereClause, args)
}
func (r *repository) GetByID(ctx context.Context, id int64) (user.UserDTO, error) {
query := fmt.Sprintf(`SELECT %s, %s, %s%s WHERE u.id = $1`, userColumns, userBranchesJSON, userPermissionsJSON, userFromClause)
query := fmt.Sprintf(`SELECT %s, %s, %s%s WHERE u.id = $1 AND u.deleted_at IS NULL`, userColumns, userBranchesJSON, userPermissionsJSON, userFromClause)
item, err := scanUserDetail(r.client.QueryRow(ctx, query, id))
if errors.Is(err, pgx.ErrNoRows) {
return user.UserDTO{}, err
@@ -301,7 +293,7 @@ func (r *repository) Update(ctx context.Context, id int64, dto user.UpdateUserDT
email = $1, name = $2, first_name = $3, last_name = $4, username = $5, phone = $6, avatar = $7,
role = $8, active = $9,
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
WHERE id = $10
WHERE id = $10 AND deleted_at IS NULL
RETURNING %s`, userReturningColumns)
item, err := scanUser(tx.QueryRow(ctx, query,
@@ -352,7 +344,7 @@ func assignRoleByCode(ctx context.Context, tx pgx.Tx, userID int64, roleCode str
}
func replaceRoleByCode(ctx context.Context, tx pgx.Tx, userID int64, roleCode string) error {
if _, err := tx.Exec(ctx, `DELETE FROM role_user WHERE user_id = $1`, userID); err != nil {
if _, err := tx.Exec(ctx, `UPDATE role_user SET deleted_at = now() WHERE user_id = $1 AND deleted_at IS NULL`, userID); err != nil {
return err
}
return assignRoleByCode(ctx, tx, userID, roleCode)