mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-04-06 15:30:30 +08:00
fix(graphql): SendGridMailSend to handle arrays, closes #582
This commit is contained in:
parent
41bb9eee43
commit
dc0ef6c521
@ -15,6 +15,7 @@
|
||||
*/
|
||||
|
||||
import sendgrid from '@sendgrid/mail';
|
||||
import { type } from '@lowdefy/helpers';
|
||||
import schema from './SendGridMailSendSchema.json';
|
||||
|
||||
// https://sendgrid.api-docs.io/v3.0/how-to-use-the-sendgrid-v3-api/api-authentication
|
||||
@ -22,41 +23,15 @@ import schema from './SendGridMailSendSchema.json';
|
||||
|
||||
async function sendGridMailSend({ request, connection }) {
|
||||
const { apiKey, from, templateId, mailSettings } = connection;
|
||||
const {
|
||||
to,
|
||||
cc,
|
||||
bcc,
|
||||
replyTo,
|
||||
subject,
|
||||
text,
|
||||
html,
|
||||
dynamicTemplateData,
|
||||
attachments,
|
||||
categories,
|
||||
sendAt,
|
||||
batchId,
|
||||
} = request;
|
||||
|
||||
sendgrid.setApiKey(apiKey);
|
||||
const msg = {
|
||||
to,
|
||||
cc,
|
||||
bcc,
|
||||
const messages = (type.isArray(request) ? request : [request]).map((msg) => ({
|
||||
...msg,
|
||||
from,
|
||||
replyTo,
|
||||
subject,
|
||||
text,
|
||||
html,
|
||||
templateId,
|
||||
dynamicTemplateData,
|
||||
attachments,
|
||||
categories,
|
||||
sendAt,
|
||||
batchId,
|
||||
mailSettings,
|
||||
};
|
||||
}));
|
||||
try {
|
||||
await sendgrid.send(msg);
|
||||
await sendgrid.send(messages);
|
||||
} catch (error) {
|
||||
if (error.response) {
|
||||
throw new Error(JSON.stringify(error.response.body));
|
||||
|
@ -19,18 +19,21 @@ import SendGridMailSend from './SendGridMailSend';
|
||||
|
||||
const { resolver, schema } = SendGridMailSend;
|
||||
|
||||
const mockSend = jest.fn();
|
||||
|
||||
jest.mock('@sendgrid/mail', () => {
|
||||
return {
|
||||
setApiKey: jest.fn(),
|
||||
send: (msg) => {
|
||||
if (msg.to === 'response_error') {
|
||||
if (msg[0].to === 'response_error') {
|
||||
const error = new Error('Test error.');
|
||||
error.response = { body: ['Test error 1.', 'Test error 2.'] };
|
||||
throw error;
|
||||
}
|
||||
if (msg.to === 'generic_error') {
|
||||
if (msg[0].to === 'generic_error') {
|
||||
throw new Error('Test error.');
|
||||
}
|
||||
mockSend(msg);
|
||||
return Promise.resolve(msg);
|
||||
},
|
||||
};
|
||||
@ -52,6 +55,20 @@ test('send with valid request and connection', async () => {
|
||||
from: { name: 'a@b.om', email: 'a.cc@mm.co' },
|
||||
};
|
||||
const send = await resolver({ request, connection });
|
||||
expect(mockSend.mock.calls).toEqual([
|
||||
[
|
||||
[
|
||||
{
|
||||
from: { email: 'a.cc@mm.co', name: 'a@b.om' },
|
||||
mailSettings: undefined,
|
||||
subject: 'A',
|
||||
templateId: undefined,
|
||||
text: 'B',
|
||||
to: 'a@b.com',
|
||||
},
|
||||
],
|
||||
],
|
||||
]);
|
||||
expect(send).toEqual({
|
||||
response: 'Mail sent successfully',
|
||||
});
|
||||
@ -68,6 +85,20 @@ test('send to list of emails', async () => {
|
||||
from: 'x@y.com',
|
||||
};
|
||||
const send = await resolver({ request, connection });
|
||||
expect(mockSend.mock.calls).toEqual([
|
||||
[
|
||||
[
|
||||
{
|
||||
from: 'x@y.com',
|
||||
mailSettings: undefined,
|
||||
subject: 'A',
|
||||
templateId: undefined,
|
||||
text: 'B',
|
||||
to: ['a@b.com', 'aaa bbb <aaa@bbb.com>', { email: 'ddd@eee.com', name: 'ccc' }],
|
||||
},
|
||||
],
|
||||
],
|
||||
]);
|
||||
expect(send).toEqual({
|
||||
response: 'Mail sent successfully',
|
||||
});
|
||||
@ -91,6 +122,28 @@ test('send a list of different emails', async () => {
|
||||
from: 'x@y.com',
|
||||
};
|
||||
const send = await resolver({ request, connection });
|
||||
expect(mockSend.mock.calls).toEqual([
|
||||
[
|
||||
[
|
||||
{
|
||||
from: 'x@y.com',
|
||||
mailSettings: undefined,
|
||||
subject: 'A',
|
||||
templateId: undefined,
|
||||
text: 'A',
|
||||
to: 'a@b.com',
|
||||
},
|
||||
{
|
||||
from: 'x@y.com',
|
||||
mailSettings: undefined,
|
||||
subject: 'B',
|
||||
templateId: undefined,
|
||||
text: 'B',
|
||||
to: 'b@b.com',
|
||||
},
|
||||
],
|
||||
],
|
||||
]);
|
||||
expect(send).toEqual({
|
||||
response: 'Mail sent successfully',
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user