Source: repo/distributions.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 distributions_1 = require("./mongoose/model/distributions");
const factory = require("../factory");
/**
 * 配給レポジトリー
 */
class MongoRepository {
    constructor(connection) {
        this.distributionsModel = connection.model(distributions_1.default.modelName);
    }
    static CREATE_MONGO_CONDITIONS(params) {
        // MongoDB検索条件
        const andConditions = [
            { _id: { $exists: true } }
        ];
        // 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 (params.id !== undefined) {
            andConditions.push({ _id: params.id });
        }
        return andConditions;
    }
    /**
     * 配給を保管する
     */
    updateDistribution(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.distributionsModel.findOneAndUpdate({ _id: params.id }, { name: params.name }, { upsert: false, new: true })
                .exec();
            if (doc === null) {
                throw new factory.errors.NotFound('Distribution');
            }
            return doc.toObject();
        });
    }
    /**
     * 配給を作成する
     */
    createDistribution(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.distributionsModel.create({
                _id: params.id,
                name: params.name
            });
            return doc.toObject();
        });
    }
    getDistributions() {
        return __awaiter(this, void 0, void 0, function* () {
            const query = this.distributionsModel.find({});
            return query.setOptions({ maxTimeMS: 10000 })
                .exec()
                .then((docs) => docs.map((doc) => doc.toObject()));
        });
    }
    countDistributions(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
            return this.distributionsModel.countDocuments({ $and: conditions })
                .setOptions({ maxTimeMS: 10000 })
                .exec();
        });
    }
    /**
     * 配給を検索する
     */
    searchDistributions(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
            const query = this.distributionsModel.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 event = yield this.distributionsModel.findOne({
                _id: params.id
            }, {
                __v: 0,
                createdAt: 0,
                updatedAt: 0
            })
                .exec();
            if (event === null) {
                throw new factory.errors.NotFound('Distribution');
            }
            return event.toObject();
        });
    }
    /**
     * 配給削除
     */
    deleteById(params) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.distributionsModel.findOneAndRemove({
                _id: params.id
            })
                .exec();
        });
    }
}
exports.MongoRepository = MongoRepository;