diff --git a/components/google_drive/actions/create-file-from-template/create-file-from-template.mjs b/components/google_drive/actions/create-file-from-template/create-file-from-template.mjs index 529923991dbaa..bbc13b23d724b 100644 --- a/components/google_drive/actions/create-file-from-template/create-file-from-template.mjs +++ b/components/google_drive/actions/create-file-from-template/create-file-from-template.mjs @@ -1,5 +1,6 @@ import Mustaches from "google-docs-mustaches"; import googleDrive from "../../google_drive.app.mjs"; +import { parseObjectEntries } from "../../common/utils.mjs"; const MODE_GOOGLE_DOC = "Google Doc"; const MODE_PDF = "Pdf"; @@ -8,7 +9,7 @@ export default { key: "google_drive-create-file-from-template", name: "Create New File From Template", description: "Create a new Google Docs file from a template. Optionally include placeholders in the template document that will get replaced from this action. [See documentation](https://www.npmjs.com/package/google-docs-mustaches)", - version: "0.1.16", + version: "0.1.17", annotations: { destructiveHint: true, openWorldHint: true, @@ -17,10 +18,20 @@ export default { type: "action", props: { googleDrive, + drive: { + propDefinition: [ + googleDrive, + "watchedDrive", + ], + optional: true, + }, templateId: { propDefinition: [ googleDrive, "fileId", + (c) => ({ + drive: c.drive, + }), ], description: "Select the template document you'd like to use as the template, or use a custom expression to reference a document ID from a previous step. Template documents should contain placeholders in the format `{{xyz}}`.", @@ -29,6 +40,9 @@ export default { propDefinition: [ googleDrive, "folderId", + (c) => ({ + drive: c.drive, + }), ], description: "Select the folder of the newly created Google Doc and/or PDF, or use a custom expression to reference a folder ID from a previous step.", @@ -65,19 +79,38 @@ export default { mode: this.mode, }; + const isSharedDrive = this.drive && this.drive !== "My Drive"; + const client = new Mustaches.default({ token: () => this.googleDrive.$auth.oauth_access_token, }); + // COPY THE TEMPLATE + + const drive = this.googleDrive.drive(); + const copiedTemplate = await drive.files.copy({ + fileId: this.templateId, + requestBody: { + name: "template-copy", + parents: [ + "root", + ], + }, + supportsAllDrives: true, + }); + const templateId = copiedTemplate.data.id; + /* CREATE THE GOOGLE DOC */ let googleDocId; try { googleDocId = await client.interpolate({ - source: this.templateId, - destination: this.folderId, + source: templateId, + destination: !isSharedDrive + ? this.folderId + : undefined, name: this.name, - data: this.replaceValues, + data: parseObjectEntries(this.replaceValues), }); } catch (e) { const { @@ -93,22 +126,56 @@ export default { /* CREATE THE PDF */ + let pdfId; if (this.mode.includes(MODE_PDF)) { - const pdfId = await client.export({ + pdfId = await client.export({ file: googleDocId, mimeType: "application/pdf", name: this.name, - destination: this.folderId, + destination: !isSharedDrive + ? this.folderId + : undefined, }); result["pdfId"] = pdfId; } + // MOVE FILE(S) TO SHARED DRIVE + + if (isSharedDrive) { + if (this.mode.includes(MODE_GOOGLE_DOC)) { + const file = await this.googleDrive.getFile(googleDocId); + await this.googleDrive.updateFile(googleDocId, { + fields: "*", + removeParents: file.parents.join(","), + addParents: this.folderId || this.drive, + supportsAllDrives: true, + }); + } + + if (pdfId) { + const pdf = await this.googleDrive.getFile(pdfId); + await this.googleDrive.updateFile(pdfId, { + fields: "*", + removeParents: pdf.parents.join(","), + addParents: this.folderId || this.drive, + supportsAllDrives: true, + }); + } + } + /* REMOVE THE GOOGLE DOC */ if (!this.mode.includes(MODE_GOOGLE_DOC)) { await this.googleDrive.deleteFile(googleDocId); } + // REMOVE THE COPIED TEMPLATE + + await drive.files.delete({ + fileId: templateId, + supportsAllDrives: true, + }); + $.export("$summary", "New file successfully created"); return result; }, diff --git a/components/google_drive/package.json b/components/google_drive/package.json index c61df3a50cb9e..ef2890c90152c 100644 --- a/components/google_drive/package.json +++ b/components/google_drive/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/google_drive", - "version": "1.3.0", + "version": "1.3.1", "description": "Pipedream Google_drive Components", "main": "google_drive.app.mjs", "keywords": [