UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

588 lines (587 loc) 29.3 kB
"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 factory = require("../../factory"); const accountTitle_1 = require("../../repo/accountTitle"); const action_1 = require("../../repo/action"); const aggregateOffer_1 = require("../../repo/aggregateOffer"); const categoryCode_1 = require("../../repo/categoryCode"); const creativeWork_1 = require("../../repo/creativeWork"); const event_1 = require("../../repo/event"); const eventSeries_1 = require("../../repo/eventSeries"); const member_1 = require("../../repo/member"); const note_1 = require("../../repo/note"); const unitPriceInCatalog_1 = require("../../repo/offer/unitPriceInCatalog"); const offerCatalog_1 = require("../../repo/offerCatalog"); const offerCatalogItem_1 = require("../../repo/offerCatalogItem"); const paymentServiceProvider_1 = require("../../repo/paymentServiceProvider"); const hasPOS_1 = require("../../repo/place/hasPOS"); const movieTheater_1 = require("../../repo/place/movieTheater"); const screeningRoom_1 = require("../../repo/place/screeningRoom"); const product_1 = require("../../repo/product"); const productModel_1 = require("../../repo/productModel"); const productOffer_1 = require("../../repo/productOffer"); const setting_1 = require("../../repo/setting"); const task_1 = require("../../repo/task"); const onAggregateOfferUpdated_1 = require("./onResourceUpdated/onAggregateOfferUpdated"); const onCategoryCodeUpdated_1 = require("./onResourceUpdated/onCategoryCodeUpdated"); const onHasPOSUpdated_1 = require("./onResourceUpdated/onHasPOSUpdated"); const onOfferCatalogUpdated_1 = require("./onResourceUpdated/onOfferCatalogUpdated"); const onResourceDeleted_1 = require("./onResourceUpdated/onResourceDeleted"); /** * タスク実行関数 */ function call(data) { return (_a) => __awaiter(this, [_a], void 0, function* ({ connection }) { yield onResourceUpdated(data)({ accountTitle: new accountTitle_1.AccountTitleRepo(connection), action: new action_1.ActionRepo(connection), aggregateOffer: new aggregateOffer_1.AggregateOfferRepo(connection), categoryCode: new categoryCode_1.CategoryCodeRepo(connection), creativeWork: new creativeWork_1.CreativeWorkRepo(connection), event: new event_1.EventRepo(connection), eventSeries: new eventSeries_1.EventSeriesRepo(connection), hasPOS: new hasPOS_1.HasPOSRepo(connection, { id: '' }), // 先の処理で明示的に指定される member: new member_1.MemberRepo(connection), movieTheater: new movieTheater_1.MovieTheaterRepo(connection), note: new note_1.NoteRepo(connection), offer: new unitPriceInCatalog_1.OfferRepo(connection), offerCatalog: new offerCatalog_1.OfferCatalogRepo(connection), offerCatalogItem: new offerCatalogItem_1.OfferCatalogItemRepo(connection), paymentServiceProvider: new paymentServiceProvider_1.PaymentServiceProviderRepo(connection), screeningRoom: new screeningRoom_1.ScreeningRoomRepo(connection), product: new product_1.ProductRepo(connection), productModel: new productModel_1.ProductModelRepo(connection), productOffer: new productOffer_1.ProductOfferRepo(connection), setting: new setting_1.SettingRepo(connection), task: new task_1.TaskRepo(connection) }); }); } // tslint:disable-next-line:max-func-body-length function onResourceUpdated(params) { // tslint:disable-next-line:max-func-body-length return (repos) => __awaiter(this, void 0, void 0, function* () { const isDeleted = params.isDeleted === true; if (isDeleted) { yield (0, onResourceDeleted_1.onResourceDeleted)(params)(repos); } else { const setting = yield repos.setting.findOne({ project: { id: { $eq: '*' } } }, ['onResourceUpdated', 'useInformResourceTypes']); const useInformResourceTypes = setting === null || setting === void 0 ? void 0 : setting.useInformResourceTypes; const useInform = params.useInform === true && Array.isArray(useInformResourceTypes) && useInformResourceTypes.includes(params.typeOf); if (useInform) { switch (params.typeOf) { case factory.creativeWorkType.NoteDigitalDocument: // ノート通知に対応(2024-02-14~) yield createInformNoteTasks({ project: { id: params.project.id }, ids: params.id, typeOf: params.typeOf }, setting)(repos); break; case factory.creativeWorkType.Movie: yield createInformMovieTasks({ project: { id: params.project.id }, ids: params.id, typeOf: params.typeOf }, setting)(repos); break; case factory.product.ProductType.EventService: yield createInformProductTasks({ project: { id: params.project.id }, ids: params.id, typeOf: params.typeOf }, setting)(repos); break; case 'CategoryCode': yield createInformCategoryCodeTasks({ project: { id: params.project.id }, ids: params.id, typeOf: params.typeOf }, setting)(repos); break; case factory.placeType.MovieTheater: yield createInformMovieTheaterTasks({ project: { id: params.project.id }, ids: params.id, typeOf: params.typeOf }, setting)(repos); break; case 'AccountTitle': yield createInformAccountTitleTasks({ project: { id: params.project.id }, ids: params.id, typeOf: params.typeOf }, setting)(repos); break; case 'POS': yield (0, onHasPOSUpdated_1.createInformHasPOSTasks)({ project: { id: params.project.id }, ids: [params.operator.id], typeOf: factory.placeType.MovieTheater })(repos); break; case factory.offerType.AggregateOffer: yield (0, onAggregateOfferUpdated_1.createInformOfferTasks)({ project: { id: params.project.id }, ids: params.id, typeOf: params.typeOf }, setting)(repos); break; case 'OfferCatalog': yield (0, onOfferCatalogUpdated_1.createInformOfferCatalogTasks)({ project: { id: params.project.id }, ids: params.id, typeOf: params.typeOf, isOfferCatalogItem: params.isOfferCatalogItem === true }, setting)(repos); break; default: // no op } } switch (params.typeOf) { // 施設名称を同期する(2023-06-29~) case factory.placeType.MovieTheater: yield syncMovieTheater({ project: { id: params.project.id }, ids: params.id, typeOf: params.typeOf })(repos); break; // カタログに対応(2023-09-12~) case 'OfferCatalog': yield (0, onOfferCatalogUpdated_1.onOfferCatalogUpdated)({ project: { id: params.project.id }, ids: params.id, typeOf: params.typeOf, isDeleted: false, isOfferCatalogItem: params.isOfferCatalogItem === true })(repos); break; // 区分に対応(2024-04-18~) case 'CategoryCode': yield (0, onCategoryCodeUpdated_1.onCategoryCodeUpdated)({ project: { id: params.project.id }, ids: params.id, isDeleted: false })(repos); break; default: // no op } } }); } function createInformNoteTasks(params, setting) { return (repos) => __awaiter(this, void 0, void 0, function* () { var _a; const notes4inform = yield repos.note.projectFields({ id: { $in: params.ids } }, ['about', 'identifier', 'project', 'text', 'typeOf', 'version']); // const informResources = settings.onResourceUpdated.informResource; const informResources = (_a = setting === null || setting === void 0 ? void 0 : setting.onResourceUpdated) === null || _a === void 0 ? void 0 : _a.informResource; if (notes4inform.length > 0) { const taskRunsAt = new Date(); const informTasks = []; informResources === null || informResources === void 0 ? void 0 : informResources.forEach((informResource) => { var _a; const informUrl = String((_a = informResource.recipient) === null || _a === void 0 ? void 0 : _a.url); notes4inform.forEach((note4inform) => { var _a; const { about, id, identifier, project, text, typeOf, version } = note4inform; const informObject = { about, id, identifier, project, text, typeOf, version }; // 明示的に必要最低限の属性のみ通知 const informActionAttributes = { object: informObject, recipient: { id: '', name: String((_a = informResource.recipient) === null || _a === void 0 ? void 0 : _a.name), typeOf: factory.creativeWorkType.WebApplication // url: informUrl // discontinue(2025-02-13~) }, target: { httpMethod: 'POST', encodingType: factory.encodingFormat.Application.json, typeOf: 'EntryPoint', urlTemplate: informUrl } }; informTasks.push({ project: project, name: factory.taskName.TriggerWebhook, status: factory.taskStatus.Ready, runsAt: taskRunsAt, remainingNumberOfTries: 10, numberOfTried: 0, executionResults: [], data: informActionAttributes }); }); }); if (informTasks.length > 0) { yield repos.task.saveMany(informTasks, { emitImmediately: true }); } } }); } function createInformMovieTasks(params, setting) { return (repos // settings: Settings ) => __awaiter(this, void 0, void 0, function* () { var _a; const movies4inform = yield repos.creativeWork.projectFields({ id: { $in: params.ids } }, [ 'additionalProperty', 'datePublished', 'duration', 'identifier', 'name', 'project', 'typeOf', 'distributor', 'contentRating', 'headline', 'thumbnailUrl' ]); // const informResources = settings.onResourceUpdated.informResource; const informResources = (_a = setting === null || setting === void 0 ? void 0 : setting.onResourceUpdated) === null || _a === void 0 ? void 0 : _a.informResource; if (movies4inform.length > 0) { const taskRunsAt = new Date(); const informTasks = []; informResources === null || informResources === void 0 ? void 0 : informResources.forEach((informResource) => { var _a; const informUrl = String((_a = informResource.recipient) === null || _a === void 0 ? void 0 : _a.url); movies4inform.forEach((movie4inform) => { var _a; // _idは不要であり、存在すると予期せぬ影響を及ぼす可能性がある delete movie4inform._id; const informActionAttributes = { object: movie4inform, recipient: { id: '', name: String((_a = informResource.recipient) === null || _a === void 0 ? void 0 : _a.name), typeOf: factory.creativeWorkType.WebApplication // url: informUrl // discontinue(2025-02-13~) }, target: { httpMethod: 'POST', encodingType: factory.encodingFormat.Application.json, typeOf: 'EntryPoint', urlTemplate: informUrl } }; informTasks.push({ project: movie4inform.project, name: factory.taskName.TriggerWebhook, status: factory.taskStatus.Ready, runsAt: taskRunsAt, remainingNumberOfTries: 10, numberOfTried: 0, executionResults: [], data: informActionAttributes }); }); }); if (informTasks.length > 0) { yield repos.task.saveMany(informTasks, { emitImmediately: true }); } } }); } function createInformProductTasks(params, setting) { return (repos // settings: Settings ) => __awaiter(this, void 0, void 0, function* () { var _a; const products4inform = yield repos.product.projectFields({ typeOf: { $eq: params.typeOf }, id: { $in: params.ids } }, ['additionalProperty', 'description', 'name', 'productID', 'project', 'typeOf', 'serviceType'] // [] ); // const informResources = settings.onResourceUpdated.informResource; const informResources = (_a = setting === null || setting === void 0 ? void 0 : setting.onResourceUpdated) === null || _a === void 0 ? void 0 : _a.informResource; if (products4inform.length > 0) { const taskRunsAt = new Date(); const informTasks = []; informResources === null || informResources === void 0 ? void 0 : informResources.forEach((informResource) => { var _a; const informUrl = String((_a = informResource.recipient) === null || _a === void 0 ? void 0 : _a.url); products4inform.forEach((product4inform) => { var _a; // _idは不要であり、存在すると予期せぬ影響を及ぼす可能性がある delete product4inform._id; const informActionAttributes = { object: product4inform, recipient: { id: '', name: String((_a = informResource.recipient) === null || _a === void 0 ? void 0 : _a.name), typeOf: factory.creativeWorkType.WebApplication // url: informUrl // discontinue(2025-02-13~) }, target: { httpMethod: 'POST', encodingType: factory.encodingFormat.Application.json, typeOf: 'EntryPoint', urlTemplate: informUrl } }; informTasks.push({ project: product4inform.project, name: factory.taskName.TriggerWebhook, status: factory.taskStatus.Ready, runsAt: taskRunsAt, remainingNumberOfTries: 10, numberOfTried: 0, executionResults: [], data: informActionAttributes }); }); }); if (informTasks.length > 0) { yield repos.task.saveMany(informTasks, { emitImmediately: true }); } } }); } function createInformCategoryCodeTasks(params, setting) { return (repos // settings: Settings ) => __awaiter(this, void 0, void 0, function* () { var _a; const categoryCodes4inform = yield repos.categoryCode.projectFields({ id: { $in: params.ids }, // ひとまずDistributorTypeのみ inCodeSet: { identifier: { $eq: factory.categoryCode.CategorySetIdentifier.DistributorType } } }, ['name', 'additionalProperty', 'project', 'typeOf', 'codeValue', 'inCodeSet']); const informResources = (_a = setting === null || setting === void 0 ? void 0 : setting.onResourceUpdated) === null || _a === void 0 ? void 0 : _a.informResource; if (categoryCodes4inform.length > 0) { const taskRunsAt = new Date(); const informTasks = []; informResources === null || informResources === void 0 ? void 0 : informResources.forEach((informResource) => { var _a; const informUrl = String((_a = informResource.recipient) === null || _a === void 0 ? void 0 : _a.url); categoryCodes4inform.forEach((categoryCode4inform) => { var _a; // _idは不要であり、存在すると予期せぬ影響を及ぼす可能性がある delete categoryCode4inform._id; const informActionAttributes = { object: categoryCode4inform, recipient: { id: '', name: String((_a = informResource.recipient) === null || _a === void 0 ? void 0 : _a.name), typeOf: factory.creativeWorkType.WebApplication // url: informUrl // discontinue(2025-02-13~) }, target: { httpMethod: 'POST', encodingType: factory.encodingFormat.Application.json, typeOf: 'EntryPoint', urlTemplate: informUrl } }; informTasks.push({ project: categoryCode4inform.project, name: factory.taskName.TriggerWebhook, status: factory.taskStatus.Ready, runsAt: taskRunsAt, remainingNumberOfTries: 10, numberOfTried: 0, executionResults: [], data: informActionAttributes }); }); }); if (informTasks.length > 0) { yield repos.task.saveMany(informTasks, { emitImmediately: true }); } } }); } // tslint:disable-next-line:max-func-body-length function createInformMovieTheaterTasks(params, setting) { return (repos // settings: Settings ) => __awaiter(this, void 0, void 0, function* () { var _a; if (params.ids.length !== 1) { throw new factory.errors.Argument('id', 'id.length must be 1'); } // const informResources = settings.onResourceUpdated.informResource; const informResources = (_a = setting === null || setting === void 0 ? void 0 : setting.onResourceUpdated) === null || _a === void 0 ? void 0 : _a.informResource; const movieTheaters = yield repos.movieTheater.projectFields({ limit: 1, page: 1, project: { id: { $eq: params.project.id } }, id: { $eq: params.ids[0] } }, [ 'additionalProperty', 'branchCode', 'hasEntranceGate', // 'hasPOS', 'kanaName', 'name', 'parentOrganization', 'project', 'telephone', 'url', 'typeOf' ] // [] ); const movieTheater = movieTheaters.shift(); if (movieTheater === undefined) { throw new factory.errors.NotFound(factory.placeType.MovieTheater); } // ルームを検索 const screeningRooms = yield repos.screeningRoom.searchScreeningRooms({ containedInPlace: { id: { $eq: movieTheater.id } } }); const movieTheaters4inform = [Object.assign(Object.assign({}, movieTheater), { containsPlace: screeningRooms.map((room) => { return { branchCode: room.branchCode, name: room.name, typeOf: room.typeOf, additionalProperty: room.additionalProperty, address: room.address }; }) })]; if (movieTheaters4inform.length > 0) { const taskRunsAt = new Date(); const informTasks = []; informResources === null || informResources === void 0 ? void 0 : informResources.forEach((informResource) => { var _a; const informUrl = String((_a = informResource.recipient) === null || _a === void 0 ? void 0 : _a.url); movieTheaters4inform.forEach((movieTheater4inform) => { var _a; // _idは不要であり、存在すると予期せぬ影響を及ぼす可能性がある delete movieTheater4inform._id; const informActionAttributes = { object: movieTheater4inform, recipient: { id: '', name: String((_a = informResource.recipient) === null || _a === void 0 ? void 0 : _a.name), typeOf: factory.creativeWorkType.WebApplication // url: informUrl // discontinue(2025-02-13~) }, target: { httpMethod: 'POST', encodingType: factory.encodingFormat.Application.json, typeOf: 'EntryPoint', urlTemplate: informUrl } }; informTasks.push({ project: movieTheater4inform.project, name: factory.taskName.TriggerWebhook, status: factory.taskStatus.Ready, runsAt: taskRunsAt, remainingNumberOfTries: 10, numberOfTried: 0, executionResults: [], data: informActionAttributes }); }); }); if (informTasks.length > 0) { yield repos.task.saveMany(informTasks, { emitImmediately: true }); } } }); } function createInformAccountTitleTasks(params, setting) { return (repos // settings: Settings ) => __awaiter(this, void 0, void 0, function* () { var _a; if (params.ids.length !== 1) { throw new factory.errors.Argument('id', 'id.length must be 1'); } // const informResources = settings.onResourceUpdated.informResource; const informResources = (_a = setting === null || setting === void 0 ? void 0 : setting.onResourceUpdated) === null || _a === void 0 ? void 0 : _a.informResource; const accountTitle = yield repos.accountTitle.accountTitleModel.findOne({ _id: { $eq: params.ids[0] }, 'project.id': { $eq: params.project.id } }, { _id: 0, id: { $toString: '$_id' }, additionalProperty: 1, codeValue: 1, hasCategoryCode: 1, name: 1, project: 1, typeOf: 1 }) .lean() .exec() .then((doc) => { if (doc === null) { throw new factory.errors.NotFound(params.typeOf); } return doc; }); const accountTitles4inform = [accountTitle]; if (accountTitles4inform.length > 0) { const taskRunsAt = new Date(); const informTasks = []; informResources === null || informResources === void 0 ? void 0 : informResources.forEach((informResource) => { var _a; const informUrl = String((_a = informResource.recipient) === null || _a === void 0 ? void 0 : _a.url); accountTitles4inform.forEach((accountTitle4inform) => { var _a; // _idは不要であり、存在すると予期せぬ影響を及ぼす可能性がある // const { _id, ...informObject } = accountTitle4inform; const informObject = accountTitle4inform; const informActionAttributes = { object: informObject, recipient: { id: '', name: String((_a = informResource.recipient) === null || _a === void 0 ? void 0 : _a.name), typeOf: factory.creativeWorkType.WebApplication // url: informUrl // discontinue(2025-02-13~) }, target: { httpMethod: 'POST', encodingType: factory.encodingFormat.Application.json, typeOf: 'EntryPoint', urlTemplate: informUrl } }; informTasks.push({ project: accountTitle4inform.project, name: factory.taskName.TriggerWebhook, status: factory.taskStatus.Ready, runsAt: taskRunsAt, remainingNumberOfTries: 10, numberOfTried: 0, executionResults: [], data: informActionAttributes }); }); }); if (informTasks.length > 0) { yield repos.task.saveMany(informTasks, { emitImmediately: true }); } } }); } function syncMovieTheater(params) { return (repos) => __awaiter(this, void 0, void 0, function* () { if (params.ids.length !== 1) { throw new factory.errors.Argument('id', 'id.length must be 1'); } const movieTheaters = yield repos.movieTheater.projectFields({ id: { $eq: params.ids[0] }, project: { id: { $eq: params.project.id } } }, ['id', 'name'] // [] ); const movieTheater = movieTheaters.shift(); if (movieTheater === undefined) { throw new factory.errors.NotFound(factory.placeType.MovieTheater); } yield repos.screeningRoom.updateScreeningRoomsByContainedInPlaceId({ project: { id: params.project.id }, containedInPlace: movieTheater }); }); }