Configure the Event Function

We will now begin coding the WorkDrive Sync app by configuring the Event function.

Note: We will need to code and deploy the application code to the Catalyst remote console before configuring the event listener in the Catalyst console, because we have to associate it with the Event function.

The function directory functions/workdrivesync contains:

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:

copy
$
npm install axios

This will install the module.

catalyst_workdrive_npm_axios

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:

copy
$
npm install form-data

This will install the module.

catalyst_workdrive_npm_formdata

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:

copy
$
npm install fs

This will install the module.

catalyst_workdrive_npm_fs

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.

Note: Please go through the code in this section to make sure you fully understand it. We will discuss the function and client code, after you configure the client.
    
index.js
copy
const 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(); });
View more
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:

The Event function is now configured.

Last Updated 2023-12-15 18:54:08 +0530 +0530

RELATED LINKS

Event Functions