UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

105 lines (104 loc) 6.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createPlacingOrder = createPlacingOrder; exports.createSeller = createSeller; exports.createPaymentMethods = createPaymentMethods; const moment = require("moment"); const factory = require("../../../factory"); const orderedItem_1 = require("./factory/orderedItem"); function createPaymentMethods(params) { const paymentMethods = []; let price = 0; // 決済方法をセット params.authorizePaymentActions.forEach((a) => { var _a, _b, _c; const resultAsInvoice = (Array.isArray(a.result)) // ? a.result?.find(({ typeOf }) => typeOf === factory.action.authorize.paymentMethod.any.ResultType.Payment) ? a.result[0] : undefined; if (resultAsInvoice === undefined) { throw new factory.errors.NotFound('authorizePaymentAction.resultAsInvoice'); // resultは必ず存在するはず } const paymentMethodAmountCurrencyByAuthorizeAction = (_b = (_a = resultAsInvoice.paymentMethodAsObject) === null || _a === void 0 ? void 0 : _a.amount) === null || _b === void 0 ? void 0 : _b.currency; // 決済方法区分は必ず存在するはず(2023-08-15~) const paymentMethodType = (_c = resultAsInvoice.paymentMethodAsObject) === null || _c === void 0 ? void 0 : _c.typeOf; if (typeof paymentMethodType !== 'string') { throw new factory.errors.NotFound('authorizePaymentAction.result.paymentMethodAsObject.typeOf'); } const paymentMethodOfInvoice = Object.assign({ identifier: paymentMethodType }, (typeof paymentMethodAmountCurrencyByAuthorizeAction === 'string') ? { amount: { currency: paymentMethodAmountCurrencyByAuthorizeAction } } // CreditCardIFのカード通貨区分を追加(2023-08-15~) : undefined); paymentMethods.push(Object.assign({ accountId: resultAsInvoice.accountId, additionalProperty: (Array.isArray(resultAsInvoice.additionalProperty)) ? resultAsInvoice.additionalProperty : [], issuedThrough: resultAsInvoice.issuedThrough, name: resultAsInvoice.name, paymentMethodId: resultAsInvoice.paymentMethodId, paymentStatus: resultAsInvoice.paymentStatus, paymentMethod: paymentMethodOfInvoice }, (resultAsInvoice.totalPaymentDue !== undefined) ? { totalPaymentDue: resultAsInvoice.totalPaymentDue } : undefined // ...(USE_ORDER_PAYMENT_METHOD_TYPE_OF) // 完全廃止(2024-01-17~) // ? { typeOf: paymentMethodType } // : undefined )); }); // 決済方法から注文金額の計算 price += params.authorizePaymentActions .reduce((a, b) => { var _a; const resultAsInvoice = (Array.isArray(b.result)) // ? b.result?.find(({ typeOf }) => typeOf === factory.action.authorize.paymentMethod.any.ResultType.Payment) ? b.result[0] : undefined; const jpyAmount = (((_a = resultAsInvoice === null || resultAsInvoice === void 0 ? void 0 : resultAsInvoice.totalPaymentDue) === null || _a === void 0 ? void 0 : _a.currency) === factory.priceCurrency.JPY) ? resultAsInvoice.totalPaymentDue.value : 0; return a + jpyAmount; }, 0); return { paymentMethods, price }; } function createSeller(params) { var _a; const seller = params.transaction.seller; return { id: seller.id, name: (typeof seller.name === 'string') ? seller.name : String((_a = seller.name) === null || _a === void 0 ? void 0 : _a.ja), typeOf: seller.typeOf, // 追加特性を追加(2023-08-08~) additionalProperty: (Array.isArray(seller.additionalProperty)) ? seller.additionalProperty : [] }; } function createPlacingOrder(params) { var _a, _b; const { transaction, authorizePaymentActions } = params; const orderByTransaction = (_a = transaction.result) === null || _a === void 0 ? void 0 : _a.order; if (orderByTransaction === undefined) { throw new factory.errors.NotFound('transaction.result.order'); } const seller = createSeller({ transaction }); const name = (typeof transaction.object.name === 'string') ? transaction.object.name : undefined; const broker = (typeof ((_b = transaction.object.broker) === null || _b === void 0 ? void 0 : _b.typeOf) === 'string') ? transaction.object.broker : undefined; const { paymentMethods, price } = createPaymentMethods({ authorizePaymentActions }); const eventReservationAcceptedOffers = []; const productAcceptedOffers = []; const moneyTransferAcceptedOffers = []; params.acceptedOffers.forEach((acceptedOffer) => { const itemOfferedTypeOf = acceptedOffer.itemOffered.typeOf; switch (itemOfferedTypeOf) { case factory.reservationType.BusReservation: case factory.reservationType.EventReservation: eventReservationAcceptedOffers.push(acceptedOffer); break; case factory.permit.PermitType.Permit: productAcceptedOffers.push(acceptedOffer); break; case factory.actionType.MoneyTransfer: moneyTransferAcceptedOffers.push(acceptedOffer); break; default: throw new factory.errors.NotImplemented(`${itemOfferedTypeOf} not implemented`); } }); const orderedItem = (0, orderedItem_1.acceptedOffers2orderedItem)({ eventReservationAcceptedOffers, productAcceptedOffers, moneyTransferAcceptedOffers }); return Object.assign(Object.assign(Object.assign(Object.assign({}, orderByTransaction), { seller, // 2024-06-17~ paymentMethods, // 2024-06-17~ price, orderDate: moment(orderByTransaction.orderDate) .toDate(), orderedItem }), (typeof name === 'string') ? { name } : undefined), (typeof (broker === null || broker === void 0 ? void 0 : broker.typeOf) === 'string') ? { broker } : undefined // 2024-06-17~ ); }