UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

123 lines (122 loc) 7.38 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.useReservation = useReservation; const createDebug = require("debug"); const factory = require("../../factory"); const confirmReservation_1 = require("./confirmReservation"); const onReservationUsed_1 = require("./potentialActions/onReservationUsed"); const debug = createDebug('chevre-domain:service:reserve:useReservation'); /** * 予約使用 */ // tslint:disable-next-line:max-func-body-length function useReservation(params) { // tslint:disable-next-line:max-func-body-length return (repos) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; const reservationId = params.object.id; let ticketToken; // const ticketToken = params?.instrument?.ticketToken; if (Array.isArray(params.instrument)) { for (const eachInstrument of params.instrument) { if (eachInstrument.typeOf === factory.action.check.token.ObjectType.Ticket && typeof eachInstrument.ticketToken === 'string') { ticketToken = eachInstrument.ticketToken; break; } } } else { ticketToken = (_a = params === null || params === void 0 ? void 0 : params.instrument) === null || _a === void 0 ? void 0 : _a.ticketToken; } // if (params.verifyToken === true) {} // タスク作成前に検証済なので検証不要 // confirmReservationが間に合わない可能性を考慮する(2023-06-01~) yield reserveIfNotYet({ object: params.object })(repos); // 予約検索 const reservation = yield repos.reservation.projectFieldsById({ id: reservationId, inclusion: ['issuedThrough', 'project', 'reservationFor', 'reservationNumber', 'reservedTicket', 'typeOf'] }); if (typeof reservation.issuedThrough.id !== 'string') { // COA予約では予約使用アクションを想定していないので、興行idは必ず存在するはず throw new factory.errors.Internal('reservation.issuedThrough.id must be string'); } const { reservedTicket } = reservation; // optimize(2024-05-07~) const reservationAsObject = { id: reservation.id, reservationNumber: reservation.reservationNumber, typeOf: reservation.typeOf, issuedThrough: { id: reservation.issuedThrough.id, typeOf: reservation.issuedThrough.typeOf }, reservationFor: { id: reservation.reservationFor.id, typeOf: reservation.reservationFor.typeOf }, reservedTicket: Object.assign(Object.assign({ typeOf: reservedTicket.typeOf, ticketType: Object.assign({ identifier: reservedTicket.ticketType.identifier, typeOf: reservedTicket.ticketType.typeOf }, (typeof reservedTicket.ticketType.id === 'string') ? { id: reservedTicket.ticketType.id } : undefined) }, (typeof reservedTicket.identifier === 'string') ? { identifier: reservedTicket.identifier } : undefined), (typeof ((_b = reservedTicket.ticketedSeat) === null || _b === void 0 ? void 0 : _b.typeOf) === 'string') ? { ticketedSeat: reservedTicket.ticketedSeat } : undefined) }; // extend instrument to array(2025-02-18~) const instrument = [ Object.assign(Object.assign({}, (typeof ticketToken === 'string') ? { ticketToken } : undefined), { typeOf: factory.action.check.token.ObjectType.Ticket }), // support orderAsInstrument(2025-02-18~) ...(Array.isArray(params.instrument)) ? params.instrument.filter(({ typeOf }) => typeOf === factory.order.OrderType.Order) : [] ]; const actionAttributes = Object.assign({ project: reservation.project, typeOf: factory.actionType.UseAction, agent: params.agent, instrument, object: [reservationAsObject] }, (typeof ((_c = params.location) === null || _c === void 0 ? void 0 : _c.identifier) === 'string') ? { location: { typeOf: factory.placeType.Place, identifier: params.location.identifier } } : undefined); const action = yield repos.action.start(actionAttributes); // ひとまず予約数:1に限定する if (actionAttributes.object.length !== 1) { throw new factory.errors.Argument('object', 'number of using reservations must be 1'); } let attendedReservation; try { // 使用日時と同時更新(2023-01-30~) attendedReservation = yield repos.reservation.attendIfNotAttended({ id: reservationId, now: action.startDate }); } catch (error) { try { yield repos.action.giveUp({ typeOf: action.typeOf, id: action.id, error }); } catch (__) { // 失敗したら仕方ない } throw error; } yield repos.action.completeWithVoid({ typeOf: action.typeOf, id: action.id, result: {} }); yield (0, onReservationUsed_1.onReservationUsed)(attendedReservation, Object.assign(Object.assign({}, actionAttributes), { id: action.id, startDate: action.startDate }))(repos); }); } function reserveIfNotYet(params) { return (repos // settings: Settings ) => __awaiter(this, void 0, void 0, function* () { let reserveTransactions = []; if (typeof params.object.id === 'string' && params.object.id.length > 0) { reserveTransactions = yield repos.assetTransaction.search({ typeOf: factory.assetTransactionType.Reserve, object: { reservations: { id: { $in: [params.object.id] } } }, statuses: [factory.transactionStatusType.Confirmed] }, ['transactionNumber']); debug(reserveTransactions.length, 'reserveTransactions found in reserveIfNotYet. id:', params.object.id, 'reserveTransactions:', JSON.stringify(reserveTransactions)); } yield Promise.all(reserveTransactions.map((reserveTransaction) => __awaiter(this, void 0, void 0, function* () { yield (0, confirmReservation_1.confirmReservation)({ // optimize(2024-07-01~) potentialReserveAction: { object: { reservationNumber: reserveTransaction.transactionNumber } }, useOnReservationConfirmed: false, // ここでは確定予約の存在を担保すればよいだけ byTask: false })(repos); debug('confirmReservation processed in reserveIfNotYet. reservationNumber:', reserveTransaction.transactionNumber); }))); }); }