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 | 2x 23x 2x 24x 24x 6x 6x 2x 15x 15x 15x | /**
* useUserSync Hook
*
* React Query hooks for Cognito-Database user synchronization.
* Story 1.2.5: User Sync and Reconciliation Implementation
*
* Provides:
* - Sync status checking (Cognito vs Database)
* - Manual reconciliation trigger
* - Real-time sync state management
*/
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { checkSyncStatus, reconcileUsers } from '@/services/api/userManagementApi';
/**
* Check sync status between Cognito and Database
* Polls every 30 seconds when enabled
*/
export const useSyncStatus = (enabled: boolean = false) => {
return useQuery({
queryKey: ['users', 'sync-status'],
queryFn: checkSyncStatus,
staleTime: 30 * 1000, // 30 seconds
gcTime: 5 * 60 * 1000, // 5 minutes
enabled,
refetchInterval: enabled ? 30 * 1000 : false, // Poll every 30 seconds when enabled
});
};
/**
* Trigger manual user reconciliation
* Invalidates sync status after success
*/
export const useReconcileUsers = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: reconcileUsers,
onSuccess: () => {
// Invalidate sync status to refresh
queryClient.invalidateQueries({ queryKey: ['users', 'sync-status'] });
// Invalidate user list to show newly synced users
queryClient.invalidateQueries({ queryKey: ['users', 'list'] });
},
});
};
/**
* Combined hook for user sync management
* Provides both status checking and reconciliation
*/
export const useUserSync = () => {
const syncStatus = useSyncStatus(true);
const reconcileMutation = useReconcileUsers();
return {
// Sync status query
syncStatus: syncStatus.data,
isSyncStatusLoading: syncStatus.isLoading,
isSyncStatusError: syncStatus.isError,
syncStatusError: syncStatus.error,
refetchSyncStatus: syncStatus.refetch,
// Reconciliation mutation
reconcile: reconcileMutation.mutate,
reconcileAsync: reconcileMutation.mutateAsync,
reconciliationResult: reconcileMutation.data,
isReconciling: reconcileMutation.isPending,
isReconcileSuccess: reconcileMutation.isSuccess,
isReconcileError: reconcileMutation.isError,
reconcileError: reconcileMutation.error,
resetReconciliation: reconcileMutation.reset,
// Computed values
isInSync: syncStatus.data?.inSync ?? true,
needsSync: syncStatus.data ? !syncStatus.data.inSync : false,
missingCount: syncStatus.data?.missingInDatabase ?? 0,
orphanedCount: syncStatus.data?.orphanedInDatabase ?? 0,
};
};
|