@autofleet/sheilta 1.2.9 → 1.3.0

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.
@@ -17,5 +17,8 @@ declare const formatPayload: ({ order, page, perPage, include, query, attributes
17
17
  page: any;
18
18
  perPage: any;
19
19
  include: any;
20
+ scopes: (string | {
21
+ method: any[];
22
+ })[];
20
23
  };
21
24
  export default formatPayload;
@@ -4,9 +4,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const lodash_1 = __importDefault(require("lodash"));
7
+ const common_types_1 = require("@autofleet/common-types");
7
8
  const utils_1 = require("../utils");
8
9
  const DEFAULT_ORDER = 'id';
9
10
  const DESCENDING_KEY = 'DESC';
11
+ const CUSTOM_FIELDS_QUERY_PREFIX = 'customFields.';
12
+ const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
10
13
  const formatOrder = ({ order, associationModels = [], }) => order.map((o) => {
11
14
  const formattedOrder = [utils_1.extractAttributeNameFromOrder(o, associationModels)];
12
15
  const isOrderDescOrder = utils_1.isOrderDesc(o);
@@ -28,8 +31,33 @@ const formatInclude = (include, associationsMap = {}) => {
28
31
  formattedInclude = formattedInclude.map(i => lodash_1.default.omit(i, ['model']));
29
32
  return formattedInclude;
30
33
  };
31
- const formatQuery = (query, associationModels) => Object.keys(query).reduce((acc, queryItemKey) => (Object.assign(Object.assign({}, acc), { [utils_1.isAttributeByAssociation(queryItemKey, associationModels)
32
- ? utils_1.wrapAttributeWithOperator(queryItemKey) : queryItemKey]: query[queryItemKey] })), {});
34
+ const formatQuery = (query, associationModels) => {
35
+ const formattedQuery = {};
36
+ const formattedScopeMap = new Map();
37
+ Object.entries(query).forEach(([queryItemKey, queryItemValue]) => {
38
+ if (queryItemKey.startsWith(CUSTOM_FIELDS_QUERY_PREFIX)) {
39
+ if (!formattedScopeMap.has(CUSTOM_FIELDS_FILTER_SCOPE)) {
40
+ formattedScopeMap.set(CUSTOM_FIELDS_FILTER_SCOPE, {});
41
+ }
42
+ formattedScopeMap.get(CUSTOM_FIELDS_FILTER_SCOPE)[queryItemKey] = queryItemValue;
43
+ return;
44
+ }
45
+ const key = utils_1.isAttributeByAssociation(queryItemKey, associationModels) ?
46
+ utils_1.wrapAttributeWithOperator(queryItemKey) :
47
+ queryItemKey;
48
+ formattedQuery[key] = queryItemValue;
49
+ });
50
+ const formattedScopes = Array.from(formattedScopeMap.entries()).map(([scopeName, scopeValue]) => {
51
+ if (!scopeValue) {
52
+ return scopeName;
53
+ }
54
+ return { method: [scopeName, scopeValue] };
55
+ });
56
+ return {
57
+ formattedQuery,
58
+ formattedScopes,
59
+ };
60
+ };
33
61
  const formatSearchTerm = (searchTerm, attributesToSend, rawAttributes) => ({
34
62
  $and: searchTerm.split(' ').map(term => ({
35
63
  $or: attributesToSend.filter(attrKey => rawAttributes[attrKey].type.key === 'STRING').map(attr => ({
@@ -48,7 +76,8 @@ const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = util
48
76
  const formattedInclude = formatInclude(include, model === null || model === void 0 ? void 0 : model.associations);
49
77
  const formattedPage = formatPage(page);
50
78
  const formattedPerPage = formatPerPage(perPage);
51
- let formattedQuery = formatQuery(query, associationModels);
79
+ const result = formatQuery(query, associationModels);
80
+ let { formattedQuery } = result;
52
81
  if (searchTerm) {
53
82
  const attributesToSend = (attributes === null || attributes === void 0 ? void 0 : attributes.length) ? attributes : Object.keys(model.rawAttributes);
54
83
  const queryWithSearchTerm = formatSearchTerm(searchTerm, attributesToSend, model.rawAttributes);
@@ -65,6 +94,7 @@ const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = util
65
94
  page: formattedPage,
66
95
  perPage: formattedPerPage,
67
96
  include: formattedInclude,
97
+ scopes: result.formattedScopes,
68
98
  };
69
99
  };
70
100
  exports.default = formatPayload;
@@ -1,5 +1,5 @@
1
1
  import { FormatPayloadOptions } from '../formatter';
2
- export declare const newQueryValidationMiddleware: (inner?: string) => (model: any) => (req: any, res: any, next: any) => Promise<any>;
3
- export declare const newQueryFormatMiddleware: (inner?: string) => (model: any, options?: FormatPayloadOptions) => (req: any, res: any, next: any) => Promise<any>;
4
- export declare const queryValidationMiddleware: (model: any) => (req: any, res: any, next: any) => Promise<any>;
5
- export declare const queryFormatMiddleware: (model: any, options?: FormatPayloadOptions) => (req: any, res: any, next: any) => Promise<any>;
2
+ export declare const newQueryValidationMiddleware: (inner?: string) => (model: any) => (req: any, res: any, next: any) => Promise<void>;
3
+ export declare const newQueryFormatMiddleware: (inner?: string) => (model: any, options?: FormatPayloadOptions) => (req: any, res: any, next: any) => Promise<void>;
4
+ export declare const queryValidationMiddleware: (model: any) => (req: any, res: any, next: any) => Promise<void>;
5
+ export declare const queryFormatMiddleware: (model: any, options?: FormatPayloadOptions) => (req: any, res: any, next: any) => Promise<void>;
@@ -57,7 +57,7 @@ exports.newQueryValidationMiddleware = (inner = 'body') => model => (req, res, n
57
57
  });
58
58
  exports.newQueryFormatMiddleware = (inner = 'body') => (model, options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
59
59
  const { order, page, perPage, include, query, attributes, searchTerm, } = req[inner];
60
- const { query: formattedQuery, order: formattedOrder, page: formattedPage, perPage: formattedPerPage, include: formattedInclude, } = formatter_1.default({
60
+ const { query: formattedQuery, order: formattedOrder, page: formattedPage, perPage: formattedPerPage, include: formattedInclude, scopes: formattedScopes, } = formatter_1.default({
61
61
  query,
62
62
  order,
63
63
  page,
@@ -71,6 +71,7 @@ exports.newQueryFormatMiddleware = (inner = 'body') => (model, options = {}) =>
71
71
  req[inner].page = formattedPage;
72
72
  req[inner].perPage = formattedPerPage;
73
73
  req[inner].include = formattedInclude;
74
+ req[inner].scopes = formattedScopes;
74
75
  if (options.includeRawPayload) {
75
76
  req[inner].rawPayload = {
76
77
  order,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sheilta",
3
- "version": "1.2.9",
3
+ "version": "1.3.0",
4
4
  "description": "manage cache",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -27,13 +27,14 @@
27
27
  },
28
28
  "homepage": "https://github.com/Autofleet/sheilta",
29
29
  "dependencies": {
30
+ "@autofleet/common-types": "^1.7.29",
30
31
  "@autofleet/errors": "^1.0.10",
31
- "@types/node": "^14.14.20",
32
32
  "joi": "^17.9.2",
33
33
  "lodash": "^4.17.21"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@types/jest": "^24.9.0",
37
+ "@types/node": "^16.11.68",
37
38
  "@typescript-eslint/eslint-plugin": "^2.34.0",
38
39
  "@typescript-eslint/parser": "^2.23.0",
39
40
  "eslint": "^6.8.0",