Source: repo/creativeWork.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 creativeWork_1 = require("./mongoose/model/creativeWork");
const factory = require("../factory");
/**
 * 作品抽象リポジトリ
 */
class Repository {
}
exports.Repository = Repository;
/**
 * 作品リポジトリ
 */
class MongoRepository {
    constructor(connection) {
        this.creativeWorkModel = connection.model(creativeWork_1.default.modelName);
    }
    static CREATE_MONGO_CONDITIONS(params) {
        // MongoDB検索条件
        const andConditions = [
            {
                typeOf: factory.creativeWorkType.Movie
            }
        ];
        if (params.project !== undefined) {
            if (Array.isArray(params.project.ids)) {
                andConditions.push({
                    'project.id': {
                        $exists: true,
                        $in: params.project.ids
                    }
                });
            }
        }
        if (params.identifier !== undefined) {
            andConditions.push({
                identifier: {
                    $exists: true,
                    $regex: new RegExp(params.identifier, 'i')
                }
            });
        }
        if (params.name !== undefined) {
            andConditions.push({
                name: {
                    $exists: true,
                    $regex: new RegExp(params.name, 'i')
                }
            });
        }
        // tslint:disable-next-line:no-single-line-block-comment
        /* istanbul ignore else */
        if (params.datePublishedFrom !== undefined) {
            andConditions.push({
                datePublished: {
                    $exists: true,
                    $gte: params.datePublishedFrom
                }
            });
        }
        // tslint:disable-next-line:no-single-line-block-comment
        /* istanbul ignore else */
        if (params.datePublishedThrough !== undefined) {
            andConditions.push({
                datePublished: {
                    $exists: true,
                    $lte: params.datePublishedThrough
                }
            });
        }
        // tslint:disable-next-line:no-single-line-block-comment
        /* istanbul ignore else */
        if (params.offers !== undefined) {
            // tslint:disable-next-line:no-single-line-block-comment
            /* istanbul ignore else */
            if (params.offers.availableFrom instanceof Date) {
                andConditions.push({
                    'offers.availabilityEnds': {
                        $exists: true,
                        $gt: params.offers.availableFrom
                    }
                });
            }
            // tslint:disable-next-line:no-single-line-block-comment
            /* istanbul ignore else */
            if (params.offers.availableThrough instanceof Date) {
                andConditions.push({
                    'offers.availabilityStarts': {
                        $exists: true,
                        $lt: params.offers.availableThrough
                    }
                });
            }
        }
        return andConditions;
    }
    /**
     * 映画作品を保管する
     */
    saveMovie(params) {
        return __awaiter(this, void 0, void 0, function* () {
            let doc;
            if (params.id === '') {
                doc = yield this.creativeWorkModel.create(params);
            }
            else {
                doc = yield this.creativeWorkModel.findOneAndUpdate({ _id: params.id }, params, { upsert: false, new: true })
                    .exec();
                if (doc === null) {
                    throw new factory.errors.NotFound(this.creativeWorkModel.modelName);
                }
            }
            return doc.toObject();
        });
    }
    findMovieById(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.creativeWorkModel.findOne({ _id: params.id }, {
                __v: 0,
                createdAt: 0,
                updatedAt: 0
            })
                .exec();
            if (doc === null) {
                throw new factory.errors.NotFound(this.creativeWorkModel.modelName);
            }
            return doc.toObject();
        });
    }
    countMovies(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
            return this.creativeWorkModel.countDocuments({ $and: conditions })
                .setOptions({ maxTimeMS: 10000 })
                .exec();
        });
    }
    /**
     * 映画作品を検索する
     */
    searchMovies(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
            const query = this.creativeWorkModel.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));
            }
            // 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()));
        });
    }
    /**
     * 映画作品を削除する
     */
    deleteMovie(params) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.creativeWorkModel.findOneAndRemove({ _id: params.id })
                .exec();
        });
    }
}
exports.MongoRepository = MongoRepository;