| 1 | // This script lists all workbook names, URLs, and version numbers for the specified memberId |
| 2 | |
| 3 | // 1: Load environment variables from a specific .env file for configuration |
| 4 | require('dotenv').config({ path: 'sigma-api-recipes/.env' }); |
| 5 | |
| 6 | // 2: Import the function to obtain a bearer token from the authenticate-bearer module |
| 7 | const getBearerToken = require('../get-access-token'); |
| 8 | |
| 9 | // 3: Import Axios for making HTTP requests |
| 10 | const axios = require('axios'); |
| 11 | |
| 12 | // 4: Load use-case specific variables from environment variables |
| 13 | const baseURL = process.env.baseURL; // Your base URL |
| 14 | const memberId = process.env.MEMBERID; // Retrieve the memberId from .env |
| 15 | |
| 16 | // Function to fetch all items with limit = 500 |
| 17 | async function fetchMemberFiles(memberId, accessToken) { |
| 18 | try { |
| 19 | const fullUrl = `${baseURL}/members/${memberId}/files?typeFilters=workbook&limit=500`; |
| 20 | console.log(`Fetching member files: ${fullUrl}`); |
| 21 | const response = await axios.get(fullUrl, { |
| 22 | headers: { |
| 23 | 'Authorization': `Bearer ${accessToken}`, |
| 24 | }, |
| 25 | }); |
| 26 | |
| 27 | return response.data.entries; |
| 28 | } catch (error) { |
| 29 | console.error('Error fetching member files:', error); |
| 30 | return []; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // Main function to list workbooks accessible to a specific member |
| 35 | async function listAccessibleWorkbooks() { |
| 36 | const accessToken = await getBearerToken(); |
| 37 | if (!accessToken) return; |
| 38 | |
| 39 | try { |
| 40 | // Fetching all URL IDs from the member's files |
| 41 | const memberFiles = await fetchMemberFiles(memberId, accessToken); |
| 42 | const memberFileIds = memberFiles.map(file => file.id); |
| 43 | |
| 44 | // Fetching all workbooks with a manual limit of 200 rows |
| 45 | const workbooksUrl = `${baseURL}/workbooks?limit=200`; |
| 46 | const response = await axios.get(workbooksUrl, { |
| 47 | headers: { |
| 48 | 'Authorization': `Bearer ${accessToken}`, |
| 49 | }, |
| 50 | }); |
| 51 | const allWorkbooks = response.data.entries; |
| 52 | |
| 53 | // Filtering workbooks accessible to the member |
| 54 | const accessibleWorkbooks = allWorkbooks.filter(workbook => { |
| 55 | // Check if the workbookId is in the member's file IDs |
| 56 | return memberFileIds.includes(workbook.workbookId); |
| 57 | }); |
| 58 | |
| 59 | console.log(`Fetched ${accessibleWorkbooks.length} workbooks.`); |
| 60 | |
| 61 | // Displaying the filtered workbooks |
| 62 | if (accessibleWorkbooks.length > 0) { |
| 63 | accessibleWorkbooks.forEach((workbook, index) => { |
| 64 | console.log(`#${index + 1}: Name: ${workbook.name}, URL: ${workbook.url}, Latest Version: ${workbook.latestVersion}`); |
| 65 | }); |
| 66 | } else { |
| 67 | console.log('No matching workbooks found for this member based on workbookIds.'); |
| 68 | } |
| 69 | } catch (error) { |
| 70 | console.error('Error listing accessible workbooks:', error); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Execute the function to list accessible workbooks |
| 75 | listAccessibleWorkbooks(); |