@chevre/domain
Version: 
Chevre Domain Library for Node.js
175 lines (174 loc) • 9.85 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const reserve_api_abstract_client_1 = require("@movieticket/reserve-api-abstract-client");
const moment = require("moment");
const factory = require("../factory");
const DEFAULT_ELIGIBLE_QUANTITY_VALUE = 4;
/**
 * 上映イベントに対する券種オファーを検索する
 */
function searchScreeningEventTicketOffers(params) {
    // tslint:disable-next-line:max-func-body-length
    return (repos) => __awaiter(this, void 0, void 0, function* () {
        const event = yield repos.event.findById({
            typeOf: factory.eventType.ScreeningEvent,
            id: params.eventId
        });
        const eventSoundFormatTypes = (Array.isArray(event.superEvent.soundFormat)) ? event.superEvent.soundFormat.map((f) => f.typeOf) : [];
        const eventVideoFormatTypes = (Array.isArray(event.superEvent.videoFormat))
            ? event.superEvent.videoFormat.map((f) => f.typeOf)
            : [factory.videoFormatType['2D']];
        const ticketTypes = yield repos.ticketType.findByTicketGroupId({ ticketGroupId: event.ticketTypeGroup });
        // 価格仕様を検索する
        const soundFormatCompoundPriceSpecifications = yield repos.priceSpecification.searchCompoundPriceSpecifications({
            typeOf: factory.priceSpecificationType.CompoundPriceSpecification,
            priceComponent: { typeOf: factory.priceSpecificationType.SoundFormatChargeSpecification }
        });
        const videoFormatCompoundPriceSpecifications = yield repos.priceSpecification.searchCompoundPriceSpecifications({
            typeOf: factory.priceSpecificationType.CompoundPriceSpecification,
            priceComponent: { typeOf: factory.priceSpecificationType.VideoFormatChargeSpecification }
        });
        const movieTicketTypeCompoundPriceSpecifications = yield repos.priceSpecification.searchCompoundPriceSpecifications({
            typeOf: factory.priceSpecificationType.CompoundPriceSpecification,
            priceComponent: { typeOf: factory.priceSpecificationType.MovieTicketTypeChargeSpecification }
        });
        const soundFormatChargeSpecifications = soundFormatCompoundPriceSpecifications.reduce((a, b) => [...a, ...b.priceComponent], []).filter((spec) => eventSoundFormatTypes.indexOf(spec.appliesToSoundFormat) >= 0);
        const videoFormatChargeSpecifications = videoFormatCompoundPriceSpecifications.reduce((a, b) => [...a, ...b.priceComponent], []).filter((spec) => eventVideoFormatTypes.indexOf(spec.appliesToVideoFormat) >= 0);
        const movieTicketTypeChargeSpecs = movieTicketTypeCompoundPriceSpecifications.reduce((a, b) => [...a, ...b.priceComponent], []).filter((spec) => eventVideoFormatTypes.indexOf(spec.appliesToVideoFormat) >= 0);
        // Defaultオファーをセット
        let offers = {
            typeOf: 'Offer',
            priceCurrency: factory.priceCurrency.JPY,
            availabilityEnds: moment(event.endDate).toDate(),
            availabilityStarts: moment(event.endDate).toDate(),
            validFrom: moment(event.endDate).toDate(),
            validThrough: moment(event.endDate).toDate(),
            eligibleQuantity: {
                value: DEFAULT_ELIGIBLE_QUANTITY_VALUE,
                unitCode: factory.unitCode.C62,
                typeOf: 'QuantitativeValue'
            }
        };
        // オファー設定があれば上書きする
        if (event.offers !== undefined && event.offers !== null) {
            offers = event.offers;
        }
        // ムビチケが決済方法として許可されていれば、ムビチケ券種区分ごとにムビチケオファーを作成
        const movieTicketOffers = [];
        const movieTicketPaymentAccepted = event.superEvent.offers === undefined
            || event.superEvent.offers.acceptedPaymentMethod === undefined
            || event.superEvent.offers.acceptedPaymentMethod.indexOf(factory.paymentMethodType.MovieTicket) >= 0;
        if (movieTicketPaymentAccepted) {
            const movieTicketTypeCodes = [...new Set(movieTicketTypeChargeSpecs.map((s) => s.appliesToMovieTicketType))];
            movieTicketOffers.push(...movieTicketTypeCodes.map((movieTicketTypeCode) => {
                const movieTicketType = reserve_api_abstract_client_1.mvtk.util.constants.TICKET_TYPE.find((ticketType) => ticketType.code === movieTicketTypeCode);
                const unitPriceSpecification = {
                    typeOf: factory.priceSpecificationType.UnitPriceSpecification,
                    price: 0,
                    priceCurrency: factory.priceCurrency.JPY,
                    name: {
                        ja: `ムビチケ${(movieTicketType !== undefined) ? movieTicketType.name : ''}`,
                        en: 'Movie Ticket',
                        kr: 'Movie Ticket'
                    },
                    description: {
                        ja: `ムビチケ${(movieTicketType !== undefined) ? movieTicketType.name : ''}`,
                        en: 'Movie Ticket',
                        kr: 'Movie Ticket'
                    },
                    valueAddedTaxIncluded: true,
                    referenceQuantity: {
                        typeOf: 'QuantitativeValue',
                        unitCode: factory.unitCode.C62,
                        value: 1
                    }
                };
                const mvtkSpecs = movieTicketTypeChargeSpecs.filter((s) => s.appliesToMovieTicketType === movieTicketTypeCode);
                const priceComponent = [
                    unitPriceSpecification,
                    ...mvtkSpecs
                ];
                const compoundPriceSpecification = {
                    typeOf: factory.priceSpecificationType.CompoundPriceSpecification,
                    priceCurrency: factory.priceCurrency.JPY,
                    valueAddedTaxIncluded: true,
                    priceComponent: priceComponent
                };
                return {
                    typeOf: 'Offer',
                    id: `Offer-by-movieticket-${movieTicketTypeCode}`,
                    name: {
                        ja: `ムビチケ${(movieTicketType !== undefined) ? movieTicketType.name : ''}`,
                        en: 'Movie Ticket',
                        kr: 'Movie Ticket'
                    },
                    description: {
                        ja: `ムビチケ${(movieTicketType !== undefined) ? movieTicketType.name : ''}`,
                        en: 'Movie Ticket',
                        kr: 'Movie Ticket'
                    },
                    valueAddedTaxIncluded: true,
                    priceCurrency: factory.priceCurrency.JPY,
                    priceSpecification: compoundPriceSpecification,
                    availability: factory.itemAvailability.InStock,
                    availabilityEnds: offers.availabilityEnds,
                    availabilityStarts: offers.availabilityStarts,
                    eligibleQuantity: offers.eligibleQuantity,
                    validFrom: offers.validFrom,
                    validThrough: offers.validThrough
                };
            }));
        }
        const ticketTypeOffers = ticketTypes.map((ticketType) => {
            // イベントに関係のある価格仕様に絞り、ひとつの複合価格仕様としてまとめる
            const unitPriceSpecification = {
                typeOf: factory.priceSpecificationType.UnitPriceSpecification,
                price: ticketType.price,
                priceCurrency: factory.priceCurrency.JPY,
                name: ticketType.name,
                description: ticketType.description,
                valueAddedTaxIncluded: true,
                referenceQuantity: {
                    typeOf: 'QuantitativeValue',
                    unitCode: factory.unitCode.C62,
                    value: 1
                }
            };
            const priceComponent = [
                unitPriceSpecification,
                ...videoFormatChargeSpecifications,
                ...soundFormatChargeSpecifications
            ];
            const compoundPriceSpecification = {
                typeOf: factory.priceSpecificationType.CompoundPriceSpecification,
                priceCurrency: factory.priceCurrency.JPY,
                valueAddedTaxIncluded: true,
                priceComponent: priceComponent
            };
            return {
                typeOf: 'Offer',
                id: ticketType.id,
                name: ticketType.name,
                description: ticketType.description,
                priceCurrency: factory.priceCurrency.JPY,
                priceSpecification: compoundPriceSpecification,
                availability: ticketType.availability,
                availabilityEnds: offers.availabilityEnds,
                availabilityStarts: offers.availabilityStarts,
                eligibleQuantity: offers.eligibleQuantity,
                validFrom: offers.validFrom,
                validThrough: offers.validThrough
            };
        });
        return [...ticketTypeOffers, ...movieTicketOffers];
    });
}
exports.searchScreeningEventTicketOffers = searchScreeningEventTicketOffers;