not-node 5.1.30 → 5.1.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "not-node",
3
- "version": "5.1.30",
3
+ "version": "5.1.31",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,31 @@
1
+ //DB related validation tools
2
+ const Form = require("../form/form");
3
+ const { firstLetterToUpper, firstLetterToLower } = require("../common");
4
+ //not-node
5
+ const { getIP } = require("../auth");
6
+ //form
7
+ const FIELDS = [
8
+ ["targetID", { required: true }, "not-node//ID"],
9
+ ["activeUserId", { required: true }, "not-node//objectId"],
10
+ ["activeUser", "not-node//requiredObject"],
11
+ ["ip", "not-node//ip"],
12
+ ];
13
+
14
+ module.exports = ({ MODULE_NAME, MODEL_NAME, actionName }) => {
15
+ const FORM_NAME = `${MODULE_NAME}:${firstLetterToUpper(actionName)}Form`;
16
+ return class extends Form {
17
+ constructor({ app }) {
18
+ super({ FIELDS, FORM_NAME, app });
19
+ }
20
+
21
+ extract(req) {
22
+ const incField = `${firstLetterToLower(MODEL_NAME)}ID`;
23
+ return {
24
+ targetId: req.params[incField],
25
+ activeUser: req.user,
26
+ activeUserId: req.user._id,
27
+ ip: getIP(req),
28
+ };
29
+ }
30
+ };
31
+ };
@@ -307,6 +307,100 @@ module.exports = ({
307
307
  });
308
308
  }
309
309
 
310
+ /**
311
+ * get item with populated sub-documents
312
+ * @param {Object} prepared
313
+ * @param {Object} prepared.targetID target item ID
314
+ * @param {Object} prepared.activeUser current user info
315
+ * @param {string} prepared.ip current user ip
316
+ * @param {boolean} prepared.root current user is root
317
+ * @param {boolean} prepared.shouldOwn if user should be owner of target
318
+ * @returns {Promise<Object>} requested document
319
+ **/
320
+ static async _getOneByID({
321
+ targetID,
322
+ action,
323
+ ip,
324
+ root,
325
+ activeUser,
326
+ shouldOwn = true,
327
+ }) {
328
+ Log.debug(
329
+ `${MODULE_NAME}//Logic//${MODEL_NAME}//${action}`,
330
+ targetID,
331
+ ip,
332
+ root
333
+ );
334
+ let query = {};
335
+ if (shouldOwn) {
336
+ query[ownerFieldName] = activeUser._id;
337
+ }
338
+ let populate = getPopulate(action, {
339
+ targetID,
340
+ activeUser,
341
+ ip,
342
+ root,
343
+ });
344
+ const result = await getModel().getOneByID(
345
+ targetID,
346
+ query,
347
+ populate
348
+ );
349
+ LogAction(
350
+ {
351
+ action,
352
+ by: activeUser._id,
353
+ role: activeUser.role,
354
+ ip,
355
+ },
356
+ {
357
+ targetID,
358
+ version: result.__version,
359
+ }
360
+ );
361
+ return result;
362
+ }
363
+
364
+ /**
365
+ * get item with populated sub-documents
366
+ * @param {Object} prepared
367
+ * @param {Object} prepared.targetID target item ID
368
+ * @param {Object} prepared.activeUser current user info
369
+ * @param {string} prepared.ip current user ip
370
+ * @param {boolean} prepared.root current user is root
371
+ * @returns {Promise<Object>} requested document
372
+ **/
373
+ static async getByID({ targetID, activeUser, ip, root = false }) {
374
+ return await this._getOneByID({
375
+ targetID,
376
+ action: "getByID",
377
+ activeUser,
378
+ ip,
379
+ root,
380
+ shouldOwn: false,
381
+ });
382
+ }
383
+
384
+ /**
385
+ * get activeUser own item with populated sub-documents
386
+ * @param {Object} prepared
387
+ * @param {Object} prepared.targetID target item ID
388
+ * @param {Object} prepared.activeUser current user info
389
+ * @param {string} prepared.ip current user ip
390
+ * @param {boolean} prepared.root current user is root
391
+ * @returns {Promise<Object>} requested document
392
+ **/
393
+ static async getByIDOwn({ targetID, activeUser, ip, root = false }) {
394
+ return await this._getOneByID({
395
+ targetID,
396
+ action: "getByIDOwn",
397
+ activeUser,
398
+ ip,
399
+ root,
400
+ shouldOwn: true,
401
+ });
402
+ }
403
+
310
404
  /**
311
405
  * get item without populated sub-documents
312
406
  * @param {Object} prepared
@@ -26,6 +26,15 @@ module.exports = ({ getLogic, before, after }) => {
26
26
  return await getLogic().getOwn(prepared);
27
27
  }
28
28
 
29
+ static async _getByID(req, res, next, prepared) {
30
+ prepared.root = true;
31
+ return await getLogic().getByID(prepared);
32
+ }
33
+
34
+ static async getByID(req, res, next, prepared) {
35
+ return await getLogic().getByIDOwn(prepared);
36
+ }
37
+
29
38
  static async _getRaw(req, res, next, prepared) {
30
39
  prepared.root = true;
31
40
  return await getLogic().getRaw(prepared);
@@ -111,9 +111,10 @@ function getOne(id, population = [], condition = {}) {
111
111
  * @static
112
112
  * @param {number} ID some unique numeric identificator
113
113
  * @param {Object} condition optional if needed additional condition
114
+ * @param {Array} population optional if needed population of some fields
114
115
  * @return {Promise} Promise of document, if increment is OFF - then Promise.resolve(null)
115
116
  **/
116
- function getOneByID(ID, condition = {}) {
117
+ function getOneByID(ID, condition = {}, population = []) {
117
118
  if (this.schema.statics.__incField) {
118
119
  let by, query;
119
120
  if (this.schema.statics.__versioning) {
@@ -129,6 +130,7 @@ function getOneByID(ID, condition = {}) {
129
130
  }
130
131
  by[this.schema.statics.__incField] = ID;
131
132
  query = this.findOne(by);
133
+ populateQuery(query, population, this.schema.statics.__versioning);
132
134
  return query.exec();
133
135
  } else {
134
136
  return Promise.resolve(null);