UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

442 lines (441 loc) 21.5 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.onResourceDeleted = onResourceDeleted; const factory = require("../../../factory"); const onHasPOSUpdated_1 = require("./onHasPOSUpdated"); const syncCategoryCode_1 = require("./syncCategoryCode"); const syncOfferCatalog_1 = require("./syncOfferCatalog"); function onResourceDeleted(params) { // tslint:disable-next-line:max-func-body-length return (repos) => __awaiter(this, void 0, void 0, function* () { const setting = yield repos.setting.findOne({ project: { id: { $eq: '*' } } }, ['useInformResourceTypes']); const useInformResourceTypes = setting === null || setting === void 0 ? void 0 : setting.useInformResourceTypes; const useInform = params.useInform === true && Array.isArray(useInformResourceTypes) && useInformResourceTypes.includes(params.typeOf); const isDeleted = params.isDeleted === true; if (isDeleted) { if (useInform) { switch (params.typeOf) { case 'POS': yield (0, onHasPOSUpdated_1.createInformHasPOSTasks)({ project: { id: params.project.id }, ids: [params.operator.id], typeOf: factory.placeType.MovieTheater })(repos); break; default: // no op } } switch (params.typeOf) { case 'POS': break; case factory.eventType.ScreeningEventSeries: yield deleteResourcesByScreeningEventSeries({ project: { id: params.project.id }, ids: params.id })(repos); break; case factory.placeType.ScreeningRoom: yield deleteResourcesByScreeningRoom({ project: { id: params.project.id }, branchCode: params.branchCode, containedInPlace: { id: params.containedInPlace.id } })(repos); break; case factory.placeType.MovieTheater: yield deleteResourcesByMovieTheater({ project: { id: params.project.id }, ids: params.id })(repos); break; case factory.organizationType.Corporation: yield deleteResourcesBySeller({ project: { id: params.project.id }, ids: params.id })(repos); break; case factory.offerType.AggregateOffer: yield deleteResourcesByAggregateOffer({ project: { id: params.project.id }, ids: params.id })(repos); break; case 'OfferCatalog': yield deleteResourcesByOfferCatalog({ project: { id: params.project.id }, ids: params.id })(repos); yield (0, syncOfferCatalog_1.syncOfferCatalog)({ project: { id: params.project.id }, ids: params.id, typeOf: params.typeOf, isDeleted: true, isOfferCatalogItem: params.isOfferCatalogItem === true })(repos); break; case factory.product.ProductType.EventService: case factory.product.ProductType.Product: case factory.product.ProductType.Transportation: case factory.product.ProductType.MembershipService: case factory.product.ProductType.PaymentCard: yield deleteResourcesByProduct({ project: { id: params.project.id }, ids: params.id, typeOf: params.typeOf })(repos); break; // 区分削除に対応(2024-04-18~) case 'CategoryCode': yield (0, syncCategoryCode_1.syncCategoryCode)({ project: { id: params.project.id }, ids: params.id, isDeleted: true })(repos); break; default: // no op throw new factory.errors.NotImplemented(`${params.typeOf} onDeleted not implemented`); } } }); } function deleteResourcesByScreeningEventSeries(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'); } for (const eventId of params.ids) { const deleteActionAttributes = { agent: { id: params.project.id, typeOf: factory.organizationType.Project }, object: { id: eventId, typeOf: factory.eventType.ScreeningEventSeries }, project: { id: params.project.id, typeOf: factory.organizationType.Project }, typeOf: factory.actionType.DeleteAction }; let deleteResult; const action = yield repos.action.start(deleteActionAttributes); try { // イベント削除 const deleteScreeningEventResult = yield repos.event.deleteManyBySuperEventId({ project: { id: params.project.id }, superEvent: { id: eventId } }); deleteResult = { deleteScreeningEventResult }; } catch (error) { try { yield repos.action.giveUp({ typeOf: deleteActionAttributes.typeOf, id: action.id, error }); } catch (_) { // no op } throw error; } yield repos.action.completeWithVoid({ typeOf: deleteActionAttributes.typeOf, id: action.id, result: deleteResult }); } }); } function deleteResourcesByScreeningRoom(params) { return (repos) => __awaiter(this, void 0, void 0, function* () { const deleteActionAttributes = { agent: { id: params.project.id, typeOf: factory.organizationType.Project }, object: { branchCode: params.branchCode, containedInPlace: params.containedInPlace, typeOf: factory.placeType.ScreeningRoom }, project: { id: params.project.id, typeOf: factory.organizationType.Project }, typeOf: factory.actionType.DeleteAction }; let deleteResult; const action = yield repos.action.start(deleteActionAttributes); try { // イベント削除 const deleteScreeningEventResult = yield repos.event.deleteManyEventsByScreeningRoom({ project: { id: params.project.id }, location: { branchCode: params.branchCode, containedInPlace: { id: params.containedInPlace.id } } }); deleteResult = { deleteScreeningEventResult }; } catch (error) { try { yield repos.action.giveUp({ typeOf: deleteActionAttributes.typeOf, id: action.id, error }); } catch (_) { // no op } throw error; } yield repos.action.completeWithVoid({ typeOf: deleteActionAttributes.typeOf, id: action.id, result: deleteResult }); }); } function deleteResourcesByMovieTheater(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'); } for (const placeId of params.ids) { const deleteActionAttributes = { agent: { id: params.project.id, typeOf: factory.organizationType.Project }, object: { id: placeId, typeOf: factory.placeType.MovieTheater }, project: { id: params.project.id, typeOf: factory.organizationType.Project }, typeOf: factory.actionType.DeleteAction }; let deleteResult; const action = yield repos.action.start(deleteActionAttributes); try { // イベント削除 const deleteScreeningEventResult = yield repos.event.deleteManyBySuperEventLocationId({ project: { id: params.project.id }, superEvent: { location: { id: placeId } } }); // 施設コンテンツ削除 const deleteScreeningEventSeriesResult = yield repos.eventSeries.deleteManyByLocationId({ project: { id: params.project.id }, location: { id: placeId } }); // ルーム削除 const deletePlaceResult = yield repos.screeningRoom.deleteScreeningRoomsByMovieTheaterId({ project: { id: params.project.id }, containedInPlace: { id: placeId } }); deleteResult = { deleteScreeningEventResult, deleteScreeningEventSeriesResult, deletePlaceResult }; } catch (error) { try { yield repos.action.giveUp({ typeOf: deleteActionAttributes.typeOf, id: action.id, error }); } catch (_) { // no op } throw error; } yield repos.action.completeWithVoid({ typeOf: deleteActionAttributes.typeOf, id: action.id, result: deleteResult }); } }); } function deleteResourcesBySeller(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'); } for (const sellerId of params.ids) { const deleteActionAttributes = { agent: { id: params.project.id, typeOf: factory.organizationType.Project }, object: { id: sellerId, typeOf: factory.organizationType.Corporation }, project: { id: params.project.id, typeOf: factory.organizationType.Project }, typeOf: factory.actionType.DeleteAction }; let deleteResult; const action = yield repos.action.start(deleteActionAttributes); try { // メンバー削除 const deleteMemberResult = yield repos.member.deleteManyByMemberOfId({ project: { id: params.project.id }, member: { memberOf: { id: { $eq: sellerId }, typeOf: { $eq: factory.organizationType.Corporation } } } }); // 提供決済サービス削除 const deletePaymentServiceProviderResult = yield repos.paymentServiceProvider.deleteManyByProviderId({ project: { id: params.project.id }, id: sellerId }); // プロダクトオファー削除 const deleteProductOfferResult = yield repos.productOffer.deleteManyBySellerId({ project: { id: params.project.id }, seller: { id: sellerId } }); // イベント削除 const deleteEventResult = yield repos.event.deleteManyEventByOrganizerId({ project: { id: params.project.id }, organizer: { id: sellerId } }); // 施設コンテンツ削除 const deleteEventSeriesResult = yield repos.eventSeries.deleteManyByOrganizerId({ project: { id: params.project.id }, organizer: { id: sellerId } }); // ルーム削除 const deleteScreeningRoomResult = yield repos.screeningRoom.deleteScreeningRoomsByParentOrganizationId({ project: { id: params.project.id }, parentOrganization: { id: sellerId } }); // 施設削除 const deleteMovieTheaterResult = yield repos.movieTheater.deleteMovieTheatersByParentOrganizationId({ project: { id: params.project.id }, parentOrganization: { id: sellerId } }); deleteResult = { deleteMemberResult, deletePaymentServiceProviderResult, deleteProductOfferResult, deleteEventResult, deleteEventSeriesResult, deleteScreeningRoomResult, deleteMovieTheaterResult }; } catch (error) { try { yield repos.action.giveUp({ typeOf: deleteActionAttributes.typeOf, id: action.id, error }); } catch (_) { // no op } throw error; } yield repos.action.completeWithVoid({ typeOf: deleteActionAttributes.typeOf, id: action.id, result: deleteResult }); } }); } function deleteResourcesByAggregateOffer(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'); } for (const offerId of params.ids) { const deleteActionAttributes = { agent: { id: params.project.id, typeOf: factory.organizationType.Project }, object: { id: offerId, typeOf: factory.offerType.AggregateOffer }, project: { id: params.project.id, typeOf: factory.organizationType.Project }, typeOf: factory.actionType.DeleteAction }; let deleteResult; const action = yield repos.action.start(deleteActionAttributes); try { // カタログからpullItemListElement const updateCatalogResult = yield repos.offerCatalog.pullItemListElement({ project: { id: params.project.id }, $pull: { itemListElement: { $elemMatch: { id: { $in: [offerId] } } } } }); // サブカタログ対応 const updateCatalogItemResult = yield repos.offerCatalogItem.pullItemListElement({ project: { id: params.project.id }, $pull: { itemListElement: { $elemMatch: { id: { $in: [offerId] } } } } }); deleteResult = { updateCatalogResult, updateCatalogItemResult }; } catch (error) { try { yield repos.action.giveUp({ typeOf: deleteActionAttributes.typeOf, id: action.id, error }); } catch (_) { // no op } throw error; } yield repos.action.completeWithVoid({ typeOf: deleteActionAttributes.typeOf, id: action.id, result: deleteResult }); } }); } function deleteResourcesByOfferCatalog(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'); } for (const catalogId of params.ids) { const deleteActionAttributes = { agent: { id: params.project.id, typeOf: factory.organizationType.Project }, object: { id: catalogId, typeOf: 'OfferCatalog' }, project: { id: params.project.id, typeOf: factory.organizationType.Project }, typeOf: factory.actionType.DeleteAction }; let deleteResult; const action = yield repos.action.start(deleteActionAttributes); try { // カタログからプロダクト検索 const productsWithCatalog = yield repos.product.projectFields({ project: { id: { $eq: params.project.id } }, hasOfferCatalog: { id: { $eq: catalogId } } }, ['id'] // [] ); let deleteEventResult; let updateOfferResult; if (productsWithCatalog.length > 0) { const deletingProductIds = productsWithCatalog.map((product) => product.id); // 興行を設定されたイベント削除 deleteEventResult = yield repos.event.deleteManyEventsByItemOfferedId({ project: { id: params.project.id }, offers: { itemOffered: { id: { $in: deletingProductIds } } } }); // アドオンから除外 updateOfferResult = yield repos.aggregateOffer.pullAddOns({ project: { id: params.project.id }, addOn: { itemOffered: { id: { $in: deletingProductIds } } } }); } // プロダクト削除 const deleteProductResult = yield repos.product.deleteByHasOfferCatalog({ project: { id: params.project.id }, hasOfferCatalog: { id: catalogId } }); // カタログからpullItemListElement(サブカタログ対応) const updateCatalogResult = yield repos.offerCatalog.pullItemListElement({ project: { id: params.project.id }, $pull: { itemListElement: { $elemMatch: { id: { $in: [catalogId] } } } } }); deleteResult = { deleteEventResult, deleteProductResult, updateOfferResult, updateCatalogResult }; } catch (error) { try { yield repos.action.giveUp({ typeOf: deleteActionAttributes.typeOf, id: action.id, error }); } catch (_) { // no op } throw error; } yield repos.action.completeWithVoid({ typeOf: deleteActionAttributes.typeOf, id: action.id, result: deleteResult }); } }); } function deleteResourcesByProduct(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'); } for (const productId of params.ids) { const deleteActionAttributes = { agent: { id: params.project.id, typeOf: factory.organizationType.Project }, object: { id: productId, typeOf: params.typeOf }, project: { id: params.project.id, typeOf: factory.organizationType.Project }, typeOf: factory.actionType.DeleteAction }; let deleteResult; const action = yield repos.action.start(deleteActionAttributes); try { let deleteEventResult; let updateOfferResult; // 興行を設定されたイベント削除 deleteEventResult = yield repos.event.deleteManyEventsByItemOfferedId({ project: { id: params.project.id }, offers: { itemOffered: { id: { $in: [productId] } } } }); // アドオンから除外 updateOfferResult = yield repos.aggregateOffer.pullAddOns({ project: { id: params.project.id }, addOn: { itemOffered: { id: { $in: [productId] } } } }); deleteResult = { deleteEventResult, updateOfferResult }; } catch (error) { try { yield repos.action.giveUp({ typeOf: deleteActionAttributes.typeOf, id: action.id, error }); } catch (_) { // no op } throw error; } yield repos.action.completeWithVoid({ typeOf: deleteActionAttributes.typeOf, id: action.id, result: deleteResult }); } }); }