@chevre/domain
Version:
Chevre Domain Library for Node.js
174 lines (173 loc) • 7.46 kB
JavaScript
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.MerchantReturnPolicyRepo = void 0;
const merchantReturnPolicy_1 = require("./mongoose/schemas/merchantReturnPolicy");
const factory = require("../factory");
const settings_1 = require("../settings");
/**
* 返品ポリシーリポジトリ
*/
class MerchantReturnPolicyRepo {
constructor(connection) {
this.merchantReturnPolicyModel = connection.model(merchantReturnPolicy_1.modelName, (0, merchantReturnPolicy_1.createSchema)());
}
// tslint:disable-next-line:max-func-body-length
static CREATE_MONGO_CONDITIONS(params) {
var _a, _b, _c, _d, _e, _f, _g, _h;
// MongoDB検索条件
const andConditions = [];
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 idEq = (_c = params.id) === null || _c === void 0 ? void 0 : _c.$eq;
if (typeof idEq === 'string') {
andConditions.push({
_id: { $eq: idEq }
});
}
const idIn = (_d = params.id) === null || _d === void 0 ? void 0 : _d.$in;
if (Array.isArray(idIn)) {
andConditions.push({
_id: { $in: idIn }
});
}
const identifierEq = (_e = params.identifier) === null || _e === void 0 ? void 0 : _e.$eq;
if (typeof identifierEq === 'string') {
andConditions.push({
identifier: { $eq: identifierEq }
});
}
const identifierIn = (_f = params.identifier) === null || _f === void 0 ? void 0 : _f.$in;
if (Array.isArray(identifierIn)) {
andConditions.push({
identifier: { $in: identifierIn }
});
}
const identifierRegex = (_g = params.identifier) === null || _g === void 0 ? void 0 : _g.$regex;
if (typeof identifierRegex === 'string' && identifierRegex.length > 0) {
andConditions.push({
identifier: { $regex: new RegExp(identifierRegex) }
});
}
const nameRegex = (_h = params.name) === null || _h === void 0 ? void 0 : _h.$regex;
if (typeof nameRegex === 'string' && nameRegex.length > 0) {
andConditions.push({
$or: [
{
'name.ja': {
$exists: true,
$regex: new RegExp(nameRegex)
}
},
{
'name.en': {
$exists: true,
$regex: new RegExp(nameRegex)
}
}
]
});
}
return andConditions;
}
save(params) {
return __awaiter(this, void 0, void 0, function* () {
let doc;
if (params.id === '') {
delete params.id;
doc = yield this.merchantReturnPolicyModel.create(params);
}
else {
// 上書き禁止属性を除外(2022-08-24~)
const { id, identifier, project, typeOf } = params, updateFields = __rest(params, ["id", "identifier", "project", "typeOf"]);
doc = yield this.merchantReturnPolicyModel.findOneAndUpdate({ _id: params.id }, updateFields, { upsert: false, new: true })
.exec();
}
if (doc === null) {
throw new factory.errors.NotFound(this.merchantReturnPolicyModel.modelName);
}
return doc.toObject();
});
}
findById(params) {
return __awaiter(this, void 0, void 0, function* () {
const doc = yield this.merchantReturnPolicyModel.findOne({ _id: params.id }, {
__v: 0,
createdAt: 0,
updatedAt: 0
})
.exec();
if (doc === null) {
throw new factory.errors.NotFound(this.merchantReturnPolicyModel.modelName);
}
return doc.toObject();
});
}
search(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const conditions = MerchantReturnPolicyRepo.CREATE_MONGO_CONDITIONS(params);
const query = this.merchantReturnPolicyModel.find((conditions.length > 0) ? { $and: conditions } : {}, {
__v: 0,
createdAt: 0,
updatedAt: 0
});
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.identifier) !== undefined) {
query.sort({ identifier: params.sort.identifier });
}
return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
.exec()
.then((docs) => docs.map((doc) => doc.toObject()));
});
}
deleteById(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.merchantReturnPolicyModel.findByIdAndDelete({ _id: params.id })
.exec();
});
}
deleteByProject(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.merchantReturnPolicyModel.deleteMany({
'project.id': { $eq: params.project.id }
})
.exec();
});
}
getCursor(conditions, projection) {
return this.merchantReturnPolicyModel.find(conditions, projection)
.sort({ identifier: factory.sortType.Descending })
.cursor();
}
}
exports.MerchantReturnPolicyRepo = MerchantReturnPolicyRepo;
;