| 1 | // This script updates the ownership of a specified inode in Sigma using the "Update an inode" endpoint. |
| 2 | |
| 3 | // Load environment variables from a specific .env file for configuration |
| 4 | require('dotenv').config({ path: 'sigma-api-recipes/.env' }); |
| 5 | |
| 6 | // Import the function to obtain a bearer token from the authenticate-bearer module |
| 7 | const getBearerToken = require('../get-access-token'); |
| 8 | |
| 9 | // Import Axios for making HTTP requests |
| 10 | const axios = require('axios'); |
| 11 | |
| 12 | // Load use-case specific variables from environment variables |
| 13 | const baseURL = process.env.baseURL; // Base URL for the Sigma API |
| 14 | const memberid = process.env.MEMBERID; // New member ID to assign to the inode as owner |
| 15 | const workbookId = process.env.WORKBOOK_ID; // Workbook ID to be used as the urlId for the inode |
| 16 | |
| 17 | // Function to update the ownerId of a specified inode using the workbookId |
| 18 | async function updateInodeOwner(workbookId) { |
| 19 | const token = await getBearerToken(); |
| 20 | if (!token) { |
| 21 | console.error('Failed to obtain Bearer token.'); |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | try { |
| 26 | const response = await axios.patch( |
| 27 | `${baseURL}/files/${workbookId}`, |
| 28 | { ownerId: memberid }, // Use `ownerId` with `memberid` value from .env |
| 29 | { |
| 30 | headers: { |
| 31 | 'Authorization': `Bearer ${token}`, |
| 32 | 'Content-Type': 'application/json' |
| 33 | } |
| 34 | } |
| 35 | ); |
| 36 | console.log('Inode updated successfully:', response.data); |
| 37 | } catch (error) { |
| 38 | console.error('Error updating inode:', error.response ? error.response.data : error); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // Execute the update function |
| 43 | updateInodeOwner(workbookId); |