@chevre/domain
Version:
Chevre Domain Library for Node.js
136 lines (135 loc) • 7.04 kB
JavaScript
"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.call = call;
const COA = require("@motionpicture/coa-service");
const factory = require("../../factory");
const action_1 = require("../../repo/action");
const credentials_1 = require("../../repo/credentials");
const event_1 = require("../../repo/event");
const orderNumber_1 = require("../../repo/orderNumber");
const project_1 = require("../../repo/project");
const reserveInterface_1 = require("../../repo/reserveInterface");
const transaction_1 = require("../../repo/transaction");
const transactionProcess_1 = require("../../repo/transactionProcess");
const acceptOffer_1 = require("../offer/eventServiceByCOA/acceptOffer");
let coaAuthClient;
/**
* タスク実行関数
*/
function call(params) {
// tslint:disable-next-line:max-func-body-length
return (_a, options_1) => __awaiter(this, [_a, options_1], void 0, function* ({ connection, redisClient, settings }, options) {
var _b, _c, _d, _e;
if (redisClient === undefined) {
throw new factory.errors.Argument('settings', 'redisClient required');
}
// 遅延実行(executeByName)には対応しない
if (!options.executeById) {
return;
}
const reserveInterfaceRepo = new reserveInterface_1.ReserveInterfaceRepo(connection);
const coaAPI = yield reserveInterfaceRepo.findOne({ project: { id: { $eq: params.project.id } } });
if (typeof (coaAPI === null || coaAPI === void 0 ? void 0 : coaAPI.id) !== 'string') {
throw new factory.errors.NotFound('WebAPI');
}
const credentials = (_b = coaAPI.availableChannel) === null || _b === void 0 ? void 0 : _b.credentials;
const memberClients = (_c = coaAPI.availableChannel) === null || _c === void 0 ? void 0 : _c.memberClients;
if (typeof (credentials === null || credentials === void 0 ? void 0 : credentials.refreshToken) !== 'string') {
throw new factory.errors.NotFound('WebAPI');
}
if (!Array.isArray(memberClients)) {
throw new factory.errors.NotFound('WebAPI.availableChannel.memberClients');
}
if (coaAuthClient === undefined) {
let credentialsRepo;
const credentialsExpireInSeconds = (_e = (_d = coaAPI.availableChannel) === null || _d === void 0 ? void 0 : _d.credentials) === null || _e === void 0 ? void 0 : _e.expireInSeconds;
if (typeof credentialsExpireInSeconds === 'number') {
// set credentialsRepo(2024-11-20~)
credentialsRepo = new credentials_1.CredentialsRepo(redisClient, {
scope: `COA:${coaAPI.id}`,
expireInSeconds: credentialsExpireInSeconds
});
}
coaAuthClient = new COA.auth.RefreshToken(Object.assign(Object.assign({}, credentials), (credentialsRepo !== undefined) ? { credentialsRepo } : undefined));
}
const actionRepo = new action_1.ActionRepo(connection);
const transactionProcessRepo = new transactionProcess_1.TransactionProcessRepo(redisClient, { lockExpiresInSeconds: 120 });
try {
const reserveService = new COA.service.Reserve({
endpoint: coaAuthClient.options.endpoint, // same as authClient(2024-07-17~)
auth: coaAuthClient
}, { timeout: settings.coa.timeout });
const masterService = new COA.service.Master({
endpoint: coaAuthClient.options.endpoint, // same as authClient(2024-07-17~)
auth: coaAuthClient
}, { timeout: settings.coa.timeout });
const { agent, object, potentialActions, purpose } = params.data;
// agent.idからflgMemberを自動セット(2024-12-16~)
const isMember = memberClients.includes(agent.id);
if (typeof potentialActions.id === 'string') {
yield (0, acceptOffer_1.reAcceptOffer)({
agent,
object,
purpose,
potentialActions: Object.assign(Object.assign({}, potentialActions), { id: potentialActions.id }),
sameAs: { id: params.id },
isMember
})({
action: actionRepo,
event: new event_1.EventRepo(connection),
orderNumber: new orderNumber_1.OrderNumberRepo({ redisClient, connection }),
project: new project_1.ProjectRepo(connection),
transaction: new transaction_1.TransactionRepo(connection),
reserveService,
masterService
});
}
else {
yield (0, acceptOffer_1.acceptOffer)({
agent,
object,
potentialActions,
purpose,
sameAs: { id: params.id },
isMember
})({
action: actionRepo,
event: new event_1.EventRepo(connection),
orderNumber: new orderNumber_1.OrderNumberRepo({ redisClient, connection }),
project: new project_1.ProjectRepo(connection),
transaction: new transaction_1.TransactionRepo(connection),
reserveService,
masterService
});
}
}
catch (error) {
let throwsError = true;
// アクションが存在すればタスクを実行済扱いにする
const action = (yield actionRepo.searchBySameAs({
sameAs: { id: { $eq: params.id } },
purpose: { id: { $eq: params.data.purpose.id } },
typeOf: { $eq: factory.actionType.AcceptAction }
})).shift();
if (typeof (action === null || action === void 0 ? void 0 : action.id) === 'string') {
throwsError = false;
}
if (throwsError) {
throw error;
}
}
finally {
// 取引プロセスロック解除
yield transactionProcessRepo.unlock({ typeOf: factory.transactionType.PlaceOrder, id: params.data.purpose.id });
}
});
}