Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions backend/src/modules/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ export class UserController {
return this.userService.getAllUsers(req);
}

/**
* Get a list of users with only name and email.
* Accessible by tenant admins and super admins.
* @param req - Authenticated request object
* @param query - Query parameters for pagination and filtering
* @returns List of users with name and email
*/
@Get("ordered-list")
@Roles(["super_admin", "tenant_admin"], {
match: "any",
sameOrg: true,
})
async getAllOrderedUsersList(
@Req() req: AuthRequest,
@Query() query: GetOrderedUsersDto,
) {
return this.userService.getAllOrderedUsersList(req, query);
}

/**
* Get all users (paginated and filtered).
* Accessible by super admins and tenant admins (within their organization).
Expand Down
59 changes: 59 additions & 0 deletions backend/src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,65 @@ export class UserService {
return data;
}

/**
* Get a list of users with only name and email.
* Accessible by both super_admin and tenant_admin.
* @param req - Authenticated request object
* @param dto - Query parameters for pagination and filtering
* @returns List of users with name and email
*/
async getAllOrderedUsersList(req: AuthRequest, dto: GetOrderedUsersDto) {
const supabase = req.supabase;

// Build the main user query
let query = supabase.from("user_profiles").select("id, full_name, email");

// Apply search query if provided
if (dto.searchquery) {
query = query.or(
`email.ilike.%${dto.searchquery}%,full_name.ilike.%${dto.searchquery}%`,
);
}

// Apply ordering
const orderColumn = dto.ordered_by || "created_at";
const ascending = dto.ascending === true;
query = query.order(orderColumn, { ascending });

// Apply pagination
const page = Number(dto.page) || 1;
const limit = Number(dto.limit) || 10;
const offset = (page - 1) * limit;
query = query.range(offset, offset + limit - 1);

const { data, error } = await query;

if (error) {
handleSupabaseError(error);
}

if (!data) {
return {
data: [],
metadata: getPaginationMeta(0, page, limit),
};
}

// Ensure the data matches the expected type
const result: Pick<UserProfile, "id" | "full_name" | "email">[] = data.map(
(user) => ({
id: user.id,
full_name: user.full_name,
email: user.email,
}),
);

return {
result,
metadata: getPaginationMeta(data.length, page, limit),
};
}

// Paginated, filtered, org-aware (org_filter + Global)
async getAllOrderedUsers(
req: AuthRequest,
Expand Down
Loading