@chevre/domain
Version:
Chevre Domain Library for Node.js
255 lines (254 loc) • 12.1 kB
JavaScript
"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.MovieTheaterRepo = void 0;
const factory = require("../../factory");
const settings_1 = require("../../settings");
const civicStructure_1 = require("../mongoose/schemas/civicStructure");
const AVAILABLE_PROJECT_FIELDS = [
'additionalProperty', 'branchCode', 'hasEntranceGate', 'kanaName', 'name',
'offers', 'parentOrganization', 'project', 'telephone', 'typeOf', 'url'
];
/**
* 施設リポジトリ
*/
class MovieTheaterRepo {
constructor(connection) {
this.civicStructureModel = connection.model(civicStructure_1.modelName, (0, civicStructure_1.createSchema)());
}
// tslint:disable-next-line:max-func-body-length
static CREATE_MOVIE_THEATER_MONGO_CONDITIONS(params) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
const andConditions = [{ typeOf: { $eq: factory.placeType.MovieTheater } }];
const projectIdEq = (_b = (_a = params.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$eq;
if (typeof projectIdEq === 'string') {
andConditions.push({ 'project.id': { $eq: projectIdEq } });
}
const branchCodeEq = (_c = params.branchCode) === null || _c === void 0 ? void 0 : _c.$eq;
if (typeof branchCodeEq === 'string') {
andConditions.push({ branchCode: { $eq: branchCodeEq } });
}
const branchCodeRegex = (_d = params.branchCode) === null || _d === void 0 ? void 0 : _d.$regex;
if (typeof branchCodeRegex === 'string' && branchCodeRegex.length > 0) {
andConditions.push({ branchCode: { $regex: new RegExp(branchCodeRegex) } });
}
if (Array.isArray(params.branchCodes)) {
andConditions.push({ branchCode: { $in: params.branchCodes } });
}
const idEq = (_e = params.id) === null || _e === void 0 ? void 0 : _e.$eq;
if (typeof idEq === 'string') {
andConditions.push({ _id: { $eq: idEq } });
}
const idIn = (_f = params.id) === null || _f === void 0 ? void 0 : _f.$in;
if (Array.isArray(idIn)) {
andConditions.push({ _id: { $in: idIn } });
}
if (typeof params.name === 'string' && params.name.length > 0) {
andConditions.push({
$or: [
{ 'name.ja': { $exists: true, $regex: new RegExp(params.name) } },
{ 'name.en': { $exists: true, $regex: new RegExp(params.name) } },
{ kanaName: { $exists: true, $regex: new RegExp(params.name) } }
]
});
}
const parentOrganizationIdEq = (_h = (_g = params.parentOrganization) === null || _g === void 0 ? void 0 : _g.id) === null || _h === void 0 ? void 0 : _h.$eq;
if (typeof parentOrganizationIdEq === 'string') {
andConditions.push({ 'parentOrganization.id': { $exists: true, $eq: parentOrganizationIdEq } });
}
const additionalPropertyElemMatch = (_j = params.additionalProperty) === null || _j === void 0 ? void 0 : _j.$elemMatch;
if (additionalPropertyElemMatch !== undefined && additionalPropertyElemMatch !== null) {
andConditions.push({
additionalProperty: {
$exists: true,
$elemMatch: additionalPropertyElemMatch
}
});
}
return andConditions;
}
/**
* 施設を保管する
*/
saveMovieTheater(params) {
return __awaiter(this, void 0, void 0, function* () {
let savedId;
let doc;
if (params.id === '') {
const { id } = params, creatingParams = __rest(params, ["id"]); // omit id(2024-09-12~)
doc = yield this.civicStructureModel.create(creatingParams);
savedId = doc.id;
}
else {
// 上書き禁止属性を除外(2022-08-24~)
const { id, branchCode, project, typeOf } = params, updateFields = __rest(params, ["id", "branchCode", "project", "typeOf"]);
doc = yield this.civicStructureModel.findOneAndUpdate({ _id: { $eq: params.id } }, updateFields, { upsert: false, new: true, projection: { _id: 1 } })
.exec();
savedId = params.id;
}
if (doc === null) {
throw new factory.errors.NotFound(this.civicStructureModel.modelName);
}
return { id: savedId };
});
}
saveMovieTheaterByBranchCode4coa(params) {
return __awaiter(this, void 0, void 0, function* () {
const { containsPlace } = params, movieTheater4update = __rest(params, ["containsPlace"]);
const movieTheaterDoc = yield this.civicStructureModel.findOneAndUpdate({
typeOf: { $eq: factory.placeType.MovieTheater },
'project.id': { $eq: params.project.id },
branchCode: { $eq: params.branchCode }
}, movieTheater4update, {
new: true,
projection: { _id: 1 }
})
.exec();
if (movieTheaterDoc === null) {
throw new factory.errors.NotFound(factory.placeType.MovieTheater);
}
});
}
/**
* 施設のpublic属性検索
* 2024-09-18~
*/
projectPublicFields(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const conditions = MovieTheaterRepo.CREATE_MOVIE_THEATER_MONGO_CONDITIONS(params);
const projection = {
_id: 0,
id: { $toString: '$_id' },
additionalProperty: 1, branchCode: 1,
kanaName: 1, name: 1,
parentOrganization: 1, project: 1, telephone: 1, typeOf: 1, url: 1,
// hasEntranceGate: 1,
// offers: 1,
// 'offers.typeOf': 1,
// 'offers.eligibleQuantity': 1,
'offers.availabilityStartsGraceTime': 1,
'offers.availabilityEndsGraceTime': 1
// 'offers.availabilityStartsGraceTimeOnPOS': 1,
// 'offers.availabilityEndsGraceTimeOnPOS': 1,
};
const query = this.civicStructureModel.find((conditions.length > 0) ? { $and: conditions } : {}, projection);
if (typeof params.limit === 'number' && params.limit > 0) {
const page = (typeof params.page === 'number' && params.page > 0) ? params.page : 1;
query.limit(params.limit)
.skip(params.limit * (page - 1));
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (((_a = params.sort) === null || _a === void 0 ? void 0 : _a.branchCode) !== undefined) {
query.sort({ branchCode: params.sort.branchCode });
}
return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
.lean() // 2024-09-18~
.exec();
});
}
/**
* 施設検索
* redefine(2024-09-18~)
*/
projectFields(params, inclusion) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const conditions = MovieTheaterRepo.CREATE_MOVIE_THEATER_MONGO_CONDITIONS(params);
let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS;
if (Array.isArray(inclusion) && inclusion.length > 0) {
positiveProjectionFields = inclusion.filter((key) => AVAILABLE_PROJECT_FIELDS.includes(key));
}
else {
throw new factory.errors.ArgumentNull('inclusion', 'inclusion must be specified');
}
const projection = Object.assign({ _id: 0, id: { $toString: '$_id' } }, Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1]))));
const query = this.civicStructureModel.find((conditions.length > 0) ? { $and: conditions } : {}, projection);
if (typeof params.limit === 'number' && params.limit > 0) {
const page = (typeof params.page === 'number' && params.page > 0) ? params.page : 1;
query.limit(params.limit)
.skip(params.limit * (page - 1));
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (((_a = params.sort) === null || _a === void 0 ? void 0 : _a.branchCode) !== undefined) {
query.sort({ branchCode: params.sort.branchCode });
}
return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
.lean() // 2024-09-18~
.exec();
});
}
deleteMovieTheaterById(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.civicStructureModel.findOneAndDelete({
typeOf: { $eq: factory.placeType.MovieTheater },
_id: { $eq: params.id },
'project.id': { $eq: params.project.id }
}, { projection: { _id: 1 } })
.exec()
.then((doc) => {
if (doc === null) {
throw new factory.errors.NotFound(this.civicStructureModel.modelName);
}
});
});
}
/**
* 販売者に属する施設を全削除
*/
deleteMovieTheatersByParentOrganizationId(params) {
return __awaiter(this, void 0, void 0, function* () {
return this.civicStructureModel.deleteMany({
typeOf: { $eq: factory.placeType.MovieTheater },
'project.id': { $eq: params.project.id },
'parentOrganization.id': { $exists: true, $eq: params.parentOrganization.id }
})
.exec();
});
}
/**
* プロジェクトに属する施設を全削除
*/
deleteMovieTheatersByProject(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.civicStructureModel.deleteMany({
typeOf: { $eq: factory.placeType.MovieTheater },
'project.id': { $eq: params.project.id }
})
.exec();
});
}
getCursor(conditions, projection) {
return this.civicStructureModel.find(conditions, projection)
.sort({ branchCode: factory.sortType.Ascending })
.cursor();
}
unsetUnnecessaryFields(params) {
return __awaiter(this, void 0, void 0, function* () {
return this.civicStructureModel.updateMany(params.filter, { $unset: params.$unset }, { timestamps: false })
.exec();
});
}
}
exports.MovieTheaterRepo = MovieTheaterRepo;