@chevre/domain
Version:
Chevre Domain Library for Node.js
176 lines (175 loc) • 10.2 kB
JavaScript
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.onReservationConfirmedByAction = onReservationConfirmedByAction;
exports.onReservationConfirmed = onReservationConfirmed;
/**
* 予約確定時アクション
*/
const factory = require("../../../factory");
// import { Settings } from '../../../settings';
const factory_1 = require("../factory");
/**
* 予約確定後のアクション
*/
function onReservationConfirmedByAction(actionAttributes) {
return (repos) => __awaiter(this, void 0, void 0, function* () {
const potentialActions = actionAttributes.potentialActions;
const now = new Date();
const taskAttributes = [];
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (potentialActions !== undefined) {
if (Array.isArray(potentialActions.moneyTransfer)) {
taskAttributes.push(...potentialActions.moneyTransfer.map((a) => {
return {
project: a.project,
name: factory.taskName.MoneyTransfer,
status: factory.taskStatus.Ready,
runsAt: now,
remainingNumberOfTries: 10,
numberOfTried: 0,
executionResults: [],
data: a
};
}));
}
}
// タスク保管
if (taskAttributes.length > 0) {
yield repos.task.saveMany(taskAttributes, { emitImmediately: true });
}
});
}
function onReservationConfirmed(confirmedReservations, reserveAction) {
// tslint:disable-next-line:max-func-body-length
return (repos
// settings: Settings
) => __awaiter(this, void 0, void 0, function* () {
var _a;
const setting = yield repos.setting.findOne({ project: { id: { $eq: '*' } } }, ['onReservationStatusChanged']);
// const informReservations = settings.onReservationStatusChanged.informReservation;
const informReservations = (_a = setting === null || setting === void 0 ? void 0 : setting.onReservationStatusChanged) === null || _a === void 0 ? void 0 : _a.informReservation;
let orderAsAbout;
if (Array.isArray(reserveAction.instrument)) {
for (const eachInstrument of reserveAction.instrument) {
if (eachInstrument.typeOf === factory.order.OrderType.Order && typeof eachInstrument.orderNumber === 'string') {
orderAsAbout = { orderNumber: eachInstrument.orderNumber, typeOf: factory.order.OrderType.Order };
break;
}
}
}
if (Array.isArray(confirmedReservations) && confirmedReservations.length > 0) {
// ひとつめの予約からReservationPackageの共有属性を取り出す
const { bookingTime, issuedThrough, project, provider, reservationFor, reservationNumber, underName } = confirmedReservations[0];
// create AggregateScreeningEvent task -> migrate to agg(2024-10-29~)
const now = new Date();
const taskAttributes = [];
let onAuthorizationCreatedTask;
// support OnAuthorizationCreated(2025-03-15~)
if (typeof (orderAsAbout === null || orderAsAbout === void 0 ? void 0 : orderAsAbout.orderNumber) === 'string') {
const existingAuthorization = (yield repos.code.projectFields({
limit: 1,
page: 1,
object: {
orderNumber: { $eq: orderAsAbout.orderNumber },
typeOfs: [factory.order.OrderType.Order]
}
}, ['code'])).shift();
if (existingAuthorization !== undefined) {
// console.log(
// 'onReservationConfirmed: existingAuthorization found.',
// orderAsAbout.orderNumber, reservationNumber, JSON.stringify(existingAuthorization)
// );
const taskIdentifier = `${project.id}:${factory.taskName.OnAuthorizationCreated}:${factory.assetTransactionType.Reserve}:${reservationNumber}:onReservationConfirmed`;
onAuthorizationCreatedTask = {
alternateName: taskIdentifier,
identifier: taskIdentifier,
name: factory.taskName.OnAuthorizationCreated,
status: factory.taskStatus.Ready,
runsAt: now,
remainingNumberOfTries: 3,
numberOfTried: 0,
executionResults: [],
data: {
// project: { id: project.id },
code: existingAuthorization.code,
retryOnReservationNotFound: true
},
project
};
}
}
const subReservations4inform = confirmedReservations.map((r) => {
const { additionalProperty, additionalTicketText,
// attended, checkedIn,
id, modifiedTime,
// numSeats,
price, programMembershipUsed, reservedTicket, subReservation, typeOf } = r;
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({
// bookingTime, // discontinue(2024-10-27~)
id,
typeOf,
reservedTicket }, (Array.isArray(additionalProperty)) ? { additionalProperty } : undefined), (typeof additionalTicketText === 'string') ? { additionalTicketText } : undefined), (modifiedTime instanceof Date) ? { modifiedTime } : undefined), (price !== undefined) ? { price } : undefined), (programMembershipUsed !== undefined) ? { programMembershipUsed } : undefined), (Array.isArray(subReservation)) ? { subReservation } : undefined);
});
if (typeof issuedThrough.id !== 'string') {
// COA予約では予約アクションを想定していないので、興行idは必ず存在するはず
throw new factory.errors.Internal('reservation.issuedThrough.id must be string');
}
const informObject = Object.assign({ bookingTime,
issuedThrough, // subReservationから移行(2024-03-20~)
project,
provider, // subReservationから移行(2024-03-20~)
reservationFor,
reservationNumber, reservationStatus: factory.reservationStatusType.ReservationConfirmed, subReservation: subReservations4inform, typeOf: factory.reservationType.ReservationPackage }, (typeof (underName === null || underName === void 0 ? void 0 : underName.typeOf) === 'string')
? { underName: (0, factory_1.optimizeUnderName4inform)({ underName }) }
: undefined);
const informIdentifier = `${factory.reservationType.ReservationPackage}:${informObject.reservationNumber}:${informObject.reservationStatus}`;
// inform galobally
if (Array.isArray(informReservations)) {
taskAttributes.push(...informReservations.map((informReservation) => {
var _a, _b;
const informUrl = String((_a = informReservation.recipient) === null || _a === void 0 ? void 0 : _a.url);
const recipientName = (_b = informReservation.recipient) === null || _b === void 0 ? void 0 : _b.name;
const informReservationAttributes = Object.assign({ object: informObject, recipient: {
id: '',
name: (typeof recipientName === 'string') ? recipientName : 'unknown', // redefine name(2025-02-17~)
typeOf: factory.creativeWorkType.WebApplication
// url: informUrl // discontinue(2025-02-13~)
}, target: {
httpMethod: 'POST',
encodingType: factory.encodingFormat.Application.json,
typeOf: 'EntryPoint',
urlTemplate: informUrl
}, identifier: informIdentifier }, (orderAsAbout !== undefined) ? { about: orderAsAbout } : undefined // add about(2025-02-19~)
);
return {
project,
name: factory.taskName.TriggerWebhook,
status: factory.taskStatus.Ready,
runsAt: now,
remainingNumberOfTries: factory_1.NUM_TRY_INFORM_RESERVATION,
numberOfTried: 0,
executionResults: [],
data: informReservationAttributes
};
}));
}
// タスク保管
if (taskAttributes.length > 0) {
yield repos.task.saveMany(taskAttributes, { emitImmediately: true });
}
if (onAuthorizationCreatedTask !== undefined) {
yield repos.task.createIfNotExistByAlternateName(onAuthorizationCreatedTask, { emitImmediately: true });
}
}
});
}
;