@chevre/domain
Version:
Chevre Domain Library for Node.js
128 lines (127 loc) • 7.41 kB
JavaScript
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.searchProductOffers = searchProductOffers;
const factory = require("../../../factory");
/**
* プロダクトオファーを検索する
*/
function searchProductOffers(params) {
// tslint:disable-next-line:max-func-body-length
return (repos) => __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
// プロダクト検索
const productWithOffers = (yield repos.product.projectFields({
limit: 1,
page: 1,
id: { $eq: params.itemOffered.id }
}, ['hasOfferCatalog', 'project']
// []
)).shift();
if (productWithOffers === undefined) {
throw new factory.errors.NotFound('Product');
}
// const offerCatalogId = productWithOffers.hasOfferCatalog?.id; // migrate to itemListElement(2024-09-30~)
const offerCatalogId = (_b = (_a = productWithOffers.hasOfferCatalog) === null || _a === void 0 ? void 0 : _a.itemListElement.at(0)) === null || _b === void 0 ? void 0 : _b.id;
if (typeof offerCatalogId !== 'string') {
return [];
}
// サブカタログIDを決定
let subOfferCatalogId = offerCatalogId;
let isOfferCatalogItem = false;
const offerCatalogFirstElement = yield repos.offerCatalog.findFirstItemListElementById({ id: offerCatalogId });
if (offerCatalogFirstElement.typeOf === 'OfferCatalog') {
subOfferCatalogId = offerCatalogFirstElement.id;
isOfferCatalogItem = true;
}
let offers;
let sortedOfferIds;
if (params.addSortIndex) {
// addSortIndexの場合はid指定廃止(2023-09-13~)
if (Array.isArray(params.ids)) {
throw new factory.errors.NotImplemented('id specification on addSortIndex not implemented');
}
// 明示的にカタログ指定可能にする
if (typeof ((_c = params.includedInDataCatalog) === null || _c === void 0 ? void 0 : _c.id) === 'string' && params.includedInDataCatalog.id.length > 0) {
if (isOfferCatalogItem) {
// カタログに含まれるサブカタログか検証(2024-09-30~)
const offerCatalogIncludingSpecifiedElement = yield repos.offerCatalog.projectFields({
limit: 1,
page: 1,
id: { $in: [offerCatalogId] },
itemListElement: { id: { $in: [params.includedInDataCatalog.id] } }
}, ['id']);
if (offerCatalogIncludingSpecifiedElement.length === 0) {
throw new factory.errors.Argument('includedInDataCatalog.id', 'specified sub catalog not found in product.hasOfferCatalog');
}
}
else {
if (subOfferCatalogId !== params.includedInDataCatalog.id) {
throw new factory.errors.Argument('includedInDataCatalog.id', 'invalid offerCatalog');
}
}
subOfferCatalogId = params.includedInDataCatalog.id;
}
const searchByOfferCatalogIdResult = yield repos.offer.searchByOfferCatalogIdWithSortIndex({
offerCatalog: { id: subOfferCatalogId, isOfferCatalogItem },
excludeAppliesToMovieTicket: false,
priceSpecification: {}, // 絞る必要なし
limit: params.limit,
page: params.page,
onlyValid: params.onlyValid === true,
availableAtOrFrom: params.availableAt,
useIncludeInDataCatalog: params.useIncludeInDataCatalog
});
offers = searchByOfferCatalogIdResult.offers;
sortedOfferIds = searchByOfferCatalogIdResult.sortedOfferIds;
}
else {
// id指定必須(2023-09-13~)
if (!Array.isArray(params.ids)) {
throw new factory.errors.ArgumentNull('ids', 'ids must be specified');
}
let includedInDataCatalogIds = [subOfferCatalogId];
if (isOfferCatalogItem) {
// 複数カタログ対応(2023-09-27~)
const offerCatalogItems = yield repos.offerCatalog.findItemListElementById({
id: offerCatalogId,
project: { id: productWithOffers.project.id }
});
includedInDataCatalogIds = offerCatalogItems.itemListElement.map((element) => element.id);
}
const searchByOfferCatalogIdResult = yield repos.offer.searchAllByIdsAndOfferCatalogId({
ids: params.ids,
includedInDataCatalog: { id: includedInDataCatalogIds, isOfferCatalogItem },
excludeAppliesToMovieTicket: false,
onlyValid: params.onlyValid === true,
availableAtOrFrom: params.availableAt
});
offers = searchByOfferCatalogIdResult;
}
return offers.map((o) => {
var _a;
let sortIndex;
if (params.addSortIndex) {
sortIndex = sortedOfferIds === null || sortedOfferIds === void 0 ? void 0 : sortedOfferIds.indexOf(String(o.id));
}
const unitSpec = o.priceSpecification;
const compoundPriceSpecification = {
typeOf: factory.priceSpecificationType.CompoundPriceSpecification,
priceCurrency: factory.priceCurrency.JPY,
valueAddedTaxIncluded: true,
priceComponent: [
...(unitSpec !== undefined) ? [unitSpec] : []
]
};
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ additionalProperty: (Array.isArray(o.additionalProperty)) ? o.additionalProperty : [], alternateName: o.alternateName, availability: o.availability, color: o.color, description: o.description, id: o.id, identifier: o.identifier, itemOffered: o.itemOffered, name: o.name, priceCurrency: o.priceCurrency, typeOf: o.typeOf, priceSpecification: compoundPriceSpecification }, (o.validFrom instanceof Date) ? { validFrom: o.validFrom } : undefined), (o.validThrough instanceof Date) ? { validThrough: o.validThrough } : undefined), (typeof sortIndex === 'number') ? { sortIndex } : undefined), (typeof o.offerIndex === 'number') ? { offerIndex: o.offerIndex } : undefined), (typeof ((_a = o.parentOffer) === null || _a === void 0 ? void 0 : _a.id) === 'string') ? { parentOffer: { id: o.parentOffer.id } } : undefined);
});
});
}
;