UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

152 lines (151 loc) 6.96 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AggregateReservationRepo = void 0; const factory = require("../factory"); const settings_1 = require("../settings"); const aggregateReservation_1 = require("./mongoose/schemas/aggregateReservation"); /** * 予約集計リポジトリ */ class AggregateReservationRepo { constructor(connection) { this.aggregateReservationModel = connection.model(aggregateReservation_1.modelName, (0, aggregateReservation_1.createSchema)()); } static CREATE_MONGO_CONDITIONS(conditions) { var _a, _b, _c, _d, _e, _f, _g, _h, _j; const matchStages = []; const projectIdEq = (_b = (_a = conditions.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$eq; if (typeof projectIdEq === 'string') { matchStages.push({ $match: { 'project.id': { $eq: projectIdEq } } }); } const typeOfEq = (_c = conditions.reservationFor) === null || _c === void 0 ? void 0 : _c.typeOf; if (typeof typeOfEq === 'string') { matchStages.push({ $match: { 'reservationFor.typeOf': { $eq: typeOfEq } } }); } const idEq = (_e = (_d = conditions.reservationFor) === null || _d === void 0 ? void 0 : _d.id) === null || _e === void 0 ? void 0 : _e.$eq; if (typeof idEq === 'string') { matchStages.push({ $match: { 'reservationFor.id': { $eq: idEq } } }); } const idIn = (_g = (_f = conditions.reservationFor) === null || _f === void 0 ? void 0 : _f.id) === null || _g === void 0 ? void 0 : _g.$in; if (Array.isArray(idIn)) { matchStages.push({ $match: { 'reservationFor.id': { $in: idIn } } }); } const reservationForStartDateGte = (_h = conditions.reservationFor) === null || _h === void 0 ? void 0 : _h.startFrom; if (reservationForStartDateGte instanceof Date) { matchStages.push({ $match: { 'reservationFor.startDate': { $gte: reservationForStartDateGte } } }); } const reservationForStartDateLte = (_j = conditions.reservationFor) === null || _j === void 0 ? void 0 : _j.startThrough; if (reservationForStartDateLte instanceof Date) { matchStages.push({ $match: { 'reservationFor.startDate': { $lte: reservationForStartDateLte } } }); } return matchStages; } /** * 予約集計を検索する */ searchWithReservationForId(params, inclusion) { return __awaiter(this, void 0, void 0, function* () { var _a; const matchStages = AggregateReservationRepo.CREATE_MONGO_CONDITIONS(params); let projectStage = { _id: 0, id: '$reservationFor.id' }; if (Array.isArray(inclusion) && inclusion.length > 0) { inclusion.forEach((field) => { projectStage[field] = { $ifNull: [`$${field}`, '$false'] }; // projectStage[field] = 1; }); } else { projectStage = { _id: 0, id: '$reservationFor.id', // aggregateEntranceGate: 1, // discontinue(2024-12-23~) aggregateOffer: 1 }; } const sortByStartDate = (_a = params.sort) === null || _a === void 0 ? void 0 : _a['reservationFor.startDate']; const aggregate = this.aggregateReservationModel.aggregate([ ...matchStages, ...(typeof sortByStartDate === 'number') ? [{ $sort: { 'reservationFor.startDate': sortByStartDate } }] : [], // 現時点でreservationFor.idへのunique indexがないので、重複ドキュメント対応として、$group { $group: { _id: '$reservationFor.id', reservationFor: { $first: '$reservationFor' }, aggregateEntranceGate: { $first: '$aggregateEntranceGate' }, aggregateOffer: { $first: '$aggregateOffer' } } }, { $project: projectStage } ]); if (typeof params.limit === 'number' && params.limit > 0) { const page = (typeof params.page === 'number' && params.page > 0) ? params.page : 1; aggregate.limit(params.limit * page) .skip(params.limit * (page - 1)); } return aggregate .option({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec(); }); } /** * 予約集計を保管する */ save(filter, update) { return __awaiter(this, void 0, void 0, function* () { const { $set } = update; const setOnInsert = { project: { id: filter.project.id, typeOf: factory.organizationType.Project }, typeOf: 'AggregateReservation', reservationFor: filter.reservationFor }; const doc = yield this.aggregateReservationModel.findOneAndUpdate({ 'project.id': { $eq: filter.project.id }, 'reservationFor.id': { $eq: filter.reservationFor.id } }, { $set: Object.assign({}, $set), $setOnInsert: setOnInsert }, { new: true, upsert: true, projection: { _id: 1 } }) .exec(); if (doc === null) { throw new factory.errors.NotFound(this.aggregateReservationModel.modelName); } }); } unsetUnnecessaryFields(params) { return __awaiter(this, void 0, void 0, function* () { return this.aggregateReservationModel.updateMany(params.filter, { $unset: params.$unset }, { timestamps: false }) .exec(); }); } } exports.AggregateReservationRepo = AggregateReservationRepo;