@chevre/domain
Version:
Chevre Domain Library for Node.js
73 lines (72 loc) • 3.75 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.createProductOwnershipInfo = createProductOwnershipInfo;
const moment = require("moment");
const factory = require("../../../factory");
const MAX_OWNED_THROUGH = '3000-01-01T00:00:00Z';
function createProductOwnershipInfo(params) {
var _a, _b;
let ownershipInfo;
let ownedThrough;
// どういう期間でいくらのオファーなのか
const priceSpec = params.acceptedOffer.priceSpecification;
if (priceSpec === undefined) {
throw new factory.errors.NotFound('Order.acceptedOffers.priceSpecification');
}
const unitPriceSpec = priceSpec.priceComponent.find((p) => p.typeOf === factory.priceSpecificationType.UnitPriceSpecification);
if (unitPriceSpec === undefined) {
throw new factory.errors.NotFound('Unit Price Specification in Order.acceptedOffers.priceSpecification');
}
// unitPriceSpec.referenceQuantity.value: 'Infinity'に対応
const referenceQuantityValue = unitPriceSpec.referenceQuantity.value;
if (referenceQuantityValue === factory.quantitativeValue.StringValue.Infinity) {
// Infinityの場合ownedThroughは固定値
ownedThrough = moment(MAX_OWNED_THROUGH)
.toDate();
}
else if (typeof referenceQuantityValue === 'number') {
switch (unitPriceSpec.referenceQuantity.unitCode) {
case factory.unitCode.Ann:
ownedThrough = moment(params.ownedFrom)
.add(referenceQuantityValue, 'years')
.toDate();
break;
case factory.unitCode.Day:
ownedThrough = moment(params.ownedFrom)
.add(referenceQuantityValue, 'days')
.toDate();
break;
case factory.unitCode.Sec:
ownedThrough = moment(params.ownedFrom)
.add(referenceQuantityValue, 'seconds')
.toDate();
break;
default:
throw new factory.errors.NotImplemented(`Reference quantity unit code '${unitPriceSpec.referenceQuantity.unitCode}' not implemented`);
}
}
else {
throw new factory.errors.NotFound('Order.acceptedOffers.priceSpecification.referenceQuantity.value');
}
const itemOffered = params.acceptedOffer.itemOffered;
// 最適化(2023-02-01~)
const issuedThrough = (typeof ((_a = itemOffered.issuedThrough) === null || _a === void 0 ? void 0 : _a.typeOf) === 'string')
? Object.assign({ id: itemOffered.issuedThrough.id, typeOf: itemOffered.issuedThrough.typeOf }, (typeof ((_b = itemOffered.issuedThrough.serviceType) === null || _b === void 0 ? void 0 : _b.typeOf) === 'string')
? { serviceType: itemOffered.issuedThrough.serviceType }
: undefined) : undefined;
const typeOfGood = Object.assign(Object.assign(Object.assign({
// project: itemOffered.project, // 最適化(2023-02-01~)
identifier: itemOffered.identifier, typeOf: itemOffered.typeOf }, (typeof (issuedThrough === null || issuedThrough === void 0 ? void 0 : issuedThrough.typeOf) === 'string') ? { issuedThrough } : undefined), (itemOffered.validFor !== undefined) ? { validFor: itemOffered.validFor } : undefined), (itemOffered.name !== undefined) ? { name: itemOffered.name } : undefined);
ownershipInfo = {
project: params.project,
typeOf: 'OwnershipInfo',
id: '',
identifier: params.identifier,
ownedBy: params.ownedBy,
acquiredFrom: params.acquiredFrom,
ownedFrom: params.ownedFrom,
ownedThrough: ownedThrough,
typeOfGood: typeOfGood
};
return ownershipInfo;
}
;