Configure the Event Function
We will now begin coding the WorkDrive Sync app by configuring the Event function.
The function directory functions/workdrivesync contains:
- The index.js main function file
- The catalyst-config.json configuration file
- Node modules
- package.json and package-lock.json dependency files
You can use any IDE to configure the function.
Install Packages for Node.js
The Node.js Event function requires three packages to be installed: axios, form-data, and fs.
axios
axios is a promise-based HTTP client that we will use to send asynchronous HTTP requests to the WorkDrive API endpoint to post files.
To install axios, navigate to the Node function’s directory (functions/workdrivesync) and execute the following command:
This will install the module.
form-data
We will use form-data to upload the file to WorkDrive, after reading it from the event data sent in by the event listener.
To install form-data, navigate to the Node function’s directory (functions/workdrivesync) and execute the following command:
This will install the module.
fs
The fs module enables us to access the physical file system and create a read stream to fetch the file from the event data.
To install fs, navigate to the Node function’s directory (functions/workdrivesync) and execute the following command:
This will install the module.
Information about these packages will also be updated in the package.json file of the Event function.
copy{ "name": "workdrivesync", "version": "1.0.0", "main": "index.js", "author": "emma@zylker.com", "dependencies": { "axios": "^0.21.4", "form-data": "^4.0.0", "fs": "^0.0.1-security", "zcatalyst-sdk-node": "latest" } }
You can now add the code in the function file.
Copy the code below and paste it in index.js located in functions/workdrivesync directory and save the file.
index.jscopyconst catalyst = require('zcatalyst-sdk-node'); const axios = require('axios').default; const FormData = require('form-data'); const fs = require('fs'); const credentials = { WorkDriveConnectorz: { client_id: '{{YOUR_CLIENT_ID}}', //Enter your Client ID client_secret: '{{YOUR_CLIENT_SECRET}}', //Enter your Client Secret auth_url: 'https://accounts.zoho.com/oauth/v2/token', refresh_url: 'https://accounts.zoho.com/oauth/v2/token', refresh_token: '{{YOUR_REFRESH_TOKEN}}' //Enter your Refresh Token } } const FOLDERID = 'p98c29a39baa7a7284693b74608708fd6ba5f'; //Enter your WorkDrive Folder ID module.exports = async (event, context) => { try { const app = catalyst.initialize(context); const accessToken = await app.connection(credentials).getConnector('WorkDriveConnectorz').getAccessToken(); let filestore = app.filestore(); let folder = filestore.folder(event.data.folder_details); let downloadPromise = folder.downloadFile(event.data.id); downloadPromise.then(async (fileObject) => { fs.writeFileSync(__dirname + '/' + event.data.file_name, fileObject, 'utf-8'); var data = new FormData(); data.append('content', fs.createReadStream(__dirname + '/' + event.data.file_name)); const config = { method: 'POST', url: `https://workdrive.zoho.com/api/v1/upload?filename=${event.data.file_name}&override-name-exist=true&parent_id=${FOLDERID}`,headers: {'Authorization': `Zoho-oauthtoken ${accessToken}`,...data.getHeaders()},data: data}; console.log(config) axios(config).then(async function (response) { const body = response.data; const WorkDriveFileID = body.data[0].attributes.resource_id; const WorkDriveSync = 'Completed'; const query = `SELECT ROWID FROM WorkDriveFileID where FileID=${event.data.id}`; const queryResult = await app.zcql().executeZCQLQuery(query); const ROWID = queryResult[0].WorkDriveFileID.ROWID; const catalystTable = app.datastore().table('WorkDriveFileID'); await catalystTable.updateRow({ WorkDriveFileID, WorkDriveSync, ROWID }); context.closeWithSuccess(); }) .catch(function (error) { console.log(error) context.closeWithFailure(); });
Note: After you copy and paste this code in your function file, ensure that you provide the following values in it as indicated by the comments:
WorkDrive Folder ID in line 15: You can obtain this value by opening the folder you created in WorkDrive earlier. The URL contains the Folder ID of the WorkDrive folder. Copy the ID displayed after folders/ from the URL.
Client ID in line 8
Client Secret in line 9
Refresh Token in line 12
The Event function is now configured.
Last Updated 2023-12-15 18:54:08 +0530 +0530