@autofleet/sheilta 1.3.2-6 → 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,14 +8,20 @@ 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, itemOrder = ASCENDING_KEY] = 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
- (found ? acc[1] : acc[0]).push(found ? [`${found.literal} ${itemOrder}`] : o);
18
+ if (found) {
19
+ acc[1].push(found.literal);
20
+ acc[0].push([DBFormatter ? DBFormatter(`${found.attribute} ${orderStyle}`) : `${found.attribute} ${orderStyle}`]);
21
+ }
22
+ else {
23
+ acc[0].push(o);
24
+ }
19
25
  return acc;
20
26
  }, [[], []]);
21
27
  return [formattedOrder, attributes];
@@ -84,7 +90,7 @@ const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = util
84
90
  order: [...order, DEFAULT_ORDER],
85
91
  associationModels,
86
92
  });
87
- const [filteredFormattedOrder, filteredLiteralAttributes] = getAttributeFromOrder(formattedOrder, options === null || options === void 0 ? void 0 : options.literalAttributes);
93
+ const [filteredFormattedOrder, filteredLiteralAttributes] = getAttributeFromOrder(formattedOrder, options);
88
94
  const allAttributes = [...filteredLiteralAttributes, ...(attributes || [])];
89
95
  const formattedAttribute = allAttributes.length ? { include: allAttributes } : null;
90
96
  const formattedInclude = formatInclude(include, model === null || model === void 0 ? void 0 : model.associations);
@@ -190,7 +190,7 @@ describe('formatting test', () => {
190
190
  expect(JSON.stringify(order)).toBe(JSON.stringify([['status.name', 'DESC'], ['id']]));
191
191
  });
192
192
  it('by literal field ascending', () => {
193
- const literalAttribute = { attribute: 'genericField', literal: 'void' };
193
+ const literalAttribute = { attribute: 'genericField', literal: [{ 0: e => `${e}0` }] };
194
194
  const { order, attributes } = _1.default({
195
195
  order: [literalAttribute.attribute],
196
196
  }, {
@@ -200,11 +200,13 @@ describe('formatting test', () => {
200
200
  }, {
201
201
  literalAttributes: [literalAttribute],
202
202
  });
203
- expect(JSON.stringify(order)).toBe(JSON.stringify([['id']]));
204
- expect(JSON.stringify(attributes)).toBe(JSON.stringify({ include: [[`${literalAttribute.literal} ASC`]] }));
203
+ expect(JSON.stringify(order)).toBe(JSON.stringify([[`${literalAttribute.attribute} ASC`], ['id']]));
204
+ console.log({ attributes });
205
+ expect(JSON.stringify(attributes))
206
+ .toBe(JSON.stringify({ include: [literalAttribute.literal] }));
205
207
  });
206
208
  it('by literal field descending', () => {
207
- const literalAttribute = { attribute: 'genericField', literal: 'void' };
209
+ const literalAttribute = { attribute: 'genericField', literal: ['void', 'field'] };
208
210
  const { order, attributes } = _1.default({
209
211
  order: [`-${literalAttribute.attribute}`, 'currencySymbol'],
210
212
  }, {
@@ -214,8 +216,11 @@ describe('formatting test', () => {
214
216
  }, {
215
217
  literalAttributes: [literalAttribute],
216
218
  });
217
- expect(JSON.stringify(order)).toBe(JSON.stringify([['currencySymbol'], ['id']]));
218
- expect(JSON.stringify(attributes)).toBe(JSON.stringify({ include: [[`${literalAttribute.literal} DESC`]] }));
219
+ expect(JSON.stringify(order)).toBe(JSON.stringify([[`${literalAttribute.attribute} DESC`], ['currencySymbol'], ['id']]));
220
+ expect(JSON.stringify(attributes))
221
+ .toBe(JSON.stringify({
222
+ include: [[literalAttribute.literal[0], literalAttribute.literal[1]]],
223
+ }));
219
224
  });
220
225
  it('by literal field - not found', () => {
221
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-6",
3
+ "version": "1.3.2-8",
4
4
  "description": "manage cache",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -29,6 +29,7 @@
29
29
  "dependencies": {
30
30
  "@autofleet/common-types": "^1.7.29",
31
31
  "@autofleet/errors": "^1.0.10",
32
+ "@autofleet/sheilta": "^1.3.2-6",
32
33
  "joi": "^17.9.2",
33
34
  "lodash": "^4.17.21"
34
35
  },