Source: repo/action.js

"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 factory = require("../factory");
const action_1 = require("./mongoose/model/action");
/**
 * アクションリポジトリー
 */
class MongoRepository {
    constructor(connection) {
        this.actionModel = connection.model(action_1.default.modelName);
    }
    /**
     * アクション開始
     */
    start(attributes) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.actionModel.create(Object.assign({}, attributes, { actionStatus: factory.actionStatusType.ActiveActionStatus, startDate: new Date() })).then((doc) => doc.toObject());
        });
    }
    /**
     * アクション完了
     */
    complete(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.actionModel.findOneAndUpdate({
                typeOf: params.typeOf,
                _id: params.id
            }, {
                actionStatus: factory.actionStatusType.CompletedActionStatus,
                result: params.result,
                endDate: new Date()
            }, { new: true }).select({ __v: 0, createdAt: 0, updatedAt: 0 }).exec();
            if (doc === null) {
                throw new factory.errors.NotFound(this.actionModel.modelName);
            }
            return doc.toObject();
        });
    }
    /**
     * アクション取消
     */
    cancel(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.actionModel.findOneAndUpdate({
                typeOf: params.typeOf,
                _id: params.id
            }, { actionStatus: factory.actionStatusType.CanceledActionStatus }, { new: true }).select({ __v: 0, createdAt: 0, updatedAt: 0 }).exec();
            if (doc === null) {
                throw new factory.errors.NotFound(this.actionModel.modelName);
            }
            return doc.toObject();
        });
    }
    /**
     * アクション失敗
     */
    giveUp(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.actionModel.findOneAndUpdate({
                typeOf: params.typeOf,
                _id: params.id
            }, {
                actionStatus: factory.actionStatusType.FailedActionStatus,
                error: params.error,
                endDate: new Date()
            }, { new: true }).select({ __v: 0, createdAt: 0, updatedAt: 0 }).exec();
            if (doc === null) {
                throw new factory.errors.NotFound(this.actionModel.modelName);
            }
            return doc.toObject();
        });
    }
    /**
     * IDで取得する
     */
    findById(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const doc = yield this.actionModel.findOne({
                typeOf: params.typeOf,
                _id: params.id
            }).select({ __v: 0, createdAt: 0, updatedAt: 0 }).exec();
            if (doc === null) {
                throw new factory.errors.NotFound(this.actionModel.modelName);
            }
            return doc.toObject();
        });
    }
}
exports.MongoRepository = MongoRepository;