google drive | list google drive files | create a sheet in google drive | Search

The insertPermission function, located in a Node.js module, takes a file ID and a user's email address as input and returns a promise that resolves to the ID of a newly created permission. The function authorizes access to Google Drive, creates a new permission on the specified file, and extracts the permission ID from the response before exporting it as a module for use elsewhere.

Run example

npm run import -- "insert google drive permissions"

insert google drive permissions

var util = require('util');
var importer = require('../Core');
var authorizeDrive = importer.import("authorize google drive");

function insertPermission(fileId, email) {
    return authorizeDrive()
        .then(drive => util.promisify(drive.permissions.create.bind(drive))({
            resource: {
                'type': 'user',
                'role': 'owner',
                'emailAddress': email
            },
            fileId: fileId,
            fields: 'id',
            transferOwnership: true
        }))
        .then(r => r.data.id)
}

module.exports = insertPermission;

What the code could have been:

// Import required modules
const { google } = require('googleapis');
const { authorizeGoogleDrive } = require('../Core');

/**
 * Inserts permission for a file in Google Drive
 *
 * @param {string} fileId - ID of the file
 * @param {string} email - Email address of the user
 * @returns {Promise<string>} ID of the newly created permission
 */
async function insertPermission(fileId, email) {
  // Authorize Google Drive using the authorized client
  const drive = await authorizeGoogleDrive();
  
  // Create a permission for the user as an owner
  const permission = await drive.permissions.create({
    requestBody: {
      type: 'user',
      role: 'owner',
      emailAddress: email
    },
    fileId,
    fields: 'id',
    transferOwnership: true
  });
  
  // Return the ID of the newly created permission
  return permission.data.id;
}

// Export the insertPermission function
module.exports = insertPermission;

Code Breakdown

Importing Modules

The code starts by importing two modules:

Defining the insertPermission Function

The insertPermission function takes two arguments:

The function returns a promise that resolves to the ID of the newly created permission.

Here's a step-by-step breakdown of the function:

  1. authorizeDrive() is called to authorize access to Google Drive. This returns a promise that resolves to the authorized Drive object.
  2. The promisify function from the util module is used to convert the permissions.create method of the Drive object into a promises-based function. This method creates a new permission on the specified file.
  3. The permissions.create function is called with the following arguments:
  4. The result of the permissions.create function is awaited and the id field of the response is extracted.

Exporting the insertPermission Function

Finally, the insertPermission function is exported as a module, making it available for use in other parts of the application.