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

+22 -18
View File
@@ -32,26 +32,28 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) auth.Reposi
const (
loginByNameQuery = `
SELECT id, password FROM users
WHERE LOWER(TRIM(name)) = LOWER(TRIM($1))
OR LOWER(TRIM(username)) = LOWER(TRIM($1))
WHERE (LOWER(TRIM(name)) = LOWER(TRIM($1))
OR LOWER(TRIM(username)) = LOWER(TRIM($1)))
AND deleted_at IS NULL
LIMIT 1`
loginByPhoneQuery = `
SELECT id, password FROM users
WHERE phone = $1
OR ($2 <> '' AND regexp_replace(phone, '[^0-9]', '', 'g') = $2)
WHERE (phone = $1
OR ($2 <> '' AND regexp_replace(phone, '[^0-9]', '', 'g') = $2))
AND deleted_at IS NULL
LIMIT 1`
userByIDQuery = `SELECT id, password FROM users WHERE id = $1`
userByIDQuery = `SELECT id, password FROM users WHERE id = $1 AND deleted_at IS NULL`
)
const profileFromClause = ` FROM users u
LEFT JOIN LATERAL (
SELECT ru.role_id
FROM role_user ru
WHERE ru.user_id = u.id
WHERE ru.user_id = u.id AND ru.deleted_at IS NULL
ORDER BY ru.id
LIMIT 1
) ru ON true
LEFT JOIN roles r ON r.id = ru.role_id`
LEFT JOIN roles r ON r.id = ru.role_id AND r.deleted_at IS NULL`
const profileBranchesJSON = `COALESCE((
SELECT json_agg(jsonb_build_object(
@@ -63,7 +65,7 @@ const profileBranchesJSON = `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)`
@@ -77,24 +79,25 @@ const profilePermissionsJSON = `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)`
const profileQuery = `SELECT u.id, u.name, u.first_name, u.last_name, u.phone, u.avatar, u.created_at,
r.id, r.code, r.name, r.guard_name, ` + profileBranchesJSON + `, ` + profilePermissionsJSON + profileFromClause + ` WHERE u.id = $1`
r.id, r.code, r.name, r.guard_name, ` + profileBranchesJSON + `, ` + profilePermissionsJSON + profileFromClause + ` WHERE u.id = $1 AND u.deleted_at IS NULL`
func (r *repository) Login(ctx context.Context, dto auth.LoginDTO) (auth.UserCredentials, error) {
phone := strings.TrimSpace(dto.Phone)
@@ -127,7 +130,7 @@ const updateProfileQuery = `
UPDATE users SET
name = $1, first_name = $2, last_name = $3, phone = $4, avatar = $5,
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
WHERE id = $6`
WHERE id = $6 AND deleted_at IS NULL`
func (r *repository) UpdateProfile(ctx context.Context, id int, dto auth.UpdateProfileDTO) (auth.UserProfileDTO, error) {
tag, err := r.client.Exec(ctx, updateProfileQuery,
@@ -198,8 +201,9 @@ func (r *repository) UserExists(ctx context.Context, username, phone string) (bo
const query = `
SELECT EXISTS (
SELECT 1 FROM users
WHERE LOWER(username) = LOWER($1)
OR regexp_replace(phone, '[^0-9]', '', 'g') = $2
WHERE (LOWER(username) = LOWER($1)
OR regexp_replace(phone, '[^0-9]', '', 'g') = $2)
AND deleted_at IS NULL
)`
var exists bool
err := r.client.QueryRow(ctx, query, username, digitString(phone)).Scan(&exists)
@@ -302,7 +306,7 @@ func (r *repository) CompleteRegistration(ctx context.Context, v auth.Verificati
func (r *repository) GetUserByName(ctx context.Context, name string) (auth.UserIdentity, error) {
const query = `
SELECT id, name, username, phone FROM users
WHERE LOWER(TRIM(name)) = LOWER(TRIM($1))
WHERE LOWER(TRIM(name)) = LOWER(TRIM($1)) AND deleted_at IS NULL
LIMIT 1`
var user auth.UserIdentity
@@ -324,7 +328,7 @@ func (r *repository) CompletePasswordReset(ctx context.Context, userID int, veri
UPDATE users SET
password = $1,
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
WHERE id = $2`, passwordHash, userID)
WHERE id = $2 AND deleted_at IS NULL`, passwordHash, userID)
if err != nil {
return err
}