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 uniqid = require("uniqid");
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 = [];
        // 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 (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 } });
        }
        // tslint:disable-next-line:no-single-line-block-comment
        /* istanbul ignore else */
        if (Array.isArray(params.identifiers)) {
            andConditions.push({ identifier: { $in: params.identifiers } });
        }
        return andConditions;
    }
    save(params) {
        return __awaiter(this, void 0, void 0, function* () {
            let doc;
            if (params.id === '') {
                const id = uniqid();
                doc = yield this.serviceTypeModel.create(Object.assign({}, params, { _id: id }));
            }
            else {
                doc = yield this.serviceTypeModel.findOneAndUpdate({ _id: params.id }, params, { upsert: false, new: true })
                    .exec();
                if (doc === null) {
                    throw new factory.errors.NotFound(this.serviceTypeModel.modelName);
                }
            }
            return doc.toObject();
        });
    }
    count(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
            return this.serviceTypeModel.countDocuments((conditions.length > 0) ? { $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((conditions.length > 0) ? { $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()));
        });
    }
    findById(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.serviceTypeModel.findOne({ _id: params.id }, {
                __v: 0,
                createdAt: 0,
                updatedAt: 0
            })
                .exec();
            if (doc === null) {
                throw new factory.errors.NotFound(this.serviceTypeModel.modelName);
            }
            return doc.toObject();
        });
    }
    /**
     * 削除する
     */
    deleteById(params) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.serviceTypeModel.findOneAndRemove({ _id: params.id })
                .exec();
        });
    }
}
exports.MongoRepository = MongoRepository;