UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

202 lines (201 loc) 9.26 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.ProductOfferRepo = void 0; const mongoose_1 = require("mongoose"); const product_1 = require("./mongoose/schemas/product"); const factory = require("../factory"); const settings_1 = require("../settings"); /** * プロダクトオファーリポジトリ */ class ProductOfferRepo { constructor(connection) { this.productModel = connection.model(product_1.modelName, (0, product_1.createSchema)()); } /** * プロダクトオファー検索 */ search(params) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f; const matchStages = [{ $match: { typeOf: { $in: [ factory.product.ProductType.MembershipService, factory.product.ProductType.PaymentCard ] } } }]; 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 itemOfferedIdEq = (_d = (_c = params.itemOffered) === null || _c === void 0 ? void 0 : _c.id) === null || _d === void 0 ? void 0 : _d.$eq; if (typeof itemOfferedIdEq === 'string') { matchStages.push({ $match: { _id: { $eq: new mongoose_1.Types.ObjectId(itemOfferedIdEq) } } }); } const sellerIdEq = (_f = (_e = params.seller) === null || _e === void 0 ? void 0 : _e.id) === null || _f === void 0 ? void 0 : _f.$eq; if (typeof sellerIdEq === 'string') { matchStages.push({ $match: { 'offers.seller.id': { $exists: true, $eq: sellerIdEq } } }); } const aggregate = this.productModel.aggregate([ { $sort: { productID: factory.sortType.Ascending } }, { $unwind: { path: '$offers' } }, ...matchStages, { $project: { _id: 0, itemOffered: { id: { $toString: '$_id' }, name: '$name', typeOf: '$typeOf' }, availabilityEnds: '$offers.availabilityEnds', availabilityStarts: '$offers.availabilityStarts', validFrom: '$offers.validFrom', validThrough: '$offers.validThrough', seller: '$offers.seller' } } ]); if (typeof params.limit === 'number' && params.limit > 0) { const page = (typeof params.page === 'number' && params.page > 0) ? params.page : 1; aggregate.limit(params.limit * page) .skip(params.limit * (page - 1)); } return aggregate.option({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec(); }); } create(params) { return __awaiter(this, void 0, void 0, function* () { var _a; const sellerId = (_a = params.seller) === null || _a === void 0 ? void 0 : _a.id; if (typeof sellerId !== 'string' || sellerId.length === 0) { throw new factory.errors.ArgumentNull('seller.id'); } // プロダクト存在確認 let doc = yield this.productModel.findOne({ 'project.id': { $eq: params.project.id }, _id: { $eq: params.itemOffered.id } }, { _id: 1 }) .exec(); if (doc === null) { throw new factory.errors.NotFound('Product'); } const creatingOffer = { priceCurrency: factory.priceCurrency.JPY, availabilityEnds: params.availabilityEnds, availabilityStarts: params.availabilityStarts, validFrom: params.validFrom, validThrough: params.validThrough, seller: { id: sellerId }, typeOf: factory.offerType.Offer }; doc = yield this.productModel.findOneAndUpdate({ 'project.id': { $eq: params.project.id }, _id: { $eq: params.itemOffered.id }, 'offers.seller.id': { $ne: sellerId } }, { $push: { offers: creatingOffer } }, { new: true, projection: { _id: 1 } }) .exec(); // 存在しなければプロバイダーID重複 if (doc === null) { throw new factory.errors.AlreadyInUse('offers.seller', ['id']); } return doc.toObject(); }); } update(params) { return __awaiter(this, void 0, void 0, function* () { var _a; const sellerId = (_a = params.seller) === null || _a === void 0 ? void 0 : _a.id; if (typeof sellerId !== 'string' || sellerId.length === 0) { throw new factory.errors.ArgumentNull('seller.id'); } const doc = yield this.productModel.findOneAndUpdate({ 'project.id': { $eq: params.project.id }, _id: { $eq: params.itemOffered.id }, 'offers.seller.id': { $exists: true, $eq: sellerId } }, Object.assign(Object.assign(Object.assign(Object.assign({}, (params.availabilityEnds instanceof Date) ? { 'offers.$[offerBySeller].availabilityEnds': params.availabilityEnds } : undefined), (params.availabilityStarts instanceof Date) ? { 'offers.$[offerBySeller].availabilityStarts': params.availabilityStarts } : undefined), (params.validFrom instanceof Date) ? { 'offers.$[offerBySeller].validFrom': params.validFrom } : undefined), (params.validThrough instanceof Date) ? { 'offers.$[offerBySeller].validThrough': params.validThrough } : undefined), { new: true, arrayFilters: [ { 'offerBySeller.seller.id': { $eq: sellerId } } ], projection: { _id: 1 } }) .exec(); if (doc === null) { throw new factory.errors.NotFound('Product'); } return doc.toObject(); }); } deleteOne(params) { return __awaiter(this, void 0, void 0, function* () { var _a; const sellerId = (_a = params.seller) === null || _a === void 0 ? void 0 : _a.id; if (typeof sellerId !== 'string' || sellerId.length === 0) { throw new factory.errors.ArgumentNull('seller.id'); } const doc = yield this.productModel.findOneAndUpdate({ 'project.id': { $eq: params.project.id }, _id: { $eq: params.itemOffered.id }, 'offers.seller.id': { $exists: true, $eq: sellerId } }, { $pull: { offers: { 'seller.id': { $exists: true, $eq: sellerId } } } }, { projection: { _id: 1 } }) .exec(); if (doc === null) { throw new factory.errors.NotFound('Product'); } return doc.toObject(); }); } deleteManyBySellerId(params) { return __awaiter(this, void 0, void 0, function* () { var _a; const sellerId = (_a = params.seller) === null || _a === void 0 ? void 0 : _a.id; if (typeof sellerId !== 'string' || sellerId.length === 0) { throw new factory.errors.ArgumentNull('seller.id'); } return this.productModel.updateMany({ 'project.id': { $eq: params.project.id }, 'offers.seller.id': { $exists: true, $eq: sellerId } }, { $pull: { offers: { 'seller.id': { $exists: true, $eq: sellerId } } } }) .exec(); }); } } exports.ProductOfferRepo = ProductOfferRepo;