const { Client } = require('@microsoft/microsoft-graph-client'); const { msalConfig } = require('./authConfig'); const AuthProvider = require('./AuthProvider'); require('isomorphic-fetch'); class GraphService { constructor() { this.authProvider = new AuthProvider(msalConfig); this.graphClient = null; } async initialize() { try { // Get account await this.authProvider.login(); // Get token with OneDrive scopes const tokenRequest = { scopes: ['Files.Read', 'Files.Read.All', 'Sites.Read.All'] }; const authResponse = await this.authProvider.getToken(tokenRequest); // Initialize Graph client this.graphClient = Client.init({ authProvider: (done) => { done(null, authResponse.accessToken); } }); return authResponse.accessToken; } catch (error) { console.error('Error initializing Graph service:', error); throw error; } } async listFolderContents(folderPath) { if (!this.graphClient) { await this.initialize(); } try { // Clean up the path const cleanPath = folderPath.replace(/^\/+|\/+$/g, ''); const endpoint = `/me/drive/root:/${cleanPath}:/children`; // Get items from the folder const response = await this.graphClient.api(endpoint) .select('id,name,size,file,@microsoft.graph.downloadUrl') .get(); return response.value; } catch (error) { console.error('Error listing folder contents:', error); throw error; } } } module.exports = new GraphService();