UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

1,024 lines 71 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()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ActionRepo = void 0; const moment = require("moment"); const factory = require("../factory"); const settings_1 = require("../settings"); const action_1 = require("./mongoose/schemas/action"); const actionRecipe_1 = require("./mongoose/schemas/actionRecipe"); const AVAILABLE_PROJECT_FIELDS = [ 'project', 'actionStatus', 'typeOf', 'description', 'agent', 'recipient', 'result', 'error', 'object', 'startDate', 'endDate', 'purpose', 'potentialActions', 'amount', 'fromLocation', 'toLocation', 'instrument', 'location', 'replacer', 'targetCollection', 'sameAs', 'cancelAction', 'about', 'target', 'identifier' ]; /** * アクションリポジトリ */ class ActionRepo { constructor(connection) { this.actionModel = connection.model(action_1.modelName, (0, action_1.createSchema)()); this.actionRecipeModel = connection.model(actionRecipe_1.modelName, (0, actionRecipe_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, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, _64, _65, _66, _67, _68, _69, _70, _71, _72, _73, _74, _75, _76; const andConditions = []; const idIn = (_a = params.id) === null || _a === void 0 ? void 0 : _a.$in; if (Array.isArray(idIn)) { andConditions.push({ _id: { $in: idIn } }); } const idNin = (_b = params.id) === null || _b === void 0 ? void 0 : _b.$nin; if (Array.isArray(idNin)) { andConditions.push({ _id: { $nin: idNin } }); } const projectIdEq = (_d = (_c = params.project) === null || _c === void 0 ? void 0 : _c.id) === null || _d === void 0 ? void 0 : _d.$eq; if (typeof projectIdEq === 'string') { andConditions.push({ 'project.id': { $eq: projectIdEq } }); } const agentTypeOfIn = (_f = (_e = params.agent) === null || _e === void 0 ? void 0 : _e.typeOf) === null || _f === void 0 ? void 0 : _f.$in; if (Array.isArray(agentTypeOfIn)) { andConditions.push({ 'agent.typeOf': { $exists: true, $in: agentTypeOfIn } }); } const agentIdIn = (_h = (_g = params.agent) === null || _g === void 0 ? void 0 : _g.id) === null || _h === void 0 ? void 0 : _h.$in; if (Array.isArray(agentIdIn)) { andConditions.push({ 'agent.id': { $exists: true, $in: agentIdIn } }); } const instrumentTransactionNumberEq = (_k = (_j = params.instrument) === null || _j === void 0 ? void 0 : _j.transactionNumber) === null || _k === void 0 ? void 0 : _k.$eq; if (typeof instrumentTransactionNumberEq === 'string') { andConditions.push({ 'instrument.transactionNumber': { $exists: true, $eq: instrumentTransactionNumberEq } }); } const instrumentTypeOfEq = (_m = (_l = params.instrument) === null || _l === void 0 ? void 0 : _l.typeOf) === null || _m === void 0 ? void 0 : _m.$eq; if (typeof instrumentTypeOfEq === 'string') { andConditions.push({ 'instrument.typeOf': { $exists: true, $eq: instrumentTypeOfEq } }); } const instrumentIdentifierEq = (_p = (_o = params.instrument) === null || _o === void 0 ? void 0 : _o.identifier) === null || _p === void 0 ? void 0 : _p.$eq; if (typeof instrumentIdentifierEq === 'string') { andConditions.push({ 'instrument.identifier': { $exists: true, $eq: instrumentIdentifierEq } }); } const instrumentIdEq = (_r = (_q = params.instrument) === null || _q === void 0 ? void 0 : _q.id) === null || _r === void 0 ? void 0 : _r.$eq; if (typeof instrumentIdEq === 'string') { andConditions.push({ 'instrument.id': { $exists: true, $eq: instrumentIdEq } }); } const instrumentOrderNumberEq = (_t = (_s = params.instrument) === null || _s === void 0 ? void 0 : _s.orderNumber) === null || _t === void 0 ? void 0 : _t.$eq; if (typeof instrumentOrderNumberEq === 'string') { andConditions.push({ 'instrument.orderNumber': { $exists: true, $eq: instrumentOrderNumberEq } }); } const locationIdEq = (_v = (_u = params.location) === null || _u === void 0 ? void 0 : _u.id) === null || _v === void 0 ? void 0 : _v.$eq; if (typeof locationIdEq === 'string') { andConditions.push({ 'location.id': { $exists: true, $eq: locationIdEq } }); } const locationIdentifierEq = (_x = (_w = params.location) === null || _w === void 0 ? void 0 : _w.identifier) === null || _x === void 0 ? void 0 : _x.$eq; if (typeof locationIdentifierEq === 'string') { andConditions.push({ 'location.identifier': { $exists: true, $eq: locationIdentifierEq } }); } const objectMovieTicketsIdentifierEq = (_0 = (_z = (_y = params.object) === null || _y === void 0 ? void 0 : _y.movieTickets) === null || _z === void 0 ? void 0 : _z.identifier) === null || _0 === void 0 ? void 0 : _0.$eq; if (typeof objectMovieTicketsIdentifierEq === 'string') { andConditions.push({ 'object.movieTickets.identifier': { $exists: true, $eq: objectMovieTicketsIdentifierEq } }); } const objectMovieTicketsServiceOutputReservationForIdEq = (_5 = (_4 = (_3 = (_2 = (_1 = params.object) === null || _1 === void 0 ? void 0 : _1.movieTickets) === null || _2 === void 0 ? void 0 : _2.serviceOutput) === null || _3 === void 0 ? void 0 : _3.reservationFor) === null || _4 === void 0 ? void 0 : _4.id) === null || _5 === void 0 ? void 0 : _5.$eq; if (typeof objectMovieTicketsServiceOutputReservationForIdEq === 'string') { andConditions.push({ 'object.movieTickets.serviceOutput.reservationFor.id': { $exists: true, $eq: objectMovieTicketsServiceOutputReservationForIdEq } }); } const objectPaymentMethodIdEq = (_7 = (_6 = params.object) === null || _6 === void 0 ? void 0 : _6.paymentMethodId) === null || _7 === void 0 ? void 0 : _7.$eq; if (typeof objectPaymentMethodIdEq === 'string') { andConditions.push({ 'object.paymentMethodId': { $exists: true, $eq: objectPaymentMethodIdEq } }); } const objectObjectPaymentMethodIdEq = (_10 = (_9 = (_8 = params.object) === null || _8 === void 0 ? void 0 : _8.object) === null || _9 === void 0 ? void 0 : _9.paymentMethodId) === null || _10 === void 0 ? void 0 : _10.$eq; if (typeof objectObjectPaymentMethodIdEq === 'string') { andConditions.push({ 'object.object.paymentMethodId': { $exists: true, $eq: objectObjectPaymentMethodIdEq } }); } const objectReservationForIdEq = (_13 = (_12 = (_11 = params.object) === null || _11 === void 0 ? void 0 : _11.reservationFor) === null || _12 === void 0 ? void 0 : _12.id) === null || _13 === void 0 ? void 0 : _13.$eq; if (typeof objectReservationForIdEq === 'string') { andConditions.push({ 'object.reservationFor.id': { $exists: true, $eq: objectReservationForIdEq } }); } const objectReservationNumberEq = (_15 = (_14 = params.object) === null || _14 === void 0 ? void 0 : _14.reservationNumber) === null || _15 === void 0 ? void 0 : _15.$eq; if (typeof objectReservationNumberEq === 'string') { andConditions.push({ 'object.reservationNumber': { $exists: true, $eq: objectReservationNumberEq } }); } const objectReservationNumberIn = (_17 = (_16 = params.object) === null || _16 === void 0 ? void 0 : _16.reservationNumber) === null || _17 === void 0 ? void 0 : _17.$in; if (Array.isArray(objectReservationNumberIn)) { andConditions.push({ 'object.reservationNumber': { $exists: true, $in: objectReservationNumberIn } }); } const objectPaymentMethodAccountIdEq = (_20 = (_19 = (_18 = params.object) === null || _18 === void 0 ? void 0 : _18.paymentMethod) === null || _19 === void 0 ? void 0 : _19.accountId) === null || _20 === void 0 ? void 0 : _20.$eq; if (typeof objectPaymentMethodAccountIdEq === 'string') { andConditions.push({ 'object.paymentMethod.accountId': { $exists: true, $eq: objectPaymentMethodAccountIdEq } }); } const objectPaymentMethodPaymentMethodIdEq = (_23 = (_22 = (_21 = params.object) === null || _21 === void 0 ? void 0 : _21.paymentMethod) === null || _22 === void 0 ? void 0 : _22.paymentMethodId) === null || _23 === void 0 ? void 0 : _23.$eq; if (typeof objectPaymentMethodPaymentMethodIdEq === 'string') { andConditions.push({ 'object.paymentMethod.paymentMethodId': { $exists: true, $eq: objectPaymentMethodPaymentMethodIdEq } }); } const objectPaymentMethodPaymentMethodIdIn = (_26 = (_25 = (_24 = params.object) === null || _24 === void 0 ? void 0 : _24.paymentMethod) === null || _25 === void 0 ? void 0 : _25.paymentMethodId) === null || _26 === void 0 ? void 0 : _26.$in; if (Array.isArray(objectPaymentMethodPaymentMethodIdIn)) { andConditions.push({ 'object.paymentMethod.paymentMethodId': { $exists: true, $in: objectPaymentMethodPaymentMethodIdIn } }); } const objectPaymentMethodTypeOfEq = (_29 = (_28 = (_27 = params.object) === null || _27 === void 0 ? void 0 : _27.paymentMethod) === null || _28 === void 0 ? void 0 : _28.typeOf) === null || _29 === void 0 ? void 0 : _29.$eq; if (typeof objectPaymentMethodTypeOfEq === 'string') { andConditions.push({ 'object.paymentMethod.typeOf': { $exists: true, $eq: objectPaymentMethodTypeOfEq } }); } const objectTypeOfEq = (_31 = (_30 = params.object) === null || _30 === void 0 ? void 0 : _30.typeOf) === null || _31 === void 0 ? void 0 : _31.$eq; if (typeof objectTypeOfEq === 'string') { andConditions.push({ 'object.typeOf': { $exists: true, $eq: objectTypeOfEq } }); } const objectTypeOfIn = (_33 = (_32 = params.object) === null || _32 === void 0 ? void 0 : _32.typeOf) === null || _33 === void 0 ? void 0 : _33.$in; if (Array.isArray(objectTypeOfIn)) { andConditions.push({ 'object.typeOf': { $exists: true, $in: objectTypeOfIn } }); } const objectIdEq = (_35 = (_34 = params.object) === null || _34 === void 0 ? void 0 : _34.id) === null || _35 === void 0 ? void 0 : _35.$eq; if (typeof objectIdEq === 'string') { andConditions.push({ 'object.id': { $exists: true, $eq: objectIdEq } }); } const objectIdIn = (_37 = (_36 = params.object) === null || _36 === void 0 ? void 0 : _36.id) === null || _37 === void 0 ? void 0 : _37.$in; if (Array.isArray(objectIdIn)) { andConditions.push({ 'object.id': { $exists: true, $in: objectIdIn } }); } const objectOrderNumberIn = (_39 = (_38 = params.object) === null || _38 === void 0 ? void 0 : _38.orderNumber) === null || _39 === void 0 ? void 0 : _39.$in; if (Array.isArray(objectOrderNumberIn)) { andConditions.push({ 'object.orderNumber': { $exists: true, $in: objectOrderNumberIn } }); } const objectEventIdIn = (_42 = (_41 = (_40 = params.object) === null || _40 === void 0 ? void 0 : _40.event) === null || _41 === void 0 ? void 0 : _41.id) === null || _42 === void 0 ? void 0 : _42.$in; if (Array.isArray(objectEventIdIn)) { andConditions.push({ 'object.event.id': { $exists: true, $in: objectEventIdIn } }); } // discontinue(2024-06-18~) // const objectAcceptedOfferSeatNumberIn = // params.object?.acceptedOffer?.itemOffered?.serviceOutput?.reservedTicket?.ticketedSeat?.seatNumber?.$in; // if (Array.isArray(objectAcceptedOfferSeatNumberIn)) { // andConditions.push({ // 'object.acceptedOffer.itemOffered.serviceOutput.reservedTicket.ticketedSeat.seatNumber': { // $exists: true, // $in: objectAcceptedOfferSeatNumberIn // } // }); // } const objectTransactionNumberEq = (_44 = (_43 = params.object) === null || _43 === void 0 ? void 0 : _43.transactionNumber) === null || _44 === void 0 ? void 0 : _44.$eq; if (typeof objectTransactionNumberEq === 'string') { andConditions.push({ 'object.transactionNumber': { $exists: true, $eq: objectTransactionNumberEq } }); } if (typeof params.typeOf === 'string') { andConditions.push({ typeOf: params.typeOf }); } else { const typeOfEq = (_45 = params.typeOf) === null || _45 === void 0 ? void 0 : _45.$eq; if (typeof typeOfEq === 'string') { andConditions.push({ typeOf: { $eq: typeOfEq } }); } } const actionStatusIn = (_46 = params.actionStatus) === null || _46 === void 0 ? void 0 : _46.$in; if (Array.isArray(actionStatusIn)) { andConditions.push({ actionStatus: { $in: actionStatusIn } }); } if (Array.isArray(params.actionStatusTypes)) { andConditions.push({ actionStatus: { $in: params.actionStatusTypes } }); } const startDateGte = params.startFrom; if (startDateGte instanceof Date) { andConditions.push({ startDate: { $gte: startDateGte } }); } const startDateLte = params.startThrough; if (startDateLte instanceof Date) { andConditions.push({ startDate: { $lte: startDateLte } }); } const fromLocationTypeOfIn = (_48 = (_47 = params.fromLocation) === null || _47 === void 0 ? void 0 : _47.typeOf) === null || _48 === void 0 ? void 0 : _48.$in; if (Array.isArray(fromLocationTypeOfIn)) { andConditions.push({ 'fromLocation.typeOf': { $exists: true, $in: fromLocationTypeOfIn } }); } const fromLocationAccountNumberIn = (_50 = (_49 = params.fromLocation) === null || _49 === void 0 ? void 0 : _49.accountNumber) === null || _50 === void 0 ? void 0 : _50.$in; if (Array.isArray(fromLocationAccountNumberIn)) { andConditions.push({ 'fromLocation.accountNumber': { $exists: true, $in: fromLocationAccountNumberIn } }); } const fromLocationAccountTypeIn = (_52 = (_51 = params.fromLocation) === null || _51 === void 0 ? void 0 : _51.accountType) === null || _52 === void 0 ? void 0 : _52.$in; if (Array.isArray(fromLocationAccountTypeIn)) { andConditions.push({ 'fromLocation.accountType': { $exists: true, $in: fromLocationAccountTypeIn } }); } const toLocationTypeOfIn = (_54 = (_53 = params.toLocation) === null || _53 === void 0 ? void 0 : _53.typeOf) === null || _54 === void 0 ? void 0 : _54.$in; if (Array.isArray(toLocationTypeOfIn)) { andConditions.push({ 'toLocation.typeOf': { $exists: true, $in: toLocationTypeOfIn } }); } const toLocationAccountNumberIn = (_56 = (_55 = params.toLocation) === null || _55 === void 0 ? void 0 : _55.accountNumber) === null || _56 === void 0 ? void 0 : _56.$in; if (Array.isArray(toLocationAccountNumberIn)) { andConditions.push({ 'toLocation.accountNumber': { $exists: true, $in: toLocationAccountNumberIn } }); } const toLocationAccountTypeIn = (_58 = (_57 = params.toLocation) === null || _57 === void 0 ? void 0 : _57.accountType) === null || _58 === void 0 ? void 0 : _58.$in; if (Array.isArray(toLocationAccountTypeIn)) { andConditions.push({ 'toLocation.accountType': { $exists: true, $in: toLocationAccountTypeIn } }); } const purposeTypeOfIn = (_60 = (_59 = params.purpose) === null || _59 === void 0 ? void 0 : _59.typeOf) === null || _60 === void 0 ? void 0 : _60.$in; if (Array.isArray(purposeTypeOfIn)) { andConditions.push({ 'purpose.typeOf': { $exists: true, $in: purposeTypeOfIn } }); } const purposeIdIn = (_62 = (_61 = params.purpose) === null || _61 === void 0 ? void 0 : _61.id) === null || _62 === void 0 ? void 0 : _62.$in; if (Array.isArray(purposeIdIn)) { andConditions.push({ 'purpose.id': { $exists: true, $in: purposeIdIn } }); } const purposeOrderNumberIn = (_64 = (_63 = params.purpose) === null || _63 === void 0 ? void 0 : _63.orderNumber) === null || _64 === void 0 ? void 0 : _64.$in; if (Array.isArray(purposeOrderNumberIn)) { andConditions.push({ 'purpose.orderNumber': { $exists: true, $in: purposeOrderNumberIn } }); } const resultTypeOfIn = (_66 = (_65 = params.result) === null || _65 === void 0 ? void 0 : _65.typeOf) === null || _66 === void 0 ? void 0 : _66.$in; if (Array.isArray(resultTypeOfIn)) { andConditions.push({ 'result.typeOf': { $exists: true, $in: resultTypeOfIn } }); } const resultIdIn = (_68 = (_67 = params.result) === null || _67 === void 0 ? void 0 : _67.id) === null || _68 === void 0 ? void 0 : _68.$in; if (Array.isArray(resultIdIn)) { andConditions.push({ 'result.id': { $exists: true, $in: resultIdIn } }); } const resultOrderNumberIn = (_70 = (_69 = params.result) === null || _69 === void 0 ? void 0 : _69.orderNumber) === null || _70 === void 0 ? void 0 : _70.$in; if (Array.isArray(resultOrderNumberIn)) { andConditions.push({ 'result.orderNumber': { $exists: true, $in: resultOrderNumberIn } }); } const resultCodeIn = (_72 = (_71 = params.result) === null || _71 === void 0 ? void 0 : _71.code) === null || _72 === void 0 ? void 0 : _72.$in; if (Array.isArray(resultCodeIn)) { andConditions.push({ 'result.code': { $exists: true, $in: resultCodeIn } }); } // sameAs(2024-04-23~) const sameAsIdEq = (_74 = (_73 = params.sameAs) === null || _73 === void 0 ? void 0 : _73.id) === null || _74 === void 0 ? void 0 : _74.$eq; if (typeof sameAsIdEq === 'string') { andConditions.push({ 'sameAs.id': { $exists: true, $eq: sameAsIdEq } }); } const aboutOrderNumberEq = (_76 = (_75 = params.about) === null || _75 === void 0 ? void 0 : _75.orderNumber) === null || _76 === void 0 ? void 0 : _76.$eq; if (typeof aboutOrderNumberEq === 'string') { andConditions.push({ 'about.orderNumber': { $exists: true, $eq: aboutOrderNumberEq } }); } return andConditions; } /** * アクション検索 */ search(params, inclusion) { return __awaiter(this, void 0, void 0, function* () { var _a; const conditions = ActionRepo.CREATE_MONGO_CONDITIONS(params); let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS; if (Array.isArray(inclusion) && inclusion.length > 0) { positiveProjectionFields = inclusion.filter((key) => AVAILABLE_PROJECT_FIELDS.includes(key)); } const projection = Object.assign({ _id: 0, id: { $toString: '$_id' } }, Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1])))); const query = this.actionModel.find((conditions.length > 0) ? { $and: conditions } : {}, projection); if (typeof params.limit === 'number' && params.limit > 0) { const page = (typeof params.page === 'number' && params.page > 0) ? params.page : 1; query.limit(params.limit) .skip(params.limit * (page - 1)); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (((_a = params.sort) === null || _a === void 0 ? void 0 : _a.startDate) !== undefined) { query.sort({ startDate: params.sort.startDate }); } // const explainResult = await (<any>query).explain(); // console.log(explainResult[0].executionStats.allPlansExecution.map((e: any) => e.executionStages.inputStage)); return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .lean() // 2024-08-26~ .exec(); }); } /** * アクション開始 */ start(attributes, options) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const startDate = new Date(); const creatingAction = Object.assign(Object.assign({}, attributes), { actionStatus: factory.actionStatusType.ActiveActionStatus, startDate }); // reimplemnt with insertMany(2024-08-29~) const result = yield this.actionModel.insertMany(creatingAction, { rawResult: true }); const id = (_b = (_a = result.insertedIds) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.toHexString(); if (typeof id !== 'string') { throw new factory.errors.Internal('action not saved'); } // add recipe(2024-06-09~) const savingRecipe = options === null || options === void 0 ? void 0 : options.recipe; if ((savingRecipe === null || savingRecipe === void 0 ? void 0 : savingRecipe.typeOf) === 'Recipe') { yield this.upsertRecipe(Object.assign(Object.assign({}, savingRecipe), { recipeFor: { id, typeOf: creatingAction.typeOf } })); } return { id, startDate, typeOf: creatingAction.typeOf }; }); } completeWithVoid(params) { return __awaiter(this, void 0, void 0, function* () { var _a; if (((_a = params.recipe) === null || _a === void 0 ? void 0 : _a.typeOf) === 'Recipe') { yield this.upsertRecipe(Object.assign(Object.assign({}, params.recipe), { recipeFor: { id: params.id, typeOf: params.typeOf } })); } const doc = yield this.actionModel.findOneAndUpdate({ _id: { $eq: params.id }, typeOf: { $eq: params.typeOf } }, { $set: { actionStatus: factory.actionStatusType.CompletedActionStatus, result: params.result, endDate: new Date() } }, { new: false, projection: { _id: 1 } }) .lean() .exec(); if (doc === null) { throw new factory.errors.NotFound(this.actionModel.modelName); } }); } /** * アクション取消 */ cancelWithVoid(params) { return __awaiter(this, void 0, void 0, function* () { const cancelDate = new Date(); const cancelAction = (params.cancelAction !== undefined) ? Object.assign(Object.assign({}, params.cancelAction), { endTime: cancelDate, typeOf: factory.actionType.CancelAction }) : undefined; const doc = yield this.actionModel.findOneAndUpdate({ _id: { $eq: params.id }, typeOf: { $eq: params.typeOf }, actionStatus: { $ne: factory.actionStatusType.CanceledActionStatus } // 冪等性確保(2024-05-26~) }, { $set: Object.assign({ actionStatus: factory.actionStatusType.CanceledActionStatus }, (cancelAction !== undefined) ? { cancelAction } : undefined // cancelAction連携(2024-05-26~) ) }, { new: false, projection: { _id: 1 } }) .lean() .exec(); if (doc === null) { // 既にCanceledActionStatusであればok const existingAction = yield this.findById({ id: params.id, typeOf: params.typeOf }, ['actionStatus'], []); if (existingAction.actionStatus !== factory.actionStatusType.CanceledActionStatus) { throw new factory.errors.NotFound(this.actionModel.modelName); } } // endDateが存在しなければセット(2024-04-22~) yield this.actionModel.updateOne({ _id: { $eq: params.id }, endDate: { $exists: false } }, { $set: { endDate: cancelDate } }) .exec(); }); } /** * アクション失敗 */ giveUp(params) { return __awaiter(this, void 0, void 0, function* () { var _a; const actionError = (Array.isArray(params.error)) ? params.error.map((e) => (Object.assign(Object.assign({}, e), { message: e.message, name: e.name }))) : Object.assign(Object.assign({}, params.error), { message: params.error.message, name: params.error.name }); if (((_a = params.recipe) === null || _a === void 0 ? void 0 : _a.typeOf) === 'Recipe') { yield this.upsertRecipe(Object.assign(Object.assign({}, params.recipe), { recipeFor: { id: params.id, typeOf: params.typeOf } })); } const doc = yield this.actionModel.findOneAndUpdate({ typeOf: { $eq: params.typeOf }, _id: { $eq: params.id } }, { $set: { actionStatus: factory.actionStatusType.FailedActionStatus, error: actionError, endDate: new Date() } }, { new: true, projection: { _id: 1 } }) .lean() .exec(); if (doc === null) { throw new factory.errors.NotFound(this.actionModel.modelName); } }); } /** * アクション再開 */ reStart(params) { return __awaiter(this, void 0, void 0, function* () { var _a; const doc = yield this.actionModel.findOneAndUpdate({ _id: { $eq: params.id }, typeOf: { $eq: params.typeOf }, actionStatus: { $eq: factory.actionStatusType.CompletedActionStatus } }, { $set: { actionStatus: factory.actionStatusType.ActiveActionStatus, startDate: new Date() }, $unset: { endDate: 1 } }, { new: false, projection: { _id: 1 } }) .lean() .exec(); if (doc === null) { throw new factory.errors.NotFound(this.actionModel.modelName); } // add recipe(2024-06-09~) if (((_a = params.recipe) === null || _a === void 0 ? void 0 : _a.typeOf) === 'Recipe') { yield this.upsertRecipe(Object.assign(Object.assign({}, params.recipe), { recipeFor: { id: params.id, typeOf: params.typeOf } })); } }); } /** * 一定期間ActiveActionStatusのアクションをFailedActionStatusにする */ giveUpStartDatePassedCertainPeriod(params) { return __awaiter(this, void 0, void 0, function* () { var _a; return this.actionModel.updateMany(Object.assign({ actionStatus: { $eq: factory.actionStatusType.ActiveActionStatus }, // 一定期間過ぎたもの startDate: { $lt: params.startDate.$lt } }, (typeof ((_a = params.id) === null || _a === void 0 ? void 0 : _a.$eq) === 'string') ? { _id: { $eq: params.id.$eq } } : undefined), { actionStatus: factory.actionStatusType.FailedActionStatus, error: params.error, endDate: new Date() }) .exec(); }); } findById(params, inclusion, exclusion) { return __awaiter(this, void 0, void 0, function* () { let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS; if (Array.isArray(inclusion) && inclusion.length > 0) { positiveProjectionFields = inclusion.filter((key) => AVAILABLE_PROJECT_FIELDS.includes(key)); } else { if (Array.isArray(exclusion) && exclusion.length > 0) { positiveProjectionFields = positiveProjectionFields.filter((key) => !exclusion.includes(key)); } } const projection = Object.assign({ _id: 0, id: { $toString: '$_id' } }, Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1])))); const doc = yield this.actionModel.findOne({ typeOf: { $eq: params.typeOf }, _id: { $eq: params.id } }, projection) .lean() // 2024-08-26~ .exec(); if (doc === null) { throw new factory.errors.NotFound(this.actionModel.modelName); } return doc; }); } findPayAction(params) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const payActions = yield this.search({ limit: 1, page: 1, actionStatus: (Array.isArray((_a = params.actionStatus) === null || _a === void 0 ? void 0 : _a.$in)) ? { $in: (_b = params.actionStatus) === null || _b === void 0 ? void 0 : _b.$in } : { $in: [factory.actionStatusType.CompletedActionStatus] }, project: { id: { $eq: params.project.id } }, typeOf: { $eq: factory.actionType.PayAction }, object: { paymentMethod: { paymentMethodId: { $eq: params.paymentMethodId } } } }, []); return payActions.shift(); }); } /** * 取引に対するアクションを検索する */ // tslint:disable-next-line:cyclomatic-complexity max-func-body-length searchByPurpose(params) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f; const andConditions = [ { 'purpose.typeOf': { $exists: true, $eq: params.purpose.typeOf } }, { 'purpose.id': { $exists: true, $eq: params.purpose.id } } ]; // const idNin = params.id?.$nin; // if (Array.isArray(idNin)) { // andConditions.push({ _id: { $nin: idNin } }); // } const objectPaymentMethodIdEq = (_b = (_a = params.object) === null || _a === void 0 ? void 0 : _a.paymentMethodId) === null || _b === void 0 ? void 0 : _b.$eq; if (typeof objectPaymentMethodIdEq === 'string') { andConditions.push({ 'object.paymentMethodId': { $exists: true, $eq: objectPaymentMethodIdEq } }); } const objectTypeOfEq = (_d = (_c = params.object) === null || _c === void 0 ? void 0 : _c.typeOf) === null || _d === void 0 ? void 0 : _d.$eq; if (typeof objectTypeOfEq === 'string') { andConditions.push({ 'object.typeOf': { $exists: true, $eq: objectTypeOfEq } }); } if (typeof params.typeOf === 'string') { andConditions.push({ typeOf: { $eq: params.typeOf } }); } const actionStatusEq = (_e = params.actionStatus) === null || _e === void 0 ? void 0 : _e.$eq; if (typeof actionStatusEq === 'string') { andConditions.push({ actionStatus: { $eq: actionStatusEq } }); } const positiveProjectionFields = [ 'project', 'actionStatus', 'typeOf', 'description', 'agent', 'recipient', 'result', 'error', 'object', 'startDate', 'endDate', 'purpose', 'potentialActions', 'instrument', 'location', 'sameAs', 'cancelAction', 'identifier' ]; const projection = Object.assign({ _id: 0, id: { $toString: '$_id' } }, Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1])))); const query = this.actionModel.find((andConditions.length > 0) ? { $and: andConditions } : {}, projection); if (typeof ((_f = params.sort) === null || _f === void 0 ? void 0 : _f.startDate) === 'number') { query.sort({ startDate: params.sort.startDate }); } return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .lean() .exec(); // return <Promise<IAction4transaction<T>[]>><Promise<unknown[]>>this.search<T>( // { // purpose: { // id: { $in: [params.purpose.id] }, // typeOf: { $in: [params.purpose.typeOf] } // }, // object: { // ...(typeof params.object?.typeOf?.$eq === 'string') // ? { typeOf: { $eq: params.object.typeOf.$eq } } // : undefined, // ...(typeof params.object?.paymentMethodId?.$eq === 'string') // ? { paymentMethodId: { $eq: params.object.paymentMethodId.$eq } } // : undefined // }, // ...(typeof params.actionStatus?.$eq === 'string') ? { actionStatus: { $in: [params.actionStatus.$eq] } } : undefined, // ...(typeof params.typeOf === 'string') ? { typeOf: { $eq: params.typeOf } } : undefined, // ...(Array.isArray(params.id?.$nin)) ? { id: { $nin: params.id?.$nin } } : undefined, // ...(typeof params.sort?.startDate === 'number') ? { sort: params.sort } : undefined // }, // [], // [] // ); }); } /** * 注文番号から、注文に対するアクションを検索する */ searchByOrderNumber(params) { return __awaiter(this, void 0, void 0, function* () { var _a; const filter = { $or: [ { 'object.orderNumber': { $exists: true, $eq: params.orderNumber } }, { 'purpose.orderNumber': { $exists: true, $eq: params.orderNumber } }, // consider inform returnAction(2025-01-22~) { 'about.orderNumber': { $exists: true, $eq: params.orderNumber } }, // consider reserveAction,cancelReservationAction(2025-02-17~) { 'instrument.orderNumber': { $exists: true, $eq: params.orderNumber } } ] }; const projection = Object.assign({ _id: 0, id: { $toString: '$_id' } }, Object.fromEntries(AVAILABLE_PROJECT_FIELDS.map((key) => ([key, 1])))); const query = this.actionModel.find(filter, projection); // .select({ __v: 0, createdAt: 0, updatedAt: 0 }); // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (((_a = params.sort) === null || _a === void 0 ? void 0 : _a.startDate) !== undefined) { query.sort({ startDate: params.sort.startDate }); } return query .lean() // 2024-08-26~ .exec(); }); } searchBySameAs(params) { return __awaiter(this, void 0, void 0, function* () { var _a; const projection = { _id: 0, id: { $toString: '$_id' }, actionStatus: 1, error: 1, purpose: 1 }; const query = this.actionModel.find(Object.assign({ typeOf: { $eq: params.typeOf.$eq }, 'sameAs.id': { $exists: true, $eq: params.sameAs.id.$eq } }, (typeof ((_a = params.purpose) === null || _a === void 0 ? void 0 : _a.id.$eq) === 'string') ? { 'purpose.id': { $exists: true, $eq: params.purpose.id.$eq } } : undefined), projection) // .select({ _id: 1, actionStatus: 1, error: 1, purpose: 1 }) .limit(1); return query .lean() // 2024-08-26~ .exec(); }); } deleteByProject(params) { return __awaiter(this, void 0, void 0, function* () { yield this.actionModel.deleteMany({ 'project.id': { $eq: params.project.id } }) .exec(); }); } reCompleteAuthorizeEventOfferAction(params) { return __awaiter(this, void 0, void 0, function* () { return this.actionModel.findOneAndUpdate({ // typeOf: factory.actionType.AuthorizeAction, _id: { $eq: params.id }, // ActiveActionStatus->CompletedActionStatusで再実装(2024-01-15~) // actionStatus: factory.actionStatusType.CompletedActionStatus actionStatus: { $eq: factory.actionStatusType.ActiveActionStatus } }, { $set: { actionStatus: factory.actionStatusType.CompletedActionStatus, object: params.object, result: params.result, endDate: new Date() } }, { new: true, projection: { _id: 1 } }) .lean() .exec() .then((doc) => { if (doc === null) { throw new factory.errors.NotFound(this.actionModel.modelName); } }); }); } updateById(params) { return __awaiter(this, void 0, void 0, function* () { yield this.actionModel.updateOne({ _id: { $eq: params.id } }, params.update) .exec(); }); } findByIdAndUpdate(params) { return __awaiter(this, void 0, void 0, function* () { yield this.actionModel.findOneAndUpdate({ _id: { $eq: params.id } }, params.update, { projection: { _id: 1 } }) .lean() .exec() .then((doc) => { if (doc === null) { throw new factory.errors.NotFound(this.actionModel.modelName); } }); }); } /** * イベントと入場ゲート指定で予約使用アクションを集計する * discontinue(2024-12-26~) */ // public async countUseActionsByEntranceGate(params: { // event: { id: string }; // entranceGateIdentifier: string; // }): Promise<IUseActionCountByOffer[]> { // return this.actionModel.aggregate([ // { // $match: { // actionStatus: { $eq: factory.actionStatusType.CompletedActionStatus } // } // }, // { // $match: { // typeOf: { $eq: factory.actionType.UseAction } // } // }, // { // $match: { // 'object.typeOf': { // $exists: true, // $eq: factory.reservationType.EventReservation // } // } // }, // { // $match: { // 'object.reservationFor.id': { // $exists: true, // $eq: params.event.id // } // } // }, // { // $match: { // 'location.identifier': { // $exists: true, // $eq: params.entranceGateIdentifier // } // } // }, // { // $group: { // _id: '$object.id', // object: { $first: '$object' } // } // }, // { // $group: { // _id: '$object.reservedTicket.ticketType.id', // useActionCount: { // $sum: 1 // } // } // } // ]) // .exec(); // } searchYkknInfoByCheckRecipe(filter) { return __awaiter(this, void 0, void 0, function* () { const filterQuery = { _id: { $eq: filter.id }, 'project.id': { $eq: filter.project.id }, 'purpose.id': { $exists: true, $eq: filter.purpose.id }, typeOf: { $eq: factory.actionType.CheckAction }, actionStatus: { $eq: factory.actionStatusType.CompletedActionStatus } }; const actionDoc = yield this.actionModel.findOne(filterQuery, { _id: 1 }, { lean: true }) .exec(); if (actionDoc === null) { throw new factory.errors.NotFound(this.actionModel.modelName); } const filterQuery4recipe = { 'recipeFor.id': { $eq: filter.id } }; const projectStage = { _id: 0, knyknrNo: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.knyknrNo', ykknshTyp: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.ykknInfo.ykknshTyp', eishhshkTyp: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.ykknInfo.eishhshkTyp', ykknKnshbtsmiNum: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.ykknInfo.ykknKnshbtsmiNum', knshknhmbiUnip: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.ykknInfo.knshknhmbiUnip', kijUnip: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.ykknInfo.kijUnip' }; const aggregate = this.actionRecipeModel.aggregate([ { $unwind: '$step' }, { $unwind: '$step.itemListElement' }, { $unwind: '$step.itemListElement.itemListElement' }, { $unwind: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut' }, { $unwind: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.ykknInfo' }, { $match: filterQuery4recipe }, { $project: projectStage } ]); if (typeof filter.limit === 'number' && filter.limit > 0) { const page = (typeof filter.page === 'number' && filter.page > 0) ? filter.page : 1; aggregate.limit(filter.limit * page) .skip(filter.limit * (page - 1)); } return aggregate .option({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec(); }); } searchMkknInfoByCheckRecipe(filter) { return __awaiter(this, void 0, void 0, function* () { const filterQuery = { _id: { $eq: filter.id }, 'project.id': { $eq: filter.project.id }, 'purpose.id': { $exists: true, $eq: filter.purpose.id }, typeOf: { $eq: factory.actionType.CheckAction }, actionStatus: { $eq: factory.actionStatusType.CompletedActionStatus } }; const actionDoc = this.actionModel.findOne(filterQuery, { _id: 1 }, { lean: true }) .exec(); if (actionDoc === null) { throw new factory.errors.NotFound(this.actionModel.modelName); } const filterQuery4recipe = { 'recipeFor.id': { $eq: filter.id } }; const projectStage = { _id: 0, knyknrNo: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.knyknrNo', mkknshTyp: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.mkknInfo.mkknshTyp', mkknKnshbtsmiNum: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.mkknInfo.mkknKnshbtsmiNum', mkjyTyp: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.mkknInfo.mkjyTyp', yykDt: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.mkknInfo.yykDt', shyJeiDt: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.mkknInfo.shyJeiDt', shyStCd: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.mkknInfo.shyStCd', shyScrnCd: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.mkknInfo.shyScrnCd', shySkhnCd: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.mkknInfo.shySkhnCd', shySkhnNm: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.mkknInfo.shySkhnNm' }; const aggregate = this.actionRecipeModel.aggregate([ { $unwind: '$step' }, { $unwind: '$step.itemListElement' }, { $unwind: '$step.itemListElement.itemListElement' }, { $unwind: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut' }, { $unwind: '$step.itemListElement.itemListElement.afterMedia.knyknrNoInfoOut.mkknInfo' }, { $match: filterQuery4recipe }, { $project: projectStage } ]); if (typeof filter.limit === 'number' && filter.limit > 0) { const page = (typeof filter.page === 'number' && filter.page > 0) ? filter.page : 1; aggregate.limit(filter.limit * page) .skip(filter.limit * (page - 1)); } ret