UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

1,036 lines 60 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.OrderRepo = void 0; const factory = require("../factory"); const order_1 = require("./mongoose/schemas/order"); const errorHandler_1 = require("../errorHandler"); const settings_1 = require("../settings"); /** * ドキュメント検索時に指定可能なprojectionフィールドを厳密に定義する */ const AVAILABLE_PROJECT_FIELDS = [ 'broker', 'confirmationNumber', 'customer', 'dateReturned', 'identifier', 'name', 'orderDate', 'orderNumber', 'orderStatus', 'orderedItem', 'paymentMethods', 'previousOrderStatus', 'price', 'priceCurrency', 'project', 'returner', 'seller', 'typeOf', 'url', 'paymentMethods.accountId', 'paymentMethods.additionalProperty', 'paymentMethods.issuedThrough', 'paymentMethods.name', 'paymentMethods.paymentMethod', 'paymentMethods.paymentMethodId', 'paymentMethods.totalPaymentDue' ]; /** * 注文リポジトリ */ class OrderRepo { constructor(connection) { this.orderModel = connection.model(order_1.modelName, (0, order_1.createSchema)()); } // tslint:disable-next-line:cyclomatic-complexity max-func-body-length 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, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26; const andConditions = [ { typeOf: { $eq: factory.order.OrderType.Order } } ]; 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') { andConditions.push({ 'project.id': { $eq: projectIdEq } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.identifier !== undefined) { // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(params.identifier.$all)) { andConditions.push({ identifier: { $exists: true, $all: params.identifier.$all } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(params.identifier.$in)) { andConditions.push({ identifier: { $exists: true, $in: params.identifier.$in } }); } } const providerIdEq = (_d = (_c = params.provider) === null || _c === void 0 ? void 0 : _c.id) === null || _d === void 0 ? void 0 : _d.$eq; if (typeof providerIdEq === 'string') { andConditions.push({ 'seller.id': { $exists: true, $eq: providerIdEq } }); } const sellerIdEq = (_f = (_e = params.seller) === null || _e === void 0 ? void 0 : _e.id) === null || _f === void 0 ? void 0 : _f.$eq; if (typeof sellerIdEq === 'string') { andConditions.push({ 'seller.id': { $exists: true, $eq: sellerIdEq } }); } const sellerIds = (_g = params.seller) === null || _g === void 0 ? void 0 : _g.ids; if (Array.isArray(sellerIds)) { if (sellerIds.length === 1) { // use $eq(2025-04-01~) andConditions.push({ 'seller.id': { $exists: true, $eq: sellerIds.at(0) } }); } else { andConditions.push({ 'seller.id': { $exists: true, $in: sellerIds } }); } } const brokerIdEq = (_j = (_h = params.broker) === null || _h === void 0 ? void 0 : _h.id) === null || _j === void 0 ? void 0 : _j.$eq; if (typeof brokerIdEq === 'string') { andConditions.push({ 'broker.id': { $exists: true, $eq: brokerIdEq } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.customer !== undefined) { // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.customer.typeOf !== undefined) { andConditions.push({ 'customer.typeOf': { $exists: true, $eq: params.customer.typeOf } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(params.customer.ids)) { andConditions.push({ 'customer.id': { $exists: true, $in: params.customer.ids } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(params.customer.identifiers)) { andConditions.push({ 'customer.identifier': { $exists: true, $in: params.customer.identifiers } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.customer.identifier !== undefined) { // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(params.customer.identifier.$all)) { andConditions.push({ 'customer.identifier': { $exists: true, $all: params.customer.identifier.$all } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(params.customer.identifier.$in)) { andConditions.push({ 'customer.identifier': { $exists: true, $in: params.customer.identifier.$in } }); } } if (params.customer.additionalProperty !== undefined && params.customer.additionalProperty !== null) { if (Array.isArray(params.customer.additionalProperty.$all)) { andConditions.push({ 'customer.additionalProperty': { $exists: true, $all: params.customer.additionalProperty.$all } }); } if (Array.isArray(params.customer.additionalProperty.$in)) { andConditions.push({ 'customer.additionalProperty': { $exists: true, $in: params.customer.additionalProperty.$in } }); } } if (params.customer.memberOf !== undefined && params.customer.memberOf !== null) { if (params.customer.memberOf.membershipNumber !== undefined && params.customer.memberOf.membershipNumber !== null) { if (typeof params.customer.memberOf.membershipNumber.$eq === 'string') { andConditions.push({ 'customer.memberOf.membershipNumber': { $exists: true, $eq: params.customer.memberOf.membershipNumber.$eq } }); } if (Array.isArray(params.customer.memberOf.membershipNumber.$in)) { andConditions.push({ 'customer.memberOf.membershipNumber': { $exists: true, $in: params.customer.memberOf.membershipNumber.$in } }); } } } if (typeof params.customer.givenName === 'string') { if (params.customer.givenName.length > 0) { andConditions.push({ 'customer.givenName': { $exists: true, $regex: new RegExp(params.customer.givenName) } }); } } else if (params.customer.givenName !== undefined && params.customer.givenName !== null) { if (typeof params.customer.givenName.$eq === 'string') { andConditions.push({ 'customer.givenName': { $exists: true, $eq: params.customer.givenName.$eq } }); } if (typeof params.customer.givenName.$regex === 'string' && params.customer.givenName.$regex.length > 0) { andConditions.push({ 'customer.givenName': { $exists: true, $regex: new RegExp(params.customer.givenName.$regex) } }); } } if (typeof params.customer.familyName === 'string') { if (params.customer.familyName.length > 0) { andConditions.push({ 'customer.familyName': { $exists: true, $regex: new RegExp(params.customer.familyName) } }); } } else if (params.customer.familyName !== undefined && params.customer.familyName !== null) { if (typeof params.customer.familyName.$eq === 'string') { andConditions.push({ 'customer.familyName': { $exists: true, $eq: params.customer.familyName.$eq } }); } if (typeof params.customer.familyName.$regex === 'string' && params.customer.familyName.$regex.length > 0) { andConditions.push({ 'customer.familyName': { $exists: true, $regex: new RegExp(params.customer.familyName.$regex) } }); } } if (typeof params.customer.email === 'string') { if (params.customer.email.length > 0) { andConditions.push({ 'customer.email': { $exists: true, $regex: new RegExp(params.customer.email) } }); } } else if (params.customer.email !== undefined && params.customer.email !== null) { if (typeof params.customer.email.$eq === 'string') { andConditions.push({ 'customer.email': { $exists: true, $eq: params.customer.email.$eq } }); } if (typeof params.customer.email.$regex === 'string' && params.customer.email.$regex.length > 0) { andConditions.push({ 'customer.email': { $exists: true, $regex: new RegExp(params.customer.email.$regex) } }); } } if (typeof params.customer.telephone === 'string') { if (params.customer.telephone.length > 0) { andConditions.push({ 'customer.telephone': { $exists: true, $regex: new RegExp(params.customer.telephone) } }); } } else if (params.customer.telephone !== undefined && params.customer.telephone !== null) { if (typeof params.customer.telephone.$eq === 'string') { andConditions.push({ 'customer.telephone': { $exists: true, $eq: params.customer.telephone.$eq } }); } if (typeof params.customer.telephone.$regex === 'string' && params.customer.telephone.$regex.length > 0) { andConditions.push({ 'customer.telephone': { $exists: true, $regex: new RegExp(params.customer.telephone.$regex) } }); } } } const nameEq = (_k = params.name) === null || _k === void 0 ? void 0 : _k.$eq; if (typeof nameEq === 'string') { andConditions.push({ name: { $exists: true, $eq: nameEq } }); } const nameRegex = (_l = params.name) === null || _l === void 0 ? void 0 : _l.$regex; if (typeof nameRegex === 'string' && nameRegex.length > 0) { andConditions.push({ name: { $exists: true, $regex: new RegExp(nameRegex) } }); } if (Array.isArray(params.orderNumbers)) { if (params.orderNumbers.length === 1) { // use $eq(2025-04-01~) andConditions.push({ orderNumber: { $eq: params.orderNumbers.at(0) } }); } else { andConditions.push({ orderNumber: { $in: params.orderNumbers } }); } } if (Array.isArray(params.orderStatuses)) { if (params.orderStatuses.length === 1) { // use $eq(2025-04-01~) andConditions.push({ orderStatus: { $eq: params.orderStatuses.at(0) } }); } else { andConditions.push({ orderStatus: { $in: params.orderStatuses } }); } } if (Array.isArray(params.confirmationNumbers)) { if (params.confirmationNumbers.length === 1) { // use $eq(2025-04-01~) andConditions.push({ confirmationNumber: { $exists: true, $eq: params.confirmationNumbers.at(0) } }); } else { andConditions.push({ confirmationNumber: { $exists: true, $in: params.confirmationNumbers } }); } } const orderedItemSize = (_m = params.orderedItem) === null || _m === void 0 ? void 0 : _m.$size; if (typeof orderedItemSize === 'number') { andConditions.push({ orderedItem: { $size: orderedItemSize } }); } const acceptedOffersSize = (_o = params.acceptedOffers) === null || _o === void 0 ? void 0 : _o.$size; if (typeof acceptedOffersSize === 'number') { andConditions.push({ acceptedOffers: { $size: acceptedOffersSize } }); } const serialNumberEq = (_q = (_p = params.acceptedOffers) === null || _p === void 0 ? void 0 : _p.serialNumber) === null || _q === void 0 ? void 0 : _q.$eq; if (typeof serialNumberEq === 'string') { andConditions.push({ 'acceptedOffers.serialNumber': { $exists: true, $eq: serialNumberEq } }); } const itemOfferedIdentifierIn = (_t = (_s = (_r = params.acceptedOffers) === null || _r === void 0 ? void 0 : _r.itemOffered) === null || _s === void 0 ? void 0 : _s.identifier) === null || _t === void 0 ? void 0 : _t.$in; if (Array.isArray(itemOfferedIdentifierIn)) { andConditions.push({ 'acceptedOffers.itemOffered.identifier': { $exists: true, $in: itemOfferedIdentifierIn } }); } const itemOfferedTypeOfIn = (_w = (_v = (_u = params.acceptedOffers) === null || _u === void 0 ? void 0 : _u.itemOffered) === null || _v === void 0 ? void 0 : _v.typeOf) === null || _w === void 0 ? void 0 : _w.$in; if (Array.isArray(itemOfferedTypeOfIn)) { andConditions.push({ 'acceptedOffers.itemOffered.typeOf': { $exists: true, $in: itemOfferedTypeOfIn } }); } const itemOfferedIssuedThroughTypeOfEq = (_0 = (_z = (_y = (_x = params.acceptedOffers) === null || _x === void 0 ? void 0 : _x.itemOffered) === null || _y === void 0 ? void 0 : _y.issuedThrough) === null || _z === void 0 ? void 0 : _z.typeOf) === null || _0 === void 0 ? void 0 : _0.$eq; if (typeof itemOfferedIssuedThroughTypeOfEq === 'string') { andConditions.push({ 'acceptedOffers.itemOffered.issuedThrough.typeOf': { $exists: true, $eq: itemOfferedIssuedThroughTypeOfEq } }); } const itemOfferedIssuedThroughIdIn = (_4 = (_3 = (_2 = (_1 = params.acceptedOffers) === null || _1 === void 0 ? void 0 : _1.itemOffered) === null || _2 === void 0 ? void 0 : _2.issuedThrough) === null || _3 === void 0 ? void 0 : _3.id) === null || _4 === void 0 ? void 0 : _4.$in; if (Array.isArray(itemOfferedIssuedThroughIdIn)) { andConditions.push({ 'acceptedOffers.itemOffered.issuedThrough.id': { $exists: true, $in: itemOfferedIssuedThroughIdIn } }); } const itemOfferedProgramMembershipUsedIdentifierEq = (_8 = (_7 = (_6 = (_5 = params.acceptedOffers) === null || _5 === void 0 ? void 0 : _5.itemOffered) === null || _6 === void 0 ? void 0 : _6.programMembershipUsed) === null || _7 === void 0 ? void 0 : _7.identifier) === null || _8 === void 0 ? void 0 : _8.$eq; if (typeof itemOfferedProgramMembershipUsedIdentifierEq === 'string') { andConditions.push({ 'acceptedOffers.itemOffered.programMembershipUsed.identifier': { $exists: true, $eq: itemOfferedProgramMembershipUsedIdentifierEq } }); } const itemOfferedProgramMembershipUsedIssuedThroughServiceTypeCodeValueEq = (_14 = (_13 = (_12 = (_11 = (_10 = (_9 = params.acceptedOffers) === null || _9 === void 0 ? void 0 : _9.itemOffered) === null || _10 === void 0 ? void 0 : _10.programMembershipUsed) === null || _11 === void 0 ? void 0 : _11.issuedThrough) === null || _12 === void 0 ? void 0 : _12.serviceType) === null || _13 === void 0 ? void 0 : _13.codeValue) === null || _14 === void 0 ? void 0 : _14.$eq; if (typeof itemOfferedProgramMembershipUsedIssuedThroughServiceTypeCodeValueEq === 'string') { andConditions.push({ 'acceptedOffers.itemOffered.programMembershipUsed.issuedThrough.serviceType.codeValue': { $exists: true, $eq: itemOfferedProgramMembershipUsedIssuedThroughServiceTypeCodeValueEq } }); } const itemOfferedReservedTicketIdentifierEq = (_18 = (_17 = (_16 = (_15 = params.acceptedOffers) === null || _15 === void 0 ? void 0 : _15.itemOffered) === null || _16 === void 0 ? void 0 : _16.reservedTicket) === null || _17 === void 0 ? void 0 : _17.identifier) === null || _18 === void 0 ? void 0 : _18.$eq; if (typeof itemOfferedReservedTicketIdentifierEq === 'string') { andConditions.push({ 'acceptedOffers.itemOffered.reservedTicket.identifier': { $exists: true, $eq: itemOfferedReservedTicketIdentifierEq } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.acceptedOffers !== undefined) { // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.acceptedOffers.itemOffered !== undefined) { // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(params.acceptedOffers.itemOffered.ids)) { andConditions.push({ 'acceptedOffers.itemOffered.id': { $exists: true, $in: params.acceptedOffers.itemOffered.ids } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(params.acceptedOffers.itemOffered.reservationNumbers)) { andConditions.push({ 'acceptedOffers.itemOffered.reservationNumber': { $exists: true, $in: params.acceptedOffers.itemOffered.reservationNumbers } }); } const reservationForConditions = params.acceptedOffers.itemOffered.reservationFor; // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (reservationForConditions !== undefined) { // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(reservationForConditions.ids)) { andConditions.push({ 'acceptedOffers.itemOffered.reservationFor.id': { $exists: true, $in: reservationForConditions.ids } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (typeof reservationForConditions.name === 'string' && reservationForConditions.name.length > 0) { andConditions.push({ $or: [ { 'acceptedOffers.itemOffered.reservationFor.name.ja': { $exists: true, $regex: new RegExp(reservationForConditions.name) } }, { 'acceptedOffers.itemOffered.reservationFor.name.en': { $exists: true, $regex: new RegExp(reservationForConditions.name) } } ] }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (reservationForConditions.location !== undefined) { if (Array.isArray(reservationForConditions.location.branchCodes)) { if (reservationForConditions.location.branchCodes.length === 1) { // use $eq(2025-04-01~) andConditions.push({ 'acceptedOffers.itemOffered.reservationFor.location.branchCode': { $exists: true, $eq: reservationForConditions.location.branchCodes.at(0) } }); } else { andConditions.push({ 'acceptedOffers.itemOffered.reservationFor.location.branchCode': { $exists: true, $in: reservationForConditions.location.branchCodes } }); } } } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (reservationForConditions.superEvent !== undefined) { // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(reservationForConditions.superEvent.ids)) { andConditions.push({ 'acceptedOffers.itemOffered.reservationFor.superEvent.id': { $exists: true, $in: reservationForConditions.superEvent.ids } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (reservationForConditions.superEvent.location !== undefined) { if (Array.isArray(reservationForConditions.superEvent.location.branchCodes)) { if (reservationForConditions.superEvent.location.branchCodes.length === 1) { // use $eq(2025-04-01~) andConditions.push({ 'acceptedOffers.itemOffered.reservationFor.superEvent.location.branchCode': { $exists: true, $eq: reservationForConditions.superEvent.location.branchCodes.at(0) } }); } else { andConditions.push({ 'acceptedOffers.itemOffered.reservationFor.superEvent.location.branchCode': { $exists: true, $in: reservationForConditions.superEvent.location.branchCodes } }); } } } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (reservationForConditions.superEvent.workPerformed !== undefined) { if (Array.isArray(reservationForConditions.superEvent.workPerformed.identifiers)) { andConditions.push({ 'acceptedOffers.itemOffered.reservationFor.superEvent.workPerformed.identifier': { $exists: true, $in: reservationForConditions.superEvent.workPerformed.identifiers } }); } } } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (reservationForConditions.inSessionFrom instanceof Date) { andConditions.push({ 'acceptedOffers.itemOffered.reservationFor.endDate': { $exists: true, $gt: reservationForConditions.inSessionFrom } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (reservationForConditions.inSessionThrough instanceof Date) { andConditions.push({ 'acceptedOffers.itemOffered.reservationFor.startDate': { $exists: true, $lt: reservationForConditions.inSessionThrough } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (reservationForConditions.startFrom instanceof Date) { andConditions.push({ 'acceptedOffers.itemOffered.reservationFor.startDate': { $exists: true, $gte: reservationForConditions.startFrom } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (reservationForConditions.startThrough instanceof Date) { andConditions.push({ 'acceptedOffers.itemOffered.reservationFor.startDate': { $exists: true, $lt: reservationForConditions.startThrough } }); } } } } const paymentMethodIdentifierIn = (_21 = (_20 = (_19 = params.paymentMethods) === null || _19 === void 0 ? void 0 : _19.paymentMethod) === null || _20 === void 0 ? void 0 : _20.identifier) === null || _21 === void 0 ? void 0 : _21.$in; if (Array.isArray(paymentMethodIdentifierIn)) { andConditions.push({ 'paymentMethods.paymentMethod.identifier': { $exists: true, $in: paymentMethodIdentifierIn } }); } const paymentMethodsTypeOfIn = (_22 = params.paymentMethods) === null || _22 === void 0 ? void 0 : _22.typeOfs; if (Array.isArray(paymentMethodsTypeOfIn)) { // paymentMethod.identifierで検索(2023-11-15~) // andConditions.push({ 'paymentMethods.typeOf': { $exists: true, $in: paymentMethodsTypeOfIn } }); andConditions.push({ 'paymentMethods.paymentMethod.identifier': { $exists: true, $in: paymentMethodsTypeOfIn } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.paymentMethods !== undefined) { // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(params.paymentMethods.accountIds)) { andConditions.push({ 'paymentMethods.accountId': { $exists: true, $in: params.paymentMethods.accountIds } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(params.paymentMethods.paymentMethodIds)) { andConditions.push({ 'paymentMethods.paymentMethodId': { $exists: true, $in: params.paymentMethods.paymentMethodIds } }); } } const paymentMethodAdditionalPropertyAll = (_24 = (_23 = params.paymentMethods) === null || _23 === void 0 ? void 0 : _23.additionalProperty) === null || _24 === void 0 ? void 0 : _24.$all; if (Array.isArray(paymentMethodAdditionalPropertyAll)) { andConditions.push({ 'paymentMethods.additionalProperty': { $exists: true, $all: paymentMethodAdditionalPropertyAll } }); } const paymentMethodAdditionalPropertyIn = (_26 = (_25 = params.paymentMethods) === null || _25 === void 0 ? void 0 : _25.additionalProperty) === null || _26 === void 0 ? void 0 : _26.$in; if (Array.isArray(paymentMethodAdditionalPropertyIn)) { andConditions.push({ 'paymentMethods.additionalProperty': { $exists: true, $in: paymentMethodAdditionalPropertyIn } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.orderDateFrom instanceof Date) { andConditions.push({ orderDate: { $gte: params.orderDateFrom } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.orderDateThrough instanceof Date) { andConditions.push({ orderDate: { $lte: params.orderDateThrough } }); } if (params.orderDate !== undefined && params.orderDate !== null) { if (params.orderDate.$gte instanceof Date) { andConditions.push({ orderDate: { $gte: params.orderDate.$gte } }); } if (params.orderDate.$lte instanceof Date) { andConditions.push({ orderDate: { $lte: params.orderDate.$lte } }); } } if (params.price !== undefined && params.price !== null) { if (typeof params.price.$gte === 'number') { andConditions.push({ price: { $exists: true, $gte: params.price.$gte } }); } if (typeof params.price.$lte === 'number') { andConditions.push({ price: { $exists: true, $lte: params.price.$lte } }); } } return andConditions; } /** * なければ作成する */ createIfNotExist(order) { return __awaiter(this, void 0, void 0, function* () { try { // 存在しなければ上書き // updateOneに変更(2024-01-14~) // await this.orderModel.findOneAndUpdate( yield this.orderModel.updateOne({ orderNumber: order.orderNumber }, { $setOnInsert: order }, { // new: true, 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; } } }); } /** * 注文ステータスを変更する */ changeStatus(params) { return __awaiter(this, void 0, void 0, function* () { const { orderNumber, previousOrderStatus, orderStatus } = params; const doc = yield this.orderModel.findOneAndUpdate({ orderNumber: { $eq: orderNumber }, orderStatus: { $eq: previousOrderStatus }, 'project.id': { $eq: params.project.id }, typeOf: { $eq: factory.order.OrderType.Order } }, { $set: { previousOrderStatus, // 追加(2023-08-31~) orderStatus } }, { new: true, // inclusion projection(2024-07-25~) projection: { _id: 0, // hide _id(2024-07-25~) id: { $toString: '$_id' }, orderNumber: 1, broker: 1, confirmationNumber: 1, customer: 1, dateReturned: 1, name: 1, orderDate: 1, orderStatus: 1, orderedItem: 1, paymentMethods: 1, previousOrderStatus: 1, price: 1, priceCurrency: 1, project: 1, returner: 1, seller: 1, typeOf: 1 } // projection: { // __v: 0, // createdAt: 0, // updatedAt: 0, // acceptedOffers: 0 // 除外(2023-12-08~) // } }) .lean() // lean(2024-07-25~) .exec(); // NotFoundであれば状態確認 if (doc === null) { const order = yield this.projectFieldsByOrderNumber({ orderNumber: params.orderNumber, project: { id: params.project.id }, inclusion: [ 'orderNumber', 'broker', 'confirmationNumber', 'customer', 'dateReturned', 'name', 'orderDate', 'orderStatus', 'orderedItem', 'paymentMethods', 'previousOrderStatus', 'price', 'priceCurrency', 'project', 'returner', 'seller', 'typeOf' ] // inclusion projection(2024-07-25~) }); // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore next */ if (order.orderStatus === params.orderStatus) { // すでに変更済の場合 return order; // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore next */ } else { throw new factory.errors.Argument('orderNumber', `Order ${order.orderNumber} already changed -> ${order.orderStatus}`); } } return doc; }); } /** * 注文を返品する */ returnOrder(params) { return __awaiter(this, void 0, void 0, function* () { const { dateReturned, returner, orderNumber, project } = params; const doc = yield this.orderModel.findOneAndUpdate({ orderNumber: { $eq: orderNumber }, orderStatus: { $eq: factory.orderStatus.OrderDelivered }, 'project.id': { $eq: project.id }, typeOf: { $eq: factory.order.OrderType.Order } }, { $set: { previousOrderStatus: factory.orderStatus.OrderDelivered, orderStatus: factory.orderStatus.OrderReturned, dateReturned, returner } }, { new: true, projection: { _id: 0, // hide _id(2024-07-30~) id: { $toString: '$_id' }, // _id: 1, project: 1, typeOf: 1, orderNumber: 1, dateReturned: 1, customer: 1, returner: 1, seller: 1, price: 1, priceCurrency: 1, orderDate: 1 // optimize(2023-12-07~) // __v: 0, // createdAt: 0, // updatedAt: 0 } }) .lean() // lean(2024-07-30~) .exec(); // NotFoundであれば状態確認 if (doc === null) { const order = yield this.projectFieldsByOrderNumber({ orderNumber: params.orderNumber, project: { id: params.project.id }, inclusion: [ 'project', 'typeOf', 'orderNumber', 'dateReturned', 'customer', 'returner', 'seller', 'price', 'priceCurrency', 'orderDate' ] }); // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore next */ if (order.orderStatus === factory.orderStatus.OrderReturned) { // すでに変更済の場合 return order; // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore next */ } else { throw new factory.errors.Argument('orderNumber', `Order ${order.orderNumber} already changed -> ${order.orderStatus}`); } } return doc; }); } /** * 変更可能な属性を更新する */ updateChangeableAttributes(params) { return __awaiter(this, void 0, void 0, function* () { const updatedAt = new Date(); const { orderNumber, project, name } = params; const doc = yield this.orderModel.findOneAndUpdate({ orderNumber: { $eq: orderNumber }, 'project.id': { $eq: project.id }, typeOf: { $eq: factory.order.OrderType.Order } }, { $set: Object.assign({}, (typeof name === 'string') ? { name } : undefined) }, { new: false, projection: { _id: 1 // updatedAt: 1 // discontinue(2024-06-17~) } }) .lean() // lean(2024-07-30~) .exec(); if (doc === null) { throw new factory.errors.NotFound(this.orderModel.modelName); } return { updatedAt }; }); } /** * 注文後に決済方法区分を確定する */ fixPaymentMethodIdentifier(params) { return __awaiter(this, void 0, void 0, function* () { const doc = yield this.orderModel.findOneAndUpdate({ orderNumber: { $eq: params.orderNumber }, 'project.id': { $eq: params.project.id }, typeOf: { $eq: factory.order.OrderType.Order }, 'paymentMethods.paymentMethodId': { $exists: true, $eq: params.invoice.paymentMethodId } }, { $set: { 'paymentMethods.$[invoice].paymentMethod.identifier': params.invoice.paymentMethod.identifier, // tslint:disable-next-line:no-suspicious-comment // TODO 互換性維持対応としてtypeOfも変更しているが、そのうち廃止(2023-08-30) 'paymentMethods.$[invoice].typeOf': params.invoice.paymentMethod.identifier } }, { arrayFilters: [{ 'invoice.paymentMethodId': { $eq: params.invoice.paymentMethodId } }], new: true, projection: { _id: 1 } }) .lean() // lean(2024-07-30~) .exec(); if (doc === null) { throw new factory.errors.NotFound(this.orderModel.modelName); } }); } projectFieldsById(params) { return __awaiter(this, void 0, void 0, function* () { const projection = { // _id: 1 _id: 0, // hide _id(2024-07-25~) id: { $toString: '$_id' } }; if (Array.isArray(params.inclusion) && params.inclusion.length > 0) { params.inclusion.forEach((field) => { // if (String(field) !== 'acceptedOffers' && field !== '_id' && field !== 'id') { // projection[field] = 1; // } if (AVAILABLE_PROJECT_FIELDS.includes(field)) { projection[field] = 1; } }); } else { // discontinue(2024-07-25~) throw new factory.errors.ArgumentNull('inclusion', 'inclusion must be specified'); } const doc = yield this.orderModel.findOne({ _id: { $eq: params.id }, typeOf: { $eq: factory.order.OrderType.Order } }, projection) .lean() // lean(2024-07-25~) .exec(); if (doc === null) { throw new factory.errors.NotFound(this.orderModel.modelName); } return doc; }); } /** * 注文番号から注文を取得する */ projectFieldsByOrderNumber( // public async findByOrderNumber( params) { return __awaiter(this, void 0, void 0, function* () { const projection = { _id: 0, // hide _id(2024-07-25~) id: { $toString: '$_id' } }; if (Array.isArray(params.inclusion) && params.inclusion.length > 0) { params.inclusion.forEach((field) => { // if (String(field) !== 'acceptedOffers' && field !== '_id' && field !== 'id') { // projection[field] = 1; // } if (AVAILABLE_PROJECT_FIELDS.includes(field)) { projection[field] = 1; } }); } else { // discontinue(2024-07-25~) throw new factory.errors.ArgumentNull('inclusion', 'inclusion must be specified'); } const doc = yield this.orderModel.findOne({ orderNumber: { $eq: params.orderNumber }, 'project.id': { $eq: params.project.id }, typeOf: { $eq: factory.order.OrderType.Order } }, projection) .lean() // lean(2024-07-25~) .exec(); if (doc === null) { throw new factory.errors.NotFound(this.orderModel.modelName); } return doc; }); } findByOrderNumberAndReservationId(params) { return __awaiter(this, void 0, void 0, function* () { var _a; const projection = { orderStatus: 1, typeOf: 1, orderNumber: 1, _id: 0 }; const doc = yield this.orderModel.findOne(Object.assign({ orderNumber: { $eq: params.orderNumber }, 'project.id': { $eq: params.project.id }, typeOf: { $eq: factory.order.OrderType.Order }, acceptedOffers: { $elemMatch: { 'itemOffered.typeOf': { $in: [factory.reservationType.BusReservation, factory.reservationType.EventReservation] }, 'itemOffered.id': { $eq: params.reservationId } } } }, (typeof ((_a = params.seller) === null || _a === void 0 ? void 0 : _a.id) === 'string') ? { 'seller.id': { $exists: true, $eq: params.seller.id } } : undefined), projection) // projection的にleanで十分 .lean() .exec(); if (doc === null) { throw new factory.errors.NotFound(this.orderModel.modelName); } return doc; }); } /** * 注文番号で削除する */ deleteByOrderNumber(params) { return __awaiter(this, void 0, void 0, function* () { yield this.orderModel.deleteOne({ orderNumber: { $eq: params.orderNumber }, typeOf: { $eq: factory.order.OrderType.Order } }) .exec(); }); } count(params) { return __awaiter(this, void 0, void 0, function* () { const conditions = OrderRepo.CREATE_MONGO_CONDITIONS(params); return this.orderModel.countDocuments((conditions.length > 0) ? { $and: conditions } : {}) .setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec(); }); } /** * 注文を検索する(inclusion projection) * redefine from search(2024-07-31~) */ projectFields(params, options) { return __awaiter(this, void 0, void 0, function* () { var _a; const { inclusion } = options; const conditions = OrderRepo.CREATE_MONGO_CONDITIONS(params); let projectStage = {}; const positiveProjectionFields = (Array.isArray(inclusion)) ? inclusion.filter((key) => AVAILABLE_PROJECT_FIELDS.includes(key)) : []; if (Array.isArray(positiveProjectionFields) && positiveProjectionFields.length > 0) { projectStage = Object.assign({ _id: 0, id: { $toString: '$_id' } }, Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1])))); } else { throw new factory.errors.ArgumentNull('inclusion', 'incl