ch users <-> branches
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
-- Reverse 20260905130000_add_soft_delete.up.sql
|
||||
|
||||
DROP INDEX IF EXISTS branch_user_branch_id_user_id_idx;
|
||||
|
||||
DROP INDEX IF EXISTS permission_role_permission_id_role_id_idx;
|
||||
ALTER TABLE permission_role ADD CONSTRAINT permission_role_permission_id_role_id_key UNIQUE (permission_id, role_id);
|
||||
|
||||
|
||||
@@ -61,3 +61,10 @@ ALTER TABLE permission_role DROP CONSTRAINT IF EXISTS permission_role_permission
|
||||
CREATE UNIQUE INDEX permission_role_permission_id_role_id_idx
|
||||
ON permission_role (permission_id, role_id)
|
||||
WHERE deleted_at IS NULL;
|
||||
|
||||
-- branch_user is many-to-many (a user may be assigned to several branches at
|
||||
-- once); this index only stops the same pair being assigned twice while
|
||||
-- active, so re-assigning it can be a no-op instead of a duplicate row.
|
||||
CREATE UNIQUE INDEX branch_user_branch_id_user_id_idx
|
||||
ON branch_user (branch_id, user_id)
|
||||
WHERE deleted_at IS NULL;
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
-- Reverse 20260918120000_add_branch_user_unique_index.up.sql
|
||||
DROP INDEX IF EXISTS branch_user_branch_id_user_id_idx;
|
||||
@@ -0,0 +1,14 @@
|
||||
-- branch_user_branch_id_user_id_idx has been added in place to
|
||||
-- 20260905130000_add_soft_delete.up.sql for fresh databases, but per this
|
||||
-- project's migration convention (see 20260905150000_widen_loan_paid_off_letters_region),
|
||||
-- that edit alone does not reach a database that was migrated before the
|
||||
-- edit. This migration adds the index there too. Running it against a
|
||||
-- database created after the edit is a harmless no-op.
|
||||
--
|
||||
-- The index scopes branch_user's many-to-many pairing (a user may belong to
|
||||
-- several branches) to one active row per (branch_id, user_id) pair, so the
|
||||
-- application can upsert an assignment instead of creating duplicate rows.
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS branch_user_branch_id_user_id_idx
|
||||
ON branch_user (branch_id, user_id)
|
||||
WHERE deleted_at IS NULL;
|
||||
@@ -74,7 +74,11 @@ const userPermissionsJSON = `COALESCE((
|
||||
), '[]'::json)`
|
||||
|
||||
func (r *repository) List(ctx context.Context, filter user.ListFilter) (user.ListUserDTO, error) {
|
||||
fromClause, whereClause, args := buildFilteredListQuery(filter, listVisibleRoles...)
|
||||
roles := listVisibleRoles
|
||||
if filter.AllRoles {
|
||||
roles = nil
|
||||
}
|
||||
fromClause, whereClause, args := buildFilteredListQuery(filter, roles...)
|
||||
return r.paginateUsers(ctx, filter, fromClause, whereClause, args)
|
||||
}
|
||||
|
||||
|
||||
@@ -70,6 +70,9 @@ type ListFilter struct {
|
||||
BranchIds []int64
|
||||
Page int
|
||||
Limit int
|
||||
// AllRoles, when true, skips the role=user filter so admin/superadmin
|
||||
// callers see every user regardless of role.
|
||||
AllRoles bool
|
||||
}
|
||||
|
||||
type ListUserDTO struct {
|
||||
|
||||
@@ -62,7 +62,7 @@ func (h *handler) RegisterPublic(engine *gin.Engine) {
|
||||
|
||||
// list godoc
|
||||
// @Summary List users
|
||||
// @Description Returns paginated users with role user, including roleId
|
||||
// @Description Returns paginated users, including roleId. Admin-guard callers (admin, superadmin, and other admin-guard roles) see users of every role; branch-scoped operators see role=user only, scoped to their branches.
|
||||
// @Tags user
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
@@ -80,6 +80,7 @@ func (h *handler) list(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
filter.AllRoles = rbac.IsAdminGuard(c)
|
||||
rbac.BindBranchIDs(c, &filter.BranchIds)
|
||||
|
||||
result, err := h.repository.List(c.Request.Context(), filter)
|
||||
|
||||
@@ -23,24 +23,16 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) userbranch.
|
||||
}
|
||||
|
||||
func (r *repository) Create(ctx context.Context, dto userbranch.CreateUserBranchDTO) (userbranch.UserBranchDTO, error) {
|
||||
// users <-> branches is many-to-many: a user keeps every other branch
|
||||
// assignment they already have, this only adds (or, if already assigned,
|
||||
// idempotently touches) the one pair given here.
|
||||
query := `
|
||||
WITH existing AS (
|
||||
SELECT id FROM branch_user WHERE user_id = $2 AND deleted_at IS NULL 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 (
|
||||
WITH upserted AS (
|
||||
INSERT INTO branch_user (branch_id, user_id)
|
||||
SELECT $1, $2
|
||||
WHERE NOT EXISTS (SELECT 1 FROM existing)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (branch_id, user_id) WHERE deleted_at IS NULL
|
||||
DO UPDATE SET updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
||||
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
|
||||
|
||||
@@ -33,7 +33,7 @@ func (h *handler) Register(router *gin.RouterGroup) {
|
||||
|
||||
// create godoc
|
||||
// @Summary Assign user to branch
|
||||
// @Description Creates a branch-user assignment, or updates the user's existing branch if one is already assigned
|
||||
// @Description Assigns a user to a branch. Users and branches are many-to-many: a user keeps their other branch assignments, and re-assigning the same pair is a no-op.
|
||||
// @Tags branch-users
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
|
||||
Reference in New Issue
Block a user