@autofleet/sheilta 1.3.7 → 1.3.8-beta.1

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.
@@ -24,8 +24,6 @@ declare const formatPayload: ({ order, page, perPage, include, query, attributes
24
24
  page: any;
25
25
  perPage: any;
26
26
  include: any;
27
- scopes: (string | {
28
- method: any[];
29
- })[];
27
+ scopes: any[];
30
28
  };
31
29
  export default formatPayload;
@@ -8,8 +8,9 @@ const common_types_1 = require("@autofleet/common-types");
8
8
  const utils_1 = require("../utils");
9
9
  const DEFAULT_ORDER = 'id';
10
10
  const DESCENDING_KEY = 'DESC';
11
+ const ASCENDING_KEY = 'ASC';
11
12
  const CUSTOM_FIELDS_QUERY_PREFIX = 'customFields.';
12
- const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
13
+ const { CUSTOM_FIELDS_FILTER_SCOPE, CUSTOM_FIELDS_SORT_SCOPE } = common_types_1.customFields;
13
14
  const getAttributeFromOrder = (order, options = {}) => {
14
15
  const { literalAttributes = [], DBFormatter = undefined } = options;
15
16
  const [formattedOrder, attributes] = order.reduce((acc, o) => {
@@ -26,20 +27,44 @@ const getAttributeFromOrder = (order, options = {}) => {
26
27
  }, [[], []]);
27
28
  return [formattedOrder, attributes];
28
29
  };
29
- const formatOrder = ({ order, associationModels = [], }) => order.map((o) => {
30
- const formattedOrder = [utils_1.extractAttributeNameFromOrder(o, associationModels)];
31
- const isOrderDescOrder = utils_1.isOrderDesc(o);
32
- const isOrderAssociation = utils_1.isAttributeByAssociation(isOrderDescOrder
33
- ? o.split(utils_1.ORDER_PREFIX)[1]
34
- : o, associationModels);
35
- if (isOrderAssociation) {
36
- formattedOrder.push(utils_1.extractAssociatedAttributeNameFromOrder(o));
37
- }
38
- if (isOrderDescOrder) {
39
- formattedOrder.push(DESCENDING_KEY);
40
- }
41
- return formattedOrder;
42
- });
30
+ const formatOrder = ({ order, associationModels = [], }) => {
31
+ const formattedOrders = [];
32
+ const orderScopesMap = new Map();
33
+ order.forEach((o) => {
34
+ if (o.startsWith(CUSTOM_FIELDS_QUERY_PREFIX) ||
35
+ o.substring(1).startsWith(CUSTOM_FIELDS_QUERY_PREFIX)) {
36
+ if (!orderScopesMap.has(CUSTOM_FIELDS_SORT_SCOPE)) {
37
+ orderScopesMap.set(CUSTOM_FIELDS_SORT_SCOPE, {});
38
+ }
39
+ const scopeKey = o.split(CUSTOM_FIELDS_QUERY_PREFIX)[1];
40
+ orderScopesMap.get(CUSTOM_FIELDS_SORT_SCOPE)[scopeKey] = (utils_1.isOrderDesc(o) ?
41
+ DESCENDING_KEY :
42
+ ASCENDING_KEY);
43
+ return;
44
+ }
45
+ const formattedOrder = [utils_1.extractAttributeNameFromOrder(o, associationModels)];
46
+ const isOrderDescOrder = utils_1.isOrderDesc(o);
47
+ const isOrderAssociation = utils_1.isAttributeByAssociation(isOrderDescOrder
48
+ ? o.split(utils_1.ORDER_PREFIX)[1]
49
+ : o, associationModels);
50
+ if (isOrderAssociation) {
51
+ formattedOrder.push(utils_1.extractAssociatedAttributeNameFromOrder(o));
52
+ }
53
+ if (isOrderDescOrder) {
54
+ formattedOrder.push(DESCENDING_KEY);
55
+ }
56
+ formattedOrders.push(formattedOrder);
57
+ });
58
+ return {
59
+ formattedOrders,
60
+ orderScopes: Array.from(orderScopesMap.entries()).map(([scopeName, scopeValue]) => {
61
+ if (!scopeValue) {
62
+ return scopeName;
63
+ }
64
+ return { method: [scopeName, scopeValue] };
65
+ }),
66
+ };
67
+ };
43
68
  const formatPage = page => page || utils_1.PAGE_DEFAULT;
44
69
  const formatPerPage = perPage => perPage || utils_1.PER_PAGE_DEFAULT;
45
70
  const formatInclude = (include, associationsMap = {}) => {
@@ -92,17 +117,18 @@ const formatSearchTerm = (searchTerm, attributesToSend, rawAttributes) => ({
92
117
  });
93
118
  const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = utils_1.PER_PAGE_DEFAULT, include = [], query = {}, attributes = null, searchTerm = null, }, model, options) => {
94
119
  const associationModels = Object.keys((model === null || model === void 0 ? void 0 : model.associations) || {});
95
- const formattedOrder = formatOrder({
120
+ const { formattedOrders, orderScopes } = formatOrder({
96
121
  order: [...order, DEFAULT_ORDER],
97
122
  associationModels,
98
123
  });
99
- const [filteredFormattedOrder, filteredLiteralAttributes] = getAttributeFromOrder(formattedOrder, options);
124
+ const [filteredFormattedOrder, filteredLiteralAttributes] = getAttributeFromOrder(formattedOrders, options);
100
125
  const allAttributes = [...filteredLiteralAttributes, ...(attributes || [])];
101
126
  const formattedAttribute = (attributes === null || attributes === void 0 ? void 0 : attributes.length) ? allAttributes : { include: allAttributes };
102
127
  const formattedInclude = formatInclude(include, model === null || model === void 0 ? void 0 : model.associations);
103
128
  const formattedPage = formatPage(page);
104
129
  const formattedPerPage = formatPerPage(perPage);
105
130
  const result = formatQuery(query, associationModels);
131
+ const queryScopes = result.formattedScopes;
106
132
  let { formattedQuery } = result;
107
133
  if (searchTerm && !(options === null || options === void 0 ? void 0 : options.skipSearchTermFormat)) {
108
134
  const attributesToSend = (attributes === null || attributes === void 0 ? void 0 : attributes.length) ? attributes : Object.keys(model.rawAttributes);
@@ -114,6 +140,6 @@ const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = util
114
140
  ],
115
141
  };
116
142
  }
117
- return Object.assign({ query: formattedQuery, order: filteredFormattedOrder, page: formattedPage, perPage: formattedPerPage, include: formattedInclude, scopes: result.formattedScopes }, (formattedAttribute && { attributes: formattedAttribute }));
143
+ return Object.assign({ query: formattedQuery, order: filteredFormattedOrder, page: formattedPage, perPage: formattedPerPage, include: formattedInclude, scopes: [...queryScopes, ...orderScopes] }, (formattedAttribute && { attributes: formattedAttribute }));
118
144
  };
119
145
  exports.default = formatPayload;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sheilta",
3
- "version": "1.3.7",
3
+ "version": "1.3.8-beta.1",
4
4
  "description": "manage cache",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -10,7 +10,8 @@
10
10
  "coverage": "jest --coverage --forceExit --runInBand",
11
11
  "test": "jest --forceExit --runInBand",
12
12
  "test-auto": "jest --watch --runInBand",
13
- "linter": "./node_modules/.bin/eslint src/**/*.ts"
13
+ "linter": "./node_modules/.bin/eslint src/**/*.ts",
14
+ "build-to-local-repo": "npm run build && cp -r lib/* ../task-ms/node_modules/$npm_package_name/lib"
14
15
  },
15
16
  "jest": {
16
17
  "setupTestFrameworkScriptFile": "jest-extended",
@@ -27,7 +28,7 @@
27
28
  },
28
29
  "homepage": "https://github.com/Autofleet/sheilta",
29
30
  "dependencies": {
30
- "@autofleet/common-types": "^1.7.29",
31
+ "@autofleet/common-types": "^1.7.42",
31
32
  "@autofleet/errors": "^1.0.10",
32
33
  "joi": "^17.9.2",
33
34
  "lodash": "^4.17.21"
@@ -48,4 +49,4 @@
48
49
  "files": [
49
50
  "lib/**/*"
50
51
  ]
51
- }
52
+ }