UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

1,152 lines (1,151 loc) 53.2 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.TransactionRepo = void 0; const moment = require("moment"); const transaction_1 = require("../eventEmitter/transaction"); const factory = require("../factory"); const settings_1 = require("../settings"); const transaction_2 = require("./mongoose/schemas/transaction"); const AVAILABLE_PROJECT_FIELDS = [ 'project', 'status', 'typeOf', 'agent', 'recipient', 'seller', 'error', 'result', 'object', 'expires', 'startDate', 'endDate', 'tasksExportAction', 'potentialActions', 'instrument' ]; /** * 取引リポジトリ */ class TransactionRepo { constructor(connection) { this.transactionModel = connection.model(transaction_2.modelName, (0, transaction_2.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; const andConditions = []; const projectIdEq = (_b = (_a = params.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$eq; // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (typeof projectIdEq === 'string') { andConditions.push({ 'project.id': { $eq: projectIdEq } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (typeof params.typeOf === 'string') { andConditions.push({ typeOf: { $eq: params.typeOf } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.startFrom !== undefined) { andConditions.push({ startDate: { $gte: params.startFrom } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.startThrough !== undefined) { andConditions.push({ startDate: { $lte: params.startThrough } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.endFrom !== undefined) { andConditions.push({ endDate: { $exists: true, $gte: params.endFrom } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.endThrough !== undefined) { andConditions.push({ endDate: { $exists: true, $lt: params.endThrough } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(params.ids)) { andConditions.push({ _id: { $in: params.ids } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(params.statuses)) { andConditions.push({ status: { $in: params.statuses } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ const statusIn = (_c = params.status) === null || _c === void 0 ? void 0 : _c.$in; if (Array.isArray(statusIn)) { andConditions.push({ status: { $in: statusIn } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.agent !== undefined) { // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.agent.typeOf !== undefined) { andConditions.push({ 'agent.typeOf': { $exists: true, $eq: params.agent.typeOf } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(params.agent.ids)) { andConditions.push({ 'agent.id': { $exists: true, $in: params.agent.ids } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(params.agent.identifiers)) { andConditions.push({ 'agent.identifier': { $exists: true, $in: params.agent.identifiers } }); } // if (typeof params.agent.familyName === 'string' && params.agent.familyName.length > 0) { // andConditions.push({ // 'agent.familyName': { // $exists: true, // $regex: new RegExp(params.agent.familyName) // } // }); // } // if (typeof params.agent.givenName === 'string' && params.agent.givenName.length > 0) { // andConditions.push({ // 'agent.givenName': { // $exists: true, // $regex: new RegExp(params.agent.givenName) // } // }); // } // if (typeof params.agent.email === 'string' && params.agent.email.length > 0) { // andConditions.push({ // 'agent.email': { // $exists: true, // $regex: new RegExp(params.agent.email) // } // }); // } // if (typeof params.agent.telephone === 'string' && params.agent.telephone.length > 0) { // andConditions.push({ // 'agent.telephone': { // $exists: true, // $regex: new RegExp(params.agent.telephone) // } // }); // } } const tasksExportActionStatusIn = (_e = (_d = params.tasksExportAction) === null || _d === void 0 ? void 0 : _d.actionStatus) === null || _e === void 0 ? void 0 : _e.$in; // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(tasksExportActionStatusIn)) { andConditions.push({ 'tasksExportAction.actionStatus': { $exists: true, $in: tasksExportActionStatusIn } }); } const tasksExportActionStatusEq = (_g = (_f = params.tasksExportAction) === null || _f === void 0 ? void 0 : _f.actionStatus) === null || _g === void 0 ? void 0 : _g.$eq; // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (typeof tasksExportActionStatusEq === 'string') { andConditions.push({ 'tasksExportAction.actionStatus': { $exists: true, $eq: tasksExportActionStatusEq } }); } switch (params.typeOf) { case factory.transactionType.PlaceOrder: const sellerIdIn = (_h = params.seller) === null || _h === void 0 ? void 0 : _h.ids; if (Array.isArray(sellerIdIn)) { andConditions.push({ 'seller.id': { $exists: true, $in: sellerIdIn } }); } const resultOrderNumberIn = (_k = (_j = params.result) === null || _j === void 0 ? void 0 : _j.order) === null || _k === void 0 ? void 0 : _k.orderNumbers; if (Array.isArray(resultOrderNumberIn)) { andConditions.push({ 'result.order.orderNumber': { $exists: true, $in: resultOrderNumberIn } }); } const resultOrderConfirmationNumberEq = (_o = (_m = (_l = params.result) === null || _l === void 0 ? void 0 : _l.order) === null || _m === void 0 ? void 0 : _m.confirmationNumber) === null || _o === void 0 ? void 0 : _o.$eq; if (typeof resultOrderConfirmationNumberEq === 'string') { andConditions.push({ 'result.order.confirmationNumber': { $exists: true, $eq: resultOrderConfirmationNumberEq } }); } const objectOrderNumberEq = (_q = (_p = params.object) === null || _p === void 0 ? void 0 : _p.orderNumber) === null || _q === void 0 ? void 0 : _q.$eq; if (typeof objectOrderNumberEq === 'string') { andConditions.push({ 'object.orderNumber': { $exists: true, $eq: objectOrderNumberEq } }); } break; case factory.transactionType.ReturnOrder: // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.object !== undefined) { // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.object.order !== undefined) { // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (Array.isArray(params.object.order.orderNumbers)) { andConditions.push({ 'object.order.orderNumber': { $exists: true, $in: params.object.order.orderNumbers } }); } } } break; default: } return andConditions; } /** * 取引を開始する */ start(params) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const status = factory.transactionStatusType.InProgress; const tasksExportAction = { actionStatus: factory.actionStatusType.PotentialActionStatus }; // const tasksExportationStatus = factory.transactionTasksExportationStatus.Unexported; // discontinue(2024-06-20~) const startDate = new Date(); let expires; const { typeOf } = params; if (typeOf === factory.transactionType.PlaceOrder || typeOf === factory.transactionType.ReturnOrder) { // expiresInSecondsの指定があれば優先して適用する(2022-11-25~) if (typeof params.expiresInSeconds === 'number' && params.expiresInSeconds > 0) { expires = moment(startDate) .add(params.expiresInSeconds, 'seconds') .toDate(); } else { throw new factory.errors.ArgumentNull('expiresInSeconds'); } } else { expires = params.expires; } let creatingTransaction; if (typeOf === factory.transactionType.MoneyTransfer) { const { agent, project, object, seller, recipient } = params; creatingTransaction = { status, startDate, expires, typeOf, tasksExportAction, agent, project, seller, object, recipient }; } else if (typeOf === factory.transactionType.PlaceOrder) { const { agent, project, object, seller, instrument } = params; creatingTransaction = Object.assign({ status, startDate, expires, typeOf, tasksExportAction, agent, project, seller, object }, (typeof (instrument === null || instrument === void 0 ? void 0 : instrument.id) === 'string') ? { instrument } : undefined); } else if (typeOf === factory.transactionType.ReturnOrder) { const { agent, project, object, seller } = params; creatingTransaction = { status, startDate, expires, typeOf, tasksExportAction, agent, project, seller, object }; } else { throw new factory.errors.NotImplemented(`${typeOf} not implemented`); } // reimplemnt with insertMany(2024-05-30~) const result = yield this.transactionModel.insertMany(creatingTransaction, { 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('transaction not saved'); } // 取引開始時にも取引イベントエミッター連携(2024-03-21~) transaction_1.transactionEventEmitter.emitTransactionStatusChanged({ id, typeOf, status }); return { expires, id, startDate, status }; // minimize response(2024-05-30~) }); } /** * 特定取引検索 */ projectFieldsById(params, inclusion // make required(2024-05-31~) ) { return __awaiter(this, void 0, void 0, function* () { let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS; if (Array.isArray(inclusion) && inclusion.length > 0) { positiveProjectionFields = positiveProjectionFields.filter((key) => inclusion.includes(key)); } else { throw new factory.errors.NotImplemented('inclusion must be specified'); // 2024-08-26~ } const projection = Object.assign({ _id: 0, id: { $toString: '$_id' } }, Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1])))); // let projection: { [key in (IKeyOfProjection<T> | '__v' | 'createdAt' | 'updatedAt')]?: AnyExpression } = {}; // if (Array.isArray(inclusion) && inclusion.length > 0) { // inclusion.forEach((field) => { // projection[field] = 1; // }); // } else { // projection = { // __v: 0, // createdAt: 0, // updatedAt: 0 // }; // } const doc = yield this.transactionModel.findOne({ _id: { $eq: params.id }, typeOf: { $eq: params.typeOf } }, projection) .lean() // 2024-08-26~ .exec(); if (doc === null) { throw new factory.errors.NotFound(this.transactionModel.modelName); } return doc; }); } /** * 進行中の取引を取得する */ projectFieldsInProgressById(params, inclusion // make required(2024-05-29~) ) { return __awaiter(this, void 0, void 0, function* () { let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS; if (Array.isArray(inclusion) && inclusion.length > 0) { positiveProjectionFields = positiveProjectionFields.filter((key) => inclusion.includes(key)); } else { throw new factory.errors.NotImplemented('inclusion must be specified'); // 2024-08-26~ } const projection = Object.assign({ _id: 0, id: { $toString: '$_id' } }, Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1])))); // let projection: { [key in (IKeyOfProjection<T> | '__v' | 'createdAt' | 'updatedAt')]?: ProjectionElementType } = {}; // if (Array.isArray(inclusion) && inclusion.length > 0) { // inclusion.forEach((field) => { // projection[field] = 1; // }); // } else { // // minimize(2024-05-29~) // projection = { // __v: 0, // createdAt: 0, // updatedAt: 0, // tasksExportAction: 0, // ...{ // tasksExportedAt: 0, // tasksExportationStatus: 0 // } // }; // } const filter = { _id: { $eq: params.id }, typeOf: { $eq: params.typeOf }, status: { $eq: factory.transactionStatusType.InProgress }, // expiresを条件に追加(2023-05-12~), expires: { $gt: new Date() } }; const doc = yield this.transactionModel.findOne(filter, projection) .lean() // 2024-08-26~ .exec(); if (doc === null) { throw new factory.errors.NotFound(this.transactionModel.modelName); } return doc; }); } /** * 進行中取引に保管された採用済決済方法を検索する */ findInProgressPaymentMethodId(params) { return __awaiter(this, void 0, void 0, function* () { const doc = yield this.transactionModel.findOne({ _id: { $eq: params.id }, typeOf: { $eq: factory.transactionType.PlaceOrder }, status: { $eq: factory.transactionStatusType.InProgress } }, { 'object.paymentMethods': 1 }) .lean() // 2024-08-26~ .exec(); if (doc === null) { throw new factory.errors.NotFound(this.transactionModel.modelName); } return doc.object.paymentMethods; }); } /** * 取引の注文番号を検索する */ findInProgressOrderNumberById(params) { return __awaiter(this, void 0, void 0, function* () { const doc = yield this.transactionModel.findOne({ _id: { $eq: params.id }, typeOf: { $eq: factory.transactionType.PlaceOrder }, status: { $eq: factory.transactionStatusType.InProgress } }, { 'object.orderNumber': 1 }) .lean() // 2024-08-26~ .exec(); if (doc === null) { throw new factory.errors.NotFound(this.transactionModel.modelName); } return doc.object.orderNumber; }); } /** * 取引の確認番号を検索する */ findInProgressConfirmationNumberById(params) { return __awaiter(this, void 0, void 0, function* () { const doc = yield this.transactionModel.findOne({ _id: { $eq: params.id }, typeOf: { $eq: factory.transactionType.PlaceOrder }, status: { $in: params.status.$in } }, { 'object.confirmationNumber': 1 }) .lean() // 2024-08-26~ .exec(); if (doc === null) { throw new factory.errors.NotFound(this.transactionModel.modelName); } return doc.object.confirmationNumber; }); } /** * 取引進行者プロフィールを更新 */ updateAgent(params) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const doc = yield this.transactionModel.findOneAndUpdate({ _id: { $eq: params.id }, typeOf: { $eq: params.typeOf }, status: { $eq: factory.transactionStatusType.InProgress } }, { $set: Object.assign({ 'agent.id': params.agent.id }, (typeof ((_b = (_a = params.object) === null || _a === void 0 ? void 0 : _a.customer) === null || _b === void 0 ? void 0 : _b.typeOf) === 'string') ? { 'object.customer': params.object.customer } : undefined) }, { projection: { _id: 1 } }) .lean() .exec(); if (doc === null) { throw new factory.errors.NotFound(this.transactionModel.modelName); } }); } /** * 取引期限変更 */ updateExpires(params) { return __awaiter(this, void 0, void 0, function* () { const doc = yield this.transactionModel.findOneAndUpdate({ _id: { $eq: params.id }, typeOf: { $eq: params.typeOf }, status: { $eq: factory.transactionStatusType.InProgress } }, { $set: { expires: params.expires } }, { projection: { _id: 1 } }) .lean() .exec(); if (doc === null) { throw new factory.errors.NotFound(this.transactionModel.modelName); } }); } /** * 取引オブジェクトを更新 * 注文名称など */ updateObject(params) { return __awaiter(this, void 0, void 0, function* () { var _a; const doc = yield this.transactionModel.findOneAndUpdate({ _id: { $eq: params.id }, typeOf: { $eq: params.typeOf }, status: { $eq: factory.transactionStatusType.InProgress } }, { $set: Object.assign({}, (typeof ((_a = params.object) === null || _a === void 0 ? void 0 : _a.name) === 'string') ? { 'object.name': params.object.name } : undefined) }, { projection: { _id: 1 } }) .lean() .exec(); if (doc === null) { throw new factory.errors.NotFound(this.transactionModel.modelName); } }); } /** * 取引を確定する */ confirm(params) { return __awaiter(this, void 0, void 0, function* () { const endDate = new Date(); const doc = yield this.transactionModel.findOneAndUpdate({ _id: { $eq: params.id }, typeOf: { $eq: params.typeOf }, status: { $eq: factory.transactionStatusType.InProgress }, expires: { $gt: endDate } // add expires(2025-02-27~) }, { status: factory.transactionStatusType.Confirmed, // ステータス変更 endDate, // 'object.authorizeActions': params.authorizeActions, result: params.result, // resultを更新 potentialActions: params.potentialActions // resultを更新 }, { new: true, projection: { _id: 1 } }) .lean() .exec(); // NotFoundであれば取引状態確認 if (doc === null) { const { expires, status } = yield this.projectFieldsById({ typeOf: params.typeOf, id: params.id }, ['expires', 'status']); if (status === factory.transactionStatusType.Confirmed) { // すでに確定済の場合スルー } else if (status === factory.transactionStatusType.Expired) { throw new factory.errors.Argument('Transaction id', 'Already expired'); } else if (status === factory.transactionStatusType.Canceled) { throw new factory.errors.Argument('Transaction id', 'Already canceled'); } else { if (moment(expires) .isSameOrBefore(moment(endDate))) { throw new factory.errors.Argument('Transaction id', 'potentially expired'); } throw new factory.errors.NotFound(this.transactionModel.modelName); } } transaction_1.transactionEventEmitter.emitTransactionStatusChanged({ id: params.id, typeOf: params.typeOf, status: factory.transactionStatusType.Confirmed }); }); } countPotentiallyExportTasks(params) { return __awaiter(this, void 0, void 0, function* () { const { endDate, limit } = params; const endDateLt = endDate === null || endDate === void 0 ? void 0 : endDate.$lt; if (!(endDateLt instanceof Date)) { throw new factory.errors.Argument('endDate.$lt', 'must be Date'); } const query = this.transactionModel.countDocuments({ status: { $in: [ factory.transactionStatusType.Canceled, factory.transactionStatusType.Confirmed, factory.transactionStatusType.Expired ] }, 'tasksExportAction.actionStatus': { $exists: true, $eq: factory.actionStatusType.PotentialActionStatus }, endDate: { $exists: true, $lt: endDateLt } }); if (typeof limit === 'number' && limit >= 0) { query.limit(limit); } const count = yield query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec(); return { count }; }); } /** * タスク未エクスポートの取引をひとつ取得してエクスポートを開始する */ startExportTasks(params) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; const statusEq = (_a = params.status) === null || _a === void 0 ? void 0 : _a.$eq; const endDateLt = (_b = params.endDate) === null || _b === void 0 ? void 0 : _b.$lt; if (typeof statusEq === 'string') { switch (statusEq) { case factory.transactionStatusType.InProgress: throw new factory.errors.NotImplemented(`status "${params.status}" not implemented on startExportTasks`); default: // no op } } if (!(endDateLt instanceof Date)) { throw new factory.errors.Argument('endDate.$lt', 'must be Date'); } const query = this.transactionModel.findOneAndUpdate(Object.assign({ // ...(Array.isArray(typeOfIn)) ? { typeOf: { $in: typeOfIn } } : undefined, status: Object.assign({}, (typeof statusEq === 'string') ? { $eq: statusEq } : { $in: [ factory.transactionStatusType.Canceled, factory.transactionStatusType.Confirmed, factory.transactionStatusType.Expired ] }), 'tasksExportAction.actionStatus': { $exists: true, $eq: factory.actionStatusType.PotentialActionStatus }, endDate: { $exists: true, $lt: endDateLt } }, (typeof params.id === 'string') ? { _id: { $eq: params.id } } : undefined), { 'tasksExportAction.actionStatus': factory.actionStatusType.ActiveActionStatus, 'tasksExportAction.agent': { name: params.tasksExportAction.agent.name }, 'tasksExportAction.startDate': new Date() }, { new: true, projection: { // _id: 1, _id: 0, id: { $toString: '$_id' }, typeOf: 1 } }); if (typeof ((_c = params.sort) === null || _c === void 0 ? void 0 : _c.endDate) === 'number' || typeof ((_d = params.sort) === null || _d === void 0 ? void 0 : _d.startDate) === 'number') { query.sort(params.sort); } return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .lean() // 2024-08-26~ .exec() // tslint:disable-next-line:no-null-keyword .then((doc) => (doc === null) ? null : doc); }); } // discontinue(2025-03-10~) // public async reexportTasksByExportAction(params: { // intervalInMinutes: number; // limit: number; // }): Promise<void> { // const reexportingTransactions: Pick<factory.transaction.ITransaction<factory.transactionType>, 'id' | 'status' | 'typeOf'>[] = // await this.transactionModel.find( // { // 'tasksExportAction.actionStatus': { // $exists: true, // $eq: factory.actionStatusType.ActiveActionStatus // }, // 'tasksExportAction.startDate': { // $exists: true, // $lt: moment() // .add(-params.intervalInMinutes, 'minutes') // .toDate() // } // }, // { _id: 1, typeOf: 1, status: 1 } // ) // .limit(params.limit) // .setOptions({ maxTimeMS: MONGO_MAX_TIME_MS }) // .exec(); // if (reexportingTransactions.length > 0) { // for (const reexportingTransaction of reexportingTransactions) { // await this.transactionModel.updateOne( // { // _id: { $eq: reexportingTransaction.id }, // 'tasksExportAction.actionStatus': { // $exists: true, // $eq: factory.actionStatusType.ActiveActionStatus // } // }, // { // tasksExportAction: { // actionStatus: factory.actionStatusType.PotentialActionStatus // } // // tasksExportationStatus: factory.transactionTasksExportationStatus.Unexported // discontinue(2024-06-20~) // } // ) // .exec(); // transactionEventEmitter.emitTransactionStatusChanged({ // id: reexportingTransaction.id, // typeOf: reexportingTransaction.typeOf, // status: reexportingTransaction.status // }); // } // } // } /** * tasksExportAction.actionStatusがActiveActionStatusのまま一定時間経過した取引について * PotentialActionStatusに変更する * 2025-03-10~ */ reExportAction(params) { return __awaiter(this, void 0, void 0, function* () { const { startDate } = params; if (!(startDate.$lt instanceof Date)) { throw new factory.errors.Argument('startDate.$lt', 'must be Date'); } return this.transactionModel.updateMany({ 'tasksExportAction.actionStatus': { $exists: true, $eq: factory.actionStatusType.ActiveActionStatus }, 'tasksExportAction.startDate': { $exists: true, $lt: startDate.$lt } }, { $set: { tasksExportAction: { actionStatus: factory.actionStatusType.PotentialActionStatus } } }) .exec(); }); } /** * タスクエクスポートの遅延している取引について明示的にemitTransactionStatusChangedを実行する */ exportTasksMany(params) { return __awaiter(this, void 0, void 0, function* () { const delayedTransactions = yield this.transactionModel.find({ status: { $in: params.status.$in }, 'tasksExportAction.actionStatus': { $exists: true, $eq: factory.actionStatusType.PotentialActionStatus }, endDate: { $exists: true, $lt: moment(params.now) .add(-params.delayInSeconds, 'seconds') .toDate() } }) .select({ _id: 0, id: { $toString: '$_id' }, typeOf: 1, status: 1 }) .limit(params.limit) .setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .lean() .exec(); if (delayedTransactions.length > 0) { delayedTransactions.forEach((delayedTransaction) => { transaction_1.transactionEventEmitter.emitTransactionStatusChanged({ id: delayedTransaction.id, typeOf: delayedTransaction.typeOf, status: delayedTransaction.status }); }); } return delayedTransactions; }); } /** * set task status exported by transaction id * IDでタスクをエクスポート済に変更する */ setTasksExportedById(params) { return __awaiter(this, void 0, void 0, function* () { const endDate = new Date(); yield this.transactionModel.updateOne({ _id: { $eq: params.id }, // remove dependency on tasksExportationStatus(2024-06-17~) // tasksExportationStatus: { $eq: factory.transactionTasksExportationStatus.Exporting } 'tasksExportAction.actionStatus': { $exists: true, $eq: factory.actionStatusType.ActiveActionStatus } }, { // tasksExportationStatus: factory.transactionTasksExportationStatus.Exported, // discontinue(2024-06-20~) // tasksExportedAt: endDate, // discontinue(2024-06-20~) 'tasksExportAction.actionStatus': factory.actionStatusType.CompletedActionStatus, 'tasksExportAction.endDate': endDate }) .exec(); }); } /** * add(2025-03-13~) */ countPotentiallyExpired(params) { return __awaiter(this, void 0, void 0, function* () { const { expires, limit } = params; const query = this.transactionModel.countDocuments({ status: { $eq: factory.transactionStatusType.InProgress }, expires: { $lt: expires.$lt } }); if (typeof limit === 'number' && limit >= 0) { query.limit(limit); } const count = yield query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec(); return { count }; }); } /** * add(2025-03-03~) */ makeOneExpiredIfExists(params) { return __awaiter(this, void 0, void 0, function* () { const endDate = new Date(); const sort = { expires: factory.sortType.Ascending }; const doc = yield this.transactionModel.findOneAndUpdate({ status: { $eq: factory.transactionStatusType.InProgress }, expires: { $lt: params.expires.$lt } }, { status: factory.transactionStatusType.Expired, endDate }, { new: true, projection: { _id: 0, id: { $toString: '$_id' }, typeOf: 1 } }) .sort(sort) .lean() .exec(); if (doc === null) { // no op } else { transaction_1.transactionEventEmitter.emitTransactionStatusChanged({ id: doc.id, typeOf: doc.typeOf, status: factory.transactionStatusType.Expired }); return doc; } }); } /** * 取引を期限切れにする */ makeExpired(params) { return __awaiter(this, void 0, void 0, function* () { if (typeof params.limit !== 'number' || params.limit === 0) { throw new factory.errors.ArgumentNull('limit'); } const sort = { expires: factory.sortType.Ascending }; // IDをemitしたいのでまずリスト検索(2023-04-27~) const expiringTransactions = yield this.transactionModel.find({ status: { $eq: factory.transactionStatusType.InProgress }, expires: { $lt: params.expires.$lt } }) .select({ _id: 0, id: { $toString: '$_id' }, typeOf: 1 }) .limit(params.limit) .sort(sort) // sort(2025-03-03~) .setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .lean() .exec(); if (expiringTransactions.length > 0) { // ステータスと期限を見て更新 yield this.transactionModel.updateMany({ _id: { $in: expiringTransactions.map((t) => t.id) }, status: { $eq: factory.transactionStatusType.InProgress }, expires: { $lt: params.expires.$lt } }, { status: factory.transactionStatusType.Expired, endDate: new Date() }) .exec(); expiringTransactions.forEach((expiringTransaction) => { transaction_1.transactionEventEmitter.emitTransactionStatusChanged({ id: expiringTransaction.id, typeOf: expiringTransaction.typeOf, status: factory.transactionStatusType.Expired }); }); } return expiringTransactions; }); } /** * 取引を中止する */ cancel(params) { return __awaiter(this, void 0, void 0, function* () { const endDate = new Date(); // 進行中ステータスの取引を中止する const doc = yield this.transactionModel.findOneAndUpdate({ typeOf: params.typeOf, _id: params.id, status: factory.transactionStatusType.InProgress }, { status: factory.transactionStatusType.Canceled, endDate: endDate }, { new: true, projection: { _id: 1 } }) .lean() .exec(); // NotFoundであれば取引状態確認 if (doc === null) { const { status } = yield this.projectFieldsById(params, ['status']); if (status === factory.transactionStatusType.Canceled) { // すでに中止済の場合スルー } else if (status === factory.transactionStatusType.Expired) { throw new factory.errors.Argument('Transaction id', 'Transaction already expired'); } else if (status === factory.transactionStatusType.Confirmed) { throw new factory.errors.Argument('Transaction id', 'Confirmed transaction unable to cancel'); } else { throw new factory.errors.NotFound(this.transactionModel.modelName); } } transaction_1.transactionEventEmitter.emitTransactionStatusChanged({ id: params.id, typeOf: params.typeOf, status: factory.transactionStatusType.Canceled }); }); } count(params) { return __awaiter(this, void 0, void 0, function* () { const { limit } = params; const conditions = TransactionRepo.CREATE_MONGO_CONDITIONS(params); const query = this.transactionModel.countDocuments((conditions.length > 0) ? { $and: conditions } : {}); if (typeof limit === 'number' && limit >= 0) { query.limit(limit); } const count = yield query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec(); return { count }; }); } /** * 取引を検索する */ projectFields(params) { return __awaiter(this, void 0, void 0, function* () { var _a; const { inclusion } = params; const conditions = TransactionRepo.CREATE_MONGO_CONDITIONS(params); let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS; if (Array.isArray(inclusion) && inclusion.length > 0) { positiveProjectionFields = positiveProjectionFields.filter((key) => inclusion.includes(key)); } const projection = Object.assign({ _id: 0, id: { $toString: '$_id' } }, Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1])))); // let projection: { [key in (IKeyOfProjection<T> | '__v' | 'createdAt' | 'updatedAt')]?: AnyExpression } = {}; // if (Array.isArray(params.inclusion) && params.inclusion.length > 0) { // params.inclusion.forEach((field) => { // projection[field] = 1; // }); // } else { // projection = { // __v: 0, // createdAt: 0, // updatedAt: 0 // }; // } const query = this.transactionModel.find((conditions.length > 0) ? { $and: conditions } : {}) .select(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(); }); } /** * 特定の進行中取引を更新する(汎用) */ findByIdAndUpdateInProgress(params) { return __awaiter(this, void 0, void 0, function* () { yield this.transactionModel.findOneAndUpdate({ _id: { $eq: params.id }, status: { $eq: factory.transactionStatusType.InProgress } }, params.update, { // new: true, projection: { _id: 1 } }) .lean() .exec() .then((doc) => { if (doc === null) { throw new factory.errors.ArgumentNull(this.transactionModel.modelName); } }); }); } saveOrderNumberIfNotExist(params) { return __awaiter(this, void 0, void 0, function* () { yield this.transactionModel.updateOne({ _id: { $eq: params.id }, status: { $eq: factory.transactionStatusType.InProgress }, 'object.orderNumber': { $exists: false } }, { 'object.orderNumber': params.orderNumber }) .exec(); }); } saveConfirmationNumberIfNotExist(params) { return __awaiter(this, void 0, void 0, function* () { yield this.transactionModel.updateOne({ _id: { $eq: params.id }, // status: { $eq: factory.transactionStatusType.InProgress }, status: { $in: params.status.$in }, 'object.confirmationNumber': { $exists: false } }, { $set: { 'object.confirmationNumber': params.confirmationNumber } }) .exec(); }); } findByIdAndDelete(params) { return __awaiter(this, void 0, void 0, function* () { yield this.transactionModel.findByIdAndDelete(params.id) .exec(); }); } unsetUnnecessaryFields(params) { return __awaiter(this, void 0, void 0, function* () { return this.transactionModel.updateMany(params.filter, { $unset: params.$unset }, { timestamps: false }) .exec(); }); } /** * 終了日時を一定期間過ぎたアクションを削除する */ deleteEndDatePassedCertainPeriod(params) { return __awaiter(this, void 0, void 0, function* () { return this.transactionModel.deleteMany({ typeOf: { $eq: params.typeOf }, // 終了日時を一定期間過ぎたもの endDate: { $exists: true, $gte: params.endDate.$gte, $lt: params.endDate.$lt } }) .exec(); }); } getCursor(conditions, projection) { return this.transactionModel.find(conditions, projection) .sort({ startDate: factory.sortType.Descending }) .cursor(); } aggregatePlaceOrder(params) { return __awaiter(this, void 0, void 0, function* () { const statuses = yield Promise.all([ factory.transactionStatusType.Confirmed, factory.transactionStatusType.Canceled, factory.transactionStatusType.Expired ].map((transactionStatus) => __awaiter(this, void 0, void 0, function* () { var _a, _b; const matchConditions = Object.assign(Object.assign({ startDate: { $gte: params.startFrom, $lte: params.startThrough }, typeOf: { $eq: params.typeOf }, status: { $eq: transactionStatus } }, (typeof ((_b = (_a = params.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$ne) === 'string') ? { 'project.id': { $ne: params.project.id.$ne } } : undefined), (typeof params.clientId === 'string') // customerIdentifierAll.push(); ? { 'agent.identifier': { $exists: true, $all: [{ name: 'clientId', value: params.clientId }] } } : undefined); return this.agggregateByStatus({ matchConditions, status: transactionStatus }); }))); return { statuses }; }); } // tslint:disable-next-line:max-func-body-length agggregateByStatus(params) { return __awaiter(this, void 0, void 0, function* () { const matchConditions = params.matchConditions; const transactionStatus = params.status; const aggregations = yield this.transactionModel.aggregate([ { $match: matchConditions }, { $project: { duration: { $subtract: ['$endDate', '$startDate'] }, status: '$status', startDate: '$startDate', endDate: '$endDate', typeOf: '$typeOf' } }, { $group: { _id: '$typeOf', transactionCount: { $sum: 1 }, maxDuration: { $max: '$duration' }, minDuration: { $min: '$duration' }, avgDuration: { $avg: '$duration' } } }, { $project: { _id: 0, transactionCount: '$transactionCount', avgDuration: '$avgDuration', maxDuration: '$maxDuration', minDuration: '$minDuration' } } ]) .exec(); // tslint:disable-next-line:no-magic-numbers const percents = [50, 95, 99]; if (aggregations.length === 0) { return { status: transactionStatus, aggregation: { transactionCount: 0, avgDuration: 0, maxDuration: 0, minDuration: 0, percentilesDuration: percents.map((percent) => { return { name: String(percent), value: 0 }; }) // totalPrice: 0, // maxPrice: 0, // minPrice: 0, // avgPrice: 0 } }; } const ranks4percentile = percents.map((percentile) => { return { percentile, // tslint:disable-next-line:no-magic-numbers