UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

107 lines (106 loc) 5.6 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.validateStartParams = validateStartParams; exports.start = start; const factory = require("../../../factory"); const factory_1 = require("./factory"); function validateStartParams(params) { return (repos) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; let reserveTransaction; let reservations; const sellerId = (_a = params.seller) === null || _a === void 0 ? void 0 : _a.id; // 予約番号で取引存在確認 if (typeof ((_b = params.object.reservation) === null || _b === void 0 ? void 0 : _b.reservationNumber) === 'string') { const transactions = yield repos.assetTransaction.search({ limit: 1, page: 1, typeOf: factory.assetTransactionType.Reserve, object: Object.assign({ reservationNumber: { $eq: params.object.reservation.reservationNumber } }, (typeof sellerId === 'string') ? { provider: { id: { $eq: sellerId } } } : undefined) }); reserveTransaction = transactions.shift(); // ここでReservationConfirmedでない予約をスルーしても、 // ReservationRepo.cancel()で冪等性を保証しているのでvalidation不要か(2022-05-12~) // 予約ステータス確認 // const unconfirmedReservations = await repos.reservation.search({ // limit: 1, // page: 1, // typeOf: factory.reservationType.EventReservation, // reservationNumber: { $eq: params.object.reservation.reservationNumber }, // reservationStatus: { $ne: factory.reservationStatusType.ReservationConfirmed } // }); // if (unconfirmedReservations.length > 0) { // tslint:disable-next-line:max-line-length // throw new factory.errors.Argument('object.reservation.reservationNumber', `Reservation ${unconfirmedReservations[0].id} not confirmed`); // } } // 取引指定が確認できなければ、予約指定を確認 if (reserveTransaction === undefined) { // 予約存在確認 if (typeof ((_c = params.object.reservation) === null || _c === void 0 ? void 0 : _c.id) === 'string') { const reservation = yield repos.reservation.projectFieldsById({ id: params.object.reservation.id, inclusion: [ 'issuedThrough', 'typeOf', 'reservationNumber', 'reservationFor.id', 'reservationFor.typeOf', 'provider', 'reservationStatus' ] }); // 販売者検証(2023-08-01~) if (typeof sellerId === 'string') { if (((_d = reservation.provider) === null || _d === void 0 ? void 0 : _d.id) !== sellerId) { throw new factory.errors.NotFound(factory.reservationType.EventReservation); } } // 予約ステータス確認 if (reservation.reservationStatus !== factory.reservationStatusType.ReservationConfirmed) { throw new factory.errors.Argument('object.reservation.id', `Reservation ${reservation.id} not confirmed`); } reservations = [reservation]; } } if (reserveTransaction === undefined && reservations === undefined) { throw new factory.errors.Argument('object', 'Transaction or reservation must be specified'); } return { reserveTransaction, reservations }; }); } /** * 取引開始 */ function start(params) { return (repos) => __awaiter(this, void 0, void 0, function* () { const project = yield repos.project.findById({ id: params.project.id, inclusion: ['typeOf'] }); const { reserveTransaction, reservations } = yield validateStartParams(params)(repos); const transactionNumber = params.transactionNumber; if (typeof transactionNumber !== 'string' || transactionNumber.length === 0) { throw new factory.errors.ArgumentNull('transactionNumber'); } const startParams = (0, factory_1.createStartParams)({ project: { id: project.id, typeOf: project.typeOf }, paramsWithoutDetail: params, transactionNumber, transaction: reserveTransaction, reservations: reservations }); // 予約ホールドする?要検討... // await Promise.all(reservations.map(async (r) => { // await repos.reservation.reservationModel.create({ ...r, _id: r.id }); // })); // 取引作成 return repos.assetTransaction.start(startParams); }); }