@chevre/domain
Version:
Chevre Domain Library for Node.js
211 lines (210 loc) • 10.4 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.ProjectRepo = void 0;
const project_1 = require("./mongoose/schemas/project");
const factory = require("../factory");
const settings_1 = require("../settings");
const AVAILABLE_PROJECT_FIELDS = [
// 'aggregateReservation',
'alternateName',
'logo',
'hasMerchantReturnPolicy',
// 'makesOffer',
'name',
'settings',
'subscription',
'typeOf'
];
/**
* プロジェクトリポジトリ
* makesOfferについてはsee ProjectMakesOfferRepo
*/
class ProjectRepo {
constructor(connection) {
this.projectModel = connection.model(project_1.modelName, (0, project_1.createSchema)());
}
static CREATE_MONGO_CONDITIONS(params) {
var _a;
const andConditions = [];
const idEq = (_a = params.id) === null || _a === void 0 ? void 0 : _a.$eq;
if (typeof idEq === 'string') {
andConditions.push({ _id: { $eq: idEq } });
}
if (Array.isArray(params.ids)) {
andConditions.push({ _id: { $in: params.ids } });
}
if (typeof params.name === 'string' && params.name.length > 0) {
andConditions.push({
$or: [
{ alternateName: { $exists: true, $regex: new RegExp(params.name) } },
{ name: { $exists: true, $regex: new RegExp(params.name) } }
]
});
}
return andConditions;
}
findById(params) {
return __awaiter(this, void 0, void 0, function* () {
const project = (yield this.projectFields({
limit: 1,
page: 1,
id: { $eq: params.id }
}, params.inclusion)).shift();
if (project === undefined) {
throw new factory.errors.NotFound(this.projectModel.modelName);
}
return project;
});
}
findAlternateNameById(params) {
return __awaiter(this, void 0, void 0, function* () {
const doc = yield this.projectModel.findOne({ _id: { $eq: params.id } }, { alternateName: 1, _id: 0 })
.lean()
.exec();
if (doc === null) {
throw new factory.errors.NotFound(this.projectModel.modelName);
}
return doc;
});
}
/**
* プロジェクト検索
*/
projectFields(conditions, inclusion) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const andConditions = ProjectRepo.CREATE_MONGO_CONDITIONS(conditions);
let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS;
if (Array.isArray(inclusion) && inclusion.length > 0) {
positiveProjectionFields = inclusion.filter((key) => AVAILABLE_PROJECT_FIELDS.includes(key));
}
else {
// discontinue(2024-10-09~)
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.projectModel.find((andConditions.length > 0) ? { $and: andConditions } : {}, projection);
if (typeof conditions.limit === 'number' && conditions.limit > 0) {
const page = (typeof conditions.page === 'number' && conditions.page > 0) ? conditions.page : 1;
query.limit(conditions.limit)
.skip(conditions.limit * (page - 1));
}
if (((_a = conditions.sort) === null || _a === void 0 ? void 0 : _a._id) !== undefined) {
query.sort({ _id: conditions.sort._id });
}
return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
.lean() // 2024-08-22~
.exec();
});
}
createById(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const { id } = params, createParams = __rest(params, ["id"]);
if (typeof id !== 'string' || id === '') {
throw new factory.errors.ArgumentNull('id');
}
const result = yield this.projectModel.insertMany(Object.assign(Object.assign({}, createParams), { typeOf: factory.organizationType.Project, _id: id }), { rawResult: true });
const insertedId = (_a = result.insertedIds) === null || _a === void 0 ? void 0 : _a[0];
if (typeof insertedId !== 'string') {
throw new factory.errors.Internal(`project not saved unexpectedly. result:${JSON.stringify(result)}`);
}
return { id: insertedId };
});
}
updateById(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f;
let hasMerchantReturnPolicy;
if (typeof ((_a = params.hasMerchantReturnPolicy) === null || _a === void 0 ? void 0 : _a.sameAs) === 'string') {
hasMerchantReturnPolicy = Object.assign({ sameAs: params.hasMerchantReturnPolicy.sameAs, typeOf: 'MerchantReturnPolicy' }, (typeof params.hasMerchantReturnPolicy.identifier === 'string')
? { identifier: params.hasMerchantReturnPolicy.identifier } : undefined);
}
yield this.projectModel.findOneAndUpdate({ _id: { $eq: params.id } }, Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (typeof params.alternateName === 'string' && params.alternateName.length > 0)
? { alternateName: params.alternateName }
: undefined), (typeof params.name === 'string' && params.name.length > 0) ? { name: params.name } : undefined), (typeof params.logo === 'string' && params.logo.length > 0) ? { logo: params.logo } : undefined), (typeof ((_d = (_c = (_b = params.settings) === null || _b === void 0 ? void 0 : _b.sendEmailMessage) === null || _c === void 0 ? void 0 : _c.sender) === null || _d === void 0 ? void 0 : _d.email) === 'string')
? {
'settings.sendEmailMessage': {
sender: { email: params.settings.sendEmailMessage.sender.email }
}
}
: undefined), (typeof ((_e = params.settings) === null || _e === void 0 ? void 0 : _e.sendgridApiKey) === 'string')
? { 'settings.sendgridApiKey': params.settings.sendgridApiKey }
: undefined), (typeof ((_f = params.subscription) === null || _f === void 0 ? void 0 : _f.useEventServiceAsProduct) === 'boolean')
? { 'subscription.useEventServiceAsProduct': params.subscription.useEventServiceAsProduct }
: undefined), (hasMerchantReturnPolicy !== undefined) ? { hasMerchantReturnPolicy } : undefined), { $unset: {
'settings.cognito': 1 // 廃止(2023-11-10~)
} }), { projection: { _id: 1 } })
.exec();
});
}
updateMakesOfferById(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.projectModel.findOneAndUpdate({ _id: { $eq: params.id } }, {
$set: { makesOffer: params.makesOffer }
}, { projection: { _id: 1 } })
.exec();
});
}
activateOptionalAggregationSettings(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.projectModel.findOneAndUpdate({ _id: { $eq: params.id } }, {
$set: {
'settings.useAggregateEntranceGate': true,
'settings.useAggregateOffer': true,
'settings.useOfferRateLimit': true
}
}, { projection: { _id: 1 } })
.exec();
});
}
updateAggregateReservation(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.projectModel.findOneAndUpdate({ _id: { $eq: params.id } }, {
$set: Object.assign({}, (params.aggregateReservation !== undefined) ? { aggregateReservation: params.aggregateReservation } : undefined)
// $unset: {
// noExistingAttributeName: 1 // $unsetは空だとエラーになるので
// }
}, { new: true, projection: { _id: 1 } })
.exec();
});
}
deleteById(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.projectModel.deleteOne({ _id: { $eq: params.id } }, { projection: { _id: 1 } })
.exec();
});
}
getCursor(conditions, projection) {
return this.projectModel.find(conditions, projection)
.sort({ _id: factory.sortType.Ascending })
.cursor();
}
unsetUnnecessaryFields(params) {
return __awaiter(this, void 0, void 0, function* () {
return this.projectModel.updateMany(params.filter, { $unset: params.$unset }, { timestamps: false })
.exec();
});
}
}
exports.ProjectRepo = ProjectRepo;
;