Source: repo/place.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 place_1 = require("./mongoose/model/place");
const factory = require("../factory");
/**
 * 場所リポジトリ
 */
class MongoRepository {
    constructor(connection) {
        this.placeModel = connection.model(place_1.default.modelName);
    }
    static CREATE_MOVIE_THEATER_MONGO_CONDITIONS(params) {
        // MongoDB検索条件
        const andConditions = [
            {
                typeOf: factory.placeType.MovieTheater
            }
        ];
        // tslint:disable-next-line:no-single-line-block-comment
        /* istanbul ignore else */
        if (params.project !== undefined) {
            if (Array.isArray(params.project.ids)) {
                andConditions.push({
                    'project.id': {
                        $exists: true,
                        $in: params.project.ids
                    }
                });
            }
        }
        // tslint:disable-next-line:no-single-line-block-comment
        /* istanbul ignore else */
        if (Array.isArray(params.branchCodes)) {
            andConditions.push({
                branchCode: {
                    $exists: true,
                    $in: params.branchCodes
                }
            });
        }
        // tslint:disable-next-line:no-single-line-block-comment
        /* istanbul ignore else */
        if (params.name !== undefined) {
            andConditions.push({
                $or: [
                    { 'name.ja': new RegExp(params.name, 'i') },
                    { 'name.en': new RegExp(params.name, 'i') },
                    {
                        kanaName: {
                            $exists: true,
                            $regex: new RegExp(params.name, 'i')
                        }
                    }
                ]
            });
        }
        return andConditions;
    }
    /**
     * 劇場を保管する
     */
    saveMovieTheater(params) {
        return __awaiter(this, void 0, void 0, function* () {
            let doc;
            if (params.id === '') {
                doc = yield this.placeModel.create(params);
            }
            else {
                doc = yield this.placeModel.findOneAndUpdate({ _id: params.id }, params, { upsert: false, new: true })
                    .exec();
                if (doc === null) {
                    throw new factory.errors.NotFound(this.placeModel.modelName);
                }
            }
            return doc.toObject();
        });
    }
    countMovieTheaters(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_MOVIE_THEATER_MONGO_CONDITIONS(params);
            return this.placeModel.countDocuments({ $and: conditions })
                .setOptions({ maxTimeMS: 10000 })
                .exec();
        });
    }
    /**
     * 劇場検索
     */
    searchMovieTheaters(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_MOVIE_THEATER_MONGO_CONDITIONS(params);
            // containsPlaceを含めるとデータサイズが大きくなるので、検索結果には含めない
            const query = this.placeModel.find({ $and: conditions }, {
                __v: 0,
                createdAt: 0,
                updatedAt: 0,
                containsPlace: 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()));
        });
    }
    /**
     * 劇場取得
     */
    findById(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.placeModel.findOne({ _id: params.id }, {
                __v: 0,
                createdAt: 0,
                updatedAt: 0
            })
                .exec();
            if (doc === null) {
                throw new factory.errors.NotFound(this.placeModel.modelName);
            }
            return doc.toObject();
        });
    }
}
exports.MongoRepository = MongoRepository;