ToolJet/server/plugins/datasources/sendgrid/index.ts
Arpit 8a89bc8b71
[Feature] : SendGrid integration (Datasource API) (#1608)
* SendGrid integration

* unused logs

* unused logs

* update help text

* fixestypos for schema description

* fixestypos for schema description

* adding sendgrid docs

* updates logo from  Sendgrid's media kit

* sendgrid query schema updated

* fixes typos

* updates docs

* typos fix

* fix jest module transform

Co-authored-by: Akshay Sasidharan <akshaysasidharan93@gmail.com>
2021-12-22 17:23:45 +05:30

42 lines
1.4 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { QueryError } from 'src/modules/data_sources/query.error';
import { QueryResult } from 'src/modules/data_sources/query_result.type';
import { QueryService } from 'src/modules/data_sources/query_service.interface';
import * as SendGrid from '@sendgrid/mail';
import { EmailOptions } from './sendgrid.interface';
@Injectable()
export default class SendGridQueryService implements QueryService {
async run(sourceOptions: any, queryOptions: any, dataSourceId: string): Promise<QueryResult> {
if (!(queryOptions && sourceOptions.api_key)) {
throw new QueryError('Query could not be completed as API key is not set', 'Missing API key', {});
}
SendGrid.setApiKey(sourceOptions.api_key);
let result = {};
const sendgridEmailOptions: EmailOptions = {
to: queryOptions.send_mail_to,
from: queryOptions.send_mail_from,
subject: queryOptions.subject,
text: queryOptions.text,
isMultiple: queryOptions.multiple_recipients ?? false,
};
if (queryOptions.html && queryOptions.html.length > 0) {
sendgridEmailOptions.html = queryOptions.html;
}
try {
result = await SendGrid.send(sendgridEmailOptions);
} catch (error) {
console.log(error.response);
throw new QueryError('Query could not be completed', error.message, {});
}
return {
status: 'ok',
data: result,
};
}
}