Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "sendgrid-add-email-to-global-suppression",
name: "Add Email to Global Suppression",
description: "Allows you to add one or more email addresses to the global suppressions group. [See the docs here](https://sendgrid.api-docs.io/v3.0/suppressions-global-suppressions/add-recipient-addresses-to-the-global-suppression-group)",
version: "0.0.4",
version: "0.0.5",
type: "action",
props: {
...common.props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
key: "sendgrid-add-or-update-contact",
name: "Add or Update Contact",
description: "Adds or updates a contact. [See the docs here](https://docs.sendgrid.com/api-reference/contacts/add-or-update-a-contact)",
version: "0.0.4",
version: "0.0.5",
type: "action",
props: {
...common.props,
Expand Down
10 changes: 9 additions & 1 deletion components/sendgrid/actions/common/common.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ export default {
* @param {object} value the value to check for returning `undefined`.
*/
convertEmptyStringToUndefined(value) {
if (Array.isArray(value) && value.length === 0) {
return undefined;
}
if (value === "" || value === null) {
return undefined;
}
Expand Down Expand Up @@ -171,6 +174,11 @@ export default {
}
return asmConfig;
},

checkTmp(filename) {
if (!filename.startsWith("/tmp")) {
return `/tmp/${filename}`;
}
return filename;
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "sendgrid-create-contact-list",
name: "Create Contact List",
description: "Allows you to create a new contact list. [See the docs here](https://docs.sendgrid.com/api-reference/lists/create-list)",
version: "0.0.4",
version: "0.0.5",
type: "action",
props: {
...common.props,
Expand Down
2 changes: 1 addition & 1 deletion components/sendgrid/actions/create-send/create-send.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
key: "sendgrid-create-send",
name: "Create Send",
description: "Create a single send. [See the docs here](https://www.twilio.com/docs/sendgrid/api-reference/single-sends/create-single-send)",
version: "0.0.1",
version: "0.0.2",
type: "action",
props: {
...common.props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "sendgrid-delete-blocks",
name: "Delete Blocks",
description: "Allows you to delete all email addresses on your blocks list. [See the docs here](https://docs.sendgrid.com/api-reference/blocks-api/delete-blocks)",
version: "0.0.4",
version: "0.0.5",
type: "action",
props: {
...common.props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "sendgrid-delete-bounces",
name: "Delete Bounces",
description: "Allows you to delete all emails on your bounces list. [See the docs here](https://docs.sendgrid.com/api-reference/bounces-api/delete-bounces)",
version: "0.0.4",
version: "0.0.5",
type: "action",
props: {
...common.props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "sendgrid-delete-contacts",
name: "Delete Contacts",
description: "Allows you to delete one or more contacts. [See the docs here](https://docs.sendgrid.com/api-reference/contacts/delete-contacts)",
version: "0.0.4",
version: "0.0.5",
type: "action",
props: {
...common.props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
key: "sendgrid-delete-global-suppression",
name: "Delete Global Suppression",
description: "Allows you to remove an email address from the global suppressions group. [See the docs here](https://docs.sendgrid.com/api-reference/suppressions-global-suppressions/delete-a-global-suppression)",
version: "0.0.4",
version: "0.0.5",
type: "action",
props: {
...common.props,
Expand Down
2 changes: 1 addition & 1 deletion components/sendgrid/actions/delete-list/delete-list.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "sendgrid-delete-list",
name: "Delete List",
description: "Allows you to delete a specific contact list. [See the docs here](https://docs.sendgrid.com/api-reference/lists/delete-a-list)",
version: "0.0.4",
version: "0.0.5",
type: "action",
props: {
...common.props,
Expand Down
2 changes: 1 addition & 1 deletion components/sendgrid/actions/get-a-block/get-a-block.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
key: "sendgrid-get-a-block",
name: "Get a Block",
description: "Gets a specific block. [See the docs here](https://docs.sendgrid.com/api-reference/blocks-api/retrieve-a-specific-block)",
version: "0.0.4",
version: "0.0.5",
type: "action",
props: {
...common.props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
key: "sendgrid-get-a-global-suppression",
name: "Get A Global Suppression",
description: "Gets a global suppression. [See the docs here](https://docs.sendgrid.com/api-reference/suppressions-global-suppressions/retrieve-a-global-suppression)",
version: "0.0.4",
version: "0.0.5",
type: "action",
props: {
...common.props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
key: "sendgrid-get-all-bounces",
name: "Get All Bounces",
description: "Allows you to get all of your bounces. [See the docs here](https://docs.sendgrid.com/api-reference/bounces-api/retrieve-all-bounces)",
version: "0.0.4",
version: "0.0.5",
type: "action",
props: {
...common.props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "sendgrid-get-contact-lists",
name: "Get Contact Lists",
description: "Allows you to get details of your contact lists. [See the docs here](https://docs.sendgrid.com/api-reference/lists/get-all-lists)",
version: "0.0.4",
version: "0.0.5",
type: "action",
props: {
...common.props,
Expand Down
2 changes: 1 addition & 1 deletion components/sendgrid/actions/list-blocks/list-blocks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
key: "sendgrid-list-blocks",
name: "List Blocks",
description: "Allows you to list all email addresses that are currently on your blocks list. [See the docs here](https://docs.sendgrid.com/api-reference/blocks-api/retrieve-all-blocks)",
version: "0.0.4",
version: "0.0.5",
type: "action",
props: {
...common.props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
key: "sendgrid-list-global-suppressions",
name: "List Global Suppressions",
description: "Allows you to get a list of all email address that are globally suppressed. [See the docs here](https://docs.sendgrid.com/api-reference/suppressions-global-suppressions/retrieve-all-global-suppressions)",
version: "0.0.4",
version: "0.0.5",
type: "action",
props: {
...common.props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "sendgrid-remove-contact-from-list",
name: "Remove Contact From List",
description: "Allows you to remove contacts from a given list. [See the docs here](https://docs.sendgrid.com/api-reference/lists/remove-contacts-from-a-list)",
version: "0.0.4",
version: "0.0.5",
type: "action",
props: {
...common.props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
key: "sendgrid-search-contacts",
name: "Search Contacts",
description: "Searches contacts with a SGQL query. [See the docs here](https://docs.sendgrid.com/api-reference/contacts/search-contacts)",
version: "0.0.4",
version: "0.0.5",
type: "action",
props: {
...common.props,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { ConfigurationError } from "@pipedream/platform";
import fs from "fs";
import mime from "mime";
import validate from "validate.js";
import common from "../common/common.mjs";

Expand All @@ -6,7 +9,7 @@ export default {
key: "sendgrid-send-email-multiple-recipients",
name: "Send Email Multiple Recipients",
description: "This action sends a personalized e-mail to multiple specified recipients. [See the docs here](https://docs.sendgrid.com/api-reference/mail-send/mail-send)",
version: "0.0.5",
version: "0.0.6",
type: "action",
props: {
...common.props,
Expand Down Expand Up @@ -73,12 +76,6 @@ export default {
],
optional: true,
},
attachments: {
propDefinition: [
common.props.sendgrid,
"attachments",
],
},
headers: {
propDefinition: [
common.props.sendgrid,
Expand Down Expand Up @@ -139,19 +136,51 @@ export default {
"trackingSettings",
],
},
numberOfAttachments: {
propDefinition: [
common.props.sendgrid,
"numberOfAttachments",
],
optional: true,
},
},
async additionalProps() {
const props = {};
if (this.numberOfAttachments) {
for (let i = 1; i <= this.numberOfAttachments; i++) {
props[`attachmentsName${i}`] = {
type: "string",
label: `Attachment File Name ${i}`,
description: "The name of the file.",
optional: true,
};
props[`attachmentsPath${i}`] = {
type: "string",
label: `Attachment File Path ${i}`,
description: "The path to your file in /tmp dir. [See the documentation](https://pipedream.com/docs/code/nodejs/working-with-files/#writing-a-file-to-tmp) for how to work with tmp dir.",
optional: true,
};
}
}
return props;
},
async run({ $ }) {
if (!this.personalizations && !this.toEmails) {
throw new ConfigurationError("Please input either Personalization or Recipient Emails.");
}
const personalizations = this.personalizations || [];
if (personalizations.length == 0) {
for (const toEmail of this.toEmails) {
const personalization = {
to: [
{
email: toEmail,
},
],
};
personalizations.push(personalization);
if (this.convertEmptyStringToUndefined(this.toEmails)) {
for (const toEmail of this.toEmails) {
const personalization = {
to: [
{
email: toEmail,
},
],
};
personalizations.push(personalization);
}
}
}
if (this.dynamicTemplateData) {
Expand All @@ -177,23 +206,27 @@ export default {
email: true,
};
}
let attachments = this.convertEmptyStringToUndefined(this.attachments);
if (this.attachments) {
constraints.attachments = {
arrayValidator: {
value: this.attachments,
key: "attachments",
},
};
attachments = this.getArrayObject(this.attachments);
const attachments = [];
for (let i = 1; i <= this.numberOfAttachments; i++) {
const filepath = this.checkTmp(this["attachmentsPath" + i]);
const content = fs.readFileSync(filepath, {
encoding: "base64",
});
const type = mime.getType(filepath);
attachments.push({
content,
type,
filename: this[`attachmentsName${i}`],
});
}
Comment on lines +209 to 221
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance file error handling for attachments.

While the new attachment handling implementation is good, it lacks error checking for file existence or handling of missing paths.

Add error handling to prevent runtime errors when files don't exist:

const attachments = [];
for (let i = 1; i <= this.numberOfAttachments; i++) {
+  const pathKey = `attachmentsPath${i}`;
+  const nameKey = `attachmentsName${i}`;
+  
+  if (!this[pathKey]) {
+    continue; // Skip if no path provided
+  }
  
-  const filepath = this.checkTmp(this["attachmentsPath" + i]);
+  const filepath = this.checkTmp(this[pathKey]);
+  
+  try {
    const content = fs.readFileSync(filepath, {
      encoding: "base64",
    });
    const type = mime.getType(filepath);
    attachments.push({
      content,
      type,
-     filename: this[`attachmentsName${i}`],
+     filename: this[nameKey] || `attachment${i}`,
    });
+  } catch (error) {
+    throw new ConfigurationError(`Failed to read attachment file at ${filepath}: ${error.message}`);
+  }
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const attachments = [];
for (let i = 1; i <= this.numberOfAttachments; i++) {
const filepath = this.checkTmp(this["attachmentsPath" + i]);
const content = fs.readFileSync(filepath, {
encoding: "base64",
});
const type = mime.getType(filepath);
attachments.push({
content,
type,
filename: this[`attachmentsName${i}`],
});
}
const attachments = [];
for (let i = 1; i <= this.numberOfAttachments; i++) {
const pathKey = `attachmentsPath${i}`;
const nameKey = `attachmentsName${i}`;
if (!this[pathKey]) {
continue; // Skip if no path provided
}
const filepath = this.checkTmp(this[pathKey]);
try {
const content = fs.readFileSync(filepath, {
encoding: "base64",
});
const type = mime.getType(filepath);
attachments.push({
content,
type,
filename: this[nameKey] || `attachment${i}`,
});
} catch (error) {
throw new ConfigurationError(`Failed to read attachment file at ${filepath}: ${error.message}`);
}
}


if (this.categories) {
constraints.categories = {
type: "array",
};
}
this.sendAt = this.convertEmptyStringToUndefined(this.sendAt);
if (this.sendAt != null) {
this.sendAt = this.convertEmptyStringToUndefined(Date.parse(this.sendAt));
if (this.sendAt) {
constraints.sendAt = this.getIntegerGtZeroConstraint();
}
if (this.asm || this.asmGroupsToDisplay) {
Expand Down
Loading
Loading