@autofleet/sheilta 1.3.2-7 → 1.3.2-8

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.
@@ -4,6 +4,7 @@ declare type SequelizeOrder = string | OrderItem[];
4
4
  export declare type FormatPayloadOptions = {
5
5
  includeRawPayload?: boolean;
6
6
  literalAttributes?: Attribute[];
7
+ DBFormatter?: any;
7
8
  };
8
9
  declare const formatPayload: ({ order, page, perPage, include, query, attributes, searchTerm, }: {
9
10
  order?: any[];
@@ -8,18 +8,16 @@ 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'; */
12
11
  const CUSTOM_FIELDS_QUERY_PREFIX = 'customFields.';
13
12
  const { CUSTOM_FIELDS_FILTER_SCOPE } = common_types_1.customFields;
14
- const getAttributeFromOrder = (order, literalAttributes) => {
13
+ const getAttributeFromOrder = (order, options = {}) => {
14
+ const { literalAttributes = [], DBFormatter = undefined } = options;
15
15
  const [formattedOrder, attributes] = order.reduce((acc, o) => {
16
- const [item] = Array.isArray(o) ? o : [o];
16
+ const [item, orderStyle = 'ASC'] = Array.isArray(o) ? o : [o];
17
17
  const found = literalAttributes === null || literalAttributes === void 0 ? void 0 : literalAttributes.find(obj => obj.attribute === item);
18
18
  if (found) {
19
- /* const [literal, name = found.attribute] =
20
- Array.isArray(found.literal) ? found.literal : [found.literal];
21
- console.log({ found, itemOrder }); */
22
19
  acc[1].push(found.literal);
20
+ acc[0].push([DBFormatter ? DBFormatter(`${found.attribute} ${orderStyle}`) : `${found.attribute} ${orderStyle}`]);
23
21
  }
24
22
  else {
25
23
  acc[0].push(o);
@@ -92,7 +90,7 @@ const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = util
92
90
  order: [...order, DEFAULT_ORDER],
93
91
  associationModels,
94
92
  });
95
- const [filteredFormattedOrder, filteredLiteralAttributes] = getAttributeFromOrder(formattedOrder, options === null || options === void 0 ? void 0 : options.literalAttributes);
93
+ const [filteredFormattedOrder, filteredLiteralAttributes] = getAttributeFromOrder(formattedOrder, options);
96
94
  const allAttributes = [...filteredLiteralAttributes, ...(attributes || [])];
97
95
  const formattedAttribute = allAttributes.length ? { include: allAttributes } : null;
98
96
  const formattedInclude = formatInclude(include, model === null || model === void 0 ? void 0 : model.associations);
@@ -200,7 +200,7 @@ describe('formatting test', () => {
200
200
  }, {
201
201
  literalAttributes: [literalAttribute],
202
202
  });
203
- expect(JSON.stringify(order)).toBe(JSON.stringify([['id']]));
203
+ expect(JSON.stringify(order)).toBe(JSON.stringify([[`${literalAttribute.attribute} ASC`], ['id']]));
204
204
  console.log({ attributes });
205
205
  expect(JSON.stringify(attributes))
206
206
  .toBe(JSON.stringify({ include: [literalAttribute.literal] }));
@@ -216,9 +216,11 @@ describe('formatting test', () => {
216
216
  }, {
217
217
  literalAttributes: [literalAttribute],
218
218
  });
219
- expect(JSON.stringify(order)).toBe(JSON.stringify([['currencySymbol'], ['id']]));
219
+ expect(JSON.stringify(order)).toBe(JSON.stringify([[`${literalAttribute.attribute} DESC`], ['currencySymbol'], ['id']]));
220
220
  expect(JSON.stringify(attributes))
221
- .toBe(JSON.stringify({ include: [[literalAttribute.literal[0], literalAttribute.literal[1]]] }));
221
+ .toBe(JSON.stringify({
222
+ include: [[literalAttribute.literal[0], literalAttribute.literal[1]]],
223
+ }));
222
224
  });
223
225
  it('by literal field - not found', () => {
224
226
  const literalAttribute = { attribute: 'genericField', literal: 'void' };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sheilta",
3
- "version": "1.3.2-7",
3
+ "version": "1.3.2-8",
4
4
  "description": "manage cache",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -49,4 +49,4 @@
49
49
  "files": [
50
50
  "lib/**/*"
51
51
  ]
52
- }
52
+ }