From a65ffb2752216e89b29a3620f4697daca731cd28 Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Wed, 17 Jul 2024 15:58:31 -0400 Subject: [PATCH 1/3] documerge init --- .../actions/combine-files/combine-files.mjs | 23 ++++ .../convert-file-to-pdf.mjs | 28 +++++ .../extract-field-values-from-pdf.mjs | 39 +++++++ components/documerge/documerge.app.mjs | 104 +++++++++++++++++- components/documerge/package.json | 2 +- .../new-merged-document-instant.mjs | 53 +++++++++ .../new-merged-route-instant.mjs | 66 +++++++++++ 7 files changed, 309 insertions(+), 6 deletions(-) create mode 100644 components/documerge/actions/combine-files/combine-files.mjs create mode 100644 components/documerge/actions/convert-file-to-pdf/convert-file-to-pdf.mjs create mode 100644 components/documerge/actions/extract-field-values-from-pdf/extract-field-values-from-pdf.mjs create mode 100644 components/documerge/sources/new-merged-document-instant/new-merged-document-instant.mjs create mode 100644 components/documerge/sources/new-merged-route-instant/new-merged-route-instant.mjs diff --git a/components/documerge/actions/combine-files/combine-files.mjs b/components/documerge/actions/combine-files/combine-files.mjs new file mode 100644 index 0000000000000..c7c9d8169b9a1 --- /dev/null +++ b/components/documerge/actions/combine-files/combine-files.mjs @@ -0,0 +1,23 @@ +import documerge from "../../documerge.app.mjs"; +import { axios } from "@pipedream/platform"; + +export default { + key: "documerge-combine-files", + name: "Combine Files", + description: "Merges multiple user-specified files into a single PDF or DOCX.", + version: "0.0.{{ts}}", + type: "action", + props: { + documerge, + fileUrlsOrIds: documerge.propDefinitions.fileUrlsOrIds, + }, + async run({ $ }) { + const response = await this.documerge.mergeDocuments({ + data: { + files: this.fileUrlsOrIds, + }, + }); + $.export("$summary", "Successfully merged files"); + return response; + }, +}; diff --git a/components/documerge/actions/convert-file-to-pdf/convert-file-to-pdf.mjs b/components/documerge/actions/convert-file-to-pdf/convert-file-to-pdf.mjs new file mode 100644 index 0000000000000..299bcf067b454 --- /dev/null +++ b/components/documerge/actions/convert-file-to-pdf/convert-file-to-pdf.mjs @@ -0,0 +1,28 @@ +import documerge from "../../documerge.app.mjs"; +import { axios } from "@pipedream/platform"; + +export default { + key: "documerge-convert-file-to-pdf", + name: "Convert File to PDF", + description: "Converts a specified file into a PDF. [See the documentation](https://app.documerge.ai/api-docs/)", + version: "0.0.{{ts}}", + type: "action", + props: { + documerge, + fileUrlOrId: { + propDefinition: [ + documerge, + "fileUrlOrId", + ], + }, + }, + async run({ $ }) { + const response = await this.documerge.convertToPdf({ + data: { + file: this.fileUrlOrId, + }, + }); + $.export("$summary", "Successfully converted file to PDF"); + return response.data; + }, +}; diff --git a/components/documerge/actions/extract-field-values-from-pdf/extract-field-values-from-pdf.mjs b/components/documerge/actions/extract-field-values-from-pdf/extract-field-values-from-pdf.mjs new file mode 100644 index 0000000000000..3cf3e2161f6b0 --- /dev/null +++ b/components/documerge/actions/extract-field-values-from-pdf/extract-field-values-from-pdf.mjs @@ -0,0 +1,39 @@ +import { axios } from "@pipedream/platform"; +import documerge from "../../documerge.app.mjs"; + +export default { + key: "documerge-extract-field-values-from-pdf", + name: "Extract Field Values from PDF", + description: "Extracts and returns data from specified fields in a given pdf file.", + version: "0.0.{{ts}}", + type: "action", + props: { + documerge, + pdfFileUrlOrId: { + propDefinition: [ + documerge, + "pdfFileUrlOrId", + ], + }, + fieldNames: { + propDefinition: [ + documerge, + "fieldNames", + ], + }, + }, + async run({ $ }) { + const response = await this.documerge._makeRequest({ + method: "POST", + path: "/tools/pdf/split", + data: { + file: { + url: this.pdfFileUrlOrId, + }, + extract: this.fieldNames, + }, + }); + $.export("$summary", `Successfully extracted field values from PDF file ${this.pdfFileUrlOrId}`); + return response; + }, +}; diff --git a/components/documerge/documerge.app.mjs b/components/documerge/documerge.app.mjs index 9baf44c69ad57..0d75257f85a53 100644 --- a/components/documerge/documerge.app.mjs +++ b/components/documerge/documerge.app.mjs @@ -1,11 +1,105 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "documerge", - propDefinitions: {}, + propDefinitions: { + documentTypes: { + type: "string[]", + label: "Document Types", + description: "The types of documents to listen to for the trigger", + }, + routeTypes: { + type: "string[]", + label: "Route Types", + description: "The types of routes to listen to for the trigger", + }, + fileUrlsOrIds: { + type: "string[]", + label: "File URLs or IDs", + description: "Array of file URLs or IDs to merge", + }, + pdfFileUrlOrId: { + type: "string", + label: "PDF File URL or ID", + description: "URL or ID of the PDF file to extract data from", + }, + fieldNames: { + type: "string[]", + label: "Field Names", + description: "Array of field names to extract data from in the PDF file", + }, + fileUrlOrId: { + type: "string", + label: "File URL or ID", + description: "URL or ID of the file to convert into a PDF", + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return "https://app.documerge.ai/api"; + }, + async _makeRequest(opts = {}) { + const { + $ = this, + method = "GET", + path, + headers, + ...otherOpts + } = opts; + return axios($, { + ...otherOpts, + method, + url: this._baseUrl() + path, + headers: { + ...headers, + "Authorization": `Bearer ${this.$auth.oauth_access_token}`, + "Content-Type": "application/json", + "Accept": "application/json", + }, + }); + }, + async mergeDocuments(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/documents/merge", + ...opts, + }); + }, + async mergeRoutes(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/routes/merge", + ...opts, + }); + }, + async createDocument(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/documents", + ...opts, + }); + }, + async createRoute(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/routes", + ...opts, + }); + }, + async splitPdf(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/tools/pdf/split", + ...opts, + }); + }, + async convertToPdf(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/tools/pdf/convert", + ...opts, + }); }, }, -}; \ No newline at end of file +}; diff --git a/components/documerge/package.json b/components/documerge/package.json index 453ca0dc85e9c..59bbee86b6471 100644 --- a/components/documerge/package.json +++ b/components/documerge/package.json @@ -12,4 +12,4 @@ "publishConfig": { "access": "public" } -} \ No newline at end of file +} diff --git a/components/documerge/sources/new-merged-document-instant/new-merged-document-instant.mjs b/components/documerge/sources/new-merged-document-instant/new-merged-document-instant.mjs new file mode 100644 index 0000000000000..357931d9432a4 --- /dev/null +++ b/components/documerge/sources/new-merged-document-instant/new-merged-document-instant.mjs @@ -0,0 +1,53 @@ +import documerge from "../../documerge.app.mjs"; + +export default { + key: "documerge-new-merged-document-instant", + name: "New Merged Document Instant", + description: "Emits a new event when a merged document is created in documerge.", + version: "0.0.{{ts}}", + type: "source", + dedupe: "unique", + props: { + documerge, + documentTypes: documerge.propDefinitions.documentTypes, + http: { + type: "$.interface.http", + customResponse: true, + }, + db: "$.service.db", + }, + hooks: { + async activate() { + const { documentTypes } = this; + for (const type of documentTypes) { + await this.documerge.createDocument({ + data: { + type, + status: "Active", + }, + }); + } + }, + async deactivate() { + const documentId = this.db.get("documentId"); + await this.documerge.deleteDocument(documentId); + }, + }, + async run(event) { + const { + headers, body, + } = event; + if (headers["Content-Type"] !== "application/json") { + return; + } + const { data } = body; + if (!this.documentTypes.includes(data.type)) { + return; + } + this.$emit(data, { + id: data.id, + summary: `New merged document of type ${data.type}`, + ts: Date.parse(data.created_at), + }); + }, +}; diff --git a/components/documerge/sources/new-merged-route-instant/new-merged-route-instant.mjs b/components/documerge/sources/new-merged-route-instant/new-merged-route-instant.mjs new file mode 100644 index 0000000000000..e1f29ccb727a7 --- /dev/null +++ b/components/documerge/sources/new-merged-route-instant/new-merged-route-instant.mjs @@ -0,0 +1,66 @@ +import documerge from "../../documerge.app.mjs"; + +export default { + key: "documerge-new-merged-route-instant", + name: "New Merged Route Instant", + description: "Emit new event when a merged route is created. [See the documentation](https://app.documerge.ai/api-docs/)", + version: "0.0.{{ts}}", + type: "source", + dedupe: "unique", + props: { + documerge, + http: { + type: "$.interface.http", + customResponse: true, + }, + db: "$.service.db", + routeTypes: { + propDefinition: [ + documerge, + "routeTypes", + ], + }, + }, + hooks: { + async activate() { + const { data } = await this.documerge.createRoute(); + this.db.set("routeId", data.id); + }, + async deactivate() { + const routeId = this.db.get("routeId"); + await this.documerge.mergeRoutes({ + data: { + routeId, + }, + }); + }, + }, + async run(event) { + const { + body, headers, + } = event; + + // validate headers + if (headers["Content-Type"] !== "application/json") { + this.http.respond({ + status: 400, + }); + return; + } + + // validate body + if (!this.routeTypes.includes(body.routeType)) { + this.http.respond({ + status: 422, + }); + return; + } + + // emit the event + this.$emit(body, { + id: body.id, + summary: `New Route Created: ${body.name}`, + ts: Date.now(), + }); + }, +}; From e4a3096cbcba554c3de051a675b78ec45b4fc100 Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Thu, 18 Jul 2024 14:15:04 -0400 Subject: [PATCH 2/3] new components --- .../actions/combine-files/combine-files.mjs | 46 +++++-- .../convert-file-to-pdf.mjs | 38 ++++-- .../extract-field-values-from-pdf.mjs | 39 ------ .../get-document-fields.mjs | 26 ++++ components/documerge/documerge.app.mjs | 118 +++++++++++------- components/documerge/package.json | 5 +- components/documerge/sources/common/base.mjs | 44 +++++++ .../new-merged-document-instant.mjs | 71 +++++------ .../test-event.mjs | 12 ++ .../new-merged-route-instant.mjs | 82 ++++++------ .../new-merged-route-instant/test-event.mjs | 13 ++ 11 files changed, 306 insertions(+), 188 deletions(-) delete mode 100644 components/documerge/actions/extract-field-values-from-pdf/extract-field-values-from-pdf.mjs create mode 100644 components/documerge/actions/get-document-fields/get-document-fields.mjs create mode 100644 components/documerge/sources/common/base.mjs create mode 100644 components/documerge/sources/new-merged-document-instant/test-event.mjs create mode 100644 components/documerge/sources/new-merged-route-instant/test-event.mjs diff --git a/components/documerge/actions/combine-files/combine-files.mjs b/components/documerge/actions/combine-files/combine-files.mjs index c7c9d8169b9a1..944177fd8af30 100644 --- a/components/documerge/actions/combine-files/combine-files.mjs +++ b/components/documerge/actions/combine-files/combine-files.mjs @@ -1,23 +1,53 @@ import documerge from "../../documerge.app.mjs"; -import { axios } from "@pipedream/platform"; +import fs from "fs"; export default { key: "documerge-combine-files", name: "Combine Files", - description: "Merges multiple user-specified files into a single PDF or DOCX.", - version: "0.0.{{ts}}", + description: "Merges multiple user-specified files into a single PDF or DOCX. [See the documentation](https://app.documerge.ai/api-docs/#tools-POSTapi-tools-combine)", + version: "0.0.1", type: "action", props: { documerge, - fileUrlsOrIds: documerge.propDefinitions.fileUrlsOrIds, + name: { + type: "string", + label: "Name", + description: "Name of the new file", + }, + output: { + type: "string", + label: "Output", + description: "The output file type", + options: [ + "pdf", + "docx", + ], + }, + urls: { + type: "string[]", + label: "URLs", + description: "Array of URLs to combine", + }, }, async run({ $ }) { - const response = await this.documerge.mergeDocuments({ + const fileContent = await this.documerge.combineFiles({ + $, data: { - files: this.fileUrlsOrIds, + output: this.output, + files: this.urls.map((url) => ({ + name: this.name, + url, + })), }, }); - $.export("$summary", "Successfully merged files"); - return response; + + const filename = this.name.includes(".pdf") || this.name.includes(".docx") + ? this.name + : `${this.name}.${this.output}`; + const path = `/tmp/${filename}`; + await fs.writeFileSync(path, Buffer.from(fileContent)); + + $.export("$summary", "Successfully combined files"); + return path; }, }; diff --git a/components/documerge/actions/convert-file-to-pdf/convert-file-to-pdf.mjs b/components/documerge/actions/convert-file-to-pdf/convert-file-to-pdf.mjs index 299bcf067b454..c5751f6fc93ba 100644 --- a/components/documerge/actions/convert-file-to-pdf/convert-file-to-pdf.mjs +++ b/components/documerge/actions/convert-file-to-pdf/convert-file-to-pdf.mjs @@ -1,28 +1,44 @@ import documerge from "../../documerge.app.mjs"; -import { axios } from "@pipedream/platform"; +import fs from "fs"; export default { key: "documerge-convert-file-to-pdf", name: "Convert File to PDF", - description: "Converts a specified file into a PDF. [See the documentation](https://app.documerge.ai/api-docs/)", - version: "0.0.{{ts}}", + description: "Converts a specified file into a PDF. [See the documentation](https://app.documerge.ai/api-docs/#tools-POSTapi-tools-pdf-convert)", + version: "0.0.1", type: "action", props: { documerge, - fileUrlOrId: { - propDefinition: [ - documerge, - "fileUrlOrId", - ], + name: { + type: "string", + label: "Name", + description: "Name of the new file", + }, + url: { + type: "string", + label: "URL", + description: "The URL of the file to convert", + optional: true, }, }, async run({ $ }) { - const response = await this.documerge.convertToPdf({ + const fileContent = await this.documerge.convertToPdf({ + $, data: { - file: this.fileUrlOrId, + file: { + name: this.name, + url: this.url, + }, }, }); + + const filename = this.name.includes(".pdf") + ? this.name + : `${this.name}.pdf`; + const path = `/tmp/${filename}`; + await fs.writeFileSync(path, Buffer.from(fileContent)); + $.export("$summary", "Successfully converted file to PDF"); - return response.data; + return path; }, }; diff --git a/components/documerge/actions/extract-field-values-from-pdf/extract-field-values-from-pdf.mjs b/components/documerge/actions/extract-field-values-from-pdf/extract-field-values-from-pdf.mjs deleted file mode 100644 index 3cf3e2161f6b0..0000000000000 --- a/components/documerge/actions/extract-field-values-from-pdf/extract-field-values-from-pdf.mjs +++ /dev/null @@ -1,39 +0,0 @@ -import { axios } from "@pipedream/platform"; -import documerge from "../../documerge.app.mjs"; - -export default { - key: "documerge-extract-field-values-from-pdf", - name: "Extract Field Values from PDF", - description: "Extracts and returns data from specified fields in a given pdf file.", - version: "0.0.{{ts}}", - type: "action", - props: { - documerge, - pdfFileUrlOrId: { - propDefinition: [ - documerge, - "pdfFileUrlOrId", - ], - }, - fieldNames: { - propDefinition: [ - documerge, - "fieldNames", - ], - }, - }, - async run({ $ }) { - const response = await this.documerge._makeRequest({ - method: "POST", - path: "/tools/pdf/split", - data: { - file: { - url: this.pdfFileUrlOrId, - }, - extract: this.fieldNames, - }, - }); - $.export("$summary", `Successfully extracted field values from PDF file ${this.pdfFileUrlOrId}`); - return response; - }, -}; diff --git a/components/documerge/actions/get-document-fields/get-document-fields.mjs b/components/documerge/actions/get-document-fields/get-document-fields.mjs new file mode 100644 index 0000000000000..2cde3e238e5a9 --- /dev/null +++ b/components/documerge/actions/get-document-fields/get-document-fields.mjs @@ -0,0 +1,26 @@ +import documerge from "../../documerge.app.mjs"; + +export default { + key: "documerge-get-document-fields", + name: "Get Document Fields", + description: "Extracts and returns data from fields in a given document. [See the documentation](https://app.documerge.ai/api-docs/#documents-GETapi-documents-fields--document_id-)", + version: "0.0.1", + type: "action", + props: { + documerge, + documentId: { + propDefinition: [ + documerge, + "documentId", + ], + }, + }, + async run({ $ }) { + const response = await this.documerge.getDocumentFields({ + $, + documentId: this.documentId, + }); + $.export("$summary", `Successfully extracted field values from document with ID: ${this.documentId}`); + return response; + }, +}; diff --git a/components/documerge/documerge.app.mjs b/components/documerge/documerge.app.mjs index 0d75257f85a53..7ce530e34725f 100644 --- a/components/documerge/documerge.app.mjs +++ b/components/documerge/documerge.app.mjs @@ -4,100 +4,124 @@ export default { type: "app", app: "documerge", propDefinitions: { - documentTypes: { - type: "string[]", - label: "Document Types", - description: "The types of documents to listen to for the trigger", - }, - routeTypes: { - type: "string[]", - label: "Route Types", - description: "The types of routes to listen to for the trigger", - }, - fileUrlsOrIds: { - type: "string[]", - label: "File URLs or IDs", - description: "Array of file URLs or IDs to merge", - }, - pdfFileUrlOrId: { + documentId: { type: "string", - label: "PDF File URL or ID", - description: "URL or ID of the PDF file to extract data from", + label: "Document ID", + description: "Identifier of a document", + async options() { + const { data } = await this.listDocuments(); + return data?.map(({ + id: value, name: label, + }) => ({ + value, + label, + })) || []; + }, }, - fieldNames: { - type: "string[]", - label: "Field Names", - description: "Array of field names to extract data from in the PDF file", - }, - fileUrlOrId: { + routeId: { type: "string", - label: "File URL or ID", - description: "URL or ID of the file to convert into a PDF", + label: "Routet ID", + description: "Identifier of a route", + async options() { + const { data } = await this.listRoutes(); + return data?.map(({ + id: value, name: label, + }) => ({ + value, + label, + })) || []; + }, }, }, methods: { _baseUrl() { return "https://app.documerge.ai/api"; }, - async _makeRequest(opts = {}) { + _makeRequest(opts = {}) { const { $ = this, - method = "GET", path, - headers, ...otherOpts } = opts; return axios($, { ...otherOpts, - method, - url: this._baseUrl() + path, + url: `${this._baseUrl()}${path}`, headers: { - ...headers, - "Authorization": `Bearer ${this.$auth.oauth_access_token}`, + "Authorization": `Bearer ${this.$auth.api_token}`, "Content-Type": "application/json", "Accept": "application/json", }, }); }, - async mergeDocuments(opts = {}) { + listDocuments(opts = {}) { return this._makeRequest({ - method: "POST", - path: "/documents/merge", + path: "/documents", ...opts, }); }, - async mergeRoutes(opts = {}) { + listRoutes(opts = {}) { return this._makeRequest({ - method: "POST", - path: "/routes/merge", + path: "/routes", + ...opts, + }); + }, + getDocumentFields({ + documentId, ...opts + }) { + return this._makeRequest({ + path: `/documents/fields/${documentId}`, ...opts, }); }, - async createDocument(opts = {}) { + combineFiles(opts = {}) { return this._makeRequest({ method: "POST", - path: "/documents", + path: "/tools/combine", + responseType: "arraybuffer", ...opts, }); }, - async createRoute(opts = {}) { + convertToPdf(opts = {}) { return this._makeRequest({ method: "POST", - path: "/routes", + path: "/tools/pdf/convert", + responseType: "arraybuffer", ...opts, }); }, - async splitPdf(opts = {}) { + createDocumentDeliveryMethod({ + documentId, ...opts + }) { return this._makeRequest({ method: "POST", - path: "/tools/pdf/split", + path: `/documents/delivery-methods/${documentId}`, + ...opts, + }); + }, + deleteDocumentDeliveryMethod({ + documentId, deliveryMethodId, ...opts + }) { + return this._makeRequest({ + method: "DELETE", + path: `/documents/delivery-methods/${documentId}/${deliveryMethodId}`, ...opts, }); }, - async convertToPdf(opts = {}) { + createRouteDeliveryMethod({ + routeId, ...opts + }) { return this._makeRequest({ method: "POST", - path: "/tools/pdf/convert", + path: `/routes/delivery-methods/${routeId}`, + ...opts, + }); + }, + deleteRouteDeliveryMethod({ + routeId, deliveryMethodId, ...opts + }) { + return this._makeRequest({ + method: "DELETE", + path: `/routes/delivery-methods/${routeId}/${deliveryMethodId}`, ...opts, }); }, diff --git a/components/documerge/package.json b/components/documerge/package.json index 59bbee86b6471..70633a3ac5936 100644 --- a/components/documerge/package.json +++ b/components/documerge/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/documerge", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream DocuMerge Components", "main": "documerge.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.0" } } diff --git a/components/documerge/sources/common/base.mjs b/components/documerge/sources/common/base.mjs new file mode 100644 index 0000000000000..4a785fb20d30d --- /dev/null +++ b/components/documerge/sources/common/base.mjs @@ -0,0 +1,44 @@ +import documerge from "../../documerge.app.mjs"; + +export default { + props: { + documerge, + http: "$.interface.http", + db: "$.service.db", + }, + methods: { + _getDeliveryMethodIds() { + return this.db.get("deliveryMethodIds") || {}; + }, + _setDeliveryMethodIds(deliveryMethodIds) { + this.db.set("deliveryMethodIds", deliveryMethodIds); + }, + getWebhookSettings() { + return { + type: "webhook", + settings: { + url: this.http.endpoint, + always_send: true, + send_temporary_download_url: true, + send_data_using_json: true, + send_merge_data: true, + }, + }; + }, + generateMeta(body) { + return { + id: body.merge_id, + summary: this.getSummary(body), + ts: Date.now(), + }; + }, + getSummary() { + throw new Error("getSummary is not implemented"); + }, + }, + async run(event) { + const { body } = event; + const meta = this.generateMeta(body); + this.$emit(body, meta); + }, +}; diff --git a/components/documerge/sources/new-merged-document-instant/new-merged-document-instant.mjs b/components/documerge/sources/new-merged-document-instant/new-merged-document-instant.mjs index 357931d9432a4..41acdc38923cb 100644 --- a/components/documerge/sources/new-merged-document-instant/new-merged-document-instant.mjs +++ b/components/documerge/sources/new-merged-document-instant/new-merged-document-instant.mjs @@ -1,53 +1,54 @@ -import documerge from "../../documerge.app.mjs"; +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; export default { + ...common, key: "documerge-new-merged-document-instant", - name: "New Merged Document Instant", - description: "Emits a new event when a merged document is created in documerge.", - version: "0.0.{{ts}}", + name: "New Merged Document (Instant)", + description: "Emit new event when a merged document is created in documerge.", + version: "0.0.1", type: "source", dedupe: "unique", props: { - documerge, - documentTypes: documerge.propDefinitions.documentTypes, - http: { - type: "$.interface.http", - customResponse: true, + ...common.props, + documentIds: { + propDefinition: [ + common.props.documerge, + "documentId", + ], + type: "string[]", + label: "Document IDs", + description: "An array of document identifiers of the documents to watch", }, - db: "$.service.db", }, hooks: { async activate() { - const { documentTypes } = this; - for (const type of documentTypes) { - await this.documerge.createDocument({ - data: { - type, - status: "Active", - }, + const deliveryMethodIds = {}; + for (const documentId of this.documentIds) { + const { data: { id } } = await this.documerge.createDocumentDeliveryMethod({ + documentId, + data: this.getWebhookSettings(), }); + deliveryMethodIds[documentId] = id; } + this._setDeliveryMethodIds(deliveryMethodIds); }, async deactivate() { - const documentId = this.db.get("documentId"); - await this.documerge.deleteDocument(documentId); + const deliveryMethodIds = this._getDeliveryMethodIds(); + for (const documentId of this.documentIds) { + await this.documerge.deleteDocumentDeliveryMethod({ + documentId, + deliveryMethodId: deliveryMethodIds[documentId], + }); + } + this._setDeliveryMethodIds({}); }, }, - async run(event) { - const { - headers, body, - } = event; - if (headers["Content-Type"] !== "application/json") { - return; - } - const { data } = body; - if (!this.documentTypes.includes(data.type)) { - return; - } - this.$emit(data, { - id: data.id, - summary: `New merged document of type ${data.type}`, - ts: Date.parse(data.created_at), - }); + methods: { + ...common.methods, + getSummary(body) { + return `Merged Document: ${body.file_name}`; + }, }, + sampleEmit, }; diff --git a/components/documerge/sources/new-merged-document-instant/test-event.mjs b/components/documerge/sources/new-merged-document-instant/test-event.mjs new file mode 100644 index 0000000000000..95c5d8f8ee835 --- /dev/null +++ b/components/documerge/sources/new-merged-document-instant/test-event.mjs @@ -0,0 +1,12 @@ +export default { + "file_url": "https://documerge.blob.core.windows.net/documerge/documentMerges/408/294186/doc1_-_test.pdf?sv=2017-11-09&sr=b&se=2024-07-18T17:16:51Z&sp=r&spr=https&sig=%2F02yOVeuTEB66AihSptlTg7a7GYfxz3JGkxKE2kt94Y%3D", + "file_name": "doc1 - test.pdf", + "merge_id": 320214, + "fields": { + "field1": "test", + "_date": "2024-07-18", + "_datetime": "2024-07-18 12:16 pm", + "_ip": "10.1.0.4", + "_auto_number": null + } +} \ No newline at end of file diff --git a/components/documerge/sources/new-merged-route-instant/new-merged-route-instant.mjs b/components/documerge/sources/new-merged-route-instant/new-merged-route-instant.mjs index e1f29ccb727a7..5da9b0a2c7041 100644 --- a/components/documerge/sources/new-merged-route-instant/new-merged-route-instant.mjs +++ b/components/documerge/sources/new-merged-route-instant/new-merged-route-instant.mjs @@ -1,66 +1,54 @@ -import documerge from "../../documerge.app.mjs"; +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; export default { + ...common, key: "documerge-new-merged-route-instant", - name: "New Merged Route Instant", - description: "Emit new event when a merged route is created. [See the documentation](https://app.documerge.ai/api-docs/)", - version: "0.0.{{ts}}", + name: "New Merged Route (Instant)", + description: "Emit new event when a merged route is created in documerge.", + version: "0.0.1", type: "source", dedupe: "unique", props: { - documerge, - http: { - type: "$.interface.http", - customResponse: true, - }, - db: "$.service.db", - routeTypes: { + ...common.props, + routeIds: { propDefinition: [ - documerge, - "routeTypes", + common.props.documerge, + "routeId", ], + type: "string[]", + label: "Route IDs", + description: "An array of route identifiers of the routes to watch", }, }, hooks: { async activate() { - const { data } = await this.documerge.createRoute(); - this.db.set("routeId", data.id); + const deliveryMethodIds = {}; + for (const routeId of this.routeIds) { + const { data: { id } } = await this.documerge.createRouteDeliveryMethod({ + routeId, + data: this.getWebhookSettings(), + }); + deliveryMethodIds[routeId] = id; + } + this._setDeliveryMethodIds(deliveryMethodIds); }, async deactivate() { - const routeId = this.db.get("routeId"); - await this.documerge.mergeRoutes({ - data: { + const deliveryMethodIds = this.getDeliveryMethodIds(); + for (const routeId of this.routeIds) { + await this.documerge.deleteRoutetDeliveryMethod({ routeId, - }, - }); + deliveryMethodId: deliveryMethodIds[routeId], + }); + } + this._setDeliveryMethodIds({}); }, }, - async run(event) { - const { - body, headers, - } = event; - - // validate headers - if (headers["Content-Type"] !== "application/json") { - this.http.respond({ - status: 400, - }); - return; - } - - // validate body - if (!this.routeTypes.includes(body.routeType)) { - this.http.respond({ - status: 422, - }); - return; - } - - // emit the event - this.$emit(body, { - id: body.id, - summary: `New Route Created: ${body.name}`, - ts: Date.now(), - }); + methods: { + ...common.methods, + getSummary(body) { + return `Merged Route: ${body.file_name}`; + }, }, + sampleEmit, }; diff --git a/components/documerge/sources/new-merged-route-instant/test-event.mjs b/components/documerge/sources/new-merged-route-instant/test-event.mjs new file mode 100644 index 0000000000000..75e75ebe5b59b --- /dev/null +++ b/components/documerge/sources/new-merged-route-instant/test-event.mjs @@ -0,0 +1,13 @@ +export default { + "file_url": "https://documerge.blob.core.windows.net/documerge/combinedFiles/294339/route1_-_test.pdf?sv=2017-11-09&sr=b&se=2024-07-18T17:42:40Z&sp=r&spr=https&sig=6jsL2yYs3URH3g193%2Fvc1AK66vAdx7G8q5hOn1gmPVg%3D", + "file_name": "route1 - test.pdf", + "merge_id": 320379, + "fields": { + "field1": "abc", + "_date": "2024-07-18", + "_datetime": "2024-07-18 12:42 pm", + "_ip": "10.1.0.4", + "_auto_number": null, + "_route_auto_number": null + } +} \ No newline at end of file From 32cdddd3d184af12be3f67d449d5fe441a64cc7e Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Thu, 18 Jul 2024 14:17:23 -0400 Subject: [PATCH 3/3] pnpm-lock.yaml --- pnpm-lock.yaml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 69256d1d4feeb..8d176ce698002 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2451,7 +2451,10 @@ importers: form-data: 4.0.0 components/documerge: - specifiers: {} + specifiers: + '@pipedream/platform': ^3.0.0 + dependencies: + '@pipedream/platform': 3.0.0 components/documint: specifiers: