UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

212 lines (211 loc) 10 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.MemberProgramRepo = void 0; const mongoose_1 = require("mongoose"); const factory = require("../factory"); const settings_1 = require("../settings"); const issuer_1 = require("./mongoose/schemas/issuer"); /** * メンバープログラムリポジトリ */ class MemberProgramRepo { constructor(connection) { this.issuerModel = connection.model(issuer_1.modelName, (0, issuer_1.createSchema)()); } /** * memberProgramを検索する */ projectMemberPrograms(params) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f; const matchStages = []; 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') { matchStages.push({ $match: { 'project.id': { $eq: projectIdEq } } }); } const hostingOrganizationIdEq = (_d = (_c = params.hostingOrganization) === null || _c === void 0 ? void 0 : _c.id) === null || _d === void 0 ? void 0 : _d.$eq; if (typeof hostingOrganizationIdEq === 'string') { matchStages.push({ $match: { _id: { $eq: new mongoose_1.Types.ObjectId(hostingOrganizationIdEq) } } }); } const identifierEq = (_e = params.identifier) === null || _e === void 0 ? void 0 : _e.$eq; if (typeof identifierEq === 'string') { matchStages.push({ $match: { 'hasMemberProgram.identifier': { $exists: true, $eq: identifierEq } } }); } const identifierIn = (_f = params.identifier) === null || _f === void 0 ? void 0 : _f.$in; if (Array.isArray(identifierIn)) { matchStages.push({ $match: { 'hasMemberProgram.identifier': { $exists: true, $in: identifierIn } } }); } const aggregate = this.issuerModel.aggregate([ { $unwind: { path: '$hasMemberProgram' } }, ...matchStages, { $sort: { 'hasMemberProgram.identifier': factory.sortType.Ascending } }, { $project: { _id: 0, typeOf: '$hasMemberProgram.typeOf', identifier: '$hasMemberProgram.identifier', hasTiers: '$hasMemberProgram.hasTiers', hostingOrganization: { identifier: '$identifier' } } } ]); if (typeof params.limit === 'number' && params.limit > 0) { const page = (typeof params.page === 'number' && params.page > 0) ? params.page : 1; aggregate.skip(params.limit * (page - 1)) .limit(params.limit); } return aggregate.option({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec(); }); } /** * tierを検索する */ projectMemberProgramTiers(params) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h; const matchStages = []; 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') { matchStages.push({ $match: { 'project.id': { $eq: projectIdEq } } }); } const hostingOrganizationIdEq = (_e = (_d = (_c = params.isTierOf) === null || _c === void 0 ? void 0 : _c.hostingOrganization) === null || _d === void 0 ? void 0 : _d.id) === null || _e === void 0 ? void 0 : _e.$eq; if (typeof hostingOrganizationIdEq === 'string') { matchStages.push({ $match: { _id: { $eq: new mongoose_1.Types.ObjectId(hostingOrganizationIdEq) } } }); } const memberProgramIdentifierEq = (_g = (_f = params.isTierOf) === null || _f === void 0 ? void 0 : _f.identifier) === null || _g === void 0 ? void 0 : _g.$eq; if (typeof memberProgramIdentifierEq === 'string') { matchStages.push({ $match: { 'hasMemberProgram.identifier': { $exists: true, $eq: memberProgramIdentifierEq } } }); } const identifierEq = (_h = params.identifier) === null || _h === void 0 ? void 0 : _h.$eq; if (typeof identifierEq === 'string') { matchStages.push({ $match: { 'hasMemberProgram.hasTiers.identifier': { $exists: true, $eq: identifierEq } } }); } const aggregate = this.issuerModel.aggregate([ { $unwind: { path: '$hasMemberProgram' } }, { $unwind: { path: '$hasMemberProgram.hasTiers' } }, ...matchStages, { $sort: { 'hasMemberProgram.hasTiers.identifier': factory.sortType.Ascending } }, { $project: { _id: 0, typeOf: '$hasMemberProgram.hasTiers.typeOf', identifier: '$hasMemberProgram.hasTiers.identifier', isTierOf: { identifier: '$hasMemberProgram.identifier', hostingOrganization: { identifier: '$identifier' } } } } ]); if (typeof params.limit === 'number' && params.limit > 0) { const page = (typeof params.page === 'number' && params.page > 0) ? params.page : 1; aggregate.skip(params.limit * (page - 1)) .limit(params.limit); } return aggregate.option({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec(); }); } create(params) { return __awaiter(this, void 0, void 0, function* () { let doc = yield this.issuerModel.findOne({ _id: { $eq: params.hostingOrganization.id }, 'project.id': { $eq: params.project.id } }, { _id: 1 }) .exec(); if (doc === null) { throw new factory.errors.NotFound('Issuer'); } const creatingMemberProgram = { typeOf: 'MemberProgram', identifier: params.identifier }; doc = yield this.issuerModel.findOneAndUpdate({ _id: { $eq: params.hostingOrganization.id }, 'project.id': { $eq: params.project.id }, 'hasMemberProgram.identifier': { $ne: params.identifier } }, { $push: { hasMemberProgram: creatingMemberProgram } }, { projection: { _id: 1 } }) .exec(); if (doc === null) { throw new factory.errors.AlreadyInUse('hasMemberProgram', ['identifier']); } }); } updateOne(params) { return __awaiter(this, void 0, void 0, function* () { const hasTiers = (Array.isArray(params.hasTiers)) ? params.hasTiers.map(({ identifier }) => ({ identifier, typeOf: 'MemberProgramTier' })) : []; const updatingMemberProgram = { typeOf: 'MemberProgram', identifier: params.identifier, hasTiers }; const doc = yield this.issuerModel.findOneAndUpdate({ _id: { $eq: params.hostingOrganization.id }, 'project.id': { $eq: params.project.id }, 'hasMemberProgram.identifier': { $exists: true, $eq: params.identifier } }, { $set: { 'hasMemberProgram.$[hasMemberProgramElement]': updatingMemberProgram } }, { new: true, arrayFilters: [ { 'hasMemberProgramElement.identifier': { $eq: params.identifier } } ], projection: { _id: 1 } }) .exec(); if (doc === null) { throw new factory.errors.NotFound('MemberProgram'); } }); } deleteOne(params) { return __awaiter(this, void 0, void 0, function* () { const doc = yield this.issuerModel.findOneAndUpdate({ _id: { $eq: params.hostingOrganization.id }, 'project.id': { $eq: params.project.id }, 'hasMemberProgram.identifier': { $exists: true, $eq: params.identifier } }, { $pull: { hasMemberProgram: { identifier: params.identifier } } }, { projection: { _id: 1 } }) .exec(); if (doc === null) { throw new factory.errors.NotFound('MemberProgram'); } }); } } exports.MemberProgramRepo = MemberProgramRepo;