mirror of
https://github.com/ToolJet/ToolJet
synced 2026-04-23 22:47:28 +00:00
* 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
38 lines
1.2 KiB
TypeScript
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,
|
|
};
|
|
}
|
|
}
|