296 lines
No EOL
7.5 KiB
TypeScript
296 lines
No EOL
7.5 KiB
TypeScript
import { useState } from 'react';
|
|
import { useApiRequest } from './useApi';
|
|
|
|
// SharePoint interfaces
|
|
export interface SharePointConnection {
|
|
id: string;
|
|
name: string;
|
|
description: string;
|
|
status: string;
|
|
authority: string;
|
|
lastChecked: string | null;
|
|
expiresAt: string | null;
|
|
externalUsername: string;
|
|
externalEmail: string;
|
|
}
|
|
|
|
export interface SharePointDocument {
|
|
documentName: string;
|
|
documentData: any;
|
|
mimeType: string;
|
|
size?: number;
|
|
path?: string;
|
|
type?: 'file' | 'folder';
|
|
id?: string;
|
|
}
|
|
|
|
export interface SharePointResponse {
|
|
success: boolean;
|
|
data?: {
|
|
documents?: SharePointDocument[];
|
|
};
|
|
error?: string;
|
|
message: string;
|
|
}
|
|
|
|
export interface SharePointListRequest {
|
|
connectionReference: string;
|
|
siteUrl: string;
|
|
folderPaths: string[];
|
|
includeSubfolders?: boolean;
|
|
expectedDocumentFormats?: Array<{
|
|
extension: string;
|
|
mimeType: string;
|
|
description?: string;
|
|
}>;
|
|
}
|
|
|
|
export interface SharePointFindRequest {
|
|
connectionReference: string;
|
|
siteUrl: string;
|
|
query: string;
|
|
searchScope?: string;
|
|
expectedDocumentFormats?: Array<{
|
|
extension: string;
|
|
mimeType: string;
|
|
description?: string;
|
|
}>;
|
|
}
|
|
|
|
export interface SharePointReadRequest {
|
|
documentList: string;
|
|
connectionReference: string;
|
|
siteUrl: string;
|
|
documentPaths: string[];
|
|
includeMetadata?: boolean;
|
|
expectedDocumentFormats?: Array<{
|
|
extension: string;
|
|
mimeType: string;
|
|
description?: string;
|
|
}>;
|
|
}
|
|
|
|
export interface SharePointUploadRequest {
|
|
connectionReference: string;
|
|
siteUrl: string;
|
|
documentPaths: string[];
|
|
documentList: string;
|
|
fileNames: string[];
|
|
expectedDocumentFormats?: Array<{
|
|
extension: string;
|
|
mimeType: string;
|
|
description?: string;
|
|
}>;
|
|
}
|
|
|
|
// Hook for SharePoint testing operations
|
|
export function useSharePointTest() {
|
|
const { request, isLoading, error } = useApiRequest<any, any>();
|
|
const [lastResponse, setLastResponse] = useState<SharePointResponse | null>(null);
|
|
|
|
// Get user's Microsoft connections
|
|
const getConnections = async (): Promise<SharePointConnection[]> => {
|
|
try {
|
|
const response = await request({
|
|
url: '/api/test-sharepoint/connections',
|
|
method: 'get'
|
|
});
|
|
return response.data || [];
|
|
} catch (error) {
|
|
console.error('Error fetching SharePoint connections:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
// Test a specific connection
|
|
const testConnection = async (connectionId: string): Promise<any> => {
|
|
try {
|
|
const response = await request({
|
|
url: `/api/test-sharepoint/test-connection/${connectionId}`,
|
|
method: 'get'
|
|
});
|
|
return response;
|
|
} catch (error) {
|
|
console.error('Error testing connection:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
// List documents in SharePoint folders
|
|
const listDocuments = async (data: SharePointListRequest): Promise<SharePointResponse> => {
|
|
try {
|
|
const response = await request({
|
|
url: '/api/test-sharepoint/list-documents',
|
|
method: 'post',
|
|
data
|
|
});
|
|
setLastResponse(response);
|
|
return response;
|
|
} catch (error) {
|
|
console.error('Error listing SharePoint documents:', error);
|
|
const errorResponse: SharePointResponse = {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Unknown error',
|
|
message: 'Failed to list documents'
|
|
};
|
|
setLastResponse(errorResponse);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
// Find documents by query
|
|
const findDocuments = async (data: SharePointFindRequest): Promise<SharePointResponse> => {
|
|
try {
|
|
const response = await request({
|
|
url: '/api/test-sharepoint/find-documents',
|
|
method: 'post',
|
|
data
|
|
});
|
|
setLastResponse(response);
|
|
return response;
|
|
} catch (error) {
|
|
console.error('Error finding SharePoint documents:', error);
|
|
const errorResponse: SharePointResponse = {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Unknown error',
|
|
message: 'Failed to find documents'
|
|
};
|
|
setLastResponse(errorResponse);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
// Read documents from SharePoint
|
|
const readDocuments = async (data: SharePointReadRequest): Promise<SharePointResponse> => {
|
|
try {
|
|
const response = await request({
|
|
url: '/api/test-sharepoint/read-documents',
|
|
method: 'post',
|
|
data
|
|
});
|
|
setLastResponse(response);
|
|
return response;
|
|
} catch (error) {
|
|
console.error('Error reading SharePoint documents:', error);
|
|
const errorResponse: SharePointResponse = {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Unknown error',
|
|
message: 'Failed to read documents'
|
|
};
|
|
setLastResponse(errorResponse);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
// Upload documents to SharePoint
|
|
const uploadDocuments = async (data: SharePointUploadRequest): Promise<SharePointResponse> => {
|
|
try {
|
|
const response = await request({
|
|
url: '/api/test-sharepoint/upload-documents',
|
|
method: 'post',
|
|
data
|
|
});
|
|
setLastResponse(response);
|
|
return response;
|
|
} catch (error) {
|
|
console.error('Error uploading SharePoint documents:', error);
|
|
const errorResponse: SharePointResponse = {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Unknown error',
|
|
message: 'Failed to upload documents'
|
|
};
|
|
setLastResponse(errorResponse);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
// Get example request bodies
|
|
const getExamples = async (): Promise<any> => {
|
|
try {
|
|
const response = await request({
|
|
url: '/api/test-sharepoint/example-requests',
|
|
method: 'get'
|
|
});
|
|
return response.examples || {};
|
|
} catch (error) {
|
|
console.error('Error getting example requests:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
// Debug user's authentication tokens
|
|
const debugTokens = async (): Promise<any> => {
|
|
try {
|
|
const response = await request({
|
|
url: '/api/test-sharepoint/debug-tokens',
|
|
method: 'get'
|
|
});
|
|
return response;
|
|
} catch (error) {
|
|
console.error('Error debugging tokens:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
// Debug detailed token information
|
|
const debugTokenDetails = async (): Promise<any> => {
|
|
try {
|
|
const response = await request({
|
|
url: '/api/test-sharepoint/debug-token-details',
|
|
method: 'get'
|
|
});
|
|
return response;
|
|
} catch (error) {
|
|
console.error('Error debugging token details:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
// Cleanup all Microsoft tokens
|
|
const cleanupTokens = async (): Promise<any> => {
|
|
try {
|
|
const response = await request({
|
|
url: '/api/test-sharepoint/cleanup-tokens',
|
|
method: 'delete'
|
|
});
|
|
return response;
|
|
} catch (error) {
|
|
console.error('Error cleaning up tokens:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
// Discover SharePoint sites
|
|
const discoverSites = async (): Promise<any> => {
|
|
try {
|
|
const response = await request({
|
|
url: '/api/test-sharepoint/discover-sites',
|
|
method: 'get'
|
|
});
|
|
return response;
|
|
} catch (error) {
|
|
console.error('Error discovering SharePoint sites:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
return {
|
|
// API methods
|
|
getConnections,
|
|
testConnection,
|
|
listDocuments,
|
|
findDocuments,
|
|
readDocuments,
|
|
uploadDocuments,
|
|
getExamples,
|
|
debugTokens,
|
|
debugTokenDetails,
|
|
cleanupTokens,
|
|
discoverSites,
|
|
|
|
// State
|
|
isLoading,
|
|
error,
|
|
lastResponse
|
|
};
|
|
} |