@chevre/domain
Version:
Chevre Domain Library for Node.js
502 lines (439 loc) • 18.5 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Chevre Domain Library for Node.js Source: repo/transaction.js</title>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
<link type="text/css" rel="stylesheet" href="styles/site.cosmo.css">
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top navbar-inverse">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="index.html">Chevre Domain Library for Node.js</a>
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#topNavigation">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse" id="topNavigation">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a>
<ul class="dropdown-menu ">
<li><a href="MongoRepository.html">MongoRepository</a></li><li><a href="RedisRepository.html">RedisRepository</a></li><li><a href="Repository.html">Repository</a></li>
</ul>
</li>
<li class="dropdown">
<a href="global.html" class="dropdown-toggle" data-toggle="dropdown">Global<b class="caret"></b></a>
<ul class="dropdown-menu ">
<li><a href="global.html#abort">abort</a></li><li><a href="global.html#accountTitle_1">accountTitle_1</a></li><li><a href="global.html#aggregateScreeningEvent">aggregateScreeningEvent</a></li><li><a href="global.html#AggregationService">AggregationService</a></li><li><a href="global.html#call">call</a></li><li><a href="global.html#cancel">cancel</a></li><li><a href="global.html#cancelPendingReservation">cancelPendingReservation</a></li><li><a href="global.html#cancelReservation">cancelReservation</a></li><li><a href="global.html#confirm">confirm</a></li><li><a href="global.html#confirmReservation">confirmReservation</a></li><li><a href="global.html#createDebug">createDebug</a></li><li><a href="global.html#default">default</a></li><li><a href="global.html#execute">execute</a></li><li><a href="global.html#executeByName">executeByName</a></li><li><a href="global.html#exportTasks">exportTasks</a></li><li><a href="global.html#exportTasksById">exportTasksById</a></li><li><a href="global.html#mongoose">mongoose</a></li><li><a href="global.html#redis">redis</a></li><li><a href="global.html#retry">retry</a></li><li><a href="global.html#schema">schema</a></li><li><a href="global.html#searchScreeningEventTicketOffers">searchScreeningEventTicketOffers</a></li><li><a href="global.html#sortOrder4executionOfTasks">sortOrder4executionOfTasks</a></li><li><a href="global.html#start">start</a></li>
</ul>
</li>
</ul>
<div class="col-sm-3 col-md-3">
<form class="navbar-form" role="search">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="q" id="search-input">
<div class="input-group-btn">
<button class="btn btn-default" id="search-submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="container" id="toc-content">
<div class="row">
<div class="col-md-12">
<div id="main">
<h1 class="page-title">Source: repo/transaction.js</h1>
<section>
<article>
<pre
class="sunlight-highlight-javascript linenums">"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const moment = require("moment");
const factory = require("../factory");
const transaction_1 = require("./mongoose/model/transaction");
/**
* 取引リポジトリー
*/
class MongoRepository {
constructor(connection) {
this.transactionModel = connection.model(transaction_1.default.modelName);
}
// tslint:disable-next-line:cyclomatic-complexity max-func-body-length
static CREATE_MONGO_CONDITIONS(params) {
const andConditions = [
{
typeOf: params.typeOf
}
];
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.startFrom !== undefined) {
andConditions.push({
startDate: { $gt: params.startFrom }
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.startThrough !== undefined) {
andConditions.push({
startDate: { $lt: 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 */
if (params.agent !== undefined) {
if (Array.isArray(params.agent.ids)) {
andConditions.push({
'agent.id': { $in: params.agent.ids }
});
}
}
switch (params.typeOf) {
case factory.transactionType.CancelReservation:
break;
case factory.transactionType.Reserve:
break;
default:
}
return andConditions;
}
/**
* 取引を開始する
*/
start(params) {
return __awaiter(this, void 0, void 0, function* () {
return this.transactionModel.create(Object.assign({ typeOf: params.typeOf }, params, { status: factory.transactionStatusType.InProgress, startDate: new Date(), endDate: undefined, tasksExportationStatus: factory.transactionTasksExportationStatus.Unexported })).then((doc) => doc.toObject());
});
}
/**
* IDで取引を取得する
*/
findById(params) {
return __awaiter(this, void 0, void 0, function* () {
const doc = yield this.transactionModel.findOne({
_id: params.id,
typeOf: params.typeOf
}).exec();
if (doc === null) {
throw new factory.errors.NotFound('Transaction');
}
return doc.toObject();
});
}
/**
* 進行中の取引を取得する
*/
findInProgressById(params) {
return __awaiter(this, void 0, void 0, function* () {
const doc = yield this.transactionModel.findOne({
_id: params.id,
typeOf: params.typeOf,
status: factory.transactionStatusType.InProgress
}).exec();
if (doc === null) {
throw new factory.errors.NotFound('Transaction');
}
return doc.toObject();
});
}
/**
* 取引を確定する
*/
confirm(params) {
return __awaiter(this, void 0, void 0, function* () {
const doc = yield this.transactionModel.findOneAndUpdate({
_id: params.id,
typeOf: params.typeOf,
status: factory.transactionStatusType.InProgress
}, {
status: factory.transactionStatusType.Confirmed,
endDate: new Date(),
result: params.result,
potentialActions: params.potentialActions // resultを更新
}, { new: true }).exec();
// NotFoundであれば取引状態確認
if (doc === null) {
const transaction = yield this.findById({ typeOf: params.typeOf, id: params.id });
if (transaction.status === factory.transactionStatusType.Confirmed) {
// すでに確定済の場合
return transaction;
}
else if (transaction.status === factory.transactionStatusType.Expired) {
throw new factory.errors.Argument('Transaction id', 'Transaction already expired');
}
else if (transaction.status === factory.transactionStatusType.Canceled) {
throw new factory.errors.Argument('Transaction id', 'Transaction already canceled');
}
else {
throw new factory.errors.NotFound(this.transactionModel.modelName);
}
}
return doc.toObject();
});
}
/**
* タスク未エクスポートの取引をひとつ取得してエクスポートを開始する
*/
startExportTasks(params) {
return __awaiter(this, void 0, void 0, function* () {
return this.transactionModel.findOneAndUpdate({
typeOf: params.typeOf,
status: params.status,
tasksExportationStatus: factory.transactionTasksExportationStatus.Unexported
}, { tasksExportationStatus: factory.transactionTasksExportationStatus.Exporting }, { new: true }).exec().then((doc) => (doc === null) ? null : doc.toObject());
});
}
/**
* タスクエクスポートリトライ
* todo updatedAtを基準にしているが、タスクエクスポートトライ日時を持たせた方が安全か?
*/
reexportTasks(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.transactionModel.findOneAndUpdate({
tasksExportationStatus: factory.transactionTasksExportationStatus.Exporting,
updatedAt: { $lt: moment().add(-params.intervalInMinutes, 'minutes').toISOString() }
}, {
tasksExportationStatus: factory.transactionTasksExportationStatus.Unexported
}).exec();
});
}
/**
* set task status exported by transaction id
* IDでタスクをエクスポート済に変更する
*/
setTasksExportedById(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.transactionModel.findByIdAndUpdate(params.id, {
tasksExportationStatus: factory.transactionTasksExportationStatus.Exported,
tasksExportedAt: moment().toDate()
}).exec();
});
}
/**
* 取引を期限切れにする
*/
makeExpired() {
return __awaiter(this, void 0, void 0, function* () {
const endDate = moment().toDate();
// ステータスと期限を見て更新
yield this.transactionModel.update({
status: factory.transactionStatusType.InProgress,
expires: { $lt: endDate }
}, {
status: factory.transactionStatusType.Expired,
endDate: endDate
}, { multi: true }).exec();
});
}
/**
* 取引を中止する
*/
cancel(params) {
return __awaiter(this, void 0, void 0, function* () {
const endDate = moment().toDate();
// 進行中ステータスの取引を中止する
const doc = yield this.transactionModel.findOneAndUpdate({
typeOf: params.typeOf,
_id: params.id,
status: factory.transactionStatusType.InProgress
}, {
status: factory.transactionStatusType.Canceled,
endDate: endDate
}, { new: true }).exec();
// NotFoundであれば取引状態確認
if (doc === null) {
const transaction = yield this.findById(params);
if (transaction.status === factory.transactionStatusType.Canceled) {
// すでに中止済の場合
return transaction;
}
else if (transaction.status === factory.transactionStatusType.Expired) {
throw new factory.errors.Argument('Transaction id', 'Transaction already expired');
}
else if (transaction.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);
}
}
return doc.toObject();
});
}
count(params) {
return __awaiter(this, void 0, void 0, function* () {
const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
return this.transactionModel.countDocuments({ $and: conditions }).setOptions({ maxTimeMS: 10000 })
.exec();
});
}
/**
* 取引を検索する
*/
search(params) {
return __awaiter(this, void 0, void 0, function* () {
const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
const query = this.transactionModel.find({ $and: conditions }, {
__v: 0,
createdAt: 0,
updatedAt: 0
});
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.limit !== undefined && params.page !== undefined) {
query.limit(params.limit).skip(params.limit * (params.page - 1));
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.sort !== undefined) {
query.sort(params.sort);
}
return query.setOptions({ maxTimeMS: 10000 }).exec().then((docs) => docs.map((doc) => doc.toObject()));
});
}
}
exports.MongoRepository = MongoRepository;
</pre>
</article>
</section>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="modal fade" id="searchResults">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Search results</h4>
</div>
<div class="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>
<footer>
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a>
on Sun Dec 16th 2018
using the <a href="https://github.com/docstrap/docstrap">DocStrap template</a>.
</span>
</footer>
<script src="scripts/docstrap.lib.js"></script>
<script src="scripts/toc.js"></script>
<script type="text/javascript" src="scripts/fulltext-search-ui.js"></script>
<script>
$( function () {
$( "[id*='$']" ).each( function () {
var $this = $( this );
$this.attr( "id", $this.attr( "id" ).replace( "$", "__" ) );
} );
$( ".tutorial-section pre, .readme-section pre, pre.prettyprint.source" ).each( function () {
var $this = $( this );
var example = $this.find( "code" );
exampleText = example.html();
var lang = /{@lang (.*?)}/.exec( exampleText );
if ( lang && lang[1] ) {
exampleText = exampleText.replace( lang[0], "" );
example.html( exampleText );
lang = lang[1];
} else {
var langClassMatch = example.parent()[0].className.match(/lang\-(\S+)/);
lang = langClassMatch ? langClassMatch[1] : "javascript";
}
if ( lang ) {
$this
.addClass( "sunlight-highlight-" + lang )
.addClass( "linenums" )
.html( example.html() );
}
} );
Sunlight.highlightAll( {
lineNumbers : true,
showMenu : true,
enableDoclinks : true
} );
$.catchAnchorLinks( {
navbarOffset: 10
} );
$( "#toc" ).toc( {
anchorName : function ( i, heading, prefix ) {
return $( heading ).attr( "id" ) || ( prefix + i );
},
selectors : "#toc-content h1,#toc-content h2,#toc-content h3,#toc-content h4",
showAndHide : false,
smoothScrolling: true
} );
$( "#main span[id^='toc']" ).addClass( "toc-shim" );
$( '.dropdown-toggle' ).dropdown();
$( "table" ).each( function () {
var $this = $( this );
$this.addClass('table');
} );
} );
</script>
<!--Navigation and Symbol Display-->
<!--Google Analytics-->
<script type="text/javascript">
$(document).ready(function() {
SearcherDisplay.init();
});
</script>
</body>
</html>