UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

176 lines (175 loc) 9.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createRegisterServiceStartParams = createRegisterServiceStartParams; exports.createActionAttributes = createActionAttributes; exports.acceptedOffers2amount = acceptedOffers2amount; exports.responseBody2resultAcceptedOffer = responseBody2resultAcceptedOffer; exports.createResult = createResult; const moment = require("moment"); const factory = require("../../../factory"); function createRegisterServiceStartParams(params) { var _a; return { project: { typeOf: params.project.typeOf, id: params.project.id }, typeOf: factory.assetTransactionType.RegisterService, transactionNumber: params.transactionNumber, agent: { typeOf: params.transaction.seller.typeOf, id: params.transaction.seller.id, name: (typeof params.transaction.seller.name === 'string') ? params.transaction.seller.name : String((_a = params.transaction.seller.name) === null || _a === void 0 ? void 0 : _a.ja) // identifierを廃止(2024-03-05~) // identifier: [ // { name: 'transaction', value: params.transaction.id }, // { // name: 'transactionExpires', // value: moment(params.transaction.expires) // .toISOString() // } // ] }, object: params.object.map((o) => { // 許可証コードは自動発行されているはず const permitIdentifier = o.itemOffered.serviceOutput.identifier; if (typeof permitIdentifier !== 'string' || permitIdentifier === '') { throw new factory.errors.ArgumentNull('itemOffered.serviceOutput.identifier'); } return { typeOf: o.typeOf, id: String(o.id), itemOffered: Object.assign(Object.assign({}, o.itemOffered), { serviceOutput: Object.assign(Object.assign({}, o.itemOffered.serviceOutput), { identifier: permitIdentifier }) }) }; }), expires: moment(params.transaction.expires) .add(1, 'day') .toDate() // 余裕を持って }; } function createActionAttributes(params) { var _a; const transaction = params.transaction; return { project: transaction.project, typeOf: factory.actionType.AuthorizeAction, instrument: { typeOf: factory.assetTransactionType.RegisterService, transactionNumber: params.transactionNumber }, object: params.acceptedOffer, agent: { id: transaction.seller.id, typeOf: transaction.seller.typeOf, name: (typeof transaction.seller.name === 'string') ? transaction.seller.name : String((_a = transaction.seller.name) === null || _a === void 0 ? void 0 : _a.ja) }, recipient: { id: transaction.agent.id, typeOf: transaction.agent.typeOf }, purpose: { typeOf: transaction.typeOf, id: transaction.id } }; } function acceptedOffers2amount(params) { const acceptedOffers = params.acceptedOffers; // 金額計算 return acceptedOffers.reduce((a, b) => { if (b.priceSpecification === undefined || b.priceSpecification === null) { throw new factory.errors.Internal('price specification of result accepted offer undefined'); } if (b.priceSpecification.typeOf !== factory.priceSpecificationType.CompoundPriceSpecification) { throw new factory.errors.Internal('price specification of result accepted offer must be CompoundPriceSpecification'); } const priceSpecification = b.priceSpecification; return a + priceSpecification.priceComponent.reduce((a2, b2) => a2 + Number(b2.price), 0); }, 0); } // tslint:disable-next-line:max-func-body-length function responseBody2resultAcceptedOffer(params) { let acceptedOffers = []; if (Array.isArray(params.responseBody.object)) { acceptedOffers = params.responseBody.object.map((responseBodyObject) => { var _a, _b, _c, _d; const serviceOutputIdentifier = (_b = (_a = responseBodyObject.itemOffered) === null || _a === void 0 ? void 0 : _a.serviceOutput) === null || _b === void 0 ? void 0 : _b.identifier; if (serviceOutputIdentifier !== undefined && typeof serviceOutputIdentifier !== 'string') { // 基本的にありえないフロー↓ throw new factory.errors.Internal(`invalid type of serviceOutput.identifier: ${typeof serviceOutputIdentifier}`); } // itemOfferedを最小限に最適化 const permitByResponse = responseBodyObject.itemOffered.serviceOutput; const itemOffered = Object.assign(Object.assign({ // 不要なので廃止(2023-07-01~) // project: permitByResponse.project, typeOf: permitByResponse.typeOf, identifier: permitByResponse.identifier, issuedThrough: permitByResponse.issuedThrough, name: permitByResponse.name }, (typeof permitByResponse.validFor === 'string') ? { validFor: permitByResponse.validFor } : undefined), (typeof ((_c = permitByResponse.amount) === null || _c === void 0 ? void 0 : _c.typeOf) === 'string') ? { amount: permitByResponse.amount } : undefined); const offer = params.acceptedOffer.find((o) => o.id === responseBodyObject.id); if (offer === undefined) { throw new factory.errors.Internal(`Offer ${responseBodyObject.id} from registerService not found`); } if (typeof ((_d = offer.priceSpecification) === null || _d === void 0 ? void 0 : _d.typeOf) !== 'string') { throw new factory.errors.NotFound('acceptedOffer.priceSpecification'); } const priceComponent = []; offer.priceSpecification.priceComponent.forEach((component) => { var _a, _b; if (typeof component.price === 'number') { if (component.typeOf === factory.priceSpecificationType.UnitPriceSpecification) { // tslint:disable-next-line:no-unnecessary-local-variable const unitPriceComponent = Object.assign(Object.assign(Object.assign({ accounting: component.accounting, name: component.name, price: component.price, priceCurrency: component.priceCurrency, referenceQuantity: component.referenceQuantity, typeOf: component.typeOf, valueAddedTaxIncluded: component.valueAddedTaxIncluded }, (Array.isArray(component.appliesToAddOn)) ? { appliesToAddOn: component.appliesToAddOn } : undefined), (Array.isArray(component.appliesToMovieTicket)) ? { appliesToMovieTicket: component.appliesToMovieTicket } : undefined), (typeof ((_a = component.accounting) === null || _a === void 0 ? void 0 : _a.typeOf) === 'string') ? { accounting: Object.assign(Object.assign({ typeOf: component.accounting.typeOf }, (typeof component.accounting.accountsReceivable === 'number') ? { accountsReceivable: component.accounting.accountsReceivable } : undefined), (typeof ((_b = component.accounting.operatingRevenue) === null || _b === void 0 ? void 0 : _b.typeOf) === 'string') ? { operatingRevenue: { // project: component.accounting.operatingRevenue.project, // 削除(2023-12-13~) typeOf: component.accounting.operatingRevenue.typeOf, codeValue: component.accounting.operatingRevenue.codeValue } } : undefined) } : undefined); priceComponent.push(unitPriceComponent); } else { priceComponent.push(component); } } }); const priceSpecification = { typeOf: offer.priceSpecification.typeOf, valueAddedTaxIncluded: offer.priceSpecification.valueAddedTaxIncluded, priceCurrency: offer.priceSpecification.priceCurrency, priceComponent }; // tslint:disable-next-line:no-unnecessary-local-variable const permitAcceptedOffer = { typeOf: responseBodyObject.typeOf, id: offer.id, name: offer.name, itemOffered, priceSpecification, serialNumber: params.responseBody.transactionNumber }; return permitAcceptedOffer; }); } return acceptedOffers; } function createResult(params) { const acceptedOffers4result = responseBody2resultAcceptedOffer(params); // 金額計算 const amount = acceptedOffers2amount({ acceptedOffers: acceptedOffers4result }); return { price: amount, priceCurrency: factory.priceCurrency.JPY, // acceptedOffers: acceptedOffers4result, // discontinue(2024-06-17~) requestBody: params.requestBody, responseBody: params.responseBody }; }