UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

95 lines (94 loc) 5.18 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.searchOfferCatalogItems = searchOfferCatalogItems; const factory = require("../../../factory"); /** * サブカタログ検索 */ function searchOfferCatalogItems(params) { // tslint:disable-next-line:max-func-body-length return (repos) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; 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'); } let offerCatalogItems = []; // optimize(2024-07-18~) const event = yield repos.event.projectEventFieldsById({ id: params.event.id }, ['offers.itemOffered.id']); // 興行設定があれば興行のカタログを参照する 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 }); if (catalogItemListElements.length > 0) { // サブカタログ検索 const searchOfferCatalogItemsResult = yield repos.offerCatalogItem.projectFields({ id: { $in: catalogItemListElements.map((element) => element.id) } }, ['description', 'name', 'additionalProperty', 'relatedOffer']); offerCatalogItems = catalogItemListElements.map((element) => { const offerCatalogItem = searchOfferCatalogItemsResult.find((item) => item.id === element.id); if (offerCatalogItem === undefined) { throw new factory.errors.NotFound('OfferCatalog', `offerCatalogItem '${element.id}' not found`); } return Object.assign(Object.assign({}, element), offerCatalogItem); }); } } else { // Offerによるカタログの場合はひとまずempty } return offerCatalogItems; }); }