@chevre/domain
Version:
Chevre Domain Library for Node.js
133 lines (132 loc) • 6.03 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SellerPaymentAcceptedRepo = void 0;
const mongoose_1 = require("mongoose");
const seller_1 = require("./mongoose/schemas/seller");
const factory = require("../factory");
const settings_1 = require("../settings");
/**
* 販売者対応決済方法リポジトリ
*/
class SellerPaymentAcceptedRepo {
constructor(connection) {
this.sellerModel = connection.model(seller_1.modelName, (0, seller_1.createSchema)());
}
search(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e;
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 sellerIdEq = (_d = (_c = params.seller) === null || _c === void 0 ? void 0 : _c.id) === null || _d === void 0 ? void 0 : _d.$eq;
if (typeof sellerIdEq === 'string') {
matchStages.push({ $match: { _id: { $eq: new mongoose_1.Types.ObjectId(sellerIdEq) } } });
}
const codeValueEq = (_e = params.codeValue) === null || _e === void 0 ? void 0 : _e.$eq;
if (typeof codeValueEq === 'string') {
matchStages.push({ $match: { 'paymentAccepted.paymentMethodType': { $exists: true, $eq: codeValueEq } } });
}
const aggregate = this.sellerModel.aggregate([
{
$unwind: {
path: '$paymentAccepted'
}
},
...matchStages,
{ $sort: { 'paymentAccepted.paymentMethodType': factory.sortType.Ascending } },
{
$project: {
_id: 0,
codeValue: '$paymentAccepted.paymentMethodType',
seller: { id: { $toString: '$_id' } }
}
}
]);
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* () {
let doc = yield this.sellerModel.findOne({
'project.id': { $eq: params.project.id },
_id: { $eq: params.seller.id }
}, { _id: 1 })
.exec();
if (doc === null) {
throw new factory.errors.NotFound(factory.organizationType.Corporation);
}
const creatingPaymentAccepted = {
paymentMethodType: params.codeValue
};
doc = yield this.sellerModel.findOneAndUpdate({
'project.id': { $eq: params.project.id },
_id: { $eq: params.seller.id },
'paymentAccepted.paymentMethodType': { $ne: params.codeValue }
}, {
$push: { paymentAccepted: creatingPaymentAccepted }
}, {
projection: { _id: 1 }
})
.exec();
if (doc === null) {
throw new factory.errors.AlreadyInUse('paymentAccepted', ['paymentMethodType']);
}
});
}
deleteOne(params) {
return __awaiter(this, void 0, void 0, function* () {
const doc = yield this.sellerModel.findOneAndUpdate({
'project.id': { $eq: params.project.id },
_id: { $eq: params.seller.id },
'paymentAccepted.paymentMethodType': { $exists: true, $eq: params.codeValue }
}, {
$pull: { paymentAccepted: { paymentMethodType: params.codeValue } }
}, {
projection: { _id: 1 }
})
.exec();
if (doc === null) {
throw new factory.errors.NotFound(factory.organizationType.Corporation);
}
});
}
isAcceptedBySeller(params) {
return __awaiter(this, void 0, void 0, function* () {
const aggregate = this.sellerModel.aggregate([
{
$unwind: { path: '$paymentAccepted' }
},
{ $match: { _id: { $eq: new mongoose_1.Types.ObjectId(params.seller.id) } } },
{ $match: { 'paymentAccepted.paymentMethodType': { $exists: true, $eq: params.codeValue } } },
{
$project: {
_id: 0,
codeValue: '$paymentAccepted.paymentMethodType'
}
}
])
.limit(1)
.skip(0);
return (yield aggregate.option({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
.exec()).length > 0;
});
}
}
exports.SellerPaymentAcceptedRepo = SellerPaymentAcceptedRepo;