Skip to content

Commit 8a3598f

Browse files
committed
some adjusts
1 parent 6eeefa1 commit 8a3598f

File tree

3 files changed

+81
-52
lines changed

3 files changed

+81
-52
lines changed

components/sendgrid/actions/send-email-multiple-recipients/send-email-multiple-recipients.mjs

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@ export default {
7474
],
7575
optional: true,
7676
},
77-
attachments: {
78-
propDefinition: [
79-
common.props.sendgrid,
80-
"attachments",
81-
],
82-
},
8377
headers: {
8478
propDefinition: [
8579
common.props.sendgrid,
@@ -140,6 +134,33 @@ export default {
140134
"trackingSettings",
141135
],
142136
},
137+
numberOfAttachments: {
138+
propDefinition: [
139+
common.props.sendgrid,
140+
"numberOfAttachments",
141+
],
142+
optional: true,
143+
},
144+
},
145+
async additionalProps() {
146+
const props = {};
147+
if (this.numberOfAttachments) {
148+
for (let i = 1; i <= this.numberOfAttachments; i++) {
149+
props[`attachmentsName${i}`] = {
150+
type: "string",
151+
label: `Attachment File Name ${i}`,
152+
description: "The name of the file.",
153+
optional: true,
154+
};
155+
props[`attachmentsPath${i}`] = {
156+
type: "string",
157+
label: `Attachment File Path ${i}`,
158+
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.",
159+
optional: true,
160+
};
161+
}
162+
}
163+
return props;
143164
},
144165
async run({ $ }) {
145166
const personalizations = this.personalizations || [];
@@ -178,26 +199,19 @@ export default {
178199
email: true,
179200
};
180201
}
181-
let attachments = this.convertEmptyStringToUndefined(this.attachments);
182-
if (this.attachments) {
183-
constraints.attachments = {
184-
arrayValidator: {
185-
value: this.attachments,
186-
key: "attachments",
187-
},
188-
};
189-
attachments = this.getArrayObject(this.attachments);
190-
attachments.map((attachment) => {
191-
if (attachment.filepath) {
192-
const filepath = this.checkTmp(attachment.filepath);
193-
attachment.content = fs.readFileSync(filepath, {
194-
encoding: "base64",
195-
});
196-
}
197-
delete attachment.filepath;
198-
return attachment;
202+
const attachments = [];
203+
for (let i = 1; i <= this.numberOfAttachments; i++) {
204+
const filepath = this.checkTmp(this["attachmentsPath" + i]);
205+
const content = fs.readFileSync(filepath, {
206+
encoding: "base64",
207+
});
208+
attachments.push({
209+
content,
210+
type: "text/plain",
211+
filename: this[`attachmentsName${i}`],
199212
});
200213
}
214+
201215
if (this.categories) {
202216
constraints.categories = {
203217
type: "array",

components/sendgrid/actions/send-email-single-recipient/send-email-single-recipient.mjs

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,6 @@ export default {
9797
],
9898
optional: true,
9999
},
100-
attachments: {
101-
propDefinition: [
102-
common.props.sendgrid,
103-
"attachments",
104-
],
105-
},
106100
headers: {
107101
propDefinition: [
108102
common.props.sendgrid,
@@ -163,6 +157,33 @@ export default {
163157
"trackingSettings",
164158
],
165159
},
160+
numberOfAttachments: {
161+
propDefinition: [
162+
common.props.sendgrid,
163+
"numberOfAttachments",
164+
],
165+
optional: true,
166+
},
167+
},
168+
async additionalProps() {
169+
const props = {};
170+
if (this.numberOfAttachments) {
171+
for (let i = 1; i <= this.numberOfAttachments; i++) {
172+
props[`attachmentsName${i}`] = {
173+
type: "string",
174+
label: `Attachment File Name ${i}`,
175+
description: "The name of the file.",
176+
optional: true,
177+
};
178+
props[`attachmentsPath${i}`] = {
179+
type: "string",
180+
label: `Attachment File Path ${i}`,
181+
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.",
182+
optional: true,
183+
};
184+
}
185+
}
186+
return props;
166187
},
167188
async run({ $ }) {
168189
//Performs validation on parameters.
@@ -203,26 +224,19 @@ export default {
203224
},
204225
};
205226
}
206-
let attachments = this.convertEmptyStringToUndefined(this.attachments);
207-
if (this.attachments) {
208-
constraints.attachments = {
209-
arrayValidator: {
210-
value: this.attachments,
211-
key: "attachments",
212-
},
213-
};
214-
attachments = this.getArrayObject(this.attachments);
215-
attachments.map((attachment) => {
216-
if (attachment.filepath) {
217-
const filepath = this.checkTmp(attachment.filepath);
218-
attachment.content = fs.readFileSync(filepath, {
219-
encoding: "base64",
220-
});
221-
}
222-
delete attachment.filepath;
223-
return attachment;
227+
const attachments = [];
228+
for (let i = 1; i <= this.numberOfAttachments; i++) {
229+
const filepath = this.checkTmp(this["attachmentsPath" + i]);
230+
const content = fs.readFileSync(filepath, {
231+
encoding: "base64",
232+
});
233+
attachments.push({
234+
content,
235+
type: "text/plain",
236+
filename: this[`attachmentsName${i}`],
224237
});
225238
}
239+
226240
if (this.categories) {
227241
constraints.categories = {
228242
type: "array",

components/sendgrid/sendgrid.app.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,11 @@ export default {
213213
label: "Content",
214214
description: "Content of the email in `text/html`",
215215
},
216-
attachments: {
217-
type: "string",
218-
label: "Attachments",
219-
description: "An array of objects where you can specify any attachments you want to include. The fields `content` and `filename` are required. `content` must be base64 encoded. Alternatively, provide a string that will `JSON.parse` to an array of attachments objects. Example: `[{content:\"aGV5\",filepath:\"/tmp/file.txt\",type:\"text/plain\",filename:\"sample.txt\"}]` `You must provide either content in base64 or path to the /temp file`",
216+
numberOfAttachments: {
217+
type: "integer",
218+
label: "Number Of Attachments",
219+
description: "The number of attachments to be sent with the email.",
220+
reloadProps: true,
220221
optional: true,
221222
},
222223
headers: {

0 commit comments

Comments
 (0)