Source: repo/ticketType.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 factory = require("../factory");
const ticketType_1 = require("./mongoose/model/ticketType");
const ticketTypeGroup_1 = require("./mongoose/model/ticketTypeGroup");
/**
 * Mongoリポジトリー
 */
class MongoRepository {
    constructor(connection) {
        this.ticketTypeModel = connection.model(ticketType_1.default.modelName);
        this.ticketTypeGroupModel = connection.model(ticketTypeGroup_1.default.modelName);
    }
    static CREATE_TICKET_TYPE_MONGO_CONDITIONS(params) {
        // MongoDB検索条件
        const andConditions = [
            {
                _id: { $exists: true }
            }
        ];
        if (params.id !== undefined) {
            andConditions.push({ _id: new RegExp(params.id, 'i') });
        }
        if (params.name !== undefined) {
            andConditions.push({
                $or: [
                    { 'name.ja': new RegExp(params.name, 'i') },
                    { 'name.en': new RegExp(params.name, 'i') }
                ]
            });
        }
        return andConditions;
    }
    static CREATE_TICKET_TYPE_GROUP_MONGO_CONDITIONS(params) {
        // MongoDB検索条件
        const andConditions = [
            {
                _id: { $exists: true }
            }
        ];
        if (params.id !== undefined) {
            andConditions.push({ _id: new RegExp(params.id, 'i') });
        }
        if (params.name !== undefined) {
            andConditions.push({
                $or: [
                    { 'name.ja': new RegExp(params.name, 'i') },
                    { 'name.en': new RegExp(params.name, 'i') }
                ]
            });
        }
        return andConditions;
    }
    findByTicketGroupId(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const ticketTypeGroup = yield this.ticketTypeGroupModel.findById(params.ticketGroupId, {
                __v: 0,
                createdAt: 0,
                updatedAt: 0
            }).exec()
                .then((doc) => {
                if (doc === null) {
                    throw new factory.errors.NotFound('Ticket type group');
                }
                return doc.toObject();
            });
            return this.ticketTypeModel.find({ _id: { $in: ticketTypeGroup.ticketTypes } }).exec()
                .then((docs) => docs.map((doc) => doc.toObject()));
        });
    }
    /**
     * 券種グループを作成する
     */
    createTicketTypeGroup(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.ticketTypeGroupModel.create(Object.assign({}, params, { _id: params.id }));
            return doc.toObject();
        });
    }
    /**
     * IDで件券種グループを検索する
     */
    findTicketTypeGroupById(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.ticketTypeGroupModel.findOne({
                _id: params.id
            }, {
                __v: 0,
                createdAt: 0,
                updatedAt: 0
            }).exec();
            if (doc === null) {
                throw new factory.errors.NotFound('Ticket type group');
            }
            return doc.toObject();
        });
    }
    countTicketTypeGroups(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_TICKET_TYPE_GROUP_MONGO_CONDITIONS(params);
            return this.ticketTypeGroupModel.countDocuments({ $and: conditions }).setOptions({ maxTimeMS: 10000 })
                .exec();
        });
    }
    /**
     * 券種グループを検索する
     */
    searchTicketTypeGroups(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_TICKET_TYPE_GROUP_MONGO_CONDITIONS(params);
            const query = this.ticketTypeGroupModel.find({ $and: conditions }, {
                __v: 0,
                createdAt: 0,
                updatedAt: 0
            });
            if (params.limit !== undefined && params.page !== undefined) {
                query.limit(params.limit).skip(params.limit * (params.page - 1));
            }
            return query.sort({ _id: 1 })
                .setOptions({ maxTimeMS: 10000 })
                .exec()
                .then((docs) => docs.map((doc) => doc.toObject()));
        });
    }
    /**
     * 券種グループを更新する
     */
    updateTicketTypeGroup(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.ticketTypeGroupModel.findOneAndUpdate({
                _id: params.id
            }, params, { upsert: false, new: true }).exec();
            if (doc === null) {
                throw new factory.errors.NotFound('Ticket type group');
            }
        });
    }
    /**
     * 券種グループを削除する
     */
    deleteTicketTypeGroup(params) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.ticketTypeGroupModel.findOneAndRemove({
                _id: params.id
            }).exec();
        });
    }
    /**
     * 券種を作成する
     */
    createTicketType(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.ticketTypeModel.create(Object.assign({}, params, { _id: params.id }));
            return doc.toObject();
        });
    }
    /**
     * IDで件券種を検索する
     */
    findTicketTypeById(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.ticketTypeModel.findOne({
                _id: params.id
            }, {
                __v: 0,
                createdAt: 0,
                updatedAt: 0
            }).exec();
            if (doc === null) {
                throw new factory.errors.NotFound('Ticket type group');
            }
            return doc.toObject();
        });
    }
    countTicketTypes(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_TICKET_TYPE_MONGO_CONDITIONS(params);
            return this.ticketTypeModel.countDocuments({ $and: conditions }).setOptions({ maxTimeMS: 10000 })
                .exec();
        });
    }
    /**
     * 券種を検索する
     */
    searchTicketTypes(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_TICKET_TYPE_MONGO_CONDITIONS(params);
            const query = this.ticketTypeModel.find({ $and: conditions }, {
                __v: 0,
                createdAt: 0,
                updatedAt: 0
            });
            if (params.limit !== undefined && params.page !== undefined) {
                query.limit(params.limit).skip(params.limit * (params.page - 1));
            }
            return query.sort({ _id: 1 })
                .setOptions({ maxTimeMS: 10000 })
                .exec()
                .then((docs) => docs.map((doc) => doc.toObject()));
        });
    }
    /**
     * 券種を更新する
     */
    updateTicketType(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.ticketTypeModel.findOneAndUpdate({
                _id: params.id
            }, params, { upsert: false, new: true }).exec();
            if (doc === null) {
                throw new factory.errors.NotFound('Ticket type');
            }
        });
    }
    /**
     * 券種を削除する
     */
    deleteTicketType(params) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.ticketTypeModel.findOneAndRemove({
                _id: params.id
            }).exec();
        });
    }
}
exports.MongoRepository = MongoRepository;