Source: repo/reservation.js

"use strict";
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 reservation_1 = require("./mongoose/model/reservation");
const factory = require("../factory");
/**
 * 予約リポジトリー
 */
class MongoRepository {
    constructor(connection) {
        this.reservationModel = connection.model(reservation_1.default.modelName);
    }
    // tslint:disable-next-line:max-func-body-length
    static CREATE_EVENT_RESERVATION_MONGO_CONDITIONS(params) {
        // MongoDB検索条件
        const andConditions = [
            {
                typeOf: factory.reservationType.EventReservation
            }
        ];
        // tslint:disable-next-line:no-single-line-block-comment
        /* istanbul ignore else */
        if (Array.isArray(params.ids)) {
            andConditions.push({
                _id: {
                    $in: params.ids
                }
            });
        }
        // tslint:disable-next-line:no-single-line-block-comment
        /* istanbul ignore else */
        if (Array.isArray(params.reservationStatuses)) {
            andConditions.push({
                reservationStatus: { $in: params.reservationStatuses }
            });
        }
        // tslint:disable-next-line:no-single-line-block-comment
        /* istanbul ignore else */
        if (params.modifiedFrom !== undefined) {
            andConditions.push({
                modifiedTime: { $gte: params.modifiedFrom }
            });
        }
        // tslint:disable-next-line:no-single-line-block-comment
        /* istanbul ignore else */
        if (params.modifiedThrough !== undefined) {
            andConditions.push({
                modifiedTime: { $lte: params.modifiedThrough }
            });
        }
        // tslint:disable-next-line:no-single-line-block-comment
        /* istanbul ignore else */
        if (params.reservationFor !== undefined) {
            // tslint:disable-next-line:no-single-line-block-comment
            /* istanbul ignore else */
            if (params.reservationFor.typeOf !== undefined) {
                andConditions.push({
                    'reservationFor.typeOf': {
                        $exists: true,
                        $eq: params.reservationFor.typeOf
                    }
                });
            }
            // tslint:disable-next-line:no-single-line-block-comment
            /* istanbul ignore else */
            if (params.reservationFor.id !== undefined) {
                andConditions.push({
                    'reservationFor.id': {
                        $exists: true,
                        $eq: params.reservationFor.id
                    }
                });
            }
            // tslint:disable-next-line:no-single-line-block-comment
            /* istanbul ignore else */
            if (Array.isArray(params.reservationFor.ids)) {
                andConditions.push({
                    'reservationFor.id': {
                        $exists: true,
                        $in: params.reservationFor.ids
                    }
                });
            }
            // tslint:disable-next-line:no-single-line-block-comment
            /* istanbul ignore else */
            if (params.reservationFor.startFrom instanceof Date) {
                andConditions.push({
                    'reservationFor.startDate': {
                        $exists: true,
                        $gte: params.reservationFor.startFrom
                    }
                });
            }
            // tslint:disable-next-line:no-single-line-block-comment
            /* istanbul ignore else */
            if (params.reservationFor.startThrough instanceof Date) {
                andConditions.push({
                    'reservationFor.startDate': {
                        $exists: true,
                        $lt: params.reservationFor.startThrough
                    }
                });
            }
            // tslint:disable-next-line:no-single-line-block-comment
            /* istanbul ignore else */
            if (params.reservationFor.superEvent !== undefined) {
                // tslint:disable-next-line:no-single-line-block-comment
                /* istanbul ignore else */
                if (params.reservationFor.superEvent.id !== undefined) {
                    andConditions.push({
                        'reservationFor.superEvent.id': {
                            $exists: true,
                            $eq: params.reservationFor.superEvent.id
                        }
                    });
                }
                // tslint:disable-next-line:no-single-line-block-comment
                /* istanbul ignore else */
                if (Array.isArray(params.reservationFor.superEvent.ids)) {
                    andConditions.push({
                        'reservationFor.superEvent.id': {
                            $exists: true,
                            $in: params.reservationFor.superEvent.ids
                        }
                    });
                }
            }
        }
        return andConditions;
    }
    countScreeningEventReservations(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_EVENT_RESERVATION_MONGO_CONDITIONS(params);
            return this.reservationModel.countDocuments({ $and: conditions }).setOptions({ maxTimeMS: 10000 })
                .exec();
        });
    }
    /**
     * 上映イベント予約を検索する
     */
    searchScreeningEventReservations(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_EVENT_RESERVATION_MONGO_CONDITIONS(params);
            const query = this.reservationModel.find({ $and: conditions }, {
                __v: 0,
                createdAt: 0,
                updatedAt: 0
            });
            // tslint:disable-next-line:no-single-line-block-comment
            /* istanbul ignore else */
            if (params.limit !== undefined && params.page !== undefined) {
                query.limit(params.limit).skip(params.limit * (params.page - 1));
            }
            // tslint:disable-next-line:no-single-line-block-comment
            /* istanbul ignore else */
            if (params.sort !== undefined) {
                query.sort(params.sort);
            }
            return query.setOptions({ maxTimeMS: 10000 }).exec().then((docs) => docs.map((doc) => doc.toObject()));
        });
    }
    /**
     * IDで上映イベント予約を検索する
     */
    findScreeningEventReservationById(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.reservationModel.findById(params.id, {
                __v: 0,
                createdAt: 0,
                updatedAt: 0
            }).exec();
            if (doc === null) {
                throw new factory.errors.NotFound('Reservation');
            }
            return doc.toObject();
        });
    }
    /**
     * 予約確定
     */
    confirm(params) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.reservationModel.findByIdAndUpdate(params.id, Object.assign({}, params, { reservationStatus: factory.reservationStatusType.ReservationConfirmed, modifiedTime: new Date() })).exec().then((doc) => {
                if (doc === null) {
                    throw new factory.errors.NotFound('Reservation');
                }
            });
        });
    }
    /**
     * 予約取消
     */
    cancel(params) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.reservationModel.findByIdAndUpdate(params.id, {
                reservationStatus: factory.reservationStatusType.ReservationCancelled,
                modifiedTime: new Date()
            }).exec().then((doc) => {
                if (doc === null) {
                    throw new factory.errors.NotFound('Reservation');
                }
            });
        });
    }
    /**
     * 発券する
     */
    checkIn(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.reservationModel.findByIdAndUpdate(params.id, {
                checkedIn: true,
                modifiedTime: new Date()
            }, { new: true }).exec();
            if (doc === null) {
                throw new factory.errors.NotFound('Reservation');
            }
            return doc.toObject();
        });
    }
    /**
     * 入場する
     */
    attend(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.reservationModel.findByIdAndUpdate(params.id, {
                attended: true,
                modifiedTime: new Date()
            }, { new: true }).exec();
            if (doc === null) {
                throw new factory.errors.NotFound('Reservation');
            }
            return doc.toObject();
        });
    }
}
exports.MongoRepository = MongoRepository;