@chevre/domain
Version:
Chevre Domain Library for Node.js
166 lines (165 loc) • 8.52 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());
});
};
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.WebSiteRepo = void 0;
const uuid = require("uuid");
const factory = require("../factory");
const settings_1 = require("../settings");
const webSite_1 = require("./mongoose/schemas/webSite");
/**
* web site repository
*/
class WebSiteRepo {
constructor(connection) {
this.webSiteModel = connection.model(webSite_1.modelName, (0, webSite_1.createSchema)());
}
// tslint:disable-next-line:max-func-body-length
static CREATE_FILTER_QUERY(params) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
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 projectIdIn = (_d = (_c = params.project) === null || _c === void 0 ? void 0 : _c.id) === null || _d === void 0 ? void 0 : _d.$in;
if (Array.isArray(projectIdIn)) {
andConditions.push({ 'project.id': { $in: projectIdIn } });
}
const idEq = (_e = params.id) === null || _e === void 0 ? void 0 : _e.$eq;
if (typeof idEq === 'string') {
andConditions.push({ _id: { $eq: idEq } });
}
const idIn = (_f = params.id) === null || _f === void 0 ? void 0 : _f.$in;
if (Array.isArray(idIn)) {
andConditions.push({ _id: { $in: idIn } });
}
const aboutIdentifierEq = (_h = (_g = params.about) === null || _g === void 0 ? void 0 : _g.identifier) === null || _h === void 0 ? void 0 : _h.$eq;
if (typeof aboutIdentifierEq === 'string') {
andConditions.push({ 'about.identifier': { $eq: aboutIdentifierEq } });
}
const aboutIdentifierRegex = (_k = (_j = params.about) === null || _j === void 0 ? void 0 : _j.identifier) === null || _k === void 0 ? void 0 : _k.$regex;
if (typeof aboutIdentifierRegex === 'string' && aboutIdentifierRegex.length > 0) {
andConditions.push({ 'about.identifier': { $regex: new RegExp(aboutIdentifierRegex) } });
}
return andConditions;
}
save(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
let doc;
let savedId;
const savingId = params.id;
if (typeof savingId === 'string') {
if (savingId === '') {
throw new factory.errors.ArgumentNull('id');
}
const _c = params.attributes, { id, datePublished, dateModified, about, project, certificationIdentification, $unset } = _c, updateFields = __rest(_c, ["id", "datePublished", "dateModified", "about", "project", "certificationIdentification", "$unset"]);
const filter = {
_id: { $eq: savingId },
'project.id': { $eq: project.id }
};
const update = Object.assign({ $set: Object.assign(Object.assign({}, updateFields), { dateModified: new Date() }) }, ($unset !== undefined && $unset !== null) ? { $unset } : undefined);
const options = {
upsert: false,
new: true,
projection: { _id: 1, id: { $toString: '$_id' } }
};
doc = yield this.webSiteModel.findOneAndUpdate(filter, update, options)
.lean()
.exec();
if (doc === null) {
throw new factory.errors.NotFound(this.webSiteModel.modelName);
}
savedId = savingId;
}
else {
const _d = params.attributes, { $unset, id, dateModified, datePublished, auditDate } = _d, createParams = __rest(_d, ["$unset", "id", "dateModified", "datePublished", "auditDate"]);
const certificationIdentification = uuid.v4();
const creatingCertification = Object.assign(Object.assign({}, createParams), { certificationIdentification, datePublished: new Date(), typeOf: factory.creativeWorkType.Certification });
const result = yield this.webSiteModel.insertMany(creatingCertification, { rawResult: true });
const insertedId = (_b = (_a = result.insertedIds) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.toHexString();
if (typeof insertedId !== 'string') {
throw new factory.errors.Internal(`not saved unexpectedly. result:${JSON.stringify(result)}`);
}
savedId = insertedId;
}
return { id: savedId };
});
}
certifyById(params) {
return __awaiter(this, void 0, void 0, function* () {
const now = new Date();
const { id, project } = params;
const filter = {
_id: { $eq: id },
'project.id': { $eq: project.id }
};
const update = {
$set: {
auditDate: now,
dateModified: now,
certificationStatus: factory.CertificationStatusEnumeration.CertificationActive
}
};
const options = {
upsert: false,
new: true,
projection: { _id: 1, id: { $toString: '$_id' } }
};
const doc = yield this.webSiteModel.findOneAndUpdate(filter, update, options)
.lean()
.exec();
if (doc === null) {
throw new factory.errors.NotFound(this.webSiteModel.modelName);
}
});
}
projectFields(conditions, inclusion) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const andConditions = WebSiteRepo.CREATE_FILTER_QUERY(conditions);
const projection = Object.assign({ _id: 0, id: { $toString: '$_id' } }, Object.fromEntries(inclusion.map((key) => ([key, 1]))));
const query = this.webSiteModel.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 (typeof ((_a = conditions.sort) === null || _a === void 0 ? void 0 : _a['about.identifier']) === 'number') {
query.sort({ 'about.identifier': conditions.sort['about.identifier'] });
}
return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
.lean()
.exec();
});
}
deleteById(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.webSiteModel.findOneAndDelete({
_id: { $eq: params.id },
'project.id': { $eq: params.project.id }
}, { projection: { _id: 1 } })
.exec();
});
}
}
exports.WebSiteRepo = WebSiteRepo;