UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

435 lines (434 loc) 22.7 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()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ScreeningRoomRepo = void 0; const factory = require("../../factory"); const settings_1 = require("../../settings"); const civicStructure_1 = require("../mongoose/schemas/civicStructure"); const place_1 = require("../mongoose/schemas/place"); /** * ルームリポジトリ */ class ScreeningRoomRepo { constructor(connection) { this.civicStructureModel = connection.model(civicStructure_1.modelName, (0, civicStructure_1.createSchema)()); this.placeModel = connection.model(place_1.modelName, (0, place_1.createSchema)()); } saveScreeningRooms4coa(params) { return __awaiter(this, void 0, void 0, function* () { const { containsPlace } = params, movieTheater4update = __rest(params, ["containsPlace"]); const movieTheater = yield this.civicStructureModel.findOne({ typeOf: { $eq: factory.placeType.MovieTheater }, 'project.id': { $eq: params.project.id }, branchCode: { $eq: movieTheater4update.branchCode } }, { _id: 0, id: { $toString: '$_id' } }) .lean() .exec(); if (movieTheater === null) { throw new factory.errors.NotFound(factory.placeType.MovieTheater); } const creatingScreeningRooms = params.containsPlace.map((place) => { return Object.assign(Object.assign({}, place), { containedInPlace: { id: movieTheater.id, typeOf: movieTheater4update.typeOf, branchCode: movieTheater4update.branchCode, name: movieTheater4update.name }, containsPlace: (Array.isArray(place.containsPlace)) ? place.containsPlace : [], project: params.project }); }); yield Promise.all(creatingScreeningRooms.map((createScreeningRoom) => __awaiter(this, void 0, void 0, function* () { const { typeOf, project, branchCode } = createScreeningRoom, setFields = __rest(createScreeningRoom, ["typeOf", "project", "branchCode"]); if (typeof branchCode === 'string' && branchCode.length > 0) { yield this.placeModel.findOneAndUpdate({ typeOf: { $eq: factory.placeType.ScreeningRoom }, 'project.id': { $eq: createScreeningRoom.project.id }, 'containedInPlace.id': { $exists: true, $eq: createScreeningRoom.containedInPlace.id }, branchCode: { $eq: createScreeningRoom.branchCode } }, { $setOnInsert: { typeOf: createScreeningRoom.typeOf, project: createScreeningRoom.project, branchCode: createScreeningRoom.branchCode }, $set: setFields }, { upsert: true, new: true, projection: { _id: 1 } }) .exec(); } }))); }); } createScreeningRoom(screeningRoom) { return __awaiter(this, void 0, void 0, function* () { var _a; // 施設存在確認 const movieTheaterDoc = yield this.civicStructureModel.findOne(Object.assign({ typeOf: { $eq: factory.placeType.MovieTheater }, 'project.id': { $eq: screeningRoom.project.id }, branchCode: { $eq: screeningRoom.containedInPlace.branchCode } }, (typeof ((_a = screeningRoom.parentOrganization) === null || _a === void 0 ? void 0 : _a.id) === 'string') ? { 'parentOrganization.id': { $exists: true, $eq: screeningRoom.parentOrganization.id } } : undefined), { _id: 0, id: { $toString: '$_id' }, typeOf: 1, branchCode: 1, name: 1, parentOrganization: 1 }) .lean() .exec(); if (movieTheaterDoc === null) { throw new factory.errors.NotFound(factory.placeType.MovieTheater); } // const movieTheater = <Pick< // factory.place.movieTheater.IPlace, // 'id' | 'typeOf' | 'branchCode' | 'name' | 'parentOrganization' // >>movieTheaterDoc.toObject(); const movieTheater = movieTheaterDoc; const creatingScreeningRoom = Object.assign({ typeOf: screeningRoom.typeOf, branchCode: screeningRoom.branchCode, name: screeningRoom.name, address: screeningRoom.address, additionalProperty: (Array.isArray(screeningRoom.additionalProperty)) ? screeningRoom.additionalProperty : [], containedInPlace: { id: movieTheater.id, typeOf: movieTheater.typeOf, branchCode: movieTheater.branchCode, name: movieTheater.name }, containsPlace: [], project: screeningRoom.project, // 必須化(2023-07-14~) parentOrganization: movieTheater.parentOrganization }, (typeof screeningRoom.openSeatingAllowed === 'boolean') ? { openSeatingAllowed: screeningRoom.openSeatingAllowed } : undefined); const upsertScreeningRoomResult = yield this.placeModel.updateOne({ typeOf: { $eq: factory.placeType.ScreeningRoom }, 'project.id': { $eq: creatingScreeningRoom.project.id }, 'containedInPlace.id': { $exists: true, $eq: creatingScreeningRoom.containedInPlace.id }, branchCode: { $eq: creatingScreeningRoom.branchCode } }, // 既存であれば何もしない { $setOnInsert: creatingScreeningRoom }, { upsert: true }) .exec(); // 既存であればコード重複 if (upsertScreeningRoomResult.matchedCount > 0) { throw new factory.errors.AlreadyInUse(factory.placeType.ScreeningRoom, ['branchCode']); } return { containedInPlace: { id: creatingScreeningRoom.containedInPlace.id }, typeOf: factory.placeType.ScreeningRoom }; }); } updateScreeningRoom(screeningRoom, $unset) { return __awaiter(this, void 0, void 0, function* () { var _a; const doc = yield this.placeModel.findOneAndUpdate(Object.assign({ typeOf: { $eq: factory.placeType.ScreeningRoom }, 'project.id': { $eq: screeningRoom.project.id }, 'containedInPlace.branchCode': { $exists: true, $eq: screeningRoom.containedInPlace.branchCode }, branchCode: screeningRoom.branchCode }, (typeof ((_a = screeningRoom.parentOrganization) === null || _a === void 0 ? void 0 : _a.id) === 'string') ? { 'parentOrganization.id': { $exists: true, $eq: screeningRoom.parentOrganization.id } } : undefined), Object.assign(Object.assign(Object.assign(Object.assign({ name: screeningRoom.name }, (screeningRoom.address !== undefined && screeningRoom.address !== null) ? { address: screeningRoom.address } : undefined), (typeof screeningRoom.openSeatingAllowed === 'boolean') ? { openSeatingAllowed: screeningRoom.openSeatingAllowed } : undefined), (Array.isArray(screeningRoom.additionalProperty)) ? { additionalProperty: screeningRoom.additionalProperty } : undefined), (($unset === null || $unset === void 0 ? void 0 : $unset['containsPlace.$[screeningRoom].openSeatingAllowed']) === 1 || $unset.openSeatingAllowed === 1) ? { $unset: { openSeatingAllowed: 1 } } : undefined), { new: true, projection: { 'containedInPlace.id': 1, typeOf: 1 } }) .exec(); if (doc === null) { throw new factory.errors.NotFound(factory.placeType.ScreeningRoom); } return doc.toObject(); }); } updateScreeningRoomsByContainedInPlaceId(screeningRoom) { return __awaiter(this, void 0, void 0, function* () { var _a; if (typeof ((_a = screeningRoom.containedInPlace.name) === null || _a === void 0 ? void 0 : _a.ja) === 'string') { yield this.placeModel.updateMany({ typeOf: { $eq: factory.placeType.ScreeningRoom }, 'project.id': { $eq: screeningRoom.project.id }, 'containedInPlace.id': { $exists: true, $eq: screeningRoom.containedInPlace.id } }, { 'containedInPlace.name': screeningRoom.containedInPlace.name }) .exec(); } }); } deleteScreeningRoom(screeningRoom) { return __awaiter(this, void 0, void 0, function* () { var _a; const doc = yield this.placeModel.findOneAndDelete(Object.assign({ typeOf: { $eq: factory.placeType.ScreeningRoom }, 'project.id': { $eq: screeningRoom.project.id }, 'containedInPlace.branchCode': { $exists: true, $eq: screeningRoom.containedInPlace.branchCode }, branchCode: screeningRoom.branchCode }, (typeof ((_a = screeningRoom.parentOrganization) === null || _a === void 0 ? void 0 : _a.id) === 'string') ? { 'parentOrganization.id': { $exists: true, $eq: screeningRoom.parentOrganization.id } } : undefined), { projection: { 'containedInPlace.id': 1, typeOf: 1 } }) .exec(); if (doc === null) { throw new factory.errors.NotFound(factory.placeType.ScreeningRoom); } return doc.toObject(); }); } deleteScreeningRoomsByMovieTheaterId(params) { return __awaiter(this, void 0, void 0, function* () { return this.placeModel.deleteMany({ typeOf: { $eq: factory.placeType.ScreeningRoom }, 'project.id': { $eq: params.project.id }, 'containedInPlace.id': { $exists: true, $eq: params.containedInPlace.id } }) .exec(); }); } // tslint:disable-next-line:cyclomatic-complexity max-func-body-length searchScreeningRooms(searchConditions) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o; const matchStages = [{ $match: { typeOf: { $eq: factory.placeType.ScreeningRoom } } }]; const projectIdEq = (_b = (_a = searchConditions.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$eq; if (typeof projectIdEq === 'string') { matchStages.push({ $match: { 'project.id': { $eq: projectIdEq } } }); } const parentOrganizationIdEq = (_d = (_c = searchConditions.parentOrganization) === null || _c === void 0 ? void 0 : _c.id) === null || _d === void 0 ? void 0 : _d.$eq; if (typeof parentOrganizationIdEq === 'string') { matchStages.push({ $match: { 'parentOrganization.id': { $exists: true, $eq: parentOrganizationIdEq } } }); } // 施設ID const containedInPlaceIdEq = (_f = (_e = searchConditions.containedInPlace) === null || _e === void 0 ? void 0 : _e.id) === null || _f === void 0 ? void 0 : _f.$eq; if (typeof containedInPlaceIdEq === 'string') { matchStages.push({ $match: { 'containedInPlace.id': { $exists: true, $eq: containedInPlaceIdEq } } }); } if (searchConditions.containedInPlace !== undefined) { // 施設コード if (searchConditions.containedInPlace.branchCode !== undefined) { if (typeof searchConditions.containedInPlace.branchCode.$eq === 'string') { matchStages.push({ $match: { 'containedInPlace.branchCode': { $exists: true, $eq: searchConditions.containedInPlace.branchCode.$eq } } }); } } } const branchCodeEq = (_g = searchConditions.branchCode) === null || _g === void 0 ? void 0 : _g.$eq; if (typeof branchCodeEq === 'string') { matchStages.push({ $match: { branchCode: { $eq: branchCodeEq } } }); } const branchCodeIn = (_h = searchConditions.branchCode) === null || _h === void 0 ? void 0 : _h.$in; if (Array.isArray(branchCodeIn)) { matchStages.push({ $match: { branchCode: { $in: branchCodeIn } } }); } const branchCodeRegex = (_j = searchConditions.branchCode) === null || _j === void 0 ? void 0 : _j.$regex; if (typeof branchCodeRegex === 'string') { matchStages.push({ $match: { branchCode: { $regex: new RegExp(branchCodeRegex) } } }); } const nameCodeRegex = (_k = searchConditions.name) === null || _k === void 0 ? void 0 : _k.$regex; if (typeof nameCodeRegex === 'string') { matchStages.push({ $match: { $or: [ { 'name.ja': { $exists: true, $regex: new RegExp(nameCodeRegex) } }, { 'name.en': { $exists: true, $regex: new RegExp(nameCodeRegex) } } ] } }); } const openSeatingAllowed = searchConditions.openSeatingAllowed; if (typeof openSeatingAllowed === 'boolean') { matchStages.push({ $match: { openSeatingAllowed: { $exists: true, $eq: openSeatingAllowed } } }); } const additionalPropertyElemMatch = (_l = searchConditions.additionalProperty) === null || _l === void 0 ? void 0 : _l.$elemMatch; if (additionalPropertyElemMatch !== undefined && additionalPropertyElemMatch !== null) { matchStages.push({ $match: { additionalProperty: { $exists: true, $elemMatch: additionalPropertyElemMatch } } }); } const aggregate = this.placeModel.aggregate([ { $sort: { branchCode: factory.sortType.Ascending } }, // { $unwind: '$containsPlace' }, ...matchStages, { $project: Object.assign(Object.assign({ _id: 0, typeOf: '$typeOf', branchCode: '$branchCode', name: '$name', address: '$address', containedInPlace: { id: '$containedInPlace.id', typeOf: '$containedInPlace.typeOf', branchCode: '$containedInPlace.branchCode', name: '$containedInPlace.name' }, openSeatingAllowed: '$openSeatingAllowed', additionalProperty: '$additionalProperty', maximumAttendeeCapacity: '$maximumAttendeeCapacity' }, (((_m = searchConditions.$projection) === null || _m === void 0 ? void 0 : _m.sectionCount) === 1) ? { sectionCount: { $cond: { if: { $isArray: '$containsPlace' }, then: { $size: '$containsPlace' }, else: 0 } } } : undefined), (((_o = searchConditions.$projection) === null || _o === void 0 ? void 0 : _o.seatCount) === 1) ? { seatCount: { $sum: { $map: { input: '$containsPlace', in: { $cond: { if: { $isArray: '$$this.containsPlace' }, then: { $size: '$$this.containsPlace' }, else: 0 } } } } } } : undefined) } ]); // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (typeof searchConditions.limit === 'number' && searchConditions.limit > 0) { const page = (typeof searchConditions.page === 'number' && searchConditions.page > 0) ? searchConditions.page : 1; aggregate.limit(searchConditions.limit * page) .skip(searchConditions.limit * (page - 1)); } return aggregate .option({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec(); }); } /** * セクションと座席も含めたひとつのルームを取得する */ findScreeningRoomsByBranchCode(params) { return __awaiter(this, void 0, void 0, function* () { const matchStages = [ { $match: { typeOf: { $eq: factory.placeType.ScreeningRoom } } }, { $match: { 'project.id': { $eq: params.project.id } } }, { $match: { 'containedInPlace.id': { $exists: true, $eq: params.containedInPlace.id.$eq } } }, { $match: { branchCode: { $eq: params.branchCode.$eq } } } ]; const aggregate = this.placeModel.aggregate([ ...matchStages, { $project: { _id: 0, typeOf: '$typeOf', branchCode: '$branchCode', name: '$name', containsPlace: '$containsPlace', seatCount: { $sum: { $map: { input: '$containsPlace', in: { $cond: { if: { $isArray: '$$this.containsPlace' }, then: { $size: '$$this.containsPlace' }, else: 0 } } } } } } } ]); const docs = yield aggregate.limit(1) .option({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec(); if (docs.length < 1) { throw new factory.errors.NotFound(factory.placeType.ScreeningRoom); } return docs[0]; }); } /** * 販売者に属するルームを全削除 */ deleteScreeningRoomsByParentOrganizationId(params) { return __awaiter(this, void 0, void 0, function* () { return this.placeModel.deleteMany({ typeOf: { $eq: factory.placeType.ScreeningRoom }, 'project.id': { $eq: params.project.id }, 'parentOrganization.id': { $exists: true, $eq: params.parentOrganization.id } }) .exec(); }); } /** * プロジェクトに属するルームを全削除 */ deleteScreeningRoomsByProject(params) { return __awaiter(this, void 0, void 0, function* () { yield this.placeModel.deleteMany({ typeOf: { $eq: factory.placeType.ScreeningRoom }, 'project.id': { $eq: params.project.id } }) .exec(); }); } } exports.ScreeningRoomRepo = ScreeningRoomRepo;