UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

97 lines (96 loc) 4.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ABORT_REPORT_SUBJECT = void 0; exports.task2lineNotify = task2lineNotify; exports.tasks2lineNotify = tasks2lineNotify; exports.createSendEmailMessageRecipe = createSendEmailMessageRecipe; const moment = require("moment"); const factory = require("../../factory"); exports.ABORT_REPORT_SUBJECT = 'tasks aborted'; function task2lineNotify(params) { var _a; const task = params.task; const lastExecutionResult = (task.executionResults.length > 0) ? task.executionResults.slice(-1)[0] : undefined; let lastError = lastExecutionResult === null || lastExecutionResult === void 0 ? void 0 : lastExecutionResult.error; if (typeof lastError === 'string') { lastError = { message: lastError, name: 'Error' }; } const lastMessage = `${String(lastError === null || lastError === void 0 ? void 0 : lastError.name)} ${String(lastError === null || lastError === void 0 ? void 0 : lastError.message)}`; const content = `project:${(_a = task.project) === null || _a === void 0 ? void 0 : _a.id} id:${task.id} name:${task.name} runsAt:${moment(task.runsAt) .toISOString()} lastTriedAt:${moment(task.lastTriedAt) .toISOString()} numberOfTried:${task.numberOfTried} lastMessage:${lastMessage}`; return { subject: exports.ABORT_REPORT_SUBJECT, content: content }; } const MAX_LAST_MESSAGE_LENGTH = 60; function tasks2lineNotify(params) { const subject = `${params.tasks.length} ${exports.ABORT_REPORT_SUBJECT}`; let content = subject; // あまりに通知タスク数が多いケースに対応 // tslint:disable-next-line:no-magic-numbers if (params.tasks.length > 10) { content = params.tasks.map((task) => { return `* project:${task.project.id} ${task.id} ${task.name}`; }) .join('\n'); } else { // tslint:disable-next-line:no-magic-numbers const maxLastMessageLength = (params.tasks.length <= 5) ? 150 : MAX_LAST_MESSAGE_LENGTH; // 通知タスク数に応じてメッセージ長を調整 content = params.tasks.map((task) => { const lastExecutionResult = (task.executionResults.length > 0) ? task.executionResults.slice(-1)[0] : undefined; let lastError = lastExecutionResult === null || lastExecutionResult === void 0 ? void 0 : lastExecutionResult.error; if (typeof lastError === 'string') { lastError = { message: lastError, name: 'Error' }; } let lastMessage = String(lastError === null || lastError === void 0 ? void 0 : lastError.name); if (typeof (lastError === null || lastError === void 0 ? void 0 : lastError.message) === 'string' && lastError.message.length > 0) { lastMessage = lastError.message; } const abbreviatedMessage = (lastMessage.length > maxLastMessageLength) ? `${lastMessage.slice(0, maxLastMessageLength)}...` : lastMessage; return `* project:${task.project.id} ${task.id} ${task.name} ${moment(task.runsAt) .toISOString()} ${abbreviatedMessage}`; }) .join('\n'); } return { subject, content }; } function createSendEmailMessageRecipe(params) { const { mailData, afterMedia, project } = params; let beforeMedia; if (mailData !== undefined) { const { to, from, customArgs } = mailData; beforeMedia = { to, from, customArgs }; } return { project: { id: project.id, typeOf: factory.organizationType.Project }, typeOf: 'Recipe', recipeCategory: factory.recipe.RecipeCategory.sendEmailMessage, step: [{ typeOf: 'HowToSection', itemListElement: [ { typeOf: 'HowToStep', identifier: factory.recipe.StepIdentifier.sendMultiple, itemListElement: [Object.assign(Object.assign({ typeOf: 'HowToDirection' }, (beforeMedia !== undefined) ? { beforeMedia } : undefined), (afterMedia !== undefined) ? { afterMedia } : undefined)] } ] }] }; }