Source: repo/priceSpecification.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 priceSpecification_1 = require("./mongoose/model/priceSpecification");
/**
 * 価格仕様リポジトリ
 */
class MongoRepository {
    constructor(connection) {
        this.priceSpecificationModel = connection.model(priceSpecification_1.default.modelName);
    }
    // public static CREATE_COMPOUND_PRICE_SPECIFICATION_MONGO_CONDITIONS(
    //     params: factory.compoundPriceSpecification.ISearchConditions<factory.priceSpecificationType>
    // ) {
    //     const andConditions: any[] = [
    //         {
    //             typeOf: params.typeOf
    //         }
    //     ];
    //     // tslint:disable-next-line:no-single-line-block-comment
    //     /* istanbul ignore else */
    //     if (params.validFrom !== undefined) {
    //         andConditions.push({
    //             validThrough: { $exists: true, $gt: params.validFrom }
    //         });
    //     }
    //     // tslint:disable-next-line:no-single-line-block-comment
    //     /* istanbul ignore else */
    //     if (params.validThrough !== undefined) {
    //         andConditions.push({
    //             validFrom: { $exists: true, $lt: params.validThrough }
    //         });
    //     }
    //     // tslint:disable-next-line:no-single-line-block-comment
    //     /* istanbul ignore else */
    //     if (params.priceComponent !== undefined) {
    //         andConditions.push({
    //             'priceComponent.typeOf': { $exists: true, $eq: params.priceComponent.typeOf }
    //         });
    //     }
    //     return andConditions;
    // }
    static CREATE_MONGO_CONDITIONS(params) {
        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.typeOf !== undefined) {
            andConditions.push({
                typeOf: params.typeOf
            });
        }
        if (Array.isArray(params.appliesToVideoFormats)) {
            andConditions.push({
                appliesToVideoFormat: {
                    $exists: true,
                    $in: params.appliesToVideoFormats
                }
            });
        }
        if (Array.isArray(params.appliesToSoundFormats)) {
            andConditions.push({
                appliesToSoundFormat: {
                    $exists: true,
                    $in: params.appliesToSoundFormats
                }
            });
        }
        if (params.appliesToMovieTicket !== undefined) {
            if (Array.isArray(params.appliesToMovieTicket.serviceTypes)) {
                andConditions.push({
                    appliesToMovieTicketType: {
                        $exists: true,
                        $in: params.appliesToMovieTicket.serviceTypes
                    }
                });
            }
        }
        // tslint:disable-next-line:no-single-line-block-comment
        /* istanbul ignore else */
        if (params.validFrom !== undefined) {
            andConditions.push({
                validThrough: {
                    $exists: true,
                    $gt: params.validFrom
                }
            });
        }
        // tslint:disable-next-line:no-single-line-block-comment
        /* istanbul ignore else */
        if (params.validThrough !== undefined) {
            andConditions.push({
                validFrom: {
                    $exists: true,
                    $lt: params.validThrough
                }
            });
        }
        return andConditions;
    }
    // public async countCompoundPriceSpecifications<T extends factory.priceSpecificationType>(
    //     params: factory.compoundPriceSpecification.ISearchConditions<T>
    // ): Promise<number> {
    //     const conditions = MongoRepository.CREATE_COMPOUND_PRICE_SPECIFICATION_MONGO_CONDITIONS(params);
    //     return this.priceSpecificationModel.countDocuments(
    //         { $and: conditions }
    //     )
    //         .setOptions({ maxTimeMS: 10000 })
    //         .exec();
    // }
    // public async searchCompoundPriceSpecifications<T extends factory.priceSpecificationType>(
    //     params: factory.compoundPriceSpecification.ISearchConditions<T>
    // ): Promise<factory.compoundPriceSpecification.IPriceSpecification<T>[]> {
    //     const conditions = MongoRepository.CREATE_COMPOUND_PRICE_SPECIFICATION_MONGO_CONDITIONS(params);
    //     const query = this.priceSpecificationModel.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()));
    // }
    count(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
            return this.priceSpecificationModel.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.priceSpecificationModel.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()));
        });
    }
}
exports.MongoRepository = MongoRepository;