ToolJet/plugins/packages/sendgrid/lib/index.ts
Maurits Lourens 60f515d19c
Feature/2395 - add eslint to plugins (#2402)
* merge develop

* Add eslint dependencies, configs and scripts to plugins project

* run lint with Github action

* ignore tests and dist folders

* fun eslint with --fix and manual fixes, renamed __tests_ to __tests__

* add plugins packages folder to lint-staged config

* fix lint issue
2022-03-10 12:29:48 +05:30

38 lines
1.2 KiB
TypeScript

import { QueryError, QueryResult, QueryService } from '@tooljet-plugins/common';
import sgMail from '@sendgrid/mail';
import { EmailOptions, SourceOptions, QueryOptions } from './types';
export default class SendGridQueryService implements QueryService {
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, 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', {});
}
sgMail.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 sgMail.send(sendgridEmailOptions);
} catch (error) {
console.log(error.response);
throw new QueryError('Query could not be completed', error.message, {});
}
return {
status: 'ok',
data: result,
};
}
}