Spaces:
Paused
Paused
File size: 6,127 Bytes
34367da | 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 | /**
* 🏢 Microsoft Graph Email & SharePoint Harvester
* Bruger Graph API til at søge i TDC Outlook og SharePoint
*/
import { logger } from '../../utils/logger.js';
export interface GraphSearchResult {
id: string;
title: string;
url: string;
summary: string;
type: 'email' | 'document' | 'site' | 'list';
from?: string;
received?: string;
modified?: string;
}
export class MicrosoftGraphAdapter {
private accessToken: string | null = null;
private graphBase = 'https://graph.microsoft.com/v1.0';
constructor(accessToken?: string) {
this.accessToken = accessToken || process.env.MS_GRAPH_TOKEN || null;
}
setAccessToken(token: string) {
this.accessToken = token;
logger.info('🔐 Microsoft Graph access token set');
}
async isAvailable(): Promise<boolean> {
if (!this.accessToken) return false;
try {
const response = await fetch(`${this.graphBase}/me`, {
headers: { 'Authorization': `Bearer ${this.accessToken}` }
});
return response.ok;
} catch {
return false;
}
}
async searchEmails(query: string, limit = 25): Promise<GraphSearchResult[]> {
if (!this.accessToken) {
logger.warn('⚠️ No Graph access token - use setAccessToken()');
return [];
}
try {
// Search messages
const response = await fetch(
`${this.graphBase}/me/messages?$search="${encodeURIComponent(query)}"&$top=${limit}&$select=id,subject,from,receivedDateTime,bodyPreview,webLink`,
{
headers: {
'Authorization': `Bearer ${this.accessToken}`,
'ConsistencyLevel': 'eventual'
}
}
);
if (!response.ok) {
logger.error(`Graph email search failed: ${response.status}`);
return [];
}
const data = await response.json();
return (data.value || []).map((msg: any) => ({
id: msg.id,
title: msg.subject || '(No subject)',
url: msg.webLink || `https://outlook.office365.com/mail/deeplink/read/${msg.id}`,
summary: msg.bodyPreview?.slice(0, 300) || '',
type: 'email' as const,
from: msg.from?.emailAddress?.address || 'Unknown',
received: msg.receivedDateTime
}));
} catch (err) {
logger.error('Graph email search error:', err);
return [];
}
}
async searchSharePoint(query: string, limit = 25): Promise<GraphSearchResult[]> {
if (!this.accessToken) return [];
try {
const searchBody = {
requests: [{
entityTypes: ['driveItem', 'listItem', 'site'],
query: { queryString: query },
from: 0,
size: limit
}]
};
const response = await fetch(`${this.graphBase}/search/query`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(searchBody)
});
if (!response.ok) {
logger.error(`Graph SharePoint search failed: ${response.status}`);
return [];
}
const data = await response.json();
const results: GraphSearchResult[] = [];
for (const resultSet of data.value || []) {
for (const container of resultSet.hitsContainers || []) {
for (const hit of container.hits || []) {
const resource = hit.resource || {};
results.push({
id: resource.id || hit.hitId,
title: resource.name || resource.displayName || query,
url: resource.webUrl || '',
summary: hit.summary?.slice(0, 300) || '',
type: this.detectType(resource['@odata.type']),
modified: resource.lastModifiedDateTime
});
}
}
}
return results;
} catch (err) {
logger.error('Graph SharePoint search error:', err);
return [];
}
}
async searchAll(query: string, limit = 20): Promise<GraphSearchResult[]> {
const [emails, docs] = await Promise.all([
this.searchEmails(query, limit),
this.searchSharePoint(query, limit)
]);
return [...emails, ...docs];
}
async getSites(): Promise<{ id: string; name: string; url: string }[]> {
if (!this.accessToken) return [];
try {
const response = await fetch(
`${this.graphBase}/sites?search=*&$top=50`,
{ headers: { 'Authorization': `Bearer ${this.accessToken}` } }
);
if (!response.ok) return [];
const data = await response.json();
return (data.value || []).map((site: any) => ({
id: site.id,
name: site.displayName,
url: site.webUrl
}));
} catch {
return [];
}
}
private detectType(odataType: string): 'email' | 'document' | 'site' | 'list' {
if (!odataType) return 'document';
if (odataType.includes('site')) return 'site';
if (odataType.includes('listItem')) return 'list';
return 'document';
}
}
// Singleton instance
export const graphAdapter = new MicrosoftGraphAdapter();
|