not-node 6.5.20 → 6.5.22
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/eslint.config.cjs +2 -2
- package/package.json +3 -3
- package/src/auth/const.js +4 -0
- package/src/common.js +10 -0
- package/src/core/locales/ru.json +1 -0
- package/src/exceptions/form.js +13 -0
- package/src/fields/filter.js +6 -1
- package/src/generic/form.js +2 -1
- package/src/generic/forms/form._data.js +31 -5
- package/src/generic/forms/form.list.js +33 -19
- package/src/generic/forms/form.listAndCount.js +6 -61
- package/src/logic/actions.before/index.js +1 -0
- package/src/logic/actions.before/ownage/ownage.js +93 -33
- package/src/logic/actions.before/populate/populate.js +10 -1
- package/src/logic/actions.before/standart.queries.js +5 -3
- package/src/logic/generic.js +25 -1
- package/src/logic/logic.js +18 -15
package/eslint.config.cjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "not-node",
|
|
3
|
-
"version": "6.5.
|
|
3
|
+
"version": "6.5.22",
|
|
4
4
|
"description": "node complimentary part for client side notFramework.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"watch:build:cover:dev": "npm-run-all --parallel js-watch",
|
|
13
13
|
"cover": "nyc npm test",
|
|
14
14
|
"clear:playground": "rm -rf ./playground",
|
|
15
|
-
"prepare": "husky
|
|
15
|
+
"prepare": "husky"
|
|
16
16
|
},
|
|
17
17
|
"bin": {
|
|
18
18
|
"not-node": "bin/not-node.js",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"mongoose-validator": "*",
|
|
60
60
|
"nconf": "*",
|
|
61
61
|
"not-config": "*",
|
|
62
|
-
"not-filter": "
|
|
62
|
+
"not-filter": "^0.3.15",
|
|
63
63
|
"not-inform": "*",
|
|
64
64
|
"not-locale": "^0.0.22",
|
|
65
65
|
"not-log": "*",
|
package/src/auth/const.js
CHANGED
|
@@ -29,7 +29,10 @@ const METHOD_SIGNAURES = {
|
|
|
29
29
|
|
|
30
30
|
const OBJECT_STRING = "[object String]";
|
|
31
31
|
|
|
32
|
+
//document owned by registered user
|
|
32
33
|
const DOCUMENT_OWNER_FIELD_NAME = "owner";
|
|
34
|
+
//document owned by guest user
|
|
35
|
+
const DOCUMENT_SESSION_FIELD_NAME = "session";
|
|
33
36
|
const TOKEN_TTL = 3600;
|
|
34
37
|
|
|
35
38
|
module.exports = {
|
|
@@ -39,6 +42,7 @@ module.exports = {
|
|
|
39
42
|
DEFAULT_USER_ROLE_FOR_ROOT,
|
|
40
43
|
DEFAULT_USER_ROLE_FOR_ADMIN,
|
|
41
44
|
DOCUMENT_OWNER_FIELD_NAME,
|
|
45
|
+
DOCUMENT_SESSION_FIELD_NAME,
|
|
42
46
|
ACTION_SIGNATURES,
|
|
43
47
|
METHOD_SIGNAURES,
|
|
44
48
|
};
|
package/src/common.js
CHANGED
|
@@ -486,3 +486,13 @@ const findSignature = (obj, signatures, strict = true, typeStrict = true) => {
|
|
|
486
486
|
});
|
|
487
487
|
};
|
|
488
488
|
module.exports.findSignature = findSignature;
|
|
489
|
+
|
|
490
|
+
const isAction = (action) =>
|
|
491
|
+
action &&
|
|
492
|
+
(typeof action.run === "function" || typeof action === "function");
|
|
493
|
+
|
|
494
|
+
module.exports.isAction = isAction;
|
|
495
|
+
|
|
496
|
+
const isArrayOfActions = (list) => list.every(isAction);
|
|
497
|
+
|
|
498
|
+
module.exports.isArrayOfActions = isArrayOfActions;
|
package/src/core/locales/ru.json
CHANGED
|
@@ -76,5 +76,6 @@
|
|
|
76
76
|
"select_from_list_label": "Выберите из списка...",
|
|
77
77
|
"field_actions_label": "Действия",
|
|
78
78
|
"form_exception_field_extractor_is_undefined": "Экстрактор для поля входных данных отсутствует",
|
|
79
|
+
"form_exception_identity_or_query_is_undefined": "Identity или Query отсутствуют",
|
|
79
80
|
"versioning_error_same_old_data": "Данные не изменились, сохранение отклонено."
|
|
80
81
|
}
|
package/src/exceptions/form.js
CHANGED
|
@@ -34,3 +34,16 @@ class FormExceptionTooManyRequests extends HttpExceptionTooManyRequests {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
module.exports.FormExceptionTooManyRequests = FormExceptionTooManyRequests;
|
|
37
|
+
|
|
38
|
+
Error("no prepared identity or query");
|
|
39
|
+
|
|
40
|
+
class FormExceptionIdentityOrQueryIsUndefined extends notRequestError {
|
|
41
|
+
constructor(formName) {
|
|
42
|
+
super("not-node:form_exception_identity_or_query_is_undefined", {
|
|
43
|
+
params: { formName },
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
module.exports.FormExceptionIdentityOrQueryIsUndefined =
|
|
49
|
+
FormExceptionIdentityOrQueryIsUndefined;
|
package/src/fields/filter.js
CHANGED
|
@@ -8,6 +8,7 @@ const { getSafeFieldsForRoleAction } = require("../auth/fields");
|
|
|
8
8
|
const {
|
|
9
9
|
DEFAULT_USER_ROLE_FOR_GUEST,
|
|
10
10
|
ACTION_SIGNATURES,
|
|
11
|
+
DOCUMENT_SESSION_FIELD_NAME,
|
|
11
12
|
} = require("../auth/const");
|
|
12
13
|
/**
|
|
13
14
|
* notFieldsFilter.filter(fields, getApp().getModelSchema(MODEL_NAME), {action});
|
|
@@ -144,7 +145,11 @@ class notFieldsFilter {
|
|
|
144
145
|
system
|
|
145
146
|
);
|
|
146
147
|
},
|
|
147
|
-
[SPECIAL_SET_UNSAFE]: [
|
|
148
|
+
[SPECIAL_SET_UNSAFE]: [
|
|
149
|
+
"salt",
|
|
150
|
+
"password",
|
|
151
|
+
DOCUMENT_SESSION_FIELD_NAME,
|
|
152
|
+
],
|
|
148
153
|
[SPECIAL_SET_TIMESTAMPS]: ["createdAt", "updatedAt"],
|
|
149
154
|
[SPECIAL_SET_OWNAGE]: (schema) => {
|
|
150
155
|
const inSchema = Object.keys(schema);
|
package/src/generic/form.js
CHANGED
|
@@ -15,6 +15,7 @@ function createDefaultInstance({
|
|
|
15
15
|
MODULE_NAME,
|
|
16
16
|
MODEL_NAME,
|
|
17
17
|
actionName,
|
|
18
|
+
config,
|
|
18
19
|
/* validators = [],
|
|
19
20
|
dataValidators = [],*/
|
|
20
21
|
}) {
|
|
@@ -23,7 +24,7 @@ function createDefaultInstance({
|
|
|
23
24
|
["data", `${MODULE_NAME}//_${Common.firstLetterToLower(MODEL_NAME)}`],
|
|
24
25
|
];
|
|
25
26
|
const FORM_NAME = Form.createName(MODULE_NAME, MODEL_NAME, actionName);
|
|
26
|
-
return new Form({ FIELDS, FORM_NAME, app, MODULE_NAME });
|
|
27
|
+
return new Form({ FIELDS, FORM_NAME, app, MODULE_NAME, config });
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
module.exports = createDefaultInstance;
|
|
@@ -5,9 +5,16 @@ const Form = require("../../form/form");
|
|
|
5
5
|
* @param {object} param0
|
|
6
6
|
* @param {string} param0.MODULE_NAME
|
|
7
7
|
* @param {string} param0.MODEL_NAME
|
|
8
|
-
* @param {string} param0.actionName
|
|
9
|
-
* @param {Array<function>} param0.validators
|
|
10
|
-
* @param {function} param0.afterExtract
|
|
8
|
+
* @param {string} [param0.actionName = '_data']
|
|
9
|
+
* @param {Array<function>} [param0.validators=[]]
|
|
10
|
+
* @param {function} [param0.afterExtract = async (input, req = null) => input || req]
|
|
11
|
+
* @param {import('../../types.js').notAppConfigReader} [param0.config]
|
|
12
|
+
* @param {Object.<string, Function>} [param0.EXTRACTORS]
|
|
13
|
+
* @param {Object.<string, Function>} [param0.TRANSFORMERS]
|
|
14
|
+
* @param {import('../../types.js').notAppFormProcessingPipe} [param0.INSTRUCTIONS]
|
|
15
|
+
* @param {Array<Function>} [param0.AFTER_EXTRACT_TRANSFORMERS]
|
|
16
|
+
* @param {Object.<string, import('../../types.js').notAppFormEnvExtractor>} [param0.ENV_EXTRACTORS]
|
|
17
|
+
* @param {import('../../types.js').notAppFormRateLimiterOptions} [param0.rate]
|
|
11
18
|
* @returns
|
|
12
19
|
*/
|
|
13
20
|
module.exports = ({
|
|
@@ -16,15 +23,34 @@ module.exports = ({
|
|
|
16
23
|
actionName = "_data",
|
|
17
24
|
validators = [],
|
|
18
25
|
afterExtract = async (input, req = null) => input || req,
|
|
26
|
+
EXTRACTORS = {},
|
|
27
|
+
ENV_EXTRACTORS = {},
|
|
28
|
+
TRANSFORMERS = {},
|
|
29
|
+
INSTRUCTIONS = undefined,
|
|
30
|
+
AFTER_EXTRACT_TRANSFORMERS = [],
|
|
31
|
+
rate = undefined,
|
|
19
32
|
}) => {
|
|
20
33
|
return class extends Form {
|
|
21
34
|
/**
|
|
22
35
|
*
|
|
23
36
|
* @param {object} param0
|
|
24
37
|
* @param {import('../../app')} param0.app
|
|
38
|
+
* @param {import('../../types.js').notAppConfigReader} param0.config
|
|
25
39
|
*/
|
|
26
|
-
constructor({ app }) {
|
|
27
|
-
super({
|
|
40
|
+
constructor({ app, config }) {
|
|
41
|
+
super({
|
|
42
|
+
app,
|
|
43
|
+
config,
|
|
44
|
+
MODULE_NAME,
|
|
45
|
+
MODEL_NAME,
|
|
46
|
+
actionName,
|
|
47
|
+
EXTRACTORS,
|
|
48
|
+
ENV_EXTRACTORS,
|
|
49
|
+
TRANSFORMERS,
|
|
50
|
+
INSTRUCTIONS,
|
|
51
|
+
AFTER_EXTRACT_TRANSFORMERS,
|
|
52
|
+
rate,
|
|
53
|
+
});
|
|
28
54
|
}
|
|
29
55
|
|
|
30
56
|
extract(data) {
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
const Form = require("../../form/form");
|
|
2
|
-
|
|
2
|
+
const {
|
|
3
|
+
DOCUMENT_OWNER_FIELD_NAME,
|
|
4
|
+
DOCUMENT_SESSION_FIELD_NAME,
|
|
5
|
+
} = require("../../auth/const");
|
|
3
6
|
const notFilter = require("not-filter");
|
|
4
|
-
const
|
|
5
|
-
|
|
7
|
+
const FormExceptions = require("../../exceptions/form");
|
|
6
8
|
const FIELDS = [
|
|
7
9
|
["query", `not-filter//_filterQuery`],
|
|
8
10
|
["identity", "not-node//identity"],
|
|
@@ -30,29 +32,41 @@ const FactoryFormList = ({ MODULE_NAME, MODEL_NAME, actionName = "list" }) => {
|
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
+
* Adds owner id or session to query.filter
|
|
36
|
+
* @param {import('../../types').PreparedData} prepared
|
|
35
37
|
* @param {import('../../types').notNodeExpressRequest} req
|
|
36
38
|
* @return {Promise<import('../../types').PreparedData>}
|
|
37
39
|
*/
|
|
38
|
-
async
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
async afterExtract(prepared, req) {
|
|
41
|
+
prepared = await super.afterExtract(prepared, req);
|
|
42
|
+
if (!prepared.identity || !prepared.query) {
|
|
43
|
+
throw new FormExceptions.FormExceptionIdentityOrQueryIsUndefined(
|
|
44
|
+
this.FORM_NAME
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
if (!prepared.query.filter) {
|
|
48
|
+
prepared.query.filter = notFilter.filter.createANDFilter();
|
|
49
|
+
}
|
|
50
|
+
if (
|
|
51
|
+
prepared.identity.auth &&
|
|
52
|
+
!prepared.identity.root &&
|
|
53
|
+
!prepared.identity.admin
|
|
54
|
+
) {
|
|
55
|
+
prepared.query.filter = notFilter.filter.modifyRules(
|
|
56
|
+
prepared.query.filter,
|
|
47
57
|
{
|
|
48
|
-
|
|
58
|
+
[DOCUMENT_OWNER_FIELD_NAME]: prepared.identity.uid,
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
} else if (!prepared.identity.auth && prepared.identity.sid) {
|
|
62
|
+
prepared.query.filter = notFilter.filter.modifyRules(
|
|
63
|
+
prepared.query.filter,
|
|
64
|
+
{
|
|
65
|
+
[DOCUMENT_SESSION_FIELD_NAME]: prepared.identity.sid,
|
|
49
66
|
}
|
|
50
67
|
);
|
|
51
68
|
}
|
|
52
|
-
|
|
53
|
-
return {
|
|
54
|
-
...envs,
|
|
55
|
-
};
|
|
69
|
+
return prepared;
|
|
56
70
|
}
|
|
57
71
|
};
|
|
58
72
|
};
|
|
@@ -1,63 +1,8 @@
|
|
|
1
|
-
const
|
|
1
|
+
const FactoryFormList = require("./form.list");
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
["identity", "not-node//identity"],
|
|
9
|
-
];
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Generates generic form to get perform list and count action
|
|
13
|
-
*
|
|
14
|
-
* @param {object} params
|
|
15
|
-
* @param {string} params.MODULE_NAME //module name
|
|
16
|
-
* @param {string} params.MODEL_NAME //model name
|
|
17
|
-
* @param {string} params.actionName //action name
|
|
18
|
-
* @return {Form} form class definition
|
|
19
|
-
*/
|
|
20
|
-
const FactoryFormListAndCount = ({
|
|
21
|
-
MODULE_NAME,
|
|
22
|
-
MODEL_NAME,
|
|
23
|
-
actionName = "listAndCount",
|
|
24
|
-
}) => {
|
|
25
|
-
return class extends Form {
|
|
26
|
-
constructor(params) {
|
|
27
|
-
super({
|
|
28
|
-
...params,
|
|
29
|
-
FIELDS,
|
|
30
|
-
MODULE_NAME,
|
|
31
|
-
MODEL_NAME,
|
|
32
|
-
actionName,
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* @param {import('../../types').notNodeExpressRequest} req
|
|
40
|
-
* @return {Promise<import('../../types').PreparedData>}
|
|
41
|
-
*/
|
|
42
|
-
async extract(req) {
|
|
43
|
-
const envs = this.extractRequestEnvs(req);
|
|
44
|
-
const user = notAppIdentity.extractAuthData(req);
|
|
45
|
-
if (user.auth && !user.root && !user.admin) {
|
|
46
|
-
if (!envs.query.filter) {
|
|
47
|
-
envs.query.filter = notFilter.filter.createFilter();
|
|
48
|
-
}
|
|
49
|
-
envs.query.filter = notFilter.filter.modifyRules(
|
|
50
|
-
envs.query.filter,
|
|
51
|
-
{
|
|
52
|
-
owner: user.uid,
|
|
53
|
-
}
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
return {
|
|
57
|
-
...envs,
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
};
|
|
3
|
+
module.exports = (params) => {
|
|
4
|
+
if (!params.actionName) {
|
|
5
|
+
params.actionName = "listAndCount";
|
|
6
|
+
}
|
|
7
|
+
return FactoryFormList(params);
|
|
61
8
|
};
|
|
62
|
-
|
|
63
|
-
module.exports = FactoryFormListAndCount;
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
const notFilter = require("not-filter");
|
|
2
|
-
const {
|
|
2
|
+
const {
|
|
3
|
+
DOCUMENT_OWNER_FIELD_NAME,
|
|
4
|
+
DOCUMENT_SESSION_FIELD_NAME,
|
|
5
|
+
} = require("../../../auth/const.js");
|
|
3
6
|
const {
|
|
4
7
|
OwnageExceptionIdentityUserIdIsNotDefined,
|
|
5
8
|
} = require("../../../exceptions/action.js");
|
|
@@ -8,50 +11,107 @@ const ModelRoutine = require("../../../model/routine.js");
|
|
|
8
11
|
//checks that
|
|
9
12
|
module.exports = class OwnageBeforeAction {
|
|
10
13
|
static #ownerFieldName = DOCUMENT_OWNER_FIELD_NAME;
|
|
14
|
+
static #sessionFieldName = DOCUMENT_SESSION_FIELD_NAME;
|
|
11
15
|
|
|
12
16
|
static get ownerFieldName() {
|
|
13
17
|
return this.#ownerFieldName;
|
|
14
18
|
}
|
|
15
19
|
|
|
16
|
-
static
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
search = notFilter.filter.modifyRules(search, filter);
|
|
28
|
-
}
|
|
20
|
+
static get sessionFieldName() {
|
|
21
|
+
return this.#sessionFieldName;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static setOwnage(logic, actionName, args, ownageFilter) {
|
|
25
|
+
const { query, targetId, targetID } = args;
|
|
26
|
+
let { filter, search } = query;
|
|
27
|
+
if (filter) {
|
|
28
|
+
filter = notFilter.filter.modifyRules(filter, ownageFilter);
|
|
29
|
+
if (search) {
|
|
30
|
+
search = notFilter.filter.modifyRules(search, filter);
|
|
29
31
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
}
|
|
33
|
+
args.defaultQueryById = {
|
|
34
|
+
_id: targetId,
|
|
35
|
+
...ownageFilter,
|
|
36
|
+
};
|
|
37
|
+
const Model = logic.getModel();
|
|
38
|
+
const incFieldName = ModelRoutine.incremental(Model);
|
|
39
|
+
if (incFieldName) {
|
|
40
|
+
args.defaultQueryByID = {
|
|
41
|
+
[incFieldName]: targetID,
|
|
42
|
+
...ownageFilter,
|
|
33
43
|
};
|
|
34
|
-
|
|
35
|
-
const incFieldName = ModelRoutine.incremental(Model);
|
|
36
|
-
if (incFieldName) {
|
|
37
|
-
args.defaultQueryByID = {
|
|
38
|
-
[incFieldName]: targetID,
|
|
39
|
-
[OwnageBeforeAction.ownerFieldName]: identity?.uid,
|
|
40
|
-
};
|
|
41
|
-
}
|
|
44
|
+
}
|
|
42
45
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
args.defaultQueryMany = {
|
|
47
|
+
...ownageFilter,
|
|
48
|
+
};
|
|
49
|
+
//mark data as owned by
|
|
50
|
+
if (typeof args.data == "object" && args.data) {
|
|
51
|
+
Object.assign(args.data, ownageFilter);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static createOwnageFilterForUser(identity) {
|
|
56
|
+
return Object.freeze({
|
|
57
|
+
[OwnageBeforeAction.ownerFieldName]: identity.uid,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
static createOwnageFilterForSession(identity) {
|
|
62
|
+
return Object.freeze({
|
|
63
|
+
[OwnageBeforeAction.sessionFieldName]: identity.sid,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Returns object with filtering conditions to restrict access by owner or session
|
|
69
|
+
*
|
|
70
|
+
* @static
|
|
71
|
+
* @param {import('../../../types.js').notAppIdentityData} identity
|
|
72
|
+
* @return {object}
|
|
73
|
+
*/
|
|
74
|
+
static getOwnageFilterForIdentity(identity) {
|
|
75
|
+
if (identity.uid) {
|
|
76
|
+
return OwnageBeforeAction.createOwnageFilterForUser(identity);
|
|
77
|
+
} else if (identity.sid) {
|
|
78
|
+
return OwnageBeforeAction.createOwnageFilterForSession(identity);
|
|
50
79
|
} else {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
*
|
|
86
|
+
*
|
|
87
|
+
* @static
|
|
88
|
+
* @param {import('../../logic.js')} logic
|
|
89
|
+
* @param {String} actionName
|
|
90
|
+
* @param {import('../../../types.js').PreparedData} args
|
|
91
|
+
*/
|
|
92
|
+
static async run(logic, actionName, args) {
|
|
93
|
+
if (!args.identity) {
|
|
51
94
|
throw new OwnageExceptionIdentityUserIdIsNotDefined(
|
|
52
95
|
actionName,
|
|
53
|
-
|
|
96
|
+
undefined
|
|
54
97
|
);
|
|
55
98
|
}
|
|
99
|
+
const ownageFilter = OwnageBeforeAction.getOwnageFilterForIdentity(
|
|
100
|
+
args.identity
|
|
101
|
+
);
|
|
102
|
+
if (ownageFilter === null) {
|
|
103
|
+
throw new OwnageExceptionIdentityUserIdIsNotDefined(
|
|
104
|
+
actionName,
|
|
105
|
+
args.identity
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
OwnageBeforeAction.setOwnage(logic, actionName, args, ownageFilter);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
static ifActionNameEndsWith_Own() {
|
|
112
|
+
return Object.freeze({
|
|
113
|
+
condition: (actionName) => actionName.endsWith("Own"),
|
|
114
|
+
action: OwnageBeforeAction,
|
|
115
|
+
});
|
|
56
116
|
}
|
|
57
117
|
};
|
|
@@ -1,3 +1,12 @@
|
|
|
1
1
|
module.exports = class PopulateBeforeAction {
|
|
2
|
-
static async run(
|
|
2
|
+
static async run(logic, actionName, args) {
|
|
3
|
+
try {
|
|
4
|
+
const { identity } = args;
|
|
5
|
+
args.populate = await logic.getPopulate(actionName, {
|
|
6
|
+
identity,
|
|
7
|
+
});
|
|
8
|
+
} catch (e) {
|
|
9
|
+
logic.log.error(e);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
3
12
|
};
|
|
@@ -5,9 +5,11 @@ module.exports = class StandartQueriesBeforeAction {
|
|
|
5
5
|
static async run(logic, actionName, args) {
|
|
6
6
|
try {
|
|
7
7
|
const { targetId, targetID } = args;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
if (targetId) {
|
|
9
|
+
args.defaultQueryById = {
|
|
10
|
+
_id: targetId,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
11
13
|
const Model = logic.getModel();
|
|
12
14
|
const incFieldName = ModelRoutine.incremental(Model);
|
|
13
15
|
if (
|
package/src/logic/generic.js
CHANGED
|
@@ -11,8 +11,15 @@ module.exports = ({
|
|
|
11
11
|
actionsSets = ["standart"],
|
|
12
12
|
actions = {},
|
|
13
13
|
beforeActions = {},
|
|
14
|
-
|
|
14
|
+
beforeActionsOnCondition = [
|
|
15
|
+
require("./actions.before/ownage/ownage.js").ifActionNameEndsWith_Own(),
|
|
16
|
+
], //each item = {condition: (actionName)=>boolean, action}
|
|
17
|
+
beforeActionsAll = [
|
|
18
|
+
require("./actions.before/standart.queries.js"),
|
|
19
|
+
require("./actions.before/populate/populate.js"),
|
|
20
|
+
],
|
|
15
21
|
afterActions = {},
|
|
22
|
+
afterActionsOnCondition = [], //each item = {condition: (actionName)=>boolean, action}
|
|
16
23
|
populateBuilders = {},
|
|
17
24
|
afterActionsAll = [],
|
|
18
25
|
defaultPopulate = [],
|
|
@@ -36,11 +43,28 @@ module.exports = ({
|
|
|
36
43
|
defaultPopulate,
|
|
37
44
|
});
|
|
38
45
|
|
|
46
|
+
//adds before/after to all
|
|
39
47
|
beforeActionsAll.forEach((action) => {
|
|
40
48
|
Logic.onBefore(undefined, action);
|
|
41
49
|
});
|
|
50
|
+
|
|
42
51
|
afterActionsAll.forEach((action) => Logic.onAfter(undefined, action));
|
|
43
52
|
|
|
53
|
+
//adds before/after to as many as satisfies condition
|
|
54
|
+
Object.keys(ACTIONS).forEach((actionName) => {
|
|
55
|
+
beforeActionsOnCondition.forEach(({ condition, action }) => {
|
|
56
|
+
if (condition(actionName)) {
|
|
57
|
+
Logic.onBefore(actionName, action);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
afterActionsOnCondition.forEach(({ condition, action }) => {
|
|
61
|
+
if (condition(actionName)) {
|
|
62
|
+
Logic.onAfter(actionName, action);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
//adds before/after per actionName
|
|
44
68
|
Object.keys(beforeActions).forEach((actionName) =>
|
|
45
69
|
beforeActions[actionName].forEach((action) =>
|
|
46
70
|
Logic.onBefore(actionName, action)
|
package/src/logic/logic.js
CHANGED
|
@@ -17,8 +17,8 @@ class LogicProxied {
|
|
|
17
17
|
actions = new Map();
|
|
18
18
|
actionRunner = ActionRunner;
|
|
19
19
|
|
|
20
|
-
populateBuilders = {};
|
|
21
|
-
defaultPopulate = [];
|
|
20
|
+
#populateBuilders = {};
|
|
21
|
+
#defaultPopulate = [];
|
|
22
22
|
|
|
23
23
|
MODEL_NAME;
|
|
24
24
|
MODULE_NAME;
|
|
@@ -32,7 +32,6 @@ class LogicProxied {
|
|
|
32
32
|
logAction;
|
|
33
33
|
logDebugAction;
|
|
34
34
|
|
|
35
|
-
|
|
36
35
|
constructor(
|
|
37
36
|
actions = {},
|
|
38
37
|
actionRunner = ActionRunner,
|
|
@@ -48,9 +47,15 @@ class LogicProxied {
|
|
|
48
47
|
this.MODEL_NAME = MODEL_NAME;
|
|
49
48
|
this.MODULE_NAME = MODULE_NAME;
|
|
50
49
|
this.USER_MODEL_NAME = USER_MODEL_NAME;
|
|
50
|
+
if (defaultPopulate) {
|
|
51
|
+
this.#defaultPopulate = defaultPopulate;
|
|
52
|
+
}
|
|
53
|
+
Object.freeze(this.#defaultPopulate);
|
|
51
54
|
|
|
52
|
-
|
|
53
|
-
|
|
55
|
+
if (populateBuilders) {
|
|
56
|
+
this.#populateBuilders = populateBuilders;
|
|
57
|
+
}
|
|
58
|
+
Object.freeze(this.#populateBuilders);
|
|
54
59
|
|
|
55
60
|
actionRunner && (this.actionRunner = actionRunner);
|
|
56
61
|
this.afterPipes = new NamedActionPipes(
|
|
@@ -70,7 +75,7 @@ class LogicProxied {
|
|
|
70
75
|
|
|
71
76
|
// proxy logic, do something before each call of all methods inside class
|
|
72
77
|
// like if arg passed is 3, print something additionally
|
|
73
|
-
return
|
|
78
|
+
return (this.#proxy = new Proxy(this, {
|
|
74
79
|
get(target, prop) {
|
|
75
80
|
if (target.actions.has(prop)) {
|
|
76
81
|
return target.#getActionRunner(prop);
|
|
@@ -78,7 +83,7 @@ class LogicProxied {
|
|
|
78
83
|
return target[prop];
|
|
79
84
|
}
|
|
80
85
|
},
|
|
81
|
-
});
|
|
86
|
+
}));
|
|
82
87
|
}
|
|
83
88
|
|
|
84
89
|
#initTools(target) {
|
|
@@ -105,9 +110,7 @@ class LogicProxied {
|
|
|
105
110
|
this.log &&
|
|
106
111
|
this.log.debug(
|
|
107
112
|
new Date(),
|
|
108
|
-
`${this.MODULE_NAME}//Logic//${
|
|
109
|
-
this.MODEL_NAME
|
|
110
|
-
}//${action}`,
|
|
113
|
+
`${this.MODULE_NAME}//Logic//${this.MODEL_NAME}//${action}`,
|
|
111
114
|
identity?.ip,
|
|
112
115
|
identity?.root
|
|
113
116
|
);
|
|
@@ -132,16 +135,16 @@ class LogicProxied {
|
|
|
132
135
|
|
|
133
136
|
async getPopulate(actionName, prepared) {
|
|
134
137
|
if (
|
|
135
|
-
this
|
|
136
|
-
objHas(this
|
|
137
|
-
isFunc(this
|
|
138
|
+
this.#populateBuilders &&
|
|
139
|
+
objHas(this.#populateBuilders, actionName) &&
|
|
140
|
+
isFunc(this.#populateBuilders[actionName])
|
|
138
141
|
) {
|
|
139
142
|
return await executeFunctionAsAsync(
|
|
140
|
-
this
|
|
143
|
+
this.#populateBuilders[actionName],
|
|
141
144
|
[prepared]
|
|
142
145
|
);
|
|
143
146
|
}
|
|
144
|
-
return this
|
|
147
|
+
return [...this.#defaultPopulate];
|
|
145
148
|
}
|
|
146
149
|
|
|
147
150
|
#getActionRunner(actionName) {
|