Source: repo/serviceType.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 serviceType_1 = require("./mongoose/model/serviceType");
const factory = require("../factory");
/**
 * 興行区分リポジトリ
 */
class MongoRepository {
    constructor(connection) {
        this.serviceTypeModel = connection.model(serviceType_1.default.modelName);
    }
    static CREATE_MONGO_CONDITIONS(params) {
        // MongoDB検索条件
        const andConditions = [{ typeOf: 'ServiceType' }];
        // tslint:disable-next-line:no-single-line-block-comment
        /* istanbul ignore else */
        if (params.name !== undefined) {
            andConditions.push({ name: new RegExp(params.name, 'i') });
        }
        // tslint:disable-next-line:no-single-line-block-comment
        /* istanbul ignore else */
        if (Array.isArray(params.ids)) {
            andConditions.push({ _id: { $in: params.ids } });
        }
        return andConditions;
    }
    /**
     * 保管
     */
    save(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.serviceTypeModel.findOneAndUpdate({
                typeOf: 'ServiceType',
                _id: params.id
            }, params, { upsert: true, new: true }).exec();
            if (doc === null) {
                throw new factory.errors.NotFound('ServiceType');
            }
            return doc.toObject();
        });
    }
    count(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
            return this.serviceTypeModel.countDocuments({ $and: conditions }).setOptions({ maxTimeMS: 10000 }).exec();
        });
    }
    /**
     * 検索
     */
    search(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
            const query = this.serviceTypeModel.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));
            }
            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.serviceTypeModel.findOne({
                typeOf: 'ServiceType',
                _id: params.id
            }, {
                __v: 0,
                createdAt: 0,
                updatedAt: 0
            }).exec();
            if (doc === null) {
                throw new factory.errors.NotFound('ServiceType');
            }
            return doc.toObject();
        });
    }
}
exports.MongoRepository = MongoRepository;