Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | 24x 24x 55x 55x 1x 55x 1x 55x 55x 38x 55x 20x 55x 2x 55x 55x 49x 55x 7x 24x 2x 2x 24x 2x 2x 1x 2x 2x 24x 24x 2x 1x 24x 3x 2x 24x 4x 1x 24x 3x 24x 2x 2x 24x 2x 2x | /**
* User Management API Service (GREEN Phase)
*
* Implementation to make tests pass.
* Story 2.5.2: User Management Frontend - Task 4b (GREEN Phase)
*
* Endpoints:
* - GET /api/v1/users - List users with filters, pagination, includes
* - GET /api/v1/users/search - Search users with autocomplete
* - GET /api/v1/users/{id} - Get user by ID with includes
* - POST /api/v1/users - Create new user
* - PUT /api/v1/users/{id}/roles - Update user roles
* - DELETE /api/v1/users/{id} - Delete user (GDPR compliant)
*/
import apiClient from './apiClient';
import type {
User,
UserSearchResponse,
PaginatedUserResponse,
CreateUserFormData,
UserFilters,
UserPagination,
Role,
} from '@/types/user.types';
// API base path for user endpoints
// Note: apiClient baseURL is set from runtime config to 'http://localhost:8080/api/v1'
// so we only need '/users' (the /api/v1 prefix is already in the baseURL)
const USER_API_PATH = '/users';
/**
* List users with filters, pagination, and optional resource expansion
* AC1: User Management Screen
*/
export const listUsers = async (
filters: UserFilters,
pagination: UserPagination,
includes?: string[],
sortBy?: string,
sortDir?: 'asc' | 'desc'
): Promise<PaginatedUserResponse> => {
const params: Record<string, string | number | boolean> = {
page: pagination.page,
limit: pagination.limit,
};
// ADR-013 §3: single list-query vocabulary. Ordering goes through `sort`
// (e.g. `-name` for descending); role/company/active go through the JSON `filter`.
if (sortBy) {
params.sort = sortDir === 'desc' ? `-${sortBy}` : sortBy;
}
// Add includes parameter
if (includes && includes.length > 0) {
params.include = includes.join(',');
}
// Build the JSON filter (role, company, active status).
const filterObj: Record<string, string | boolean> = {};
if (filters.role && filters.role.length > 0) {
// Take the first role if multiple are selected (backend accepts single role)
filterObj.role = filters.role[0];
}
if (filters.company) {
filterObj.company = filters.company;
}
if (filters.status && filters.status !== 'all') {
filterObj.active = filters.status === 'active';
}
// Add search query parameter if provided (searches name and email)
Iif (filters.searchQuery && filters.searchQuery.trim()) {
params.search = filters.searchQuery;
}
// Add filter parameter if we have filters
if (Object.keys(filterObj).length > 0) {
params.filter = JSON.stringify(filterObj);
}
const response = await apiClient.get<PaginatedUserResponse>(USER_API_PATH, { params });
return response.data;
};
/**
* Search users with autocomplete (300ms debouncing should be handled by the hook/component)
* AC6: Search & Autocomplete
*/
export const searchUsers = async (
query: string,
limit: number = 10
): Promise<UserSearchResponse[]> => {
const response = await apiClient.get<UserSearchResponse[]>(`${USER_API_PATH}/search`, {
params: {
query: encodeURIComponent(query),
limit,
},
});
return response.data;
};
/**
* Get user by ID with optional resource expansion
* AC1: User Detail Modal
*/
export const getUserById = async (id: string, includes?: string[]): Promise<User> => {
const params: Record<string, string> = {};
if (includes && includes.length > 0) {
params.include = includes.join(',');
}
const response = await apiClient.get<User>(`${USER_API_PATH}/${id}`, { params });
return response.data;
};
/**
* Get a user by their meaningful username (ADR-003). The backend route at
* `/api/v1/users/{username}` accepts either UUID or username, so this is a
* thin alias that documents intent. Used by Content-tab speaker prefill
* (Epic 11 bug fix 2026-05-19) — `searchUsers` only matches name/email
* substrings and does NOT match by username, so the previous
* `searchUsers(speaker.username)` returned `[]` for promoted speakers.
*
* Returns null when the user does not exist (404 from the backend) so the
* caller can gracefully fall back to the legacy name-based search.
*/
export const getUserByUsername = async (
username: string,
includes?: string[]
): Promise<User | null> => {
const params: Record<string, string> = {};
if (includes && includes.length > 0) {
params.include = includes.join(',');
}
try {
const response = await apiClient.get<User>(`${USER_API_PATH}/${username}`, { params });
return response.data;
} catch (err) {
const status = (err as { response?: { status?: number } })?.response?.status;
if (status === 404) {
return null;
}
throw err;
}
};
/**
* Create new user
* AC4: User Creation
*/
export const createUser = async (data: CreateUserFormData): Promise<User> => {
const response = await apiClient.post<User>(USER_API_PATH, data);
return response.data;
};
/**
* Update user profile (name, bio, company)
* For admin/organizer to update any user's profile
*/
export interface UpdateUserData {
firstName?: string;
lastName?: string;
bio?: string;
companyId?: string;
}
export const updateUser = async (username: string, data: UpdateUserData): Promise<User> => {
const response = await apiClient.put<User>(`${USER_API_PATH}/${username}`, data);
return response.data;
};
/**
* Update user roles with minimum organizers validation
* AC3: Role Management
*/
export const updateUserRoles = async (id: string, roles: Role[]): Promise<User> => {
const response = await apiClient.put<User>(`${USER_API_PATH}/${id}/roles`, { roles });
return response.data;
};
/**
* Delete user (GDPR compliant cascade deletion)
* AC5: User Deletion (GDPR)
*/
export const deleteUser = async (id: string): Promise<void> => {
await apiClient.delete(`${USER_API_PATH}/${id}`);
};
/**
* Sync Status Response
* Story 1.2.5: User Reconciliation
*/
export interface SyncStatusResponse {
cognitoUserCount: number;
databaseUserCount: number;
missingInDatabase: number;
orphanedInDatabase: number;
missingCognitoIds: string[];
inSync: boolean;
message: string;
}
/**
* Reconciliation Report Response
* Story 1.2.5: User Reconciliation
*/
export interface ReconciliationReportResponse {
orphanedUsersDeactivated: number;
missingUsersCreated: number;
durationMs: number;
errors: string[];
success: boolean;
message: string;
}
/**
* Check sync status between Cognito and Database
* Story 1.2.5: User Reconciliation (Admin only)
*/
export const checkSyncStatus = async (): Promise<SyncStatusResponse> => {
const response = await apiClient.get<SyncStatusResponse>(`${USER_API_PATH}/admin/sync-status`);
return response.data;
};
/**
* Trigger manual user reconciliation from Cognito to Database
* Story 1.2.5: User Reconciliation (Admin only)
*/
export const reconcileUsers = async (): Promise<ReconciliationReportResponse> => {
const response = await apiClient.post<ReconciliationReportResponse>(
`${USER_API_PATH}/admin/reconcile`
);
return response.data;
};
|