93 lines
2.9 KiB
TypeScript
93 lines
2.9 KiB
TypeScript
import { AdminApiClient } from '@/lib/api/admin/AdminApiClient';
|
|
import { PageQueryResult } from '@/lib/contracts/page-queries/PageQueryResult';
|
|
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
|
|
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
|
import { AdminService } from '@/lib/services/admin/AdminService';
|
|
|
|
export interface AdminUsersPageDto {
|
|
users: Array<{
|
|
id: string;
|
|
email: string;
|
|
displayName: string;
|
|
roles: string[];
|
|
status: string;
|
|
isSystemAdmin: boolean;
|
|
createdAt: string;
|
|
updatedAt: string;
|
|
lastLoginAt?: string;
|
|
primaryDriverId?: string;
|
|
}>;
|
|
total: number;
|
|
page: number;
|
|
limit: number;
|
|
totalPages: number;
|
|
}
|
|
|
|
/**
|
|
* AdminUsersPageQuery
|
|
*
|
|
* Server-side composition for admin users page.
|
|
* Fetches user list from API with filtering and assembles Page DTO.
|
|
*/
|
|
export class AdminUsersPageQuery {
|
|
async execute(query: {
|
|
search?: string;
|
|
role?: string;
|
|
status?: string;
|
|
page?: number;
|
|
limit?: number;
|
|
}): Promise<PageQueryResult<AdminUsersPageDto>> {
|
|
try {
|
|
// Create required dependencies
|
|
const logger = new ConsoleLogger();
|
|
const errorReporter = new EnhancedErrorReporter(logger, {
|
|
showUserNotifications: false,
|
|
logToConsole: true,
|
|
reportToExternal: process.env.NODE_ENV === 'production',
|
|
});
|
|
|
|
const baseUrl = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3001';
|
|
const apiClient = new AdminApiClient(baseUrl, errorReporter, logger);
|
|
const adminService = new AdminService(apiClient);
|
|
|
|
// Fetch user list via service
|
|
const apiDto = await adminService.listUsers({
|
|
search: query.search,
|
|
role: query.role,
|
|
status: query.status,
|
|
page: query.page || 1,
|
|
limit: query.limit || 50,
|
|
});
|
|
|
|
// Assemble Page DTO (raw values only)
|
|
const pageDto: AdminUsersPageDto = {
|
|
users: apiDto.users.map(user => ({
|
|
id: user.id,
|
|
email: user.email,
|
|
displayName: user.displayName,
|
|
roles: user.roles,
|
|
status: user.status,
|
|
isSystemAdmin: user.isSystemAdmin,
|
|
createdAt: user.createdAt.toISOString(),
|
|
updatedAt: user.updatedAt.toISOString(),
|
|
lastLoginAt: user.lastLoginAt?.toISOString(),
|
|
primaryDriverId: user.primaryDriverId,
|
|
})),
|
|
total: apiDto.total,
|
|
page: apiDto.page,
|
|
limit: apiDto.limit,
|
|
totalPages: apiDto.totalPages,
|
|
};
|
|
|
|
return { status: 'ok', dto: pageDto };
|
|
} catch (error) {
|
|
console.error('AdminUsersPageQuery failed:', error);
|
|
|
|
if (error instanceof Error && (error.message.includes('403') || error.message.includes('401'))) {
|
|
return { status: 'notFound' };
|
|
}
|
|
|
|
return { status: 'error', errorId: 'admin_users_fetch_failed' };
|
|
}
|
|
}
|
|
} |