UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

130 lines (129 loc) 7.2 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.searchOfferCatalogItemAvailability = searchOfferCatalogItemAvailability; const factory = require("../../../factory"); function getUnacceptedPaymentMethodByEvent(params) { var _a; let unacceptedPaymentMethod = []; const unacceptedPaymentMethodByEvent = (_a = params.event.offers) === null || _a === void 0 ? void 0 : _a.unacceptedPaymentMethod; // 施設コンテンツを参照する必要はない(2022-10-31~) if (Array.isArray(unacceptedPaymentMethodByEvent)) { unacceptedPaymentMethod = unacceptedPaymentMethodByEvent; } return unacceptedPaymentMethod; } const MAX_LIMIT_SEARCH_OFFER_CATALOG_ITEMS = 10; /** * サブカタログ利用可能性検索 */ // tslint:disable-next-line:max-func-body-length function searchOfferCatalogItemAvailability(params) { // tslint:disable-next-line:max-func-body-length return (repos) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; const { considerUnacceptedPaymentMethod, useIncludeInDataCatalog } = params.options; if (typeof params.limit !== 'number') { throw new factory.errors.Argument('limit', 'must be number'); } if (typeof params.page !== 'number') { throw new factory.errors.Argument('page', 'must be number'); } // オファー検索時の_id条件がitemListElementCount * MAX_LIMIT_SEARCH_OFFER_CATALOG_ITEMS以下になるように制限 if (params.limit > MAX_LIMIT_SEARCH_OFFER_CATALOG_ITEMS) { throw new factory.errors.Argument('limit', `must be <=${MAX_LIMIT_SEARCH_OFFER_CATALOG_ITEMS}`); } let availabilities = []; // optimize(2024-07-18~) const event = yield repos.event.projectEventFieldsById({ id: params.event.id }, ['project', 'offers.itemOffered.id', 'offers.unacceptedPaymentMethod']); // 興行設定があれば興行のカタログを参照する const eventOffers = event.offers; let catalogId; if (typeof ((_a = eventOffers.itemOffered) === null || _a === void 0 ? void 0 : _a.id) === 'string') { const eventService = (yield repos.product.projectFields({ limit: 1, page: 1, id: { $eq: eventOffers.itemOffered.id } }, ['hasOfferCatalog'] // [] )).shift(); if (eventService === undefined) { throw new factory.errors.NotFound(factory.product.ProductType.EventService); } const productCatalogs = (_b = eventService.hasOfferCatalog) === null || _b === void 0 ? void 0 : _b.itemListElement; /** * 明示的なカタログID指定に対応(2024-10-01~) */ const catalogIdSpecified = (_c = params.options.includedInDataCatalog) === null || _c === void 0 ? void 0 : _c.id; if (typeof catalogIdSpecified === 'string') { const productHasSpecifiedCatalog = Array.isArray(productCatalogs) && productCatalogs.some(({ id }) => id === catalogIdSpecified); if (!productHasSpecifiedCatalog) { throw new factory.errors.Argument('includedInDataCatalog.id', 'specified catalog not found in product.hasOfferCatalog'); } catalogId = catalogIdSpecified; } else { // 指定のない場合、自動的にひとつめのカタログを選択 // const firstCatalogIdOfProduct = eventService.hasOfferCatalog?.id; // migrate to itemListElement(2024-09-30~) const firstCatalogIdOfProduct = (_d = productCatalogs === null || productCatalogs === void 0 ? void 0 : productCatalogs.at(0)) === null || _d === void 0 ? void 0 : _d.id; if (typeof firstCatalogIdOfProduct === 'string') { catalogId = firstCatalogIdOfProduct; } } } if (typeof catalogId !== 'string') { throw new factory.errors.NotFound('itemOffered.hasOfferCatalog'); } const offerCatalogFirstElement = yield repos.offerCatalog.findFirstItemListElementById({ id: catalogId }); if (offerCatalogFirstElement.typeOf === 'OfferCatalog') { const catalogItemListElements = yield repos.offerCatalog.sliceItemListElementById({ id: catalogId, limit: params.limit, page: params.page }); const unacceptedPaymentMethod = getUnacceptedPaymentMethodByEvent({ event }); if (catalogItemListElements.length > 0) { if (useIncludeInDataCatalog) { // 単価オファーから利用可能なサブカタログを検索 const availableCatalogs = yield repos.offer.searchAvailableCatalogs({ project: { id: event.project.id }, includedInDataCatalog: { id: catalogItemListElements.map((element) => element.id) }, availableAtOrFrom: { id: params.availableAtOrFrom.id }, unacceptedPaymentMethod: (considerUnacceptedPaymentMethod) ? unacceptedPaymentMethod : [] }); availabilities = catalogItemListElements.map((element) => { return { id: element.id, isAvailable: availableCatalogs.some((item) => item.id === element.id) }; }); } else { // support no dependency on includedInDataCatalog(2025-05-07~) for (const element of catalogItemListElements) { availabilities.push({ id: element.id, isAvailable: yield repos.offer.isCatalogAvailable({ includedInDataCatalog: { id: element.id }, availableAtOrFrom: { id: params.availableAtOrFrom.id }, unacceptedPaymentMethod: (considerUnacceptedPaymentMethod) ? unacceptedPaymentMethod : [] }) }); } } } } else { // Offerによるカタログの場合はひとまずempty } return availabilities; }); }