@chevre/domain
Version:
Chevre Domain Library for Node.js
100 lines (99 loc) • 5.62 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.onReservationUsed = onReservationUsed;
/**
* 予約使用時アクション
*/
const moment = require("moment");
const factory = require("../../../factory");
// import { Settings } from '../../../settings';
const factory_1 = require("../factory");
/**
* 予約使用
*/
function onReservationUsed(attendedReservation, action) {
// tslint:disable-next-line:max-func-body-length
return (repos
// settings: Settings
) => __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const setting = yield repos.setting.findOne({ project: { id: { $eq: '*' } } }, ['onReservationStatusChanged']);
const tasks = [];
const now = new Date();
// 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 informTaskRunsAt = now;
// support notification.reservation.IUseAction(2024-11-11~)
let action4inform;
let reservation4inform;
// let reservation4inform: factory.notification.reservation.IReservation4informUsed
// | factory.notification.reservation.IUseAction;
const useForTheFirstTime = attendedReservation.reservedTicket.dateUsed !== undefined
&& moment(action.startDate)
.isSame(moment(attendedReservation.reservedTicket.dateUsed), 'milliseconds');
reservation4inform = Object.assign({ typeOf: attendedReservation.typeOf, id: attendedReservation.id }, (useForTheFirstTime)
? {
modifiedTime: attendedReservation.modifiedTime,
reservedTicket: { dateUsed: attendedReservation.reservedTicket.dateUsed }
}
: undefined);
action4inform = Object.assign({ typeOf: action.typeOf, project: action.project, id: action.id, object: reservation4inform }, (typeof ((_b = action.location) === null || _b === void 0 ? void 0 : _b.identifier) === 'string')
? { location: { identifier: action.location.identifier } }
: undefined);
const modifiedTimeInMilliseconds = moment(attendedReservation.modifiedTime)
.valueOf();
const informIdentifier = `${attendedReservation.typeOf}:${attendedReservation.id}:attended:${modifiedTimeInMilliseconds}`;
// inform galobally
if (Array.isArray(informReservations)) {
let orderAsAbout;
if (Array.isArray(action.instrument)) {
for (const eachInstrument of action.instrument) {
if (eachInstrument.typeOf === factory.order.OrderType.Order && typeof eachInstrument.orderNumber === 'string') {
orderAsAbout = { orderNumber: eachInstrument.orderNumber, typeOf: factory.order.OrderType.Order };
break;
}
}
}
tasks.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: action4inform, 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: attendedReservation.project,
name: factory.taskName.TriggerWebhook,
status: factory.taskStatus.Ready,
runsAt: informTaskRunsAt,
remainingNumberOfTries: factory_1.NUM_TRY_INFORM_RESERVATION,
numberOfTried: 0,
executionResults: [],
data: informReservationAttributes
};
}));
}
if (tasks.length > 0) {
yield repos.task.saveMany(tasks, { emitImmediately: true });
}
// create AggregateScreeningEvent task -> migrate to agg(2024-10-29~)
});
}
;