@chevre/domain
Version:
Chevre Domain Library for Node.js
347 lines (346 loc) • 15.2 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 createDebug = require("debug");
const factory = require("../../factory");
const OfferService = require("../offer");
const ReserveService = require("../reserve");
const debug = createDebug('chevre-domain:service');
/**
* 取引開始
*/
// tslint:disable-next-line:max-func-body-length
function start(params) {
// tslint:disable-next-line:max-func-body-length
return (repos) => __awaiter(this, void 0, void 0, function* () {
debug('starting transaction...', params);
const now = new Date();
// イベント存在確認
const screeningEvent = yield repos.event.findById({
typeOf: factory.eventType.ScreeningEvent,
id: params.object.event.id
});
// チケット存在確認
const ticketOffers = yield OfferService.searchScreeningEventTicketOffers({ eventId: params.object.event.id })(repos);
const ticketTypes = yield repos.ticketType.findByTicketGroupId({ ticketGroupId: screeningEvent.offers.id });
debug('available ticket type:', ticketTypes);
// 座席情報取得
const movieTheater = yield repos.place.findMovieTheaterByBranchCode({ branchCode: screeningEvent.superEvent.location.branchCode });
const screeningRoom = movieTheater.containsPlace.find((p) => p.branchCode === screeningEvent.location.branchCode);
if (screeningRoom === undefined) {
throw new factory.errors.NotFound('Screening Room', `Event location 'Screening Room ${screeningEvent.location.branchCode}' not found`);
}
const screeningRoomSections = screeningRoom.containsPlace;
// 予約番号発行
const reservationNumber = yield repos.reservationNumber.publish({
reserveDate: now,
sellerBranchCode: screeningEvent.superEvent.location.branchCode
});
// 取引ファクトリーで新しい進行中取引オブジェクトを作成
const tickets = params.object.acceptedOffer.map((offer) => {
const ticketOffer = ticketOffers.find((t) => t.id === offer.id);
if (ticketOffer === undefined) {
throw new factory.errors.NotFound('Ticket Offer');
}
let ticketType = ticketTypes.find((t) => t.id === offer.id);
// 基本的に券種でID管理されていないオファーは存在しないが、念のため管理されていないケースに対応
if (ticketType === undefined) {
const unitPriceSpec = ticketOffer.priceSpecification.priceComponent.find((spec) => {
return spec.typeOf === factory.priceSpecificationType.UnitPriceSpecification;
});
if (unitPriceSpec === undefined) {
throw new factory.errors.Argument('acceptedOffer', `UnitPriceSpecification for ${offer.id} Not Found`);
}
ticketType = {
typeOf: 'Offer',
id: ticketOffer.id,
name: ticketOffer.name,
description: ticketOffer.description,
alternateName: ticketOffer.name,
priceCurrency: factory.priceCurrency.JPY,
availability: factory.itemAvailability.InStock,
priceSpecification: unitPriceSpec
};
}
const screeningRoomSection = screeningRoomSections.find((section) => section.branchCode === offer.ticketedSeat.seatSection);
if (screeningRoomSection === undefined) {
throw new factory.errors.NotFound('Screening Room Section', `Screening room section ${offer.ticketedSeat.seatSection} not found`);
}
const seat = screeningRoomSection.containsPlace.find((p) => p.branchCode === offer.ticketedSeat.seatNumber);
if (seat === undefined) {
throw new factory.errors.NotFound('Seat', `Seat ${offer.ticketedSeat.seatNumber} not found`);
}
return {
typeOf: 'Ticket',
dateIssued: now,
issuedBy: {
typeOf: screeningEvent.location.typeOf,
name: screeningEvent.location.name.ja
},
totalPrice: ticketOffer.priceSpecification,
priceCurrency: factory.priceCurrency.JPY,
ticketedSeat: Object.assign({}, offer.ticketedSeat, seat),
underName: {
typeOf: params.agent.typeOf,
name: params.agent.name
},
ticketType: ticketType
};
});
// 仮予約作成
const reservations = yield Promise.all(tickets.map((ticket, index) => __awaiter(this, void 0, void 0, function* () {
return createReservation({
id: `${reservationNumber}-${index}`,
reserveDate: now,
agent: params.agent,
reservationNumber: reservationNumber,
screeningEvent: screeningEvent,
reservedTicket: ticket
});
})));
const startParams = {
typeOf: factory.transactionType.Reserve,
agent: params.agent,
object: {
clientUser: params.object.clientUser,
event: screeningEvent,
reservations: reservations,
notes: params.object.notes
},
expires: params.expires
};
// 取引作成
let transaction;
try {
transaction = yield repos.transaction.start(startParams);
}
catch (error) {
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore next */
if (error.name === 'MongoError') {
// no op
}
throw error;
}
// 座席ロック
yield repos.eventAvailability.lock({
eventId: screeningEvent.id,
offers: tickets.map((ticket) => {
return {
seatSection: ticket.ticketedSeat.seatSection,
seatNumber: ticket.ticketedSeat.seatNumber
};
}),
expires: screeningEvent.endDate,
holder: transaction.id
});
// 予約作成
yield Promise.all(reservations.map((r) => __awaiter(this, void 0, void 0, function* () {
yield repos.reservation.reservationModel.create(Object.assign({}, r, { _id: r.id }));
})));
return transaction;
});
}
exports.start = start;
function createReservation(params) {
return {
typeOf: factory.reservationType.EventReservation,
id: params.id,
additionalTicketText: params.reservedTicket.ticketType.name.ja,
modifiedTime: params.reserveDate,
numSeats: 1,
price: params.reservedTicket.totalPrice,
priceCurrency: factory.priceCurrency.JPY,
reservationFor: params.screeningEvent,
reservationNumber: params.reservationNumber,
reservationStatus: factory.reservationStatusType.ReservationPending,
reservedTicket: params.reservedTicket,
underName: params.agent,
checkedIn: false,
attended: false
};
}
/**
* 取引確定
*/
function confirm(params) {
return (repos) => __awaiter(this, void 0, void 0, function* () {
debug(`confirming reserve transaction ${params.id}...`);
// 取引存在確認
const transaction = yield repos.transaction.findById({
typeOf: factory.transactionType.Reserve,
id: params.id
});
// 予約アクション属性作成
const reserveActionAttributes = transaction.object.reservations.map((reservation) => {
if (params.object !== undefined) {
// 予約属性の指定があれば上書き
const confirmingReservation = params.object.reservations.find((r) => r.id === reservation.id);
if (confirmingReservation !== undefined) {
if (confirmingReservation.reservedTicket !== undefined) {
if (confirmingReservation.reservedTicket.issuedBy !== undefined) {
reservation.reservedTicket.issuedBy = confirmingReservation.reservedTicket.issuedBy;
}
}
if (confirmingReservation.underName !== undefined) {
reservation.underName = confirmingReservation.underName;
reservation.reservedTicket.underName = confirmingReservation.underName;
}
}
}
return {
typeOf: factory.actionType.ReserveAction,
description: transaction.object.notes,
result: {},
object: reservation,
agent: transaction.agent,
purpose: {
typeOf: transaction.typeOf,
id: transaction.id
}
};
});
const potentialActions = {
reserve: reserveActionAttributes
};
// 取引確定
const result = {};
yield repos.transaction.confirm({
typeOf: factory.transactionType.Reserve,
id: transaction.id,
result: result,
potentialActions: potentialActions
});
});
}
exports.confirm = confirm;
/**
* 取引中止
*/
function cancel(params) {
return (repos) => __awaiter(this, void 0, void 0, function* () {
// まず取引状態変更
const transaction = yield repos.transaction.cancel({
typeOf: factory.transactionType.Reserve,
id: params.id
});
// 本来非同期でタスクが実行されるが、同期的に仮予約取消が実行されていないと、サービス利用側が困る可能性があるので、
// 一応同期的にもcancelPendingReservationを実行しておく
try {
const actionAttributes = transaction.object.reservations.map((r) => {
return {
typeOf: factory.actionType.CancelAction,
purpose: {
typeOf: transaction.typeOf,
id: transaction.id
},
agent: transaction.agent,
object: r
};
});
yield ReserveService.cancelPendingReservation(actionAttributes)(repos);
}
catch (error) {
// no op
}
});
}
exports.cancel = cancel;
/**
* ひとつの取引のタスクをエクスポートする
*/
function exportTasks(status) {
return (repos) => __awaiter(this, void 0, void 0, function* () {
const transaction = yield repos.transaction.startExportTasks({
typeOf: factory.transactionType.Reserve,
status: status
});
if (transaction === null) {
return;
}
// 失敗してもここでは戻さない(RUNNINGのまま待機)
yield exportTasksById(transaction)(repos);
yield repos.transaction.setTasksExportedById({ id: transaction.id });
});
}
exports.exportTasks = exportTasks;
/**
* ID指定で取引のタスク出力
*/
function exportTasksById(params) {
return (repos) => __awaiter(this, void 0, void 0, function* () {
const transaction = yield repos.transaction.findById({
typeOf: factory.transactionType.Reserve,
id: params.id
});
const potentialActions = transaction.potentialActions;
const taskAttributes = [];
switch (transaction.status) {
case factory.transactionStatusType.Confirmed:
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (potentialActions !== undefined) {
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (potentialActions.reserve !== undefined) {
const reserveTask = {
name: factory.taskName.Reserve,
status: factory.taskStatus.Ready,
runsAt: new Date(),
remainingNumberOfTries: 10,
lastTriedAt: null,
numberOfTried: 0,
executionResults: [],
data: {
actionAttributes: potentialActions.reserve
}
};
taskAttributes.push(reserveTask);
}
}
break;
case factory.transactionStatusType.Canceled:
case factory.transactionStatusType.Expired:
const actionAttributes = transaction.object.reservations.map((r) => {
return {
typeOf: factory.actionType.CancelAction,
purpose: {
typeOf: transaction.typeOf,
id: transaction.id
},
agent: transaction.agent,
object: r
};
});
const cancelPendingReservationTask = {
name: factory.taskName.CancelPendingReservation,
status: factory.taskStatus.Ready,
runsAt: new Date(),
remainingNumberOfTries: 10,
lastTriedAt: null,
numberOfTried: 0,
executionResults: [],
data: {
actionAttributes: actionAttributes
}
};
taskAttributes.push(cancelPendingReservationTask);
break;
default:
throw new factory.errors.NotImplemented(`Transaction status "${transaction.status}" not implemented.`);
}
debug('taskAttributes prepared', taskAttributes);
return Promise.all(taskAttributes.map((a) => __awaiter(this, void 0, void 0, function* () { return repos.task.save(a); })));
});
}
exports.exportTasksById = exportTasksById;
;