Add branch_user table and branch-user assignment endpoints

Introduces a branch_user join table linking branches to users, plus
create/delete API endpoints under the auth module for managing branch
assignments. The create response includes the branch's localized name
via a join. Regenerated swagger docs to include the new endpoints.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-08 15:02:05 +05:00
parent 1337175e5d
commit 77bc4025d3
9 changed files with 462 additions and 3 deletions

View File

@@ -2,10 +2,13 @@ package db
import (
"context"
"encoding/json"
"online-back/internal/admin/auth"
"online-back/pkg/client/postgresql"
"online-back/pkg/logging"
"github.com/jackc/pgx/v4"
)
type repository struct {
@@ -50,3 +53,42 @@ func (r *repository) GetProfile(ctx context.Context, id int) (auth.UserProfileDT
)
return profile, err
}
const (
createBranchUserQuery = `
WITH inserted AS (
INSERT INTO branch_user (branch_id, user_id)
VALUES ($1, $2)
RETURNING id, branch_id, user_id, created_at, updated_at
)
SELECT inserted.id, inserted.branch_id, b.name, inserted.user_id, inserted.created_at, inserted.updated_at
FROM inserted
JOIN branches b ON b.id = inserted.branch_id`
deleteBranchUserQuery = `DELETE FROM branch_user WHERE id = $1`
)
func (r *repository) CreateBranchUser(ctx context.Context, dto auth.CreateBranchUserDTO) (auth.BranchUserDTO, error) {
var item auth.BranchUserDTO
var branchNameJSON []byte
err := r.client.QueryRow(ctx, createBranchUserQuery, dto.BranchId, dto.UserId).Scan(
&item.Id, &item.BranchId, &branchNameJSON, &item.UserId, &item.CreatedAt, &item.UpdatedAt,
)
if err != nil {
return auth.BranchUserDTO{}, err
}
if err := json.Unmarshal(branchNameJSON, &item.BranchName); err != nil {
return auth.BranchUserDTO{}, err
}
return item, nil
}
func (r *repository) DeleteBranchUser(ctx context.Context, id int64) error {
tag, err := r.client.Exec(ctx, deleteBranchUserQuery, id)
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return pgx.ErrNoRows
}
return nil
}