UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

94 lines (93 loc) 4.68 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.notifyByEmail = notifyByEmail; const client_1 = require("@sendgrid/client"); const http_status_1 = require("http-status"); const util = require("util"); const factory = require("../../factory"); // type ILineNotifyOperation<T> = ( // repos: undefined, // credentials: { // lineNotify: LINENotifyCredentials; // } // ) => Promise<T>; // tslint:disable-next-line:max-func-body-length function notifyByEmail(params) { // tslint:disable-next-line:cyclomatic-complexity max-func-body-length return (__, credentials) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g; const { logLevel } = params; const shortSubject = params.subject; const message = util.format('\n%s\n%s\n%s\n%s\n%s\n\n%s', `[${logLevel}] ${shortSubject}`, `now:${(new Date()).toISOString()}`, `pid:${process.pid}`, `GAE_APPLICATION:${process.env.GAE_APPLICATION}`, // `GAE_INSTANCE:${process.env.GAE_INSTANCE}`, `GAE_SERVICE:${process.env.GAE_SERVICE}`, params.content); let result = {}; try { const apiKey = credentials.sendGrid.apiKey; if (typeof apiKey !== 'string') { throw new factory.errors.Internal('API Key not found'); } const senderName = (_b = (_a = credentials.sendGrid.alert) === null || _a === void 0 ? void 0 : _a.sender) === null || _b === void 0 ? void 0 : _b.name; const senderEmail = (_d = (_c = credentials.sendGrid.alert) === null || _c === void 0 ? void 0 : _c.sender) === null || _d === void 0 ? void 0 : _d.email; const toRecipientEmail = (Array.isArray((_e = credentials.sendGrid.alert) === null || _e === void 0 ? void 0 : _e.toRecipient)) ? (_g = (_f = credentials.sendGrid.alert) === null || _f === void 0 ? void 0 : _f.toRecipient.at(0)) === null || _g === void 0 ? void 0 : _g.email : undefined; if (typeof senderName !== 'string') { throw new factory.errors.Internal('senderName not found'); } if (typeof senderEmail !== 'string') { throw new factory.errors.Internal('senderEmail not found'); } if (typeof toRecipientEmail !== 'string') { throw new factory.errors.Internal('toRecipientEmail not found'); } const emailMessageFrom = { name: senderName, email: senderEmail }; const subject = `[${logLevel}] ${shortSubject}`; const emailDatas = [{ name: 'developers', email: toRecipientEmail }]; const sgClient = new client_1.Client(); sgClient.setApiKey(apiKey); // const response = await sgMail.sendMultiple(msg); const response = yield sgClient.request({ body: { content: [{ type: 'text/plain', value: message }], from: emailMessageFrom, personalizations: [{ to: emailDatas }], subject, custom_args: { emailMessage: subject // actionId: action.id, // projectId: project.id } }, method: 'POST', url: '/v3/mail/send' }); // check the response. if (response[0].statusCode !== http_status_1.ACCEPTED) { throw new factory.errors.Internal(`sendgrid request not accepted. response is ${util.inspect(response)}`); } const { statusCode } = response[0]; // const statusMessage = response[1]; result = { statusCode }; } catch (error) { throw error; } return { result }; }); }