From fb65d35d3c2a7ce75fc2a8256e573eef947df690 Mon Sep 17 00:00:00 2001 From: GuwandykA Date: Tue, 18 Aug 2026 12:01:58 +0500 Subject: [PATCH] Link seeded users to roles by code and list only clients not assigned to a branch. Co-authored-by: Cursor --- db/seed.sql | 15 ++++----------- internal/identity/user/db/postgresql.go | 6 ++++++ internal/identity/user/handler.go | 2 +- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/db/seed.sql b/db/seed.sql index 763eeb2..947b6e9 100644 --- a/db/seed.sql +++ b/db/seed.sql @@ -126,17 +126,10 @@ INSERT INTO permissions (code, name, guard_name) VALUES ('loans.manage', 'Manage loans', 'admin'), ('activity-logs.view', 'View activity logs', 'admin'); -INSERT INTO role_user (role_id, user_id) VALUES - (1, 1), - (2, 2), - (3, 3), - (4, 4), - (5, 5), - (5, 6), - (5, 7), - (5, 8), - (5, 9), - (5, 10); +INSERT INTO role_user (role_id, user_id) +SELECT r.id, u.id +FROM users u +JOIN roles r ON r.code = u.role; INSERT INTO permission_user (permission_id, user_id) SELECT p.id, 1 FROM permissions p; diff --git a/internal/identity/user/db/postgresql.go b/internal/identity/user/db/postgresql.go index 9cfce13..71eef7e 100644 --- a/internal/identity/user/db/postgresql.go +++ b/internal/identity/user/db/postgresql.go @@ -170,6 +170,12 @@ func likePattern(term string) string { func (r *repository) GetByRole(ctx context.Context, role string, filter user.ListFilter) (user.ListUserDTO, error) { fromClause, whereClause, args := buildFilteredListQuery(filter, role) + excludeStaff := `NOT EXISTS (SELECT 1 FROM branch_user bu WHERE bu.user_id = u.id)` + if whereClause == "" { + whereClause = " WHERE " + excludeStaff + } else { + whereClause += " AND " + excludeStaff + } return r.paginateUsers(ctx, filter, fromClause, whereClause, args) } diff --git a/internal/identity/user/handler.go b/internal/identity/user/handler.go index aa68aec..5a8d3bc 100644 --- a/internal/identity/user/handler.go +++ b/internal/identity/user/handler.go @@ -88,7 +88,7 @@ func (h *handler) list(c *gin.Context) { // listClients godoc // @Summary List client users -// @Description Returns paginated users from the users table with role user +// @Description Returns paginated client users (role user, not assigned to any branch) // @Tags user // @Produce json // @Security BearerAuth