UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

174 lines (173 loc) 7.66 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.AggregateOrderRepo = void 0; const factory = require("../factory"); const settings_1 = require("../settings"); const aggregateOrder_1 = require("./mongoose/schemas/aggregateOrder"); /** * 注文集計リポジトリ */ class AggregateOrderRepo { constructor(connection) { this.aggregateOrderModel = connection.model(aggregateOrder_1.modelName, (0, aggregateOrder_1.createSchema)()); } // public static CREATE_MONGO_CONDITIONS( // conditions: ISearchConditions // ): IMatchStage[] { // const matchStages: IMatchStage[] = []; // const projectIdEq = conditions.project?.id?.$eq; // if (typeof projectIdEq === 'string') { // matchStages.push({ // $match: { 'project.id': { $eq: projectIdEq } } // }); // } // const typeOfEq = conditions.reservationFor?.typeOf; // if (typeof typeOfEq === 'string') { // matchStages.push({ // $match: { 'reservationFor.typeOf': { $eq: typeOfEq } } // }); // } // const idEq = conditions.reservationFor?.id?.$eq; // if (typeof idEq === 'string') { // matchStages.push({ // $match: { 'reservationFor.id': { $eq: idEq } } // }); // } // const idIn = conditions.reservationFor?.id?.$in; // if (Array.isArray(idIn)) { // matchStages.push({ // $match: { 'reservationFor.id': { $in: idIn } } // }); // } // const reservationForStartDateGte = conditions.reservationFor?.startFrom; // if (reservationForStartDateGte instanceof Date) { // matchStages.push({ // $match: { 'reservationFor.startDate': { $gte: reservationForStartDateGte } } // }); // } // const reservationForStartDateLte = conditions.reservationFor?.startThrough; // if (reservationForStartDateLte instanceof Date) { // matchStages.push({ // $match: { 'reservationFor.startDate': { $lte: reservationForStartDateLte } } // }); // } // return matchStages; // } // /** // * 予約集計を検索する // */ // public async searchWithReservationForId( // params: ISearchConditions, // inclusion: ('aggregateOffer')[] // ): Promise<ISearchWithReservationForIdResult[]> { // const matchStages = AggregateReservationRepo.CREATE_MONGO_CONDITIONS(params); // let projectStage: { [field in IKeyOfProjection]?: AnyExpression } = { // _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 = params.sort?.['reservationFor.startDate']; // const aggregate = this.aggregateReservationModel.aggregate<ISearchWithReservationForIdResult>([ // ...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: number = (typeof params.page === 'number' && params.page > 0) ? params.page : 1; // aggregate.limit(params.limit * page) // .skip(params.limit * (page - 1)); // } // return aggregate // .option({ maxTimeMS: 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: filter.typeOf, identifier: filter.identifier }; const doc = yield this.aggregateOrderModel.findOneAndUpdate({ 'project.id': { $eq: filter.project.id }, identifier: { $eq: filter.identifier }, typeOf: { $eq: filter.typeOf } }, { $set, $setOnInsert: setOnInsert }, { new: true, upsert: true, projection: { _id: 0, id: { $toString: '$_id' } } }) .lean() .setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec(); if (doc === null) { throw new factory.errors.NotFound(this.aggregateOrderModel.modelName); } }); } searchRoyalCustomers(params) { return __awaiter(this, void 0, void 0, function* () { const { limit, project, aggregateOrder } = params; const query = this.aggregateOrderModel.find({ typeOf: { $eq: factory.personType.Person }, 'project.id': { $eq: project.id }, 'aggregateOrder.orderCount': { $exists: true, $gte: aggregateOrder.orderCount.$gte } }, { _id: 0, aggregateOrder: 1, identifier: 1 }) .sort({ 'aggregateOrder.orderCount': factory.sortType.Descending }) .limit(limit); // if (typeof params.limit === 'number' && params.limit > 0) { // const page: number = (typeof params.page === 'number' && params.page > 0) ? params.page : 1; // aggregate.limit(params.limit * page) // .skip(params.limit * (page - 1)); // } return query .setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .lean() .exec(); }); } } exports.AggregateOrderRepo = AggregateOrderRepo;