UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

1,045 lines 56 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.AssetTransactionRepo = void 0; const moment = require("moment"); const assetTransaction_1 = require("./mongoose/schemas/assetTransaction"); const factory = require("../factory"); const assetTransaction_2 = require("../eventEmitter/assetTransaction"); const settings_1 = require("../settings"); /** * 資産取引リポジトリ */ class AssetTransactionRepo { constructor(connection) { this.transactionModel = connection.model(assetTransaction_1.modelName, (0, assetTransaction_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; const andConditions = [ { typeOf: { $eq: params.typeOf } } ]; 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 (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) { if (Array.isArray(params.agent.ids)) { andConditions.push({ 'agent.id': { $in: params.agent.ids } }); } } const transactionNumberEq = (_d = params.transactionNumber) === null || _d === void 0 ? void 0 : _d.$eq; if (typeof transactionNumberEq === 'string') { andConditions.push({ transactionNumber: { $eq: transactionNumberEq } }); } const transactionNumberIn = (_e = params.transactionNumber) === null || _e === void 0 ? void 0 : _e.$in; if (Array.isArray(transactionNumberIn)) { andConditions.push({ transactionNumber: { $in: transactionNumberIn } }); } const tasksExportActionStatusIn = (_g = (_f = params.tasksExportAction) === null || _f === void 0 ? void 0 : _f.actionStatus) === null || _g === void 0 ? void 0 : _g.$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 = (_j = (_h = params.tasksExportAction) === null || _h === void 0 ? void 0 : _h.actionStatus) === null || _j === void 0 ? void 0 : _j.$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.assetTransactionType.Pay: const objectAccountIdEq = (_l = (_k = params.object) === null || _k === void 0 ? void 0 : _k.accountId) === null || _l === void 0 ? void 0 : _l.$eq; if (typeof objectAccountIdEq === 'string') { andConditions.push({ 'object.accountId': { $exists: true, $eq: objectAccountIdEq } }); } const objectTypeOfEq = (_o = (_m = params.object) === null || _m === void 0 ? void 0 : _m.typeOf) === null || _o === void 0 ? void 0 : _o.$eq; if (typeof objectTypeOfEq === 'string') { andConditions.push({ 'object.typeOf': { $exists: true, $eq: objectTypeOfEq } }); } break; case factory.assetTransactionType.Refund: const objectAccountIdEq4refund = (_q = (_p = params.object) === null || _p === void 0 ? void 0 : _p.accountId) === null || _q === void 0 ? void 0 : _q.$eq; if (typeof objectAccountIdEq4refund === 'string') { andConditions.push({ 'object.accountId': { $exists: true, $eq: objectAccountIdEq4refund } }); } const objectPaymentMethodIdEq4refund = (_s = (_r = params.object) === null || _r === void 0 ? void 0 : _r.paymentMethodId) === null || _s === void 0 ? void 0 : _s.$eq; if (typeof objectPaymentMethodIdEq4refund === 'string') { andConditions.push({ 'object.paymentMethodId': { $exists: true, $eq: objectPaymentMethodIdEq4refund } }); } const objectPaymentMethodIdIn4refund = (_u = (_t = params.object) === null || _t === void 0 ? void 0 : _t.paymentMethodId) === null || _u === void 0 ? void 0 : _u.$in; if (Array.isArray(objectPaymentMethodIdIn4refund)) { andConditions.push({ 'object.paymentMethodId': { $exists: true, $in: objectPaymentMethodIdIn4refund } }); } break; case factory.assetTransactionType.MoneyTransfer: const fromLocationIdentifierEq = (_x = (_w = (_v = params.object) === null || _v === void 0 ? void 0 : _v.fromLocation) === null || _w === void 0 ? void 0 : _w.identifier) === null || _x === void 0 ? void 0 : _x.$eq; if (typeof fromLocationIdentifierEq === 'string') { andConditions.push({ 'object.fromLocation.identifier': { $exists: true, $eq: fromLocationIdentifierEq } }); } const toLocationIdentifierEq = (_0 = (_z = (_y = params.object) === null || _y === void 0 ? void 0 : _y.toLocation) === null || _z === void 0 ? void 0 : _z.identifier) === null || _0 === void 0 ? void 0 : _0.$eq; if (typeof toLocationIdentifierEq === 'string') { andConditions.push({ 'object.toLocation.identifier': { $exists: true, $eq: toLocationIdentifierEq } }); } const pendingTransactionIdentifierEq = (_3 = (_2 = (_1 = params.object) === null || _1 === void 0 ? void 0 : _1.pendingTransaction) === null || _2 === void 0 ? void 0 : _2.identifier) === null || _3 === void 0 ? void 0 : _3.$eq; if (typeof pendingTransactionIdentifierEq === 'string') { andConditions.push({ 'object.pendingTransaction.identifier': { $exists: true, $eq: pendingTransactionIdentifierEq } }); } break; case factory.assetTransactionType.CancelReservation: const objectReservationNumberIn4cancelReservation = (_5 = (_4 = params.object) === null || _4 === void 0 ? void 0 : _4.reservationNumber) === null || _5 === void 0 ? void 0 : _5.$in; if (Array.isArray(objectReservationNumberIn4cancelReservation)) { andConditions.push({ 'object.reservationNumber': { $exists: true, $in: objectReservationNumberIn4cancelReservation } }); } const objectReservationNumberEq4cancelReservation = (_7 = (_6 = params.object) === null || _6 === void 0 ? void 0 : _6.reservationNumber) === null || _7 === void 0 ? void 0 : _7.$eq; if (typeof objectReservationNumberEq4cancelReservation === 'string') { andConditions.push({ 'object.reservationNumber': { $exists: true, $eq: objectReservationNumberEq4cancelReservation } }); } break; case factory.assetTransactionType.Reserve: const objectProviderIdEq = (_10 = (_9 = (_8 = params.object) === null || _8 === void 0 ? void 0 : _8.provider) === null || _9 === void 0 ? void 0 : _9.id) === null || _10 === void 0 ? void 0 : _10.$eq; if (typeof objectProviderIdEq === 'string') { andConditions.push({ 'object.provider.id': { $exists: true, $eq: objectProviderIdEq } }); } 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 objectReservationNumberIn = (_15 = (_14 = params.object) === null || _14 === void 0 ? void 0 : _14.reservationNumber) === null || _15 === void 0 ? void 0 : _15.$in; if (Array.isArray(objectReservationNumberIn)) { andConditions.push({ 'object.reservationNumber': { $exists: true, $in: objectReservationNumberIn } }); } const objectReservationNumberEq = (_17 = (_16 = params.object) === null || _16 === void 0 ? void 0 : _16.reservationNumber) === null || _17 === void 0 ? void 0 : _17.$eq; if (typeof objectReservationNumberEq === 'string') { andConditions.push({ 'object.reservationNumber': { $exists: true, $eq: objectReservationNumberEq } }); } const objectSubReservationIdIn = (_20 = (_19 = (_18 = params.object) === null || _18 === void 0 ? void 0 : _18.reservations) === null || _19 === void 0 ? void 0 : _19.id) === null || _20 === void 0 ? void 0 : _20.$in; if (Array.isArray(objectSubReservationIdIn)) { andConditions.push({ 'object.subReservation.id': { $exists: true, $in: objectSubReservationIdIn } }); } if (params.object !== undefined) { if (params.object.reservations !== undefined) { if (params.object.reservations.reservationNumber !== undefined) { if (Array.isArray(params.object.reservations.reservationNumber.$in)) { andConditions.push({ 'object.subReservation.reservationNumber': { $exists: true, $in: params.object.reservations.reservationNumber.$in } }); } } if (params.object.reservations.reservationFor !== undefined) { if (params.object.reservations.reservationFor.id !== undefined) { if (Array.isArray(params.object.reservations.reservationFor.id.$in)) { andConditions.push({ 'object.subReservation.reservationFor.id': { $exists: true, $in: params.object.reservations.reservationFor.id.$in } }); } } } } } const objectUnderNameIdEq = (_23 = (_22 = (_21 = params.object) === null || _21 === void 0 ? void 0 : _21.underName) === null || _22 === void 0 ? void 0 : _22.id) === null || _23 === void 0 ? void 0 : _23.$eq; if (typeof objectUnderNameIdEq === 'string') { andConditions.push({ 'object.underName.id': { $exists: true, $eq: objectUnderNameIdEq } }); } const objectSubReservationSeatNumberEq = (_28 = (_27 = (_26 = (_25 = (_24 = params.object) === null || _24 === void 0 ? void 0 : _24.reservations) === null || _25 === void 0 ? void 0 : _25.reservedTicket) === null || _26 === void 0 ? void 0 : _26.ticketedSeat) === null || _27 === void 0 ? void 0 : _27.seatNumber) === null || _28 === void 0 ? void 0 : _28.$eq; if (typeof objectSubReservationSeatNumberEq === 'string') { andConditions.push({ 'object.subReservation.reservedTicket.ticketedSeat.seatNumber': { $exists: true, $eq: objectSubReservationSeatNumberEq } }); } break; case factory.assetTransactionType.RegisterService: const objectItemOfferedServiceOutputIdentifierEq = (_32 = (_31 = (_30 = (_29 = params.object) === null || _29 === void 0 ? void 0 : _29.itemOffered) === null || _30 === void 0 ? void 0 : _30.serviceOutput) === null || _31 === void 0 ? void 0 : _31.identifier) === null || _32 === void 0 ? void 0 : _32.$eq; if (typeof objectItemOfferedServiceOutputIdentifierEq === 'string') { andConditions.push({ 'object.itemOffered.serviceOutput.identifier': { $exists: true, $eq: objectItemOfferedServiceOutputIdentifierEq } }); } const objectItemOfferedServiceOutputIdentifierIn = (_36 = (_35 = (_34 = (_33 = params.object) === null || _33 === void 0 ? void 0 : _33.itemOffered) === null || _34 === void 0 ? void 0 : _34.serviceOutput) === null || _35 === void 0 ? void 0 : _35.identifier) === null || _36 === void 0 ? void 0 : _36.$in; if (Array.isArray(objectItemOfferedServiceOutputIdentifierIn)) { andConditions.push({ 'object.itemOffered.serviceOutput.identifier': { $exists: true, $in: objectItemOfferedServiceOutputIdentifierIn } }); } break; default: } return andConditions; } /** * 取引を開始する */ start(params // ): Promise<Pick<factory.assetTransaction.ITransaction<T>, 'id'>> { ) { return __awaiter(this, void 0, void 0, function* () { return this.transactionModel.create(Object.assign(Object.assign({}, params), { typeOf: params.typeOf, status: factory.transactionStatusType.InProgress, startDate: new Date(), endDate: undefined, tasksExportAction: { actionStatus: factory.actionStatusType.PotentialActionStatus } })) .then((doc) => doc.toObject()); }); } startWithMinimalResponse(params) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const { typeOf } = params; const startDate = new Date(); const creatingTransaction = Object.assign(Object.assign({}, params), { typeOf, status: factory.transactionStatusType.InProgress, startDate, // endDate: undefined, tasksExportAction: { actionStatus: factory.actionStatusType.PotentialActionStatus } }); const result = yield this.transactionModel.insertMany(creatingTransaction, { ordered: false, 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('assetTransaction not saved'); } return { id, startDate }; }); } findById(params, inclusion) { return __awaiter(this, void 0, void 0, function* () { let projection = {}; if (Array.isArray(inclusion) && inclusion.length > 0) { inclusion.forEach((field) => { projection[field] = 1; }); } else { projection = {}; } const doc = yield this.transactionModel.findOne({ _id: { $eq: params.id }, typeOf: { $eq: params.typeOf } }, projection) .exec(); if (doc === null) { throw new factory.errors.NotFound(this.transactionModel.modelName); } return doc.toObject(); }); } findByTransactionNumber(params, inclusion) { return __awaiter(this, void 0, void 0, function* () { let projection = {}; if (Array.isArray(inclusion) && inclusion.length > 0) { inclusion.forEach((field) => { projection[field] = 1; }); } else { projection = {}; } const doc = yield this.transactionModel.findOne({ transactionNumber: { $eq: params.transactionNumber }, typeOf: { $eq: params.typeOf } }, projection) .exec(); if (doc === null) { throw new factory.errors.NotFound(this.transactionModel.modelName); } return doc.toObject(); }); } addReservations(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: { 'object.acceptedOffer': params.object.acceptedOffer, // 'object.issuedThrough': params.object.issuedThrough, // start時保管へ移行(2024-07-01~) // 'object.reservationFor': params.object.reservationFor, // start時保管へ移行(2024-07-01~) 'object.subReservation': params.object.subReservation } }, { new: false, projection: { _id: 1 } }) .exec(); if (doc === null) { throw new factory.errors.NotFound(this.transactionModel.modelName); } // return doc.toObject(); }); } /** * 取引を確定する */ confirm(params) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; const doc = yield this.transactionModel.findOneAndUpdate({ _id: { $eq: params.id }, typeOf: { $eq: params.typeOf }, status: { $eq: factory.transactionStatusType.InProgress } }, Object.assign(Object.assign({ status: factory.transactionStatusType.Confirmed, endDate: new Date(), result: params.result, potentialActions: params.potentialActions }, (typeof ((_b = (_a = params.object) === null || _a === void 0 ? void 0 : _a.underName) === null || _b === void 0 ? void 0 : _b.typeOf) === 'string') ? { 'object.underName': params.object.underName } : undefined), (typeof ((_d = (_c = params.object) === null || _c === void 0 ? void 0 : _c.paymentMethod) === null || _d === void 0 ? void 0 : _d.identifier) === 'string') ? { 'object.paymentMethod.identifier': params.object.paymentMethod.identifier } : undefined), { new: true, projection: { _id: 1 } }) .exec(); // NotFoundであれば取引状態確認 if (doc === null) { const transaction = yield this.findById({ typeOf: params.typeOf, id: params.id }, ['status']); if (transaction.status === factory.transactionStatusType.Confirmed) { // すでに確定済の場合スルー } else if (transaction.status === factory.transactionStatusType.Expired) { throw new factory.errors.Argument('Transaction id', `Transaction ${params.typeOf} ${params.id} already expired`); } else if (transaction.status === factory.transactionStatusType.Canceled) { throw new factory.errors.Argument('Transaction id', `Transaction ${params.typeOf} ${params.id} already canceled`); } else { throw new factory.errors.NotFound(this.transactionModel.modelName); } } assetTransaction_2.assetTransactionEventEmitter.emitAssetTransactionStatusChanged({ id: params.id, typeOf: params.typeOf, status: factory.transactionStatusType.Confirmed }); }); } /** * 取引を開始&確定 */ startAndConfirm(params) { return __awaiter(this, void 0, void 0, function* () { const startDate = new Date(); yield this.transactionModel.create(Object.assign(Object.assign({}, params), { _id: params.id, typeOf: params.typeOf, startDate, status: factory.transactionStatusType.Confirmed, tasksExportAction: { actionStatus: factory.actionStatusType.PotentialActionStatus }, // tasksExportationStatus: factory.transactionTasksExportationStatus.Unexported, // discontinue(2024-06-20~) endDate: startDate, result: params.result, potentialActions: params.potentialActions })) .then((doc) => doc.toObject()); assetTransaction_2.assetTransactionEventEmitter.emitAssetTransactionStatusChanged({ id: params.id, typeOf: params.typeOf, status: factory.transactionStatusType.Confirmed }); return { id: params.id }; }); } 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; 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 typeOfIn = params.typeOf?.$in; 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') { query.sort(params.sort); } return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .lean() .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.assetTransaction.ITransaction<factory.assetTransactionType>, '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(); // assetTransactionEventEmitter.emitAssetTransactionStatusChanged({ // 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) => { assetTransaction_2.assetTransactionEventEmitter.emitAssetTransactionStatusChanged({ 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-13~) // 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-17~) */ 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-12~) */ 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 { assetTransaction_2.assetTransactionEventEmitter.emitAssetTransactionStatusChanged({ id: doc.id, typeOf: doc.typeOf, status: factory.transactionStatusType.Expired }); return doc; } }); } /** * 取引を期限切れにする */ makeExpired(params) { return __awaiter(this, void 0, void 0, function* () { // IDをemitしたいのでまずリスト検索(2023-04-27~) const expiringTransactions = yield this.transactionModel.find({ status: { $eq: factory.transactionStatusType.InProgress }, expires: { $lt: params.expires.$lt } }) .select({ _id: 1, typeOf: 1 }) .setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .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) => { assetTransaction_2.assetTransactionEventEmitter.emitAssetTransactionStatusChanged({ id: expiringTransaction.id, typeOf: expiringTransaction.typeOf, status: factory.transactionStatusType.Expired }); }); } }); } /** * 取引を中止する */ cancel(params) { return __awaiter(this, void 0, void 0, function* () { // 進行中ステータスの取引を中止する const doc = yield this.transactionModel.findOneAndUpdate(Object.assign(Object.assign(Object.assign({ typeOf: { $eq: params.typeOf } }, (typeof params.id === 'string') ? { _id: { $eq: params.id } } : undefined), (typeof params.transactionNumber === 'string') ? { transactionNumber: { $eq: params.transactionNumber } } : undefined), { status: { $eq: factory.transactionStatusType.InProgress } }), { $set: { status: factory.transactionStatusType.Canceled, endDate: new Date() } }, { new: true, projection: { _id: 1 } }) .exec(); let result; // NotFoundであれば取引状態確認 if (doc === null) { let transaction; if (typeof params.id === 'string') { transaction = yield this.findById({ typeOf: params.typeOf, id: params.id }, ['status']); } else if (typeof params.transactionNumber === 'string') { transaction = yield this.findByTransactionNumber({ typeOf: params.typeOf, transactionNumber: params.transactionNumber }, ['status']); } else { throw new factory.errors.ArgumentNull('Transaction ID or Transaction Number'); } if (transaction.status === factory.transactionStatusType.Canceled) { // すでに中止済の場合スルー } else if (transaction.status === factory.transactionStatusType.Expired) { throw new factory.errors.Argument('Transaction id', `Transaction ${params.typeOf} ${transaction.id} already expired`); } else if (transaction.status === factory.transactionStatusType.Confirmed) { throw new factory.errors.Argument('Transaction id', `Confirmed transaction ${params.typeOf} ${transaction.id} unable to cancel`); } else { throw new factory.errors.NotFound(this.transactionModel.modelName); } result = { id: transaction.id }; } else { result = { id: doc.toObject().id }; } assetTransaction_2.assetTransactionEventEmitter.emitAssetTransactionStatusChanged({ id: result.id, typeOf: params.typeOf, status: factory.transactionStatusType.Canceled }); return result; }); } count(params) { return __awaiter(this, void 0, void 0, function* () { const { limit } = params; const conditions = AssetTransactionRepo.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 }; }); } /** * 取引を検索する */ search(params, inclusion, exclusion) { return __awaiter(this, void 0, void 0, function* () { var _a; const conditions = AssetTransactionRepo.CREATE_MONGO_CONDITIONS(params); let projection = {}; if (Array.isArray(inclusion) && inclusion.length > 0) { inclusion.forEach((field) => { projection[field] = 1; }); } else { projection = { __v: 0, createdAt: 0, updatedAt: 0 }; if (Array.isArray(exclusion) && exclusion.length > 0) { exclusion.forEach((field) => { projection[field] = 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 }); } return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec() .then((docs) => docs.map((doc) => doc.toObject())); }); } /** * 取引番号指定で削除する */ deleteByTransactionNumber(params) { return __awaiter(this, void 0, void 0, function* () { return this.transactionModel.deleteMany({ 'project.id': { $eq: params.project.id }, transactionNumber: { $in: params.transactionNumbers }, typeOf: { $eq: params.typeOf } }) .exec(); }); } /** * プロジェクト指定で削除する */ deleteByProject(params) { return __awaiter(this, void 0, void 0, function* () { yield this.transactionModel.deleteMany({ 'project.id': { $eq: params.project.id } }) .exec(); }); } /** * 終了日時を一定期間過ぎたアクションを削除する */ deleteEndDatePassedCertainPeriod(params) { return __awaiter(this, void 0, void 0, function* () { yield this.transactionModel.deleteMany({ // 終了日時を一定期間過ぎたもの endDate: { $exists: true, $lt: params.$lt } }) .exec(); }); } /** * 特定の取引を更新する(汎用) */ findByIdAndUpdateInProgress(params) { return __awaiter(this, void 0, void 0, function* () { return this.transactionModel.findOneAndUpdate({ _id: { $eq: params.id }, status: { $eq: factory.transactionStatusType.InProgress } }, params.update, { new: true }) .exec() .then((doc) => { if (doc === null) { throw new factory.errors.ArgumentNull(this.transactionModel.modelName); } return doc.toObject(); }); }); } /** * 互換性維持対応 */ migrateObjectReservationNumber(params) { return __awaiter(this, void 0, void 0, function* () { return this.transactionModel.findOneAndUpdate({ typeOf: { $eq: factory.assetTransactionType.CancelReservation }, _id: { $eq: params.id } }, { $set: { 'object.reservationNumber': params.object.reservationNumber, 'object.typeOf': factory.reservationType.ReservationPackage } }, { timestamps: false, new: true, projection: { _id: 1 } }) .exec() .then((doc) => { if (doc === null) { throw new factory.errors.NotFound(this.transactionModel.modelName); } return doc.toObject(); }); }); } /** * 互換性維持対応専用 */ fixReservedTicketIdentifier(params) { return __awaiter(this, void 0, void 0, function* () { const doc = yield this.transactionModel.findOneAndUpdate({ transactionNumber: { $eq: params.transactionNumber }, 'project.id': { $eq: params.project.id }, typeOf: { $eq: factory.assetTransactionType.Pay } }, { $set: { 'object.paymentMethod.movieTickets': params.object.paymentMethod.movieTickets } }, { timestamps: false, new: true, projection: { _id: 1 } }) .exec(); if (doc === null) { throw new factory.errors.NotFound(this.transactionModel.modelName); } }); } findByIdAndDelete(params) { return __awaiter(this, void 0, void 0, function* () { yield this.transactionModel.findByIdAndDelete(params.id) .exec(); }); } getCursor(conditions, projection) { return this.transactionModel.find(conditions, projection) // .sort({ startDate: factory.sortType.Ascending }) .sort({ startDate: factory.sortType.Descending }) .cursor(); } unsetUnnecessaryFields(params) { return __awaiter(this, void 0, void 0, function* () { return this.transactionModel.updateMany(params.filter, { $unset: params.$unset }, { timestamps: false }) .exec(); }); } aggregateAssetTransaction(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, _c, _d; 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 ((_d = (_c = params.project) === null || _c === void 0 ? void 0 : _c.id) === null || _d === void 0 ? void 0 : _d.$eq) === 'string') ? { 'project.id': { $eq: params.project.id.$eq } } : undefined); return this.agggregateByStatus({ matchConditions, status: transactionStatus }); }))); return { statuses }; }); } // tslint:disable-next-line:max-func-body-length a