@chevre/domain
Version:
Chevre Domain Library for Node.js
123 lines (122 loc) • 6.96 kB
JavaScript
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.issue = issue;
exports.activate = activate;
exports.search = search;
/**
* 許可証サービス
*/
const factory = require("../factory");
const AccountService = require("./account");
/**
* 許可証発行
*/
function issue(params) {
return (repos) => __awaiter(this, void 0, void 0, function* () {
switch (params.issuedThrough.typeOf) {
case factory.product.ProductType.PaymentCard:
// 口座開設
const openAccountParams = params.serviceOutputs.map((serviceOutput) => {
var _a, _b, _c, _d;
const accountTypeOf = (_a = serviceOutput === null || serviceOutput === void 0 ? void 0 : serviceOutput.paymentAccount) === null || _a === void 0 ? void 0 : _a.typeOf;
const accountNumber = (_b = serviceOutput === null || serviceOutput === void 0 ? void 0 : serviceOutput.paymentAccount) === null || _b === void 0 ? void 0 : _b.accountNumber;
const serviceOutputName = serviceOutput === null || serviceOutput === void 0 ? void 0 : serviceOutput.name;
const accountType = (_c = serviceOutput === null || serviceOutput === void 0 ? void 0 : serviceOutput.amount) === null || _c === void 0 ? void 0 : _c.currency;
const initialBalance = (_d = serviceOutput === null || serviceOutput === void 0 ? void 0 : serviceOutput.amount) === null || _d === void 0 ? void 0 : _d.value;
if (typeof accountTypeOf !== 'string' || accountTypeOf.length === 0) {
throw new factory.errors.Internal('serviceOutput.paymentAccount.typeOf undefined');
}
if (typeof accountNumber !== 'string' || accountNumber.length === 0) {
throw new factory.errors.Internal('serviceOutput.paymentAccount.accountNumber undefined');
}
if (typeof accountType !== 'string' || accountType.length === 0) {
throw new factory.errors.Internal('Account currency undefined');
}
return Object.assign({ project: { id: params.project.id }, accountType: accountType, accountNumber, name: (typeof serviceOutputName === 'string') ? serviceOutputName : accountNumber, typeOf: accountTypeOf }, (typeof initialBalance === 'number') ? { initialBalance } : undefined);
});
yield AccountService.open(openAccountParams.map((bodyParams) => {
return {
project: { id: bodyParams.project.id },
typeOf: bodyParams.typeOf,
accountType: bodyParams.accountType,
accountNumber: bodyParams.accountNumber,
name: bodyParams.name,
initialBalance: (typeof bodyParams.initialBalance === 'number') ? Number(bodyParams.initialBalance) : 0
};
}))({
account: repos.account
});
break;
default:
// no op
}
// サービスアウトプット保管
yield repos.serviceOutput.issue(params.serviceOutputs);
});
}
/**
* 許可証有効化
*/
function activate(params) {
return (repos) => __awaiter(this, void 0, void 0, function* () {
// Permitに有効期間を設定する
return repos.serviceOutput.activate(params);
});
}
/**
* プロジェクト指定で、許可証を検索する(ペイメントカードプロダクトの場合、口座詳細込み)
*/
function search(project, params, projection) {
return (repos) => __awaiter(this, void 0, void 0, function* () {
var _a, _b;
let permitsWithAccountDetail;
// アウトプット検索条件のプロジェクトは強制的に上書き
const searchConditions = Object.assign(Object.assign({}, params), { project: { id: { $eq: project.id } } });
const productType = (_b = (_a = params.issuedThrough) === null || _a === void 0 ? void 0 : _a.typeOf) === null || _b === void 0 ? void 0 : _b.$eq;
switch (productType) {
case factory.product.ProductType.PaymentCard:
const permits = yield repos.serviceOutput.search(searchConditions, projection);
permitsWithAccountDetail = permits;
// 口座詳細取得
const accountNumbers = [];
permits.forEach((permit) => {
var _a;
const accountNumber = (_a = permit.paymentAccount) === null || _a === void 0 ? void 0 : _a.accountNumber;
if (typeof accountNumber === 'string') {
accountNumbers.push(accountNumber);
}
});
if (accountNumbers.length > 0) {
const accounts = yield repos.account.search({
project: { id: { $eq: project.id } },
accountNumbers: accountNumbers
});
permitsWithAccountDetail = permits.map((permit) => {
const account = accounts.find((a) => { var _a; return a.accountNumber === ((_a = permit.paymentAccount) === null || _a === void 0 ? void 0 : _a.accountNumber); });
const paymentAccount = (typeof (account === null || account === void 0 ? void 0 : account.typeOf) === 'string')
? {
accountNumber: account.accountNumber,
availableBalance: account.availableBalance,
balance: account.balance,
typeOf: account.typeOf
}
: undefined;
return Object.assign(Object.assign({}, permit), { paymentAccount });
});
}
break;
default:
permitsWithAccountDetail = (yield repos.serviceOutput.search(searchConditions, projection));
}
return permitsWithAccountDetail;
});
}
;