Replace permission_user with permission_role, enforce RBAC on admin routes, and limit operators to their assigned branches. Co-authored-by: Cursor <cursoragent@cursor.com>
96 lines
2.6 KiB
Go
96 lines
2.6 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"online-back/internal/identity/userbranch"
|
|
"online-back/pkg/client/postgresql"
|
|
"online-back/pkg/logging"
|
|
|
|
"github.com/jackc/pgx/v4"
|
|
)
|
|
|
|
type repository struct {
|
|
client postgresql.Client
|
|
logger *logging.Logger
|
|
}
|
|
|
|
func NewRepository(client postgresql.Client, logger *logging.Logger) userbranch.Repository {
|
|
return &repository{client: client, logger: logger}
|
|
}
|
|
|
|
func (r *repository) Create(ctx context.Context, dto userbranch.CreateUserBranchDTO) (userbranch.UserBranchDTO, error) {
|
|
query := `
|
|
WITH existing AS (
|
|
SELECT id FROM branch_user WHERE user_id = $2 ORDER BY id LIMIT 1
|
|
), updated AS (
|
|
UPDATE branch_user SET
|
|
branch_id = $1,
|
|
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
|
WHERE id = (SELECT id FROM existing)
|
|
RETURNING id, branch_id, user_id, created_at, updated_at
|
|
), inserted AS (
|
|
INSERT INTO branch_user (branch_id, user_id)
|
|
SELECT $1, $2
|
|
WHERE NOT EXISTS (SELECT 1 FROM existing)
|
|
RETURNING id, branch_id, user_id, created_at, updated_at
|
|
), upserted AS (
|
|
SELECT * FROM updated
|
|
UNION ALL
|
|
SELECT * FROM inserted
|
|
)
|
|
SELECT upserted.id, upserted.branch_id, b.name, upserted.user_id, u.name, upserted.created_at, upserted.updated_at
|
|
FROM upserted
|
|
JOIN branches b ON b.id = upserted.branch_id
|
|
JOIN users u ON u.id = upserted.user_id`
|
|
|
|
row := r.client.QueryRow(ctx, query, dto.BranchId, dto.UserId)
|
|
return scanUserBranch(row)
|
|
}
|
|
|
|
func (r *repository) GetByID(ctx context.Context, id int64) (userbranch.UserBranchDTO, error) {
|
|
query := `
|
|
SELECT bu.id, bu.branch_id, b.name, bu.user_id, u.name, bu.created_at, bu.updated_at
|
|
FROM branch_user bu
|
|
JOIN branches b ON b.id = bu.branch_id
|
|
JOIN users u ON u.id = bu.user_id
|
|
WHERE bu.id = $1`
|
|
return scanUserBranch(r.client.QueryRow(ctx, query, id))
|
|
}
|
|
|
|
func (r *repository) Delete(ctx context.Context, id int64) error {
|
|
tag, err := r.client.Exec(ctx, `DELETE FROM branch_user WHERE id = $1`, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return pgx.ErrNoRows
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type scannable interface {
|
|
Scan(dest ...interface{}) error
|
|
}
|
|
|
|
func scanUserBranch(row scannable) (userbranch.UserBranchDTO, error) {
|
|
var item userbranch.UserBranchDTO
|
|
var branchNameJSON []byte
|
|
|
|
err := row.Scan(
|
|
&item.Id, &item.BranchId, &branchNameJSON, &item.UserId, &item.UserName, &item.CreatedAt, &item.UpdatedAt,
|
|
)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return userbranch.UserBranchDTO{}, err
|
|
}
|
|
if err != nil {
|
|
return userbranch.UserBranchDTO{}, err
|
|
}
|
|
if err := json.Unmarshal(branchNameJSON, &item.BranchName); err != nil {
|
|
return userbranch.UserBranchDTO{}, err
|
|
}
|
|
return item, nil
|
|
}
|