UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

80 lines (79 loc) 3.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.registerService = registerService; /** * プロダクトサービス */ const moment = require("moment"); const factory = require("../factory"); const permit_1 = require("./permit"); function registerService(params) { return (repos) => __awaiter(this, void 0, void 0, function* () { yield Promise.all(params.map((actionAttributes) => __awaiter(this, void 0, void 0, function* () { let serviceOutput = actionAttributes.object; const action = yield repos.action.start(actionAttributes); try { // Permitに有効期間を設定する serviceOutput = yield (0, permit_1.activate)(Object.assign({ typeOf: serviceOutput.typeOf, identifier: String(serviceOutput.identifier), validFrom: moment(serviceOutput.validFrom) .toDate() }, (serviceOutput.validUntil !== undefined && serviceOutput.validUntil !== null) ? { validUntil: moment(serviceOutput.validUntil) .toDate() } : undefined))({ serviceOutput: repos.serviceOutput }); } catch (error) { try { yield repos.action.giveUp({ typeOf: action.typeOf, id: action.id, error }); } catch (__) { // 失敗したら仕方ない } throw error; } // アクション完了 const actionResult = {}; yield repos.action.completeWithVoid({ typeOf: action.typeOf, id: action.id, result: actionResult }); yield onRegistered(actionAttributes, serviceOutput)(repos); }))); }); } /** * サービス登録後アクション */ function onRegistered(actionAttributes, __) { return (repos) => __awaiter(this, void 0, void 0, function* () { const potentialActions = actionAttributes.potentialActions; const now = new Date(); const taskAttributes = []; // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (potentialActions !== undefined) { if (Array.isArray(potentialActions.moneyTransfer)) { taskAttributes.push(...potentialActions.moneyTransfer.map((a) => { return { project: a.project, name: factory.taskName.MoneyTransfer, status: factory.taskStatus.Ready, runsAt: now, remainingNumberOfTries: 10, numberOfTried: 0, executionResults: [], data: a }; })); } } // タスク保管 yield repos.task.saveMany(taskAttributes, { emitImmediately: true }); }); }