The copyStudy
function duplicates a Google Drive file named "Study sauce template" and grants permissions for a specified email address. It achieves this by listing files, copying the file, inserting permissions, and retrieving a sheet identifier, before returning the new file ID.
npm run import -- "create a copy of study sauce template"
var uuid = require('uuid/v1');
var importer = require('../Core');
var getSheet = importer.import("get sheet identifier");
var copyFile = importer.import("copy a file on google drive");
var listDrive = importer.import("list google drive files");
var insertPermission = importer.import("insert google drive permissions");
function copyStudy(email) {
var fileId, newId;
if(!email) {
throw new Error('email not specified!');
}
return listDrive()
.then(r => fileId = r.filter(f => f.name === 'Study sauce template')[0].id)
.then(() => copyFile(fileId, 'Study sauce ' + uuid().substr(0, 5)))
.then(id => newId = id)
.then(() => insertPermission(newId, email))
.then(() => getSheet(newId, null, email))
.then(() => newId)
}
module.exports = copyStudy;
// Import required modules
const { v1: uuid } = require('uuid');
const Core = require('../Core');
const { getSheetIdentifier } = Core.import('get sheet identifier');
const { copyFileOnGoogleDrive } = Core.import('copy a file on google drive');
const { listGoogleDriveFiles } = Core.import('list google drive files');
const { insertPermissionOnGoogleDrive } = Core.import('insert google drive permissions');
/**
* Copies a Google Drive file named 'Study sauce template' to a new file
* with a name based on the 'Study sauce' prefix and a 5-character uuid.
* Inserts a permission for the specified email address.
* Returns the ID of the newly copied file.
* @param {string} email - Email address for which permission is inserted.
* @returns {Promise} ID of the newly copied file.
*/
function copyStudy(email) {
if (!email) {
throw new Error('Email address not specified!');
}
return listGoogleDriveFiles()
.then((files) => {
const templateFile = files.find((file) => file.name === 'Study sauce template');
if (!templateFile) {
throw new Error('Template file not found!');
}
return templateFile.id;
})
.then((fileId) => copyFileOnGoogleDrive(fileId, `Study sauce ${uuid().substr(0, 5)}`))
.then((newId) => {
return insertPermissionOnGoogleDrive(newId, email)
.then(() => getSheetIdentifier(newId, null, email))
.then(() => newId);
});
}
module.exports = copyStudy;
uuid/v1
: a module for generating unique IDs.../Core
: a module that exports various functions for interacting with Google Drive.getSheet
: retrieves a sheet identifier.copyFile
: copies a file from Google Drive.listDrive
: lists files in Google Drive.insertPermission
: inserts permissions on a Google Drive file.copyStudy
Duplicates a Google Drive file named "Study sauce template" and inserts permissions for the specified email.
email
: the email address to grant permissions for.email
parameter is specified. If not, throws an error.listDrive
.copyFile
and generates a new ID.insertPermission
.getSheet
.The copyStudy
function is exported as a module.