@digione/node-custom-api 0.1.7 → 0.1.8-beta2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digione/node-custom-api",
3
- "version": "0.1.7",
3
+ "version": "0.1.8-beta2",
4
4
  "description": "Typescript node digione-api",
5
5
  "author": "Monchai Jirayupong <monchai.j@seven.co.th>",
6
6
  "license": "MIT",
package/utils/helper.d.ts CHANGED
@@ -18,8 +18,9 @@ export declare const getTimestampMysql: (date?: any) => number;
18
18
  export declare const getDateTime: (time?: any, { utc }?: {
19
19
  utc?: boolean;
20
20
  }) => moment.Moment;
21
- export declare const getValueFunctionFile: (key: any) => (value: any, { result, fileUtil }?: {
21
+ export declare const getValueFunctionFile: (key: any) => (value: any, { result, json, fileUtil }?: {
22
22
  result?: {};
23
+ json?: {};
23
24
  fileUtil?: any;
24
25
  }) => Promise<void>;
25
26
  export declare const getValueFunctionLang: (key: any) => (value?: {}, { result, transform, spread, spread_all, raw, lang_code }?: {
package/utils/helper.js CHANGED
@@ -79,8 +79,16 @@ const getDateTime = (time = undefined, { utc = true } = {}) => {
79
79
  };
80
80
  exports.getDateTime = getDateTime;
81
81
  const getValueFunctionFile = (key) => {
82
- return (value, { result = {}, fileUtil = undefined } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
83
- if (value[key]) {
82
+ return (value, { result = {}, json = {}, fileUtil = undefined } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
83
+ if (json[key]) {
84
+ try {
85
+ result[key] = JSON.parse(json[key]);
86
+ }
87
+ catch (err) {
88
+ result[key] = null;
89
+ }
90
+ }
91
+ else if (value[key]) {
84
92
  result[key] = yield fileUtil.findOneFileById(value[key]);
85
93
  }
86
94
  });
package/utils/stream.d.ts CHANGED
@@ -5,16 +5,20 @@ import { Request } from 'express';
5
5
  import * as Joi from 'joi';
6
6
  export declare const getValueFunctionRelationship: (key: any, { field_data }?: {
7
7
  field_data?: {};
8
- }) => (value: any, { result, ref, lang_code }?: {
8
+ }) => (value: any, { result, json, ref, raw, lang_code }?: {
9
9
  result?: {};
10
+ json?: {};
10
11
  ref?: string;
12
+ raw?: boolean;
11
13
  lang_code?: string;
12
14
  }) => Promise<void>;
13
15
  export declare const getValueFunctionMultiple: (key: any, { field_data }?: {
14
16
  field_data?: {};
15
- }) => (value: any, { result, ref, lang_code }?: {
17
+ }) => (value: any, { result, json, ref, raw, lang_code }?: {
16
18
  result?: {};
19
+ json?: {};
17
20
  ref?: string;
21
+ raw?: boolean;
18
22
  lang_code?: string;
19
23
  }) => Promise<void>;
20
24
  interface Field {
@@ -71,14 +75,16 @@ export declare class StreamUtil {
71
75
  include?: any;
72
76
  include_only?: boolean;
73
77
  }): any;
74
- buildStream(slug: string, namespace: string, { query, builder, fetch, attribute, attributes, field_exclude, include }?: {
78
+ buildStream(slug: string, namespace: string, { query, builder, fetch, label, attribute, attributes, field_exclude, include, include_all }?: {
75
79
  query?: boolean;
76
80
  builder?: boolean;
77
81
  fetch?: boolean;
82
+ label?: boolean;
78
83
  attribute?: boolean;
79
84
  attributes?: any;
80
85
  field_exclude?: any[];
81
86
  include?: {};
87
+ include_all?: boolean;
82
88
  }): Promise<{
83
89
  stream: any;
84
90
  fields: any;
@@ -99,6 +105,18 @@ export declare class StreamUtil {
99
105
  raw?: boolean;
100
106
  lang_code?: any;
101
107
  }): Promise<{}>;
108
+ getValueByJson(slug: string, namespace: string, value: any, json: any, { query, lang_code }?: {
109
+ query?: boolean;
110
+ lang_code?: any;
111
+ }): Promise<{}>;
112
+ getStoreJson(slug: string, namespace: string, value: any, { query, lang_code, exclude_type }?: {
113
+ query?: boolean;
114
+ lang_code?: any;
115
+ exclude_type?: {};
116
+ }): Promise<{
117
+ result: {};
118
+ labels: any[];
119
+ }>;
102
120
  genField(streams: Array<any>, { index, option, slug, id_stream, prefix, multiple_table, multiple_spread, addition, convert_option, field_option, default_value, default_lang, lang_code, no_lang, lang, country, section }?: {
103
121
  index?: string;
104
122
  option?: {};
package/utils/stream.js CHANGED
@@ -24,10 +24,22 @@ const i18n_1 = require("i18n");
24
24
  const _ = require("lodash");
25
25
  const Joi = require("joi");
26
26
  const getValueFunctionRelationship = (key, { field_data = {} } = {}) => {
27
- return (value, { result = {}, ref = '', lang_code = '' } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
28
- if (value[key] && field_data['table']) {
27
+ return (value, { result = {}, json = {}, ref = '', raw = false, lang_code = '' } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
28
+ if (field_data['table']) {
29
29
  let title_column = field_data['title_column'] || "title";
30
- result[key] = yield (0, db_1.findOne)(ref + '_' + field_data['table'], { where: { id: value[key] }, attributes: ['*'] });
30
+ if (json[key]) {
31
+ try {
32
+ result[key] = JSON.parse(json[key]);
33
+ }
34
+ catch (err) {
35
+ result[key] = null;
36
+ }
37
+ }
38
+ else if (value[key]) {
39
+ result[key] = yield (0, db_1.findOne)(ref + '_' + field_data['table'], { where: { id: value[key] }, attributes: ['*'] });
40
+ }
41
+ if (raw)
42
+ return;
31
43
  if (result[key]) {
32
44
  (0, helper_2.getValueFunctionLang)(title_column)(result[key], { result: result[key], lang_code });
33
45
  }
@@ -36,10 +48,22 @@ const getValueFunctionRelationship = (key, { field_data = {} } = {}) => {
36
48
  };
37
49
  exports.getValueFunctionRelationship = getValueFunctionRelationship;
38
50
  const getValueFunctionMultiple = (key, { field_data = {} } = {}) => {
39
- return (value, { result = {}, ref = '', lang_code = '' } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
40
- if (field_data['association'] && field_data['table']) {
51
+ return (value, { result = {}, json = {}, ref = '', raw = false, lang_code = '' } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
52
+ if (field_data['table']) {
41
53
  let title_column = field_data['title_column'] || "title";
42
- result[key] = yield (0, db_1.findAll)(ref + '_' + field_data['table'], { where: { [`m.row_id`]: { [sequelize_1.Op.eq]: value['id'] } }, attributes: [`${ref + '_' + field_data['table']}.*`], association: [`INNER JOIN ${ref}_${field_data['association']} m ON ${ref + '_' + field_data['table']}.id = m.${field_data['table'] + '_id'}`] });
54
+ if (json[key]) {
55
+ try {
56
+ result[key] = JSON.parse(json[key]);
57
+ }
58
+ catch (err) {
59
+ result[key] = [];
60
+ }
61
+ }
62
+ else if (field_data['association']) {
63
+ result[key] = yield (0, db_1.findAll)(ref + '_' + field_data['table'], { where: { [`m.row_id`]: { [sequelize_1.Op.eq]: value['id'] } }, attributes: [`${ref + '_' + field_data['table']}.*`], association: [`INNER JOIN ${ref}_${field_data['association']} m ON ${ref + '_' + field_data['table']}.id = m.${field_data['table'] + '_id'}`] });
64
+ }
65
+ if (raw)
66
+ return;
43
67
  if (result[key] && result[key].length) {
44
68
  result[key] = result[key].map(i => (0, helper_2.getValueByLang)(i, { result: i, field: [title_column], lang_code }));
45
69
  }
@@ -211,7 +235,7 @@ class StreamUtil {
211
235
  }
212
236
  return data;
213
237
  }
214
- buildStream(slug, namespace, { query = true, builder = true, fetch = false, attribute = false, attributes = null, field_exclude = [], include = {} } = {}) {
238
+ buildStream(slug, namespace, { query = true, builder = true, fetch = false, label = false, attribute = false, attributes = null, field_exclude = [], include = {}, include_all = false } = {}) {
215
239
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
216
240
  let build = this.builder[slug + "_" + namespace] || { stream: null, fields: null, fetchFn: [], attributes: null };
217
241
  if (!build['fields']) {
@@ -222,7 +246,7 @@ class StreamUtil {
222
246
  build['stream'] = _.omit(data, ['fields']);
223
247
  }
224
248
  if ((fetch || attribute) && build['fields'].length) {
225
- let fetchFn = [];
249
+ let fetchFn = [], fetchLabel = [];
226
250
  attributes = attributes || {};
227
251
  for (let field of build['fields']) {
228
252
  let field_data = field['field_data'] || {};
@@ -232,24 +256,30 @@ class StreamUtil {
232
256
  }
233
257
  if (fetch) {
234
258
  if (field['is_lang'] == "yes") {
259
+ label && fetchLabel.push(field);
235
260
  fetchFn.push((0, helper_2.getValueFunctionLang)(field['field_slug']));
236
261
  }
237
262
  else {
238
263
  switch (field['field_type']) {
239
264
  case "file":
240
265
  case "image":
241
- include[field['field_slug']] && fetchFn.push((0, helper_2.getValueFunctionFile)(field['field_slug']));
266
+ if (include[field['field_slug']] || include_all) {
267
+ label && fetchLabel.push(field);
268
+ fetchFn.push((0, helper_2.getValueFunctionFile)(field['field_slug']));
269
+ }
242
270
  break;
243
271
  case "relationship":
244
272
  case "multiple":
245
- if (include[field['field_slug']] && field_data['choose_stream']) {
273
+ if ((include[field['field_slug']] || include_all) && field_data['choose_stream']) {
246
274
  let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { id: field_data['choose_stream'] }, raw: true, attributes: ['stream_slug', 'stream_prefix', 'title_column'] });
247
275
  if (stream['stream_slug']) {
248
276
  field_data['table'] = stream['stream_prefix'] ? stream['stream_prefix'] + stream['stream_slug'] : stream['stream_slug'];
249
277
  if (field['field_type'] == "relationship") {
278
+ label && fetchLabel.push(field);
250
279
  fetchFn.push((0, exports.getValueFunctionRelationship)(field['field_slug'], { field_data }));
251
280
  }
252
281
  else if (field['field_type'] == "multiple") {
282
+ label && fetchLabel.push(field);
253
283
  field_data['association'] = (build['stream']['stream_prefix'] ? build['stream']['stream_prefix'] + build['stream']['stream_slug'] : build['stream']['stream_slug']) + "_" + field_data['table'];
254
284
  fetchFn.push((0, exports.getValueFunctionMultiple)(field['field_slug'], { field_data }));
255
285
  }
@@ -264,6 +294,7 @@ class StreamUtil {
264
294
  }
265
295
  }
266
296
  build['fetchFn'] = fetchFn;
297
+ build['fetchLabel'] = fetchLabel;
267
298
  }
268
299
  if (attributes)
269
300
  build['attributes'] = attributes;
@@ -289,6 +320,33 @@ class StreamUtil {
289
320
  return result;
290
321
  });
291
322
  }
323
+ getValueByJson(slug, namespace, value, json, { query = false, lang_code = undefined } = {}) {
324
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
325
+ let result = {};
326
+ let fetchFn = yield this.getBuilder(slug, namespace, { query, index: "fetchFn" });
327
+ lang_code = lang_code || this.option.lang_code;
328
+ for (let f of fetchFn || []) {
329
+ yield f(value, { result, json, ref: this.ref, lang_code, fileUtil: this.fileUtil });
330
+ }
331
+ return result;
332
+ });
333
+ }
334
+ getStoreJson(slug, namespace, value, { query = false, lang_code = undefined, exclude_type = {} } = {}) {
335
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
336
+ let result = {}, labels = [];
337
+ let fetchLabel = yield this.getBuilder(slug, namespace, { query, index: "fetchLabel" });
338
+ lang_code = lang_code || this.option.lang_code;
339
+ for (let f of fetchLabel || []) {
340
+ let field_slug = f['field_slug'];
341
+ let field_type = f['field_type'];
342
+ if (field_slug && !exclude_type[field_type] && typeof value[field_slug] != "undefined") {
343
+ labels.push(field_slug);
344
+ result[field_slug] = JSON.stringify(value[field_slug]);
345
+ }
346
+ }
347
+ return { result, labels };
348
+ });
349
+ }
292
350
  genField(streams, { index = "", option = {}, slug = "", id_stream = "", prefix = "", multiple_table = false, multiple_spread = false, addition = {}, convert_option = true, field_option = {}, default_value = {}, default_lang = "en", lang_code = "en", no_lang = false, lang = [], country = undefined, section = false } = {}) {
293
351
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
294
352
  let fields = [], sections = [];
@@ -1742,7 +1800,7 @@ class StreamUtil {
1742
1800
  getValidator(streams, { schema = {}, input = {}, req = {}, isNew = false, lang = [], required = true, stripUnknown = false } = {}) {
1743
1801
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
1744
1802
  let field_lang = [];
1745
- let file = (req.file ? [req.file] : [].concat(req.files || [])).reduce((total, item) => Object.assign(total, { [item['fieldname']]: item }), {});
1803
+ let file = req ? ((req.file ? [req.file] : [].concat(req.files || [])).reduce((total, item) => Object.assign(total, { [item['fieldname']]: item }), {})) : {};
1746
1804
  streams.forEach(({ field_type, field_slug, is_required, is_lang, field_data, data }) => {
1747
1805
  let field;
1748
1806
  field_data = field_data || {};
package/utils/user.d.ts CHANGED
@@ -36,8 +36,9 @@ export declare const getUserInOrgan: (ref: string, { where, organ_id, attributes
36
36
  attributes?: string[];
37
37
  parent?: boolean;
38
38
  }) => Promise<sequelize.Model<any, any>>;
39
- export declare const convertAddress: (data: any, { lang_code }?: {
39
+ export declare const convertAddress: (data: any, { lang_code, transform }?: {
40
40
  lang_code?: string;
41
+ transform?: boolean;
41
42
  }) => Promise<any>;
42
43
  export declare const getAddressIndex: (data: any, { condition, lang_code }?: {
43
44
  condition?: {};
package/utils/user.js CHANGED
@@ -177,36 +177,54 @@ const getUserInOrgan = (ref, { where = {}, organ_id = "", attributes = ['id', 'o
177
177
  });
178
178
  });
179
179
  exports.getUserInOrgan = getUserInOrgan;
180
- const convertAddress = (data, { lang_code = "en" } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
181
- let where = { lang_code: [lang_code, "en"], status: '1' };
180
+ const convertAddress = (data, { lang_code = "en", transform = false } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
181
+ let where = { lang_code: [lang_code, "en"], status: '1' }, limit = 1, attributes = [];
182
182
  let order = [sequelize.literal(`FIELD(lang_code,${'\'' + where['lang_code'].join('\',\'') + '\''})`)];
183
+ if (transform) {
184
+ where = { status: '1' };
185
+ attributes = ['lang_code'];
186
+ limit = undefined;
187
+ order = undefined;
188
+ }
183
189
  if (data['country_id']) {
184
- let country = yield country_1.CountryModel.schema("core", "_").findOne({
185
- raw: true, where: Object.assign(Object.assign({}, where), { id: data['country_id'] }), attributes: ['country_name'], order
190
+ let country = yield country_1.CountryModel.schema("core", "_").findAll({
191
+ raw: true, where: Object.assign(Object.assign({}, where), { id: data['country_id'] }), attributes: ['country_name', ...attributes], order, limit
186
192
  });
187
- if (country)
188
- data['country'] = country['country_name'];
193
+ if (country.length) {
194
+ data['country'] = country[0]['country_name'];
195
+ if (transform)
196
+ data['country'] = JSON.stringify(country.reduce((total, item) => Object.assign(total, { [item['lang_code']]: item['country_name'] }), {}));
197
+ }
189
198
  }
190
199
  if (data['state_id']) {
191
- let state = yield state_1.StateModel.schema("core", "_").findOne({
192
- raw: true, where: Object.assign(Object.assign({}, where), { id: data['state_id'] }), attributes: ['state_name'], order
200
+ let state = yield state_1.StateModel.schema("core", "_").findAll({
201
+ raw: true, where: Object.assign(Object.assign({}, where), { id: data['state_id'] }), attributes: ['state_name', ...attributes], order, limit
193
202
  });
194
- if (state)
195
- data['state'] = state['state_name'];
203
+ if (state.length) {
204
+ data['state'] = state[0]['state_name'];
205
+ if (transform)
206
+ data['state'] = JSON.stringify(state.reduce((total, item) => Object.assign(total, { [item['lang_code']]: item['state_name'] }), {}));
207
+ }
196
208
  }
197
209
  if (data['city_id']) {
198
- let city = yield city_1.CityModel.schema("core", "_").findOne({
199
- raw: true, where: Object.assign(Object.assign({}, where), { id: data['city_id'] }), attributes: ['city_name'], order
210
+ let city = yield city_1.CityModel.schema("core", "_").findAll({
211
+ raw: true, where: Object.assign(Object.assign({}, where), { id: data['city_id'] }), attributes: ['city_name', ...attributes], order, limit
200
212
  });
201
- if (city)
202
- data['city'] = city['city_name'];
213
+ if (city.length) {
214
+ data['city'] = city[0]['city_name'];
215
+ if (transform)
216
+ data['city'] = JSON.stringify(city.reduce((total, item) => Object.assign(total, { [item['lang_code']]: item['city_name'] }), {}));
217
+ }
203
218
  }
204
219
  if (data['district_id']) {
205
- let district = yield district_1.DistrictModel.schema("core", "_").findOne({
206
- raw: true, where: Object.assign(Object.assign({}, where), { id: data['district_id'] }), attributes: ['district_name'], order
220
+ let district = yield district_1.DistrictModel.schema("core", "_").findAll({
221
+ raw: true, where: Object.assign(Object.assign({}, where), { id: data['district_id'] }), attributes: ['district_name', ...attributes], order, limit
207
222
  });
208
- if (district)
209
- data['district'] = district['district_name'];
223
+ if (district.length) {
224
+ data['district'] = district[0]['district_name'];
225
+ if (transform)
226
+ data['district'] = JSON.stringify(district.reduce((total, item) => Object.assign(total, { [item['lang_code']]: item['district_name'] }), {}));
227
+ }
210
228
  }
211
229
  return data;
212
230
  });