@digione/node-custom-api 0.1.9 → 0.2.0-alpha10
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/middlewares/paramQuery.d.ts +5 -2
- package/middlewares/paramQuery.js +30 -4
- package/models/system/file.d.ts +1 -0
- package/models/system/file.js +1 -0
- package/models/user/user.d.ts +0 -3
- package/models/user/user.js +0 -4
- package/package.json +1 -1
- package/utils/auth.d.ts +3 -1
- package/utils/auth.js +8 -3
- package/utils/db.js +4 -4
- package/utils/file.d.ts +28 -10
- package/utils/file.js +96 -48
- package/utils/helper.d.ts +17 -2
- package/utils/helper.js +56 -6
- package/utils/metadata.js +2 -2
- package/utils/stream.d.ts +18 -5
- package/utils/stream.js +112 -22
- package/utils/user.d.ts +2 -2
- package/utils/user.js +20 -11
- package/utils/validator.d.ts +8 -0
- package/utils/validator.js +24 -7
package/utils/stream.js
CHANGED
|
@@ -101,7 +101,7 @@ class StreamUtil {
|
|
|
101
101
|
this.fileUtil = fileUtil || new file_2.FileUtil(this.ref, this.option);
|
|
102
102
|
this.languageUtil = new language_1.LanguageUtil(this.ref, this.option);
|
|
103
103
|
}
|
|
104
|
-
findField(slug, namespace, { index = "", option = {}, no_lang = false, field_slug = [], field_only = true, field_required = true, lang = [], lang_code = "en", is_required = "", default_value = {}, addition = {}, include = undefined, include_only = false, convert_option = false, field_option = {}, require = false, multiple_table = false, multiple_spread = false, text = false, bullets = false, multiple = false, relationship = false, file = false, image = false, field_exclude = [], section = false } = {}) {
|
|
104
|
+
findField(slug, namespace, { index = "", option = {}, no_lang = false, field_slug = [], field_only = true, field_required = true, lang = [], lang_code = "en", is_required = "", default_value = {}, addition = {}, include = undefined, include_only = false, convert_option = false, convert_value = false, field_option = {}, require = false, multiple_table = false, multiple_spread = false, text = false, bullets = false, multiple = false, relationship = false, file = false, image = false, field_exclude = [], section = false } = {}) {
|
|
105
105
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
106
106
|
let data;
|
|
107
107
|
let where = {};
|
|
@@ -184,7 +184,7 @@ class StreamUtil {
|
|
|
184
184
|
}
|
|
185
185
|
}
|
|
186
186
|
if (convert_option) {
|
|
187
|
-
data['fields'] = yield this.genField(data['fields'], { index, option, slug, id_stream: data['id'], prefix: data['stream_prefix'], multiple_table, multiple_spread, addition, field_option, default_value, lang_code, no_lang, lang, section });
|
|
187
|
+
data['fields'] = yield this.genField(data['fields'], { index, option, slug, id_stream: data['id'], prefix: data['stream_prefix'], multiple_table, multiple_spread, addition, convert_value, field_option, default_value, lang_code, no_lang, lang, section });
|
|
188
188
|
}
|
|
189
189
|
if (field_only) {
|
|
190
190
|
return data['fields'];
|
|
@@ -235,13 +235,13 @@ class StreamUtil {
|
|
|
235
235
|
}
|
|
236
236
|
return data;
|
|
237
237
|
}
|
|
238
|
-
buildStream(slug, namespace, { query = true, builder = true, fetch = false, label = false, attribute = false, attributes = null, field_exclude = [], include = {}, include_all = false } = {}) {
|
|
238
|
+
buildStream(slug, namespace, { query = true, builder = true, fetch = false, label = false, attribute = false, attributes = null, field_option = {}, field_exclude = [], include = {}, include_all = false } = {}) {
|
|
239
239
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
240
240
|
let build = this.builder[slug + "_" + namespace] || { stream: null, fields: null, fetchFn: [], attributes: null };
|
|
241
241
|
if (!build['fields']) {
|
|
242
242
|
let data = {};
|
|
243
243
|
if (query)
|
|
244
|
-
data = yield this.findField(slug, namespace, { field_only: false, convert_option: true, field_exclude });
|
|
244
|
+
data = yield this.findField(slug, namespace, { field_only: false, convert_option: true, field_option, field_exclude });
|
|
245
245
|
build['fields'] = data['fields'] || [];
|
|
246
246
|
build['stream'] = _.omit(data, ['fields']);
|
|
247
247
|
}
|
|
@@ -255,7 +255,7 @@ class StreamUtil {
|
|
|
255
255
|
delete attributes[field['field_slug']];
|
|
256
256
|
}
|
|
257
257
|
if (fetch) {
|
|
258
|
-
if (field['is_lang'] == "yes") {
|
|
258
|
+
if (field['is_lang'] == "yes" && field['field_type'] != "stream") {
|
|
259
259
|
label && fetchLabel.push(field);
|
|
260
260
|
fetchFn.push((0, helper_2.getValueFunctionLang)(field['field_slug']));
|
|
261
261
|
}
|
|
@@ -286,6 +286,15 @@ class StreamUtil {
|
|
|
286
286
|
}
|
|
287
287
|
}
|
|
288
288
|
break;
|
|
289
|
+
case "stream":
|
|
290
|
+
if (include[field['field_slug']] || include_all) {
|
|
291
|
+
label && fetchLabel.push(field);
|
|
292
|
+
fetchFn.push((field['is_lang'] == "yes") ? (0, helper_2.getValueFunctionLang)(field['field_slug'], { field_type: field['field_type'] }) : (0, helper_2.getValueFunctionArray)(field['field_slug']));
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
delete attributes[field['field_slug']];
|
|
296
|
+
}
|
|
297
|
+
break;
|
|
289
298
|
}
|
|
290
299
|
}
|
|
291
300
|
}
|
|
@@ -303,9 +312,9 @@ class StreamUtil {
|
|
|
303
312
|
return build;
|
|
304
313
|
});
|
|
305
314
|
}
|
|
306
|
-
getBuilder(slug, namespace, { query = true, index = "", builder = true, field_exclude = [] } = {}) {
|
|
315
|
+
getBuilder(slug, namespace, { query = true, index = "", builder = true, field_option = {}, field_exclude = [] } = {}) {
|
|
307
316
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
308
|
-
let result = yield this.buildStream(slug, namespace, { query, builder, field_exclude });
|
|
317
|
+
let result = yield this.buildStream(slug, namespace, { query, builder, field_option, field_exclude });
|
|
309
318
|
return index ? result[index] : result;
|
|
310
319
|
});
|
|
311
320
|
}
|
|
@@ -347,7 +356,7 @@ class StreamUtil {
|
|
|
347
356
|
return { result, labels };
|
|
348
357
|
});
|
|
349
358
|
}
|
|
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 } = {}) {
|
|
359
|
+
genField(streams, { index = "", option = {}, slug = "", id_stream = "", prefix = "", multiple_table = false, multiple_spread = false, addition = {}, convert_option = true, convert_value = false, field_option = {}, default_value = {}, default_lang = "en", lang_code = "en", no_lang = false, lang = [], country = undefined, section = false } = {}) {
|
|
351
360
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
352
361
|
let fields = [], sections = [];
|
|
353
362
|
let exclude = option['excludeObj'] || {};
|
|
@@ -404,6 +413,15 @@ class StreamUtil {
|
|
|
404
413
|
else if (typeof default_value[item['field_slug']] !== 'undefined') {
|
|
405
414
|
item['field_value'] = default_value[item['field_slug']];
|
|
406
415
|
}
|
|
416
|
+
if (item['field_type'] == "stream" && item['field_value']) {
|
|
417
|
+
try {
|
|
418
|
+
item['field_value'] = JSON.parse(item['field_value']);
|
|
419
|
+
}
|
|
420
|
+
catch (err) { }
|
|
421
|
+
if (!(item['field_value'] instanceof Array)) {
|
|
422
|
+
item['field_value'] = [];
|
|
423
|
+
}
|
|
424
|
+
}
|
|
407
425
|
if (item['instructions']) {
|
|
408
426
|
(0, helper_2.getValueFunctionLang)('instructions')(item, { result: item, lang_code });
|
|
409
427
|
}
|
|
@@ -468,11 +486,15 @@ class StreamUtil {
|
|
|
468
486
|
}
|
|
469
487
|
return Object.assign({ field_slug: i['lang_code'], field_name: `${convert_option ? (0, i18n_1.__)({ phrase: item['field_name'], locale: lang_code }) : item['field_name']} (${i['lang_code']})`, field_type: item['field_type'], field_name_raw: item['field_name'] ? item['field_name'] : "", is_required: item['is_required'] }, include);
|
|
470
488
|
});
|
|
471
|
-
|
|
472
|
-
|
|
489
|
+
if (item['field_type'] != "stream") {
|
|
490
|
+
fields.push(Object.assign(Object.assign({}, item), { field_data }));
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
473
493
|
}
|
|
474
494
|
if (convert_option) {
|
|
495
|
+
let obj_value = {};
|
|
475
496
|
if (item['field_type'] == "choice") {
|
|
497
|
+
let opt_empty = false;
|
|
476
498
|
if (field_data['choice_data']) {
|
|
477
499
|
let data = [];
|
|
478
500
|
data = field_data['choice_data'].split('\n');
|
|
@@ -495,12 +517,16 @@ class StreamUtil {
|
|
|
495
517
|
data[k] = { key, value };
|
|
496
518
|
}
|
|
497
519
|
else {
|
|
520
|
+
opt_empty = true;
|
|
498
521
|
data[k] = { key: '', value };
|
|
499
522
|
}
|
|
500
523
|
}
|
|
501
524
|
else {
|
|
502
525
|
data[k] = { key: v, value: v };
|
|
503
526
|
}
|
|
527
|
+
if (convert_value && data[k]) {
|
|
528
|
+
obj_value[data[k]['key']] = data[k]['value'];
|
|
529
|
+
}
|
|
504
530
|
});
|
|
505
531
|
item['data'] = data;
|
|
506
532
|
}
|
|
@@ -510,9 +536,23 @@ class StreamUtil {
|
|
|
510
536
|
if (value && value.indexOf('lang:') > -1) {
|
|
511
537
|
item['value'] = (0, i18n_1.__)({ phrase: value, locale: lang_code });
|
|
512
538
|
}
|
|
539
|
+
if (!item['key'])
|
|
540
|
+
opt_empty = true;
|
|
541
|
+
if (convert_value && item['key']) {
|
|
542
|
+
obj_value[item['key']] = item['value'];
|
|
543
|
+
}
|
|
513
544
|
return item;
|
|
514
545
|
});
|
|
515
546
|
}
|
|
547
|
+
if (field_data['default_value'] && !opt_empty && (item['field_value'] == '' || item['field_value'] == null)) {
|
|
548
|
+
item['field_value'] = field_data['default_value'];
|
|
549
|
+
}
|
|
550
|
+
if (convert_value && typeof item['field_value'] != "undefined") {
|
|
551
|
+
let field_value = item['field_value'];
|
|
552
|
+
if (item['field_value'] == null && opt_empty)
|
|
553
|
+
field_value = '';
|
|
554
|
+
field_data['value'] = obj_value[field_value];
|
|
555
|
+
}
|
|
516
556
|
}
|
|
517
557
|
else if (!field_option[item['field_slug']] && (item['field_type'] == "relationship" || item['field_type'] == "multiple") && field_data['choose_stream']) {
|
|
518
558
|
try {
|
|
@@ -557,6 +597,9 @@ class StreamUtil {
|
|
|
557
597
|
item['title'] = item['title'] ? item['title'] : item[stream['title_column']];
|
|
558
598
|
(0, helper_2.getValueFunctionLang)('title')(item, { result: item, lang_code });
|
|
559
599
|
let value = item['title'] ? item['title'] : item['id'];
|
|
600
|
+
if (convert_value && item['id']) {
|
|
601
|
+
obj_value[item['id']] = value;
|
|
602
|
+
}
|
|
560
603
|
return {
|
|
561
604
|
key: item['id'],
|
|
562
605
|
value
|
|
@@ -567,6 +610,9 @@ class StreamUtil {
|
|
|
567
610
|
}
|
|
568
611
|
}
|
|
569
612
|
item['data'] = data;
|
|
613
|
+
if (convert_value && item['field_value']) {
|
|
614
|
+
field_data['value'] = obj_value[item['field_value']];
|
|
615
|
+
}
|
|
570
616
|
}
|
|
571
617
|
catch (err) { }
|
|
572
618
|
}
|
|
@@ -635,6 +681,14 @@ class StreamUtil {
|
|
|
635
681
|
field_data['range']['field_value'] = default_value[field_data['range']['field_target']];
|
|
636
682
|
}
|
|
637
683
|
}
|
|
684
|
+
else if (item['field_type'] == "stream") {
|
|
685
|
+
if (field_data['choose_stream']) {
|
|
686
|
+
let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { id: field_data['choose_stream'] }, raw: true, attributes: ['stream_slug', 'stream_namespace'] });
|
|
687
|
+
if (stream) {
|
|
688
|
+
item['stream'] = yield this.findField(stream['stream_slug'], stream['stream_namespace'], { option, lang_code, field_only: true, convert_option: true });
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
638
692
|
}
|
|
639
693
|
fields.push(Object.assign(Object.assign({}, item), { field_data }));
|
|
640
694
|
}
|
|
@@ -757,7 +811,7 @@ class StreamUtil {
|
|
|
757
811
|
if (id_stream && (streams && streams.length)) {
|
|
758
812
|
let file_id = [];
|
|
759
813
|
yield Promise.all(streams.map(({ field_type, field_slug, field_data }) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
760
|
-
if (field_type == "multiple" && field_data && field_data['choose_stream']) {
|
|
814
|
+
if (field_type == "multiple" && field_data && field_data['choose_stream'] && field_data['self_table'] != "yes") {
|
|
761
815
|
let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { id: field_data['choose_stream'] }, raw: true, attributes: ['stream_slug', 'stream_prefix'] });
|
|
762
816
|
let table_f = stream['stream_prefix'] ? stream['stream_prefix'] + stream['stream_slug'] : stream['stream_slug'];
|
|
763
817
|
yield (0, db_1.remove)(this.ref, `${table}_${table_f}`, { where: { row_id: id } });
|
|
@@ -787,7 +841,7 @@ class StreamUtil {
|
|
|
787
841
|
stream = stream || {};
|
|
788
842
|
let id_stream = stream['id'], stream_prefix = stream['stream_prefix'];
|
|
789
843
|
if (fields && fields.length) {
|
|
790
|
-
fields = fields.filter(({ is_lang, field_type, field_data }) => field_type && (is_lang == "yes" || (field_type == "multiple" && field_data['self_table'] != "yes") || field_type == "image" || field_type == "file" || field_data['field_type'] == "encrypt"));
|
|
844
|
+
fields = fields.filter(({ is_lang, field_type, field_data }) => field_type && (is_lang == "yes" || (field_type == "multiple" && field_data['self_table'] != "yes") || field_type == "image" || field_type == "file" || field_data['field_type'] == "encrypt" || field_type == "stream"));
|
|
791
845
|
}
|
|
792
846
|
if (!Object.keys(raw_input).length) {
|
|
793
847
|
raw_input = input;
|
|
@@ -867,6 +921,9 @@ class StreamUtil {
|
|
|
867
921
|
else if (field_type == "multiple") {
|
|
868
922
|
fields_post.push(fields[index]);
|
|
869
923
|
}
|
|
924
|
+
else if (field_type == "stream" && input[field_slug]) {
|
|
925
|
+
data[field_slug] = JSON.stringify(input[field_slug]);
|
|
926
|
+
}
|
|
870
927
|
})));
|
|
871
928
|
}
|
|
872
929
|
if (id) {
|
|
@@ -912,7 +969,14 @@ class StreamUtil {
|
|
|
912
969
|
}
|
|
913
970
|
upsertBySlug(slug, namespace, index, { ref = "", req = {}, parent_slug = "", input = {}, tags = [], group = undefined } = {}) {
|
|
914
971
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
915
|
-
let fields = yield this.getBuilder(slug, namespace, { index: "fields" })
|
|
972
|
+
let fields = yield this.getBuilder(slug, namespace, { index: "fields" });
|
|
973
|
+
return yield this.upsertBySlugField(fields, index, { ref, req, parent_slug, input, tags, group });
|
|
974
|
+
});
|
|
975
|
+
}
|
|
976
|
+
upsertBySlugField(fields, index, { ref = "", req = {}, parent_slug = "", input = {}, tags = [], group = undefined } = {}) {
|
|
977
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
978
|
+
fields = fields || [];
|
|
979
|
+
let where_folder = {};
|
|
916
980
|
ref = ref || this.ref;
|
|
917
981
|
let files = (req.file ? [req.file] : (req.files || [])).reduce((total, item) => Object.assign(total, { [item['fieldname']]: item }), {});
|
|
918
982
|
let id_files = {}, fileUtil = new file_2.FileUtil(ref, this.option);
|
|
@@ -923,8 +987,8 @@ class StreamUtil {
|
|
|
923
987
|
}
|
|
924
988
|
let folder = yield fileUtil.createFolder(index, { parent_id: where_folder['parent_id'] });
|
|
925
989
|
let where_file = { folder_id: folder['id'], is_hidden: "yes" };
|
|
926
|
-
|
|
927
|
-
let query = yield file_1.FileModel.schema(ref, "_").findAll({ where:
|
|
990
|
+
let option_file = fileUtil.beforeFindFile(where_file, { tags, group, verify: true });
|
|
991
|
+
let query = yield file_1.FileModel.schema(ref, "_").findAll({ where: option_file.where, attributes: ['id', 'name'], raw: true });
|
|
928
992
|
id_files = query.reduce((total, item) => Object.assign(total, { [item['name']]: item['id'] }), {});
|
|
929
993
|
let input_files = [];
|
|
930
994
|
yield Promise.all(fields.map(({ field_type, field_slug, field_data }) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
@@ -1633,11 +1697,19 @@ class StreamUtil {
|
|
|
1633
1697
|
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type: sequelize.STRING(20) }, opt));
|
|
1634
1698
|
}
|
|
1635
1699
|
break;
|
|
1700
|
+
case "stream":
|
|
1701
|
+
if (field_data['self_table'] == "yes") {
|
|
1702
|
+
opt['defaultValue'] = undefined;
|
|
1703
|
+
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type: sequelize.TEXT('long') }, opt));
|
|
1704
|
+
}
|
|
1705
|
+
break;
|
|
1636
1706
|
}
|
|
1637
1707
|
}
|
|
1638
1708
|
}
|
|
1639
|
-
|
|
1640
|
-
|
|
1709
|
+
if (typeof field['sort_order'] == "undefined") {
|
|
1710
|
+
let sort_order = yield field_1.FieldModel.schema(this.ref, "_").max('sort_order', { where: { stream_id: stream['id'] } });
|
|
1711
|
+
field['sort_order'] = sort_order ? Number(sort_order + 1) : 1;
|
|
1712
|
+
}
|
|
1641
1713
|
if (!field['stream_id']) {
|
|
1642
1714
|
field['stream_id'] = stream['id'];
|
|
1643
1715
|
}
|
|
@@ -1791,6 +1863,9 @@ class StreamUtil {
|
|
|
1791
1863
|
}
|
|
1792
1864
|
field_data = _.pick(field_data, ['range']);
|
|
1793
1865
|
break;
|
|
1866
|
+
case "stream":
|
|
1867
|
+
field_data = _.pick(field_data, ['choose_stream', 'self_table']);
|
|
1868
|
+
break;
|
|
1794
1869
|
default:
|
|
1795
1870
|
field_data = {};
|
|
1796
1871
|
break;
|
|
@@ -1801,7 +1876,8 @@ class StreamUtil {
|
|
|
1801
1876
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1802
1877
|
let field_lang = [];
|
|
1803
1878
|
let file = req ? ((req.file ? [req.file] : [].concat(req.files || [])).reduce((total, item) => Object.assign(total, { [item['fieldname']]: item }), {})) : {};
|
|
1804
|
-
|
|
1879
|
+
for (let val of streams) {
|
|
1880
|
+
let { field_type, field_slug, is_required, is_lang, field_data, data } = val;
|
|
1805
1881
|
let field;
|
|
1806
1882
|
field_data = field_data || {};
|
|
1807
1883
|
is_required = (is_required == "yes" && (isNew || (!input[field_slug] && typeof input[field_slug] != "undefined")));
|
|
@@ -1897,11 +1973,13 @@ class StreamUtil {
|
|
|
1897
1973
|
allow_type = new RegExp(/^(image.*)$/).test(file[field_slug]['mimetype']);
|
|
1898
1974
|
if (!allow_type) {
|
|
1899
1975
|
input[field_slug] = mime.extension(file[field_slug]['mimetype']);
|
|
1976
|
+
input[field_slug] = input[field_slug] ? input[field_slug] : String(file[field_slug]['mimetype']).split('/')[1];
|
|
1900
1977
|
field = Joi.string().valid('image/*');
|
|
1901
1978
|
}
|
|
1902
1979
|
}
|
|
1903
1980
|
if (!allow_type && field_data['allowed_types']) {
|
|
1904
1981
|
let extension = String(mime.extension(file[field_slug]['mimetype']));
|
|
1982
|
+
extension = extension ? extension : String(file[field_slug]['mimetype']).split('/')[1];
|
|
1905
1983
|
if (!new RegExp(`^(${field_data['allowed_types']})$`).test(extension)) {
|
|
1906
1984
|
input[field_slug] = extension;
|
|
1907
1985
|
field = Joi.string().valid(...(field_data['allowed_types'].split('|') || []));
|
|
@@ -1932,6 +2010,18 @@ class StreamUtil {
|
|
|
1932
2010
|
}
|
|
1933
2011
|
field = field.regex(/^(\+|-)?(?:90(?:(?:\.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:\.[0-9]{1,6})?))$/).messages({ 'string.pattern.base': `${field_slug} invalid format` });
|
|
1934
2012
|
break;
|
|
2013
|
+
case "stream":
|
|
2014
|
+
if (field_data['choose_stream']) {
|
|
2015
|
+
let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { id: field_data['choose_stream'] }, raw: true, attributes: ['stream_slug', 'stream_namespace'] });
|
|
2016
|
+
if (stream) {
|
|
2017
|
+
let streams = yield this.findField(stream['stream_slug'], stream['stream_namespace'], { field_only: true, convert_option: true });
|
|
2018
|
+
let validator = yield this.getValidator(streams, { isNew: true, required: is_required, lang, stripUnknown: true });
|
|
2019
|
+
field = Joi.array().items(validator);
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
2022
|
+
field = field || Joi.array();
|
|
2023
|
+
field = is_required ? field.required().min(1) : field.allow(null);
|
|
2024
|
+
break;
|
|
1935
2025
|
}
|
|
1936
2026
|
field = field || Joi.any();
|
|
1937
2027
|
if (is_required)
|
|
@@ -1939,12 +2029,12 @@ class StreamUtil {
|
|
|
1939
2029
|
if (is_lang == "yes") {
|
|
1940
2030
|
if (!lang.length) {
|
|
1941
2031
|
field_lang.push({ field_slug, field });
|
|
1942
|
-
|
|
2032
|
+
continue;
|
|
1943
2033
|
}
|
|
1944
2034
|
field = Joi.object().keys(lang.reduce((total, i) => Object.assign(total, { [i['lang_code']]: field }), {}));
|
|
1945
2035
|
}
|
|
1946
2036
|
schema[field_slug] = field;
|
|
1947
|
-
}
|
|
2037
|
+
}
|
|
1948
2038
|
if (field_lang.length) {
|
|
1949
2039
|
lang = yield this.languageUtil.getLangsLive();
|
|
1950
2040
|
for (let item of field_lang) {
|
|
@@ -1966,9 +2056,9 @@ class StreamUtil {
|
|
|
1966
2056
|
return yield this.getValidator(streams, { req, isNew, required, lang, stripUnknown });
|
|
1967
2057
|
});
|
|
1968
2058
|
}
|
|
1969
|
-
validate(slug, namespace, input, { req = {}, isNew = false, default_value = {}, lang = [], stripUnknown = false, inputLang = false, inputLangSeparate = true, prefixMessage = "", builder = true, field_exclude = [] } = {}) {
|
|
2059
|
+
validate(slug, namespace, input, { req = {}, isNew = false, default_value = {}, lang = [], stripUnknown = false, inputLang = false, inputLangSeparate = true, prefixMessage = "", builder = true, field_option = {}, field_exclude = [] } = {}) {
|
|
1970
2060
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1971
|
-
let streams = yield this.getBuilder(slug, namespace, { index: "fields", builder, field_exclude });
|
|
2061
|
+
let streams = yield this.getBuilder(slug, namespace, { index: "fields", builder, field_option, field_exclude });
|
|
1972
2062
|
return this.validateByField(streams, input, { req, isNew, default_value, lang, stripUnknown, inputLang, inputLangSeparate, prefixMessage });
|
|
1973
2063
|
});
|
|
1974
2064
|
}
|
package/utils/user.d.ts
CHANGED
|
@@ -40,9 +40,9 @@ export declare const convertAddress: (data: any, { lang_code, transform }?: {
|
|
|
40
40
|
lang_code?: string;
|
|
41
41
|
transform?: boolean;
|
|
42
42
|
}) => Promise<any>;
|
|
43
|
-
export declare const getAddressIndex: (data: any, { condition,
|
|
43
|
+
export declare const getAddressIndex: (data: any, { condition, where }?: {
|
|
44
44
|
condition?: {};
|
|
45
|
-
|
|
45
|
+
where?: {};
|
|
46
46
|
}) => Promise<{}>;
|
|
47
47
|
export declare const getCountry: (ref: string, { organ_id, lang_code, setting }?: {
|
|
48
48
|
organ_id?: string;
|
package/utils/user.js
CHANGED
|
@@ -229,12 +229,11 @@ const convertAddress = (data, { lang_code = "en", transform = false } = {}) => t
|
|
|
229
229
|
return data;
|
|
230
230
|
});
|
|
231
231
|
exports.convertAddress = convertAddress;
|
|
232
|
-
const getAddressIndex = (data, { condition = {},
|
|
233
|
-
let result = {},
|
|
234
|
-
let order = [sequelize.literal(`FIELD(lang_code,${'\'' + where['lang_code'].join('\',\'') + '\''})`)];
|
|
232
|
+
const getAddressIndex = (data, { condition = {}, where = {} } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
233
|
+
let result = {}, bol = false;
|
|
235
234
|
if (!bol && data['country'] && !condition['country_id']) {
|
|
236
235
|
let country = yield country_1.CountryModel.schema("core", "_").findOne({
|
|
237
|
-
raw: true, where: Object.assign(Object.assign({}, where), { country_name: data['country'] }), attributes: ['id']
|
|
236
|
+
raw: true, where: Object.assign(Object.assign({}, where), { country_name: data['country'] }), attributes: ['id']
|
|
238
237
|
});
|
|
239
238
|
if (country) {
|
|
240
239
|
result['country_id'] = country['id'];
|
|
@@ -245,7 +244,7 @@ const getAddressIndex = (data, { condition = {}, lang_code = "en" } = {}) => tsl
|
|
|
245
244
|
if (!bol && data['state'] && !condition['state_id']) {
|
|
246
245
|
let country_id = condition['country_id'] || null;
|
|
247
246
|
let state = yield state_1.StateModel.schema("core", "_").findOne({
|
|
248
|
-
raw: true, where: Object.assign(Object.assign({}, where), { country_id, state_name: data['state'] }), attributes: ['id']
|
|
247
|
+
raw: true, where: Object.assign(Object.assign({}, where), { country_id, state_name: data['state'] }), attributes: ['id']
|
|
249
248
|
});
|
|
250
249
|
if (state) {
|
|
251
250
|
result['state_id'] = state['id'];
|
|
@@ -256,7 +255,7 @@ const getAddressIndex = (data, { condition = {}, lang_code = "en" } = {}) => tsl
|
|
|
256
255
|
if (!bol && data['city'] && !condition['city_id']) {
|
|
257
256
|
let state_id = condition['state_id'] || null;
|
|
258
257
|
let city = yield city_1.CityModel.schema("core", "_").findOne({
|
|
259
|
-
raw: true, where: Object.assign(Object.assign({}, where), { state_id, city_name: data['city'] }), attributes: ['id']
|
|
258
|
+
raw: true, where: Object.assign(Object.assign({}, where), { state_id, city_name: data['city'] }), attributes: ['id']
|
|
260
259
|
});
|
|
261
260
|
if (city) {
|
|
262
261
|
result['city_id'] = city['id'];
|
|
@@ -267,7 +266,7 @@ const getAddressIndex = (data, { condition = {}, lang_code = "en" } = {}) => tsl
|
|
|
267
266
|
if (!bol && data['district']) {
|
|
268
267
|
let city_id = condition['city_id'] || null;
|
|
269
268
|
let district = yield district_1.DistrictModel.schema("core", "_").findOne({
|
|
270
|
-
raw: true, where: Object.assign(Object.assign({}, where), { city_id, district_name: data['district'] }), attributes: ['id']
|
|
269
|
+
raw: true, where: Object.assign(Object.assign({}, where), { city_id, district_name: data['district'] }), attributes: ['id']
|
|
271
270
|
});
|
|
272
271
|
if (district)
|
|
273
272
|
result['district_id'] = district['id'];
|
|
@@ -277,7 +276,14 @@ const getAddressIndex = (data, { condition = {}, lang_code = "en" } = {}) => tsl
|
|
|
277
276
|
exports.getAddressIndex = getAddressIndex;
|
|
278
277
|
const getCountry = (ref, { organ_id = "", lang_code = "en", setting = {} } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
279
278
|
let where = { status: 1, lang_code }, default_country;
|
|
280
|
-
if (
|
|
279
|
+
if (organ_id) {
|
|
280
|
+
let query = yield organ_1.UserOrganModel.schema(ref, "_").findOne({ where: { id: organ_id }, attributes: ['setting'], raw: true });
|
|
281
|
+
setting = query ? (query['setting'] || {}) : {};
|
|
282
|
+
}
|
|
283
|
+
if (setting['mobile_code']) {
|
|
284
|
+
where['mobilecode'] = setting['mobile_code'].split(",");
|
|
285
|
+
}
|
|
286
|
+
else if (setting['country_id']) {
|
|
281
287
|
where['id'] = setting['country_id'].split(",");
|
|
282
288
|
}
|
|
283
289
|
let data = yield country_1.CountryModel.schema("core", "_").findAll({
|
|
@@ -311,9 +317,12 @@ const getGroupSetting = (ref, id, { attributes = ['id', 'shared', 'setting'] } =
|
|
|
311
317
|
exports.getGroupSetting = getGroupSetting;
|
|
312
318
|
const getGroupPermission = (ref, id, module, { permission = {}, group_shared = undefined, organ_id = 0, attributes = ['id', 'shared', 'setting'] } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
313
319
|
if (typeof group_shared == "undefined") {
|
|
314
|
-
let
|
|
315
|
-
if (
|
|
316
|
-
|
|
320
|
+
let group = yield (0, exports.getGroupSetting)(ref, id, { attributes });
|
|
321
|
+
if (group) {
|
|
322
|
+
permission['group_setting'] = group['setting'] || {};
|
|
323
|
+
permission['admin'] = group['shared'];
|
|
324
|
+
group_shared = group['shared'];
|
|
325
|
+
}
|
|
317
326
|
}
|
|
318
327
|
if (typeof group_shared != "undefined" && group_shared == 0) {
|
|
319
328
|
let query = yield group_permission_1.UserGroupPermissionModel.schema(ref, "_").findOne({
|
package/utils/validator.d.ts
CHANGED
|
@@ -57,5 +57,13 @@ export declare const convertService: ({ convert_type, schema }?: {
|
|
|
57
57
|
convert_type?: any[];
|
|
58
58
|
schema?: {};
|
|
59
59
|
}) => (req: any, res: any, next: any) => Promise<any>;
|
|
60
|
+
export declare const entryValidateService: ({ module, schema }?: {
|
|
61
|
+
module?: string[];
|
|
62
|
+
schema?: {};
|
|
63
|
+
}) => (req: any, res: any, next: any) => Promise<any>;
|
|
64
|
+
export declare const entryParamService: ({ module, schema }?: {
|
|
65
|
+
module?: string[];
|
|
66
|
+
schema?: {};
|
|
67
|
+
}) => (req: any, res: any, next: any) => Promise<any>;
|
|
60
68
|
export declare const createdByValidate: (req: any, res: any, next: any) => Promise<any>;
|
|
61
69
|
export {};
|
package/utils/validator.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createdByValidate = exports.convertService = exports.fieldService = exports.dataService = exports.printService = exports.exportService = exports.importService = exports.streamService = exports.multiService = exports.fileService = exports.paramService = exports.validateService = exports.validate = void 0;
|
|
3
|
+
exports.createdByValidate = exports.entryParamService = exports.entryValidateService = exports.convertService = exports.fieldService = exports.dataService = exports.printService = exports.exportService = exports.importService = exports.streamService = exports.multiService = exports.fileService = exports.paramService = exports.validateService = exports.validate = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const Joi = require("joi");
|
|
6
6
|
const errors_1 = require("../errors");
|
|
@@ -104,8 +104,9 @@ const fileService = (setup, { userfiles = false, required = false, fileSize = 0,
|
|
|
104
104
|
}
|
|
105
105
|
let option = setup(validate);
|
|
106
106
|
let upload = userfiles ? option.array("userfiles", maxCount) : option.any();
|
|
107
|
+
let schema = userfiles ? { tags: Joi.array().allow('', null).items(Joi.object().keys({ slug: Joi.string(), type: Joi.string() })) } : { tags: Joi.any() };
|
|
107
108
|
return upload(req, res, (err) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
108
|
-
return (0, exports.validateService)(Joi.object().keys(
|
|
109
|
+
return (0, exports.validateService)(Joi.object().keys(schema).unknown(), { field_omit: [] })(req, res, () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
109
110
|
try {
|
|
110
111
|
let field = userfiles ? "userfiles" : undefined;
|
|
111
112
|
if (err) {
|
|
@@ -180,7 +181,7 @@ const multiService = ({ field = undefined, record = undefined, record_addition =
|
|
|
180
181
|
schema['field'] = Joi.string().valid(...field);
|
|
181
182
|
schema['value'] = value_schema;
|
|
182
183
|
}
|
|
183
|
-
return (0, exports.validateService)(Joi.object().keys(Object.assign(Object.assign({}, schema), { records: Joi.array().items(Joi.object().keys(Object.assign({ id: Joi.number().required() }, record)).options({ stripUnknown: true })).min(1).required() })).options({ stripUnknown: true }));
|
|
184
|
+
return (0, exports.validateService)(Joi.object().keys(Object.assign(Object.assign({}, schema), { records: Joi.array().items(Joi.object().keys(Object.assign({ id: Joi.number().required() }, record)).options({ stripUnknown: true })).min(1).required() })).options({ stripUnknown: true }), { field_omit: [] });
|
|
184
185
|
};
|
|
185
186
|
exports.multiService = multiService;
|
|
186
187
|
const streamService = ({ slug = "", name_space = "", param = "index" } = {}) => (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -207,7 +208,7 @@ const streamService = ({ slug = "", name_space = "", param = "index" } = {}) =>
|
|
|
207
208
|
});
|
|
208
209
|
exports.streamService = streamService;
|
|
209
210
|
const importService = ({ import_type = [], schema = {} } = {}) => (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
210
|
-
return (0, exports.validateService)(Joi.object().keys(Object.assign({ import_type: Joi.string().valid(...import_type) }, schema)).unknown())(req, res, next);
|
|
211
|
+
return (0, exports.validateService)(Joi.object().keys(Object.assign({ import_type: Joi.string().valid(...import_type) }, schema)).unknown(), { field_omit: [] })(req, res, next);
|
|
211
212
|
});
|
|
212
213
|
exports.importService = importService;
|
|
213
214
|
const exportService = ({ file_type = undefined, export_type = [], limit = 200, schema = {} } = {}) => (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -240,8 +241,9 @@ const exportService = ({ file_type = undefined, export_type = [], limit = 200, s
|
|
|
240
241
|
});
|
|
241
242
|
exports.exportService = exportService;
|
|
242
243
|
const printService = ({ print_type = [], schema = {} } = {}) => (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
244
|
+
let config = res.locals.config || {};
|
|
243
245
|
if (req.query.ids) {
|
|
244
|
-
res.locals.option = Object.assign(Object.assign({}, (res.locals.option || {})), { ids: String(req.query.ids).split(',') });
|
|
246
|
+
res.locals.option = Object.assign(Object.assign({}, (res.locals.option || {})), { ids: String(req.query.ids).split(',').slice(0, (config['maxQueryIds'] || 25)) });
|
|
245
247
|
}
|
|
246
248
|
if (print_type && !req.query.print_type) {
|
|
247
249
|
req.query.print_type = print_type[0];
|
|
@@ -261,8 +263,9 @@ const printService = ({ print_type = [], schema = {} } = {}) => (req, res, next)
|
|
|
261
263
|
});
|
|
262
264
|
exports.printService = printService;
|
|
263
265
|
const dataService = ({ schema = {} } = {}) => (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
266
|
+
let config = res.locals.config || {};
|
|
264
267
|
if (req.query.ids) {
|
|
265
|
-
res.locals.option = Object.assign(Object.assign({}, (res.locals.option || {})), { ids: String(req.query.ids).split(',') });
|
|
268
|
+
res.locals.option = Object.assign(Object.assign({}, (res.locals.option || {})), { ids: String(req.query.ids).split(',').slice(0, (config['maxQueryIds'] || 25)) });
|
|
266
269
|
}
|
|
267
270
|
let option = Object.keys(schema || {});
|
|
268
271
|
if (option.length) {
|
|
@@ -288,9 +291,23 @@ const fieldService = ({ field = [], schema = {} } = {}) => (req, res, next) => t
|
|
|
288
291
|
});
|
|
289
292
|
exports.fieldService = fieldService;
|
|
290
293
|
const convertService = ({ convert_type = [], schema = {} } = {}) => (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
291
|
-
return (0, exports.validateService)(Joi.object().keys(Object.assign({ ids: Joi.array().items(Joi.number()).min(1), convert_type: Joi.string().valid(...convert_type) }, schema)).unknown())(req, res, next);
|
|
294
|
+
return (0, exports.validateService)(Joi.object().keys(Object.assign({ ids: Joi.array().items(Joi.number()).min(1), convert_type: Joi.string().valid(...convert_type) }, schema)).unknown(), { field_omit: [] })(req, res, next);
|
|
292
295
|
});
|
|
293
296
|
exports.convertService = convertService;
|
|
297
|
+
const entryValidateService = ({ module = ['contact'], schema = {} } = {}) => (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
298
|
+
return (0, exports.validateService)(Joi.object().keys(Object.assign({ module: Joi.string().valid(...module), entry_id: Joi.number().required() }, schema)).unknown(), { field_omit: [] })(req, res, next);
|
|
299
|
+
});
|
|
300
|
+
exports.entryValidateService = entryValidateService;
|
|
301
|
+
const entryParamService = ({ module = ['contact'], schema = {} } = {}) => (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
302
|
+
if (req.query.module) {
|
|
303
|
+
res.locals.option = Object.assign(Object.assign({}, (res.locals.option || {})), { module: req.query.module });
|
|
304
|
+
}
|
|
305
|
+
if (req.query.entry_id) {
|
|
306
|
+
res.locals.option = Object.assign(Object.assign({}, (res.locals.option || {})), { entry_id: req.query.entry_id });
|
|
307
|
+
}
|
|
308
|
+
return (0, exports.paramService)(Joi.object().keys(Object.assign({ module: Joi.string().valid(...module), entry_id: Joi.number().required() }, schema)).unknown())(req, res, next);
|
|
309
|
+
});
|
|
310
|
+
exports.entryParamService = entryParamService;
|
|
294
311
|
exports.createdByValidate = (0, exports.validateService)(Joi.object().keys({
|
|
295
312
|
created_by: Joi.alternatives().try(Joi.string(), Joi.number()).required()
|
|
296
313
|
}).unknown());
|