@chevre/domain
Version:
Chevre Domain Library for Node.js
84 lines (83 loc) • 4.7 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.onReservationsCreated = onReservationsCreated;
const factory = require("../../../factory");
// import { Settings } from '../../../settings';
const factory_1 = require("../factory");
/**
* 予約作成時イベント
*/
function onReservationsCreated(params) {
return (repos
// settings: Settings
) => __awaiter(this, void 0, void 0, function* () {
var _a;
const setting = yield repos.setting.findOne({ project: { id: { $eq: '*' } } }, ['onReservationStatusChanged']);
// create AggregateScreeningEvent task -> migrate to agg(2024-10-29~)
// inform(2024-10-25~)
// 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;
const now = new Date();
const taskAttributes = [];
const informObject = {
project: { id: params.project.id, typeOf: factory.organizationType.Project },
reservationFor: { id: params.event.id },
reservationNumber: params.transaction.transactionNumber,
reservationStatus: factory.reservationStatusType.ReservationPending,
typeOf: factory.reservationType.ReservationPackage
};
const informIdentifier = `${factory.reservationType.ReservationPackage}:${informObject.reservationNumber}:${informObject.reservationStatus}`;
let placeOrderAsAbout;
if (Array.isArray(params.transaction.instrument)) {
for (const eachInstrument of params.transaction.instrument) {
if (eachInstrument.typeOf === factory.transactionType.PlaceOrder && typeof eachInstrument.id === 'string') {
placeOrderAsAbout = { id: eachInstrument.id, typeOf: factory.transactionType.PlaceOrder };
break;
}
}
}
// 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 }, (placeOrderAsAbout !== undefined) ? { about: placeOrderAsAbout } : undefined // add about(2025-02-19~)
);
return {
project: { id: params.project.id, typeOf: factory.organizationType.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 });
}
});
}
;