UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

214 lines (213 loc) 10.8 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.AccountingReportRepo = void 0; const accountingReport_1 = require("./mongoose/schemas/accountingReport"); const errorHandler_1 = require("../errorHandler"); const factory = require("../factory"); const DEFAULT_SEARCH_LIMIT = 100; /** * 経理レポートリポジトリ */ class AccountingReportRepo { constructor(connection) { this.accountingReportModel = connection.model(accountingReport_1.modelName, (0, accountingReport_1.createSchema)()); } static CREATE_MONGO_CONDITIONS(params) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0; const matchStages = []; const projectIdEq = (_b = (_a = params.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 } } }); } // req.seller.idを考慮(2023-07-21~) if (typeof ((_c = params.seller) === null || _c === void 0 ? void 0 : _c.id) === 'string') { matchStages.push({ $match: { 'mainEntity.seller.id': { $exists: true, $eq: params.seller.id } } }); } const orderNumberEq = (_e = (_d = params.order) === null || _d === void 0 ? void 0 : _d.orderNumber) === null || _e === void 0 ? void 0 : _e.$eq; if (typeof orderNumberEq === 'string') { matchStages.push({ $match: { 'mainEntity.orderNumber': { $eq: orderNumberEq } } }); } const sellerIdEq = (_h = (_g = (_f = params.order) === null || _f === void 0 ? void 0 : _f.seller) === null || _g === void 0 ? void 0 : _g.id) === null || _h === void 0 ? void 0 : _h.$eq; if (typeof sellerIdEq === 'string') { matchStages.push({ $match: { 'mainEntity.seller.id': { $exists: true, $eq: sellerIdEq } } }); } const paymentMethodIdEq = (_l = (_k = (_j = params.order) === null || _j === void 0 ? void 0 : _j.paymentMethods) === null || _k === void 0 ? void 0 : _k.paymentMethodId) === null || _l === void 0 ? void 0 : _l.$eq; if (typeof paymentMethodIdEq === 'string') { matchStages.push({ $match: { 'mainEntity.paymentMethods.paymentMethodId': { $exists: true, $eq: paymentMethodIdEq } } }); } const orderDateGte = (_o = (_m = params.order) === null || _m === void 0 ? void 0 : _m.orderDate) === null || _o === void 0 ? void 0 : _o.$gte; if (orderDateGte instanceof Date) { matchStages.push({ $match: { 'mainEntity.orderDate': { $gte: orderDateGte } } }); } const orderDateLte = (_q = (_p = params.order) === null || _p === void 0 ? void 0 : _p.orderDate) === null || _q === void 0 ? void 0 : _q.$lte; if (orderDateLte instanceof Date) { matchStages.push({ $match: { 'mainEntity.orderDate': { $lte: orderDateLte } } }); } const reservationForStartDateGte = (_v = (_u = (_t = (_s = (_r = params.order) === null || _r === void 0 ? void 0 : _r.acceptedOffers) === null || _s === void 0 ? void 0 : _s.itemOffered) === null || _t === void 0 ? void 0 : _t.reservationFor) === null || _u === void 0 ? void 0 : _u.startDate) === null || _v === void 0 ? void 0 : _v.$gte; if (reservationForStartDateGte instanceof Date) { matchStages.push({ $match: { 'mainEntity.acceptedOffers.itemOffered.reservationFor.startDate': { $exists: true, $gte: reservationForStartDateGte } } }); } const reservationForStartDateLte = (_0 = (_z = (_y = (_x = (_w = params.order) === null || _w === void 0 ? void 0 : _w.acceptedOffers) === null || _x === void 0 ? void 0 : _x.itemOffered) === null || _y === void 0 ? void 0 : _y.reservationFor) === null || _z === void 0 ? void 0 : _z.startDate) === null || _0 === void 0 ? void 0 : _0.$lte; if (reservationForStartDateLte instanceof Date) { matchStages.push({ $match: { 'mainEntity.acceptedOffers.itemOffered.reservationFor.startDate': { $exists: true, $lte: reservationForStartDateLte } } }); } return matchStages; } // public async createIfNotExist(params: IAccountingReport) { // try { // await this.accountingReportModel.findOneAndUpdate( // { 'mainEntity.orderNumber': params.mainEntity.orderNumber }, // { $setOnInsert: params }, // { upsert: true } // ) // .exec(); // } catch (error) { // let throwsError = true; // if (await isMongoError(error)) { // // すでにorderNumberが存在する場合ok // if (error.code === MongoErrorCode.DuplicateKey) { // throwsError = false; // } // } // if (throwsError) { // throw error; // } // } // } syncMainEntity(params) { return __awaiter(this, void 0, void 0, function* () { const setOnInsert = { project: { id: params.project.id, typeOf: factory.organizationType.Project }, typeOf: 'Report', hasPart: [], mainEntity: { orderNumber: params.mainEntity.orderNumber // orderNumberのみが最低限必要なのでなければ作成 } }; try { // なければ作成 yield this.accountingReportModel.updateOne({ 'mainEntity.orderNumber': { $eq: params.mainEntity.orderNumber } }, { $setOnInsert: setOnInsert }, { upsert: true }) .exec(); } catch (error) { let throwsError = true; if (yield (0, errorHandler_1.isMongoError)(error)) { // すでにorderNumberが存在する場合ok if (error.code === errorHandler_1.MongoErrorCode.DuplicateKey) { throwsError = false; } } if (throwsError) { throw error; } } // mainEntityを同期 yield this.accountingReportModel.updateOne({ 'mainEntity.orderNumber': { $eq: params.mainEntity.orderNumber } }, { $set: { mainEntity: params.mainEntity } }) .exec(); }); } /** * 注文番号で削除する */ deleteByOrderNumber(params) { return __awaiter(this, void 0, void 0, function* () { yield this.accountingReportModel.deleteOne({ 'mainEntity.orderNumber': params.mainEntity.orderNumber }) .exec(); }); } /** * 注文にアクションレポートを追加する * 注文に対する経理レポート自体が未作成であれば自動的に作成する */ addChildReport(params) { return __awaiter(this, void 0, void 0, function* () { const setOnInsert = { project: { id: params.project.id, typeOf: factory.organizationType.Project }, typeOf: 'Report', // hasPart: [], mainEntity: { orderNumber: params.mainEntity.orderNumber // orderNumberのみが最低限必要なのでなければ作成 } }; const doc = yield this.accountingReportModel.findOneAndUpdate({ 'mainEntity.orderNumber': { $eq: params.mainEntity.orderNumber } }, { $addToSet: { hasPart: params.hasPart }, $setOnInsert: setOnInsert }, { projection: { _id: 1 }, upsert: true }) .exec(); if (doc === null) { throw new factory.errors.NotFound(this.accountingReportModel.modelName); } }); } search(params) { return __awaiter(this, void 0, void 0, function* () { const limit = (typeof params.limit === 'number') ? Math.min(params.limit, DEFAULT_SEARCH_LIMIT) : DEFAULT_SEARCH_LIMIT; const page = (typeof params.page === 'number') ? Math.max(params.page, 1) : 1; const unwindAcceptedOffers = params.$unwindAcceptedOffers === '1'; const matchStages = AccountingReportRepo.CREATE_MONGO_CONDITIONS(params); const aggregate = this.accountingReportModel.aggregate([ // pipelineの順序に注意 // @see https://docs.mongodb.com/manual/reference/operator/aggregation/sort/ { $sort: { 'mainEntity.orderDate': factory.sortType.Descending } }, { $unwind: '$hasPart' }, ...(unwindAcceptedOffers) ? [{ $unwind: '$mainEntity.acceptedOffers' }] : [], ...matchStages, { $project: { _id: 0, mainEntity: '$hasPart.mainEntity', isPartOf: { mainEntity: '$mainEntity' } } } ]); return aggregate.allowDiskUse(true) .limit(limit * page) .skip(limit * (page - 1)) // .setOptions({ maxTimeMS: 10000 }) .exec(); }); } unsetUnnecessaryFields(params) { return __awaiter(this, void 0, void 0, function* () { return this.accountingReportModel.updateMany(params.filter, { $unset: params.$unset }, { timestamps: false }) .exec(); }); } } exports.AccountingReportRepo = AccountingReportRepo;