@digione/node-custom-api 0.0.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.
- package/constants/header.d.ts +10 -0
- package/constants/header.js +2 -0
- package/constants/http.d.ts +44 -0
- package/constants/http.js +2 -0
- package/database/index.d.ts +3 -0
- package/database/index.js +34 -0
- package/database/redis.d.ts +15 -0
- package/database/redis.js +68 -0
- package/errors/authError.d.ts +55 -0
- package/errors/authError.js +57 -0
- package/errors/index.d.ts +32 -0
- package/errors/index.js +113 -0
- package/index.d.ts +1 -0
- package/index.js +4 -0
- package/interface/param.d.ts +41 -0
- package/interface/param.js +2 -0
- package/middlewares/accessToken.d.ts +13 -0
- package/middlewares/accessToken.js +200 -0
- package/middlewares/coreSite.d.ts +4 -0
- package/middlewares/coreSite.js +52 -0
- package/middlewares/paramQuery.d.ts +16 -0
- package/middlewares/paramQuery.js +220 -0
- package/middlewares/refSite.d.ts +2 -0
- package/middlewares/refSite.js +58 -0
- package/middlewares/refreshToken.d.ts +2 -0
- package/middlewares/refreshToken.js +56 -0
- package/middlewares/systemSite.d.ts +6 -0
- package/middlewares/systemSite.js +106 -0
- package/middlewares/webSite.d.ts +7 -0
- package/middlewares/webSite.js +109 -0
- package/models/core/city.d.ts +66 -0
- package/models/core/city.js +79 -0
- package/models/core/country.d.ts +35 -0
- package/models/core/country.js +44 -0
- package/models/core/district.d.ts +17 -0
- package/models/core/district.js +23 -0
- package/models/core/domain.d.ts +57 -0
- package/models/core/domain.js +75 -0
- package/models/core/site.d.ts +60 -0
- package/models/core/site.js +70 -0
- package/models/core/state.d.ts +49 -0
- package/models/core/state.js +58 -0
- package/models/core/zipcode.d.ts +5 -0
- package/models/core/zipcode.js +28 -0
- package/models/stream/field.d.ts +60 -0
- package/models/stream/field.js +66 -0
- package/models/stream/field_section.d.ts +33 -0
- package/models/stream/field_section.js +39 -0
- package/models/stream/stream.d.ts +89 -0
- package/models/stream/stream.js +97 -0
- package/models/system/currency.d.ts +5 -0
- package/models/system/currency.js +32 -0
- package/models/system/email_log.d.ts +5 -0
- package/models/system/email_log.js +54 -0
- package/models/system/email_sender.d.ts +22 -0
- package/models/system/email_sender.js +53 -0
- package/models/system/email_template.d.ts +5 -0
- package/models/system/email_template.js +49 -0
- package/models/system/file.d.ts +98 -0
- package/models/system/file.js +111 -0
- package/models/system/folder.d.ts +59 -0
- package/models/system/folder.js +78 -0
- package/models/system/language.d.ts +44 -0
- package/models/system/language.js +50 -0
- package/models/system/metadata.d.ts +5 -0
- package/models/system/metadata.js +24 -0
- package/models/system/module.d.ts +5 -0
- package/models/system/module.js +35 -0
- package/models/system/notification_log.d.ts +25 -0
- package/models/system/notification_log.js +76 -0
- package/models/system/notification_module.d.ts +5 -0
- package/models/system/notification_module.js +35 -0
- package/models/system/notification_setting.d.ts +22 -0
- package/models/system/notification_setting.js +44 -0
- package/models/system/notification_template.d.ts +5 -0
- package/models/system/notification_template.js +49 -0
- package/models/system/notification_type.d.ts +5 -0
- package/models/system/notification_type.js +38 -0
- package/models/system/payment_transaction.d.ts +5 -0
- package/models/system/payment_transaction.js +46 -0
- package/models/system/pdf_page.d.ts +5 -0
- package/models/system/pdf_page.js +39 -0
- package/models/system/pdf_template.d.ts +22 -0
- package/models/system/pdf_template.js +67 -0
- package/models/system/review_template.d.ts +5 -0
- package/models/system/review_template.js +35 -0
- package/models/system/review_vote.d.ts +5 -0
- package/models/system/review_vote.js +36 -0
- package/models/system/session.d.ts +5 -0
- package/models/system/session.js +19 -0
- package/models/system/setting.d.ts +15 -0
- package/models/system/setting.js +22 -0
- package/models/system/site_token.d.ts +25 -0
- package/models/system/site_token.js +31 -0
- package/models/system/sms_log.d.ts +4 -0
- package/models/system/sms_log.js +52 -0
- package/models/system/sms_setting.d.ts +4 -0
- package/models/system/sms_setting.js +39 -0
- package/models/system/sms_template.d.ts +5 -0
- package/models/system/sms_template.js +48 -0
- package/models/system/tax.d.ts +5 -0
- package/models/system/tax.js +33 -0
- package/models/system/website.d.ts +56 -0
- package/models/system/website.js +66 -0
- package/models/system/zone.d.ts +57 -0
- package/models/system/zone.js +141 -0
- package/models/user/access_token.d.ts +37 -0
- package/models/user/access_token.js +46 -0
- package/models/user/address.d.ts +69 -0
- package/models/user/address.js +119 -0
- package/models/user/b2b_group.d.ts +57 -0
- package/models/user/b2b_group.js +63 -0
- package/models/user/group.d.ts +28 -0
- package/models/user/group.js +34 -0
- package/models/user/group_permission.d.ts +4 -0
- package/models/user/group_permission.js +27 -0
- package/models/user/notification_token.d.ts +32 -0
- package/models/user/notification_token.js +39 -0
- package/models/user/organ.d.ts +85 -0
- package/models/user/organ.js +100 -0
- package/models/user/profile.d.ts +31 -0
- package/models/user/profile.js +37 -0
- package/models/user/refresh_token.d.ts +41 -0
- package/models/user/refresh_token.js +51 -0
- package/models/user/team.d.ts +59 -0
- package/models/user/team.js +71 -0
- package/models/user/user.d.ts +79 -0
- package/models/user/user.js +100 -0
- package/models/user/user_organ.d.ts +52 -0
- package/models/user/user_organ.js +87 -0
- package/package.json +59 -0
- package/utils/auth.d.ts +25 -0
- package/utils/auth.js +137 -0
- package/utils/config.d.ts +8 -0
- package/utils/config.js +6 -0
- package/utils/core.d.ts +6 -0
- package/utils/core.js +39 -0
- package/utils/currency.d.ts +16 -0
- package/utils/currency.js +38 -0
- package/utils/db.d.ts +94 -0
- package/utils/db.js +399 -0
- package/utils/file.d.ts +232 -0
- package/utils/file.js +772 -0
- package/utils/helper.d.ts +113 -0
- package/utils/helper.js +508 -0
- package/utils/index.d.ts +10 -0
- package/utils/index.js +13 -0
- package/utils/language.d.ts +18 -0
- package/utils/language.js +48 -0
- package/utils/metadata.d.ts +18 -0
- package/utils/metadata.js +50 -0
- package/utils/session.d.ts +37 -0
- package/utils/session.js +60 -0
- package/utils/setting.d.ts +14 -0
- package/utils/setting.js +70 -0
- package/utils/stream.d.ts +303 -0
- package/utils/stream.js +1893 -0
- package/utils/user.d.ts +61 -0
- package/utils/user.js +282 -0
- package/utils/validator.d.ts +56 -0
- package/utils/validator.js +269 -0
package/utils/stream.js
ADDED
|
@@ -0,0 +1,1893 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StreamUtil = exports.getValueFunctionMultiple = exports.getValueFunctionRelationship = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const stream_1 = require("../models/stream/stream");
|
|
6
|
+
const field_section_1 = require("../models/stream/field_section");
|
|
7
|
+
const field_1 = require("../models/stream/field");
|
|
8
|
+
const file_1 = require("../models/system/file");
|
|
9
|
+
const file_2 = require("./file");
|
|
10
|
+
const sequelize_1 = require("sequelize");
|
|
11
|
+
const language_1 = require("./language");
|
|
12
|
+
const errors_1 = require("../errors");
|
|
13
|
+
const validator_1 = require("./validator");
|
|
14
|
+
const helper_1 = require("./helper");
|
|
15
|
+
const helper_2 = require("./helper");
|
|
16
|
+
const db_1 = require("./db");
|
|
17
|
+
const user_1 = require("../utils/user");
|
|
18
|
+
const redis_1 = require("../database/redis");
|
|
19
|
+
const database_1 = require("../database");
|
|
20
|
+
const sequelize = require("sequelize");
|
|
21
|
+
const dottie = require("dottie");
|
|
22
|
+
const mime = require("mime-types");
|
|
23
|
+
const i18n_1 = require("i18n");
|
|
24
|
+
const _ = require("lodash");
|
|
25
|
+
const Joi = require("joi");
|
|
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']) {
|
|
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: ['*'] });
|
|
31
|
+
if (result[key]) {
|
|
32
|
+
(0, helper_2.getValueFunctionLang)(title_column)(result[key], { result: result[key], lang_code });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
exports.getValueFunctionRelationship = getValueFunctionRelationship;
|
|
38
|
+
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']) {
|
|
41
|
+
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'}`] });
|
|
43
|
+
if (result[key] && result[key].length) {
|
|
44
|
+
result[key] = result[key].map(i => (0, helper_2.getValueByLang)(i, { result: i, field: [title_column], lang_code }));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
exports.getValueFunctionMultiple = getValueFunctionMultiple;
|
|
50
|
+
class StreamUtil {
|
|
51
|
+
constructor(ref, option, fileUtil) {
|
|
52
|
+
this.folder_parent = "stream";
|
|
53
|
+
this.initColumn = {
|
|
54
|
+
id: {
|
|
55
|
+
type: sequelize.INTEGER({ length: 11 }),
|
|
56
|
+
primaryKey: true,
|
|
57
|
+
autoIncrement: true,
|
|
58
|
+
allowNull: false
|
|
59
|
+
},
|
|
60
|
+
created: {
|
|
61
|
+
type: sequelize.DATE,
|
|
62
|
+
allowNull: false
|
|
63
|
+
},
|
|
64
|
+
updated: {
|
|
65
|
+
type: sequelize.DATE
|
|
66
|
+
},
|
|
67
|
+
ordering_count: sequelize.INTEGER({ length: 11 }),
|
|
68
|
+
created_by: {
|
|
69
|
+
type: sequelize.INTEGER,
|
|
70
|
+
allowNull: false
|
|
71
|
+
},
|
|
72
|
+
organ_id: sequelize.INTEGER
|
|
73
|
+
};
|
|
74
|
+
this.builder = {};
|
|
75
|
+
this.ref = ref;
|
|
76
|
+
this.option = option || {};
|
|
77
|
+
this.fileUtil = fileUtil || new file_2.FileUtil(this.ref, this.option);
|
|
78
|
+
this.languageUtil = new language_1.LanguageUtil(this.ref, this.option);
|
|
79
|
+
}
|
|
80
|
+
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 } = {}) {
|
|
81
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
let data;
|
|
83
|
+
let where = {};
|
|
84
|
+
let field_type = [];
|
|
85
|
+
if (require) {
|
|
86
|
+
where['is_required'] = "yes";
|
|
87
|
+
}
|
|
88
|
+
if (is_required) {
|
|
89
|
+
where['is_required'] = is_required;
|
|
90
|
+
}
|
|
91
|
+
if (text) {
|
|
92
|
+
field_type.push('text');
|
|
93
|
+
}
|
|
94
|
+
if (bullets) {
|
|
95
|
+
field_type.push('bullets');
|
|
96
|
+
}
|
|
97
|
+
if (file) {
|
|
98
|
+
field_type.push('file');
|
|
99
|
+
}
|
|
100
|
+
if (image) {
|
|
101
|
+
field_type.push('image');
|
|
102
|
+
}
|
|
103
|
+
if (relationship) {
|
|
104
|
+
field_type.push('relationship');
|
|
105
|
+
}
|
|
106
|
+
if (multiple) {
|
|
107
|
+
field_type.push('multiple');
|
|
108
|
+
}
|
|
109
|
+
if (redis_1.redisReady) {
|
|
110
|
+
let cache = yield redis_1.redisClient.get(this.ref + "_stream_" + namespace + "_" + slug);
|
|
111
|
+
if (cache) {
|
|
112
|
+
data = JSON.parse(cache);
|
|
113
|
+
data = yield this.findFieldStatic(data, { field_type, field_exclude, field_slug, include, include_only });
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
try {
|
|
117
|
+
if (!data) {
|
|
118
|
+
if (!redis_1.redisReady && field_type.length) {
|
|
119
|
+
where['field_type'] = field_type;
|
|
120
|
+
}
|
|
121
|
+
if (!redis_1.redisReady && field_exclude.length) {
|
|
122
|
+
where[sequelize_1.Op.and] = (where[sequelize_1.Op.and] || []).concat({ field_slug: { [sequelize_1.Op.not]: field_exclude } });
|
|
123
|
+
}
|
|
124
|
+
if (!redis_1.redisReady && field_slug.length) {
|
|
125
|
+
where[sequelize_1.Op.and] = (where[sequelize_1.Op.and] || []).concat({ field_slug });
|
|
126
|
+
}
|
|
127
|
+
if (!redis_1.redisReady && include) {
|
|
128
|
+
let group = Object.keys(include), op = !include_only ? [{ group: { [sequelize_1.Op.is]: null } }] : [];
|
|
129
|
+
if (group.length) {
|
|
130
|
+
op.push({ group });
|
|
131
|
+
}
|
|
132
|
+
where[sequelize_1.Op.and] = (where[sequelize_1.Op.and] || []).concat({ [sequelize_1.Op.or]: op });
|
|
133
|
+
}
|
|
134
|
+
let query = yield stream_1.StreamModel.schema(this.ref, '_').findOne({
|
|
135
|
+
where: {
|
|
136
|
+
stream_slug: slug,
|
|
137
|
+
stream_namespace: namespace,
|
|
138
|
+
},
|
|
139
|
+
include: [
|
|
140
|
+
stream_1.StreamAssociation.field.join(this.ref, { where, required: field_required })
|
|
141
|
+
],
|
|
142
|
+
order: [[{ model: stream_1.StreamAssociation.field.model, as: stream_1.StreamAssociation.field.alias }, 'sort_order', 'ASC']]
|
|
143
|
+
});
|
|
144
|
+
if (!query) {
|
|
145
|
+
return field_only ? [] : {};
|
|
146
|
+
}
|
|
147
|
+
data = query.toJSON();
|
|
148
|
+
if (redis_1.redisReady) {
|
|
149
|
+
yield redis_1.redisClient.set(this.ref + "_stream_" + namespace + "_" + slug, JSON.stringify(data));
|
|
150
|
+
data = this.findFieldStatic(data, { field_type, field_exclude, field_slug, include, include_only });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (convert_option) {
|
|
154
|
+
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 });
|
|
155
|
+
}
|
|
156
|
+
if (field_only) {
|
|
157
|
+
return data['fields'];
|
|
158
|
+
}
|
|
159
|
+
return data;
|
|
160
|
+
}
|
|
161
|
+
catch (err) {
|
|
162
|
+
return Promise.reject({ code: 400, message: err.message });
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
findFieldStatic(data, { field_type = [], field_exclude = [], field_slug = [], include = undefined, include_only = false } = {}) {
|
|
167
|
+
let filter = [];
|
|
168
|
+
if (field_type.length) {
|
|
169
|
+
let value = field_type.reduce((total, i) => Object.assign(total, { [i]: 1 }), {});
|
|
170
|
+
filter.push({ val: (field) => (typeof value[field['field_type']] != "undefined") });
|
|
171
|
+
}
|
|
172
|
+
if (field_exclude.length) {
|
|
173
|
+
let value = field_exclude.reduce((total, i) => Object.assign(total, { [i]: 1 }), {});
|
|
174
|
+
filter.push({ val: (field) => (typeof value[field['field_slug']] == "undefined") });
|
|
175
|
+
}
|
|
176
|
+
if (field_slug.length) {
|
|
177
|
+
let value = field_slug.reduce((total, i) => Object.assign(total, { [i]: 1 }), {});
|
|
178
|
+
filter.push({ val: (field) => (typeof value[field['field_slug']] != "undefined") });
|
|
179
|
+
}
|
|
180
|
+
if (include) {
|
|
181
|
+
let con1 = (field) => !field['group'];
|
|
182
|
+
let con2 = (field) => false;
|
|
183
|
+
if (include_only)
|
|
184
|
+
con1 = (field) => false;
|
|
185
|
+
let group = Object.keys(include);
|
|
186
|
+
if (group.length) {
|
|
187
|
+
con2 = (field) => (typeof include[field['group']] != "undefined");
|
|
188
|
+
}
|
|
189
|
+
filter.push({ val: (field) => con1(field) || con2(field) });
|
|
190
|
+
}
|
|
191
|
+
if (filter.length) {
|
|
192
|
+
data['fields'] = data['fields'].filter(field => {
|
|
193
|
+
let bol = true;
|
|
194
|
+
for (let item of filter) {
|
|
195
|
+
if (!item.val(field)) {
|
|
196
|
+
bol = false;
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return bol;
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
return data;
|
|
204
|
+
}
|
|
205
|
+
buildStream(slug, namespace, { query = true, builder = true, fetch = false, attribute = false, attributes = null, field_exclude = [], include = {} } = {}) {
|
|
206
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
207
|
+
let build = this.builder[slug + "_" + namespace] || { stream: null, fields: null, fetchFn: [], attributes: null };
|
|
208
|
+
if (!build['fields']) {
|
|
209
|
+
let data = {};
|
|
210
|
+
if (query)
|
|
211
|
+
data = yield this.findField(slug, namespace, { field_only: false, convert_option: true, field_exclude });
|
|
212
|
+
build['fields'] = data['fields'] || [];
|
|
213
|
+
build['stream'] = _.omit(data, ['fields']);
|
|
214
|
+
}
|
|
215
|
+
if ((fetch || attribute) && build['fields'].length) {
|
|
216
|
+
let fetchFn = [];
|
|
217
|
+
attributes = attributes || {};
|
|
218
|
+
for (let field of build['fields']) {
|
|
219
|
+
let field_data = field['field_data'] || {};
|
|
220
|
+
attributes[field['field_slug']] = 1;
|
|
221
|
+
if (field_data['date'] && field_data['date']['field_target']) {
|
|
222
|
+
delete attributes[field['field_slug']];
|
|
223
|
+
}
|
|
224
|
+
if (fetch) {
|
|
225
|
+
if (field['is_lang'] == "yes") {
|
|
226
|
+
fetchFn.push((0, helper_2.getValueFunctionLang)(field['field_slug']));
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
switch (field['field_type']) {
|
|
230
|
+
case "file":
|
|
231
|
+
case "image":
|
|
232
|
+
include[field['field_slug']] && fetchFn.push((0, helper_2.getValueFunctionFile)(field['field_slug']));
|
|
233
|
+
break;
|
|
234
|
+
case "relationship":
|
|
235
|
+
case "multiple":
|
|
236
|
+
if (include[field['field_slug']] && field_data['choose_stream']) {
|
|
237
|
+
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'] });
|
|
238
|
+
if (stream['stream_slug']) {
|
|
239
|
+
field_data['table'] = stream['stream_prefix'] ? stream['stream_prefix'] + stream['stream_slug'] : stream['stream_slug'];
|
|
240
|
+
if (field['field_type'] == "relationship") {
|
|
241
|
+
fetchFn.push((0, exports.getValueFunctionRelationship)(field['field_slug'], { field_data }));
|
|
242
|
+
}
|
|
243
|
+
else if (field['field_type'] == "multiple") {
|
|
244
|
+
field_data['association'] = (build['stream']['stream_prefix'] ? build['stream']['stream_prefix'] + build['stream']['stream_slug'] : build['stream']['stream_slug']) + "_" + field_data['table'];
|
|
245
|
+
fetchFn.push((0, exports.getValueFunctionMultiple)(field['field_slug'], { field_data }));
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (field['field_type'] == "multiple") {
|
|
254
|
+
delete attributes[field['field_slug']];
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
build['fetchFn'] = fetchFn;
|
|
258
|
+
}
|
|
259
|
+
if (attributes)
|
|
260
|
+
build['attributes'] = attributes;
|
|
261
|
+
if (builder)
|
|
262
|
+
this.builder[slug + "_" + namespace] = build;
|
|
263
|
+
return build;
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
getBuilder(slug, namespace, { query = true, index = "", builder = true, field_exclude = [] } = {}) {
|
|
267
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
268
|
+
let result = yield this.buildStream(slug, namespace, { query, builder, field_exclude });
|
|
269
|
+
return index ? result[index] : result;
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
getValue(slug, namespace, value, { query = false, transform = true, spread = false, spread_all = false, raw = false, lang_code = undefined } = {}) {
|
|
273
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
274
|
+
let result = {};
|
|
275
|
+
let fetchFn = yield this.getBuilder(slug, namespace, { query, index: "fetchFn" });
|
|
276
|
+
lang_code = lang_code || this.option.lang_code;
|
|
277
|
+
for (let f of fetchFn || []) {
|
|
278
|
+
yield f(value, { result, ref: this.ref, transform, spread, spread_all, raw, lang_code, fileUtil: this.fileUtil });
|
|
279
|
+
}
|
|
280
|
+
return result;
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
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 } = {}) {
|
|
284
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
285
|
+
let fields = [], sections = [];
|
|
286
|
+
let exclude = option['excludeObj'] || {};
|
|
287
|
+
lang_code = option['lang_code'] || lang_code;
|
|
288
|
+
if (multiple_table) {
|
|
289
|
+
let result = _.keyBy(default_value, 'lang_code');
|
|
290
|
+
if (result['undefined'] || !Object.keys(result).length)
|
|
291
|
+
multiple_table = false;
|
|
292
|
+
else
|
|
293
|
+
default_value = result;
|
|
294
|
+
}
|
|
295
|
+
section = option['section'] || section;
|
|
296
|
+
if (section && id_stream) {
|
|
297
|
+
sections = yield field_section_1.FieldSectionModel.schema(this.ref, "_").findAll({
|
|
298
|
+
where: { stream_id: id_stream, status: "show" }, attributes: ['id', 'title', 'field_width', 'index'], raw: true,
|
|
299
|
+
order: [['ordering_count', 'asc']]
|
|
300
|
+
});
|
|
301
|
+
if (sections.length) {
|
|
302
|
+
sections = sections.map(i => { (0, helper_2.getValueFunctionLang)('title')(i, { result: i, lang_code }); return i; });
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
for (let item of streams) {
|
|
306
|
+
if (!item || exclude[item['field_slug']]) {
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
let field_data = {};
|
|
310
|
+
if (field_option[item['field_slug']]) {
|
|
311
|
+
field_data = field_option[item['field_slug']]['field_data'] || field_data;
|
|
312
|
+
item = Object.assign(Object.assign({}, item), field_option[item['field_slug']]);
|
|
313
|
+
}
|
|
314
|
+
if (item['field_data']) {
|
|
315
|
+
if (typeof item['field_data'] == 'object') {
|
|
316
|
+
field_data = item['field_data'];
|
|
317
|
+
}
|
|
318
|
+
else if (typeof item['field_data'] == 'string') {
|
|
319
|
+
try {
|
|
320
|
+
field_data = JSON.parse(item['field_data']);
|
|
321
|
+
}
|
|
322
|
+
catch (err) { }
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
if (typeof field_data['default_value'] !== 'undefined' || field_data['default_color']) {
|
|
326
|
+
if (field_data['default_color']) {
|
|
327
|
+
field_data['default_value'] = field_data['default_color'];
|
|
328
|
+
}
|
|
329
|
+
item['field_value'] = item['field_value'] || field_data['default_value'];
|
|
330
|
+
}
|
|
331
|
+
if (multiple_table && default_value[lang_code] && typeof default_value[lang_code][item['field_slug']] !== 'undefined') {
|
|
332
|
+
item['field_value'] = default_value[lang_code][item['field_slug']];
|
|
333
|
+
}
|
|
334
|
+
else if (multiple_spread && typeof default_value[item['field_slug'] + "_" + default_lang] !== 'undefined') {
|
|
335
|
+
item['field_value'] = default_value[item['field_slug'] + "_" + default_lang];
|
|
336
|
+
}
|
|
337
|
+
else if (typeof default_value[item['field_slug']] !== 'undefined') {
|
|
338
|
+
item['field_value'] = default_value[item['field_slug']];
|
|
339
|
+
}
|
|
340
|
+
if (item['instructions']) {
|
|
341
|
+
(0, helper_2.getValueFunctionLang)('instructions')(item, { result: item, lang_code });
|
|
342
|
+
}
|
|
343
|
+
if (item['field_name']) {
|
|
344
|
+
(0, helper_2.getValueFunctionLang)('field_name')(item, { result: item, lang_code });
|
|
345
|
+
}
|
|
346
|
+
item['field_name_raw'] = item['field_name'] ? item['field_name'] : "";
|
|
347
|
+
item = Object.assign(Object.assign({}, item), addition);
|
|
348
|
+
if (convert_option) {
|
|
349
|
+
let field_name = String(item['field_name']);
|
|
350
|
+
if (field_name.indexOf('lang:') > -1) {
|
|
351
|
+
item['field_name'] = (0, i18n_1.__)({ phrase: field_name, locale: lang_code });
|
|
352
|
+
}
|
|
353
|
+
if (item['instructions']) {
|
|
354
|
+
item['instructions'] = (0, i18n_1.__)({ phrase: item['instructions'], locale: lang_code });
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
if (!no_lang && item['is_lang'] && item['is_lang'] == "yes" && !lang.length) {
|
|
358
|
+
lang = yield this.languageUtil.getLangsLive();
|
|
359
|
+
if (!lang.length)
|
|
360
|
+
no_lang = true;
|
|
361
|
+
}
|
|
362
|
+
if (no_lang) {
|
|
363
|
+
item['is_lang'] = "no";
|
|
364
|
+
}
|
|
365
|
+
if (item['is_lang'] && item['is_lang'] == "yes") {
|
|
366
|
+
let field_default;
|
|
367
|
+
if (multiple_table) {
|
|
368
|
+
field_default = lang.length && default_value[lang[0]['lang_code']] ? default_value[lang[0]['lang_code']][item['field_slug']] : null;
|
|
369
|
+
}
|
|
370
|
+
else if (multiple_spread) {
|
|
371
|
+
field_default = lang.length ? default_value[item['field_slug'] + "_" + lang[0]['lang_code']] : null;
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
let value = {};
|
|
375
|
+
if (default_value[item['field_slug']])
|
|
376
|
+
try {
|
|
377
|
+
value = JSON.parse(default_value[item['field_slug']]);
|
|
378
|
+
}
|
|
379
|
+
catch (err) { }
|
|
380
|
+
default_value[item['field_slug']] = value;
|
|
381
|
+
field_default = lang.length ? default_value[item['field_slug']][lang[0]['lang_code']] : null;
|
|
382
|
+
}
|
|
383
|
+
item['data'] = lang.map(i => {
|
|
384
|
+
let include = {};
|
|
385
|
+
if (multiple_table) {
|
|
386
|
+
include['field_value'] = default_value[i['lang_code']] && default_value[i['lang_code']][item['field_slug']];
|
|
387
|
+
}
|
|
388
|
+
else if (multiple_spread) {
|
|
389
|
+
include['field_value'] = default_value[item['field_slug'] + "_" + i['lang_code']];
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
include['field_value'] = default_value[item['field_slug']][i['lang_code']];
|
|
393
|
+
item['field_value'] = include['field_value'];
|
|
394
|
+
}
|
|
395
|
+
include['field_value'] = include['field_value'] || item['field_value'];
|
|
396
|
+
if (!include['field_value'] && field_default) {
|
|
397
|
+
include['field_value'] = field_default;
|
|
398
|
+
}
|
|
399
|
+
if (item['instructions']) {
|
|
400
|
+
include['instructions'] = item['instructions'];
|
|
401
|
+
}
|
|
402
|
+
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);
|
|
403
|
+
});
|
|
404
|
+
fields.push(Object.assign(Object.assign({}, item), { field_data }));
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
if (convert_option) {
|
|
408
|
+
if (item['field_type'] == "choice") {
|
|
409
|
+
if (field_data['choice_data']) {
|
|
410
|
+
let data = [];
|
|
411
|
+
data = field_data['choice_data'].split('\n');
|
|
412
|
+
_.each(data, (v, k) => {
|
|
413
|
+
if (!v) {
|
|
414
|
+
data = data.slice(0, k);
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
v = v ? v.trim() : "";
|
|
418
|
+
if (v.indexOf(':') > -1) {
|
|
419
|
+
let key = v.split(":")[0].trim();
|
|
420
|
+
let value = v.split(/:(.+)/)[1].trim();
|
|
421
|
+
if (value.indexOf('lang:') > -1) {
|
|
422
|
+
value = (0, i18n_1.__)({ phrase: value, locale: lang_code });
|
|
423
|
+
}
|
|
424
|
+
if (key && value) {
|
|
425
|
+
if (key.indexOf('\t') != -1) {
|
|
426
|
+
key = key.replace(/\t/g, '');
|
|
427
|
+
}
|
|
428
|
+
data[k] = { key, value };
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
data[k] = { key: '', value };
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
else {
|
|
435
|
+
data[k] = { key: v, value: v };
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
item['data'] = data;
|
|
439
|
+
}
|
|
440
|
+
else if (item['data']) {
|
|
441
|
+
item['data'].map(item => {
|
|
442
|
+
let value = item['value'];
|
|
443
|
+
if (value && value.indexOf('lang:') > -1) {
|
|
444
|
+
item['value'] = (0, i18n_1.__)({ phrase: value, locale: lang_code });
|
|
445
|
+
}
|
|
446
|
+
return item;
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
else if (!field_option[item['field_slug']] && (item['field_type'] == "relationship" || item['field_type'] == "multiple") && field_data['choose_stream']) {
|
|
451
|
+
try {
|
|
452
|
+
let data = [], where = {}, bol = true;
|
|
453
|
+
if (field_data['skip_data'] == "yes") {
|
|
454
|
+
if (default_value[item['field_slug']]) {
|
|
455
|
+
where['id'] = default_value[item['field_slug']];
|
|
456
|
+
}
|
|
457
|
+
bol = (typeof where['id'] != "undefined");
|
|
458
|
+
}
|
|
459
|
+
if (bol) {
|
|
460
|
+
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', 'view_options'] });
|
|
461
|
+
if (stream && stream['stream_slug']) {
|
|
462
|
+
let view_options = {};
|
|
463
|
+
try {
|
|
464
|
+
view_options = JSON.parse(stream['view_options']);
|
|
465
|
+
}
|
|
466
|
+
catch (err) { }
|
|
467
|
+
if (view_options['params']) {
|
|
468
|
+
let params = String(view_options['params'] || '').split('|').reduce((total, key) => {
|
|
469
|
+
return option[key] ? Object.assign(total, { [key]: option[key] }) : total;
|
|
470
|
+
}, {});
|
|
471
|
+
where = Object.assign(where, params);
|
|
472
|
+
}
|
|
473
|
+
if (index && slug && item['field_type'] == "multiple") {
|
|
474
|
+
let table = prefix ? prefix + slug : slug;
|
|
475
|
+
let table_f = stream['stream_prefix'] ? stream['stream_prefix'] + stream['stream_slug'] : stream['stream_slug'];
|
|
476
|
+
try {
|
|
477
|
+
let query = yield (0, db_1.findAll)(`${this.ref}_${table}_${table_f}`, { where: { row_id: index }, attributes: [`${[table_f + "_id"]}`] });
|
|
478
|
+
item['field_value'] = query ? query.map(i => i[table_f + "_id"]).join(',') : '';
|
|
479
|
+
}
|
|
480
|
+
catch (err) { }
|
|
481
|
+
}
|
|
482
|
+
let table = stream['stream_slug'];
|
|
483
|
+
if (stream['stream_prefix']) {
|
|
484
|
+
table = stream['stream_prefix'] + stream['stream_slug'];
|
|
485
|
+
}
|
|
486
|
+
table = `${this.ref}_${table}`;
|
|
487
|
+
let query = yield (0, db_1.findAll)(table, { where });
|
|
488
|
+
if (query && query.length) {
|
|
489
|
+
query = query.map(item => {
|
|
490
|
+
item['title'] = item['title'] ? item['title'] : item[stream['title_column']];
|
|
491
|
+
(0, helper_2.getValueFunctionLang)('title')(item, { result: item, lang_code });
|
|
492
|
+
let value = item['title'] ? item['title'] : item['id'];
|
|
493
|
+
return {
|
|
494
|
+
key: item['id'],
|
|
495
|
+
value
|
|
496
|
+
};
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
data = query;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
item['data'] = data;
|
|
503
|
+
}
|
|
504
|
+
catch (err) { }
|
|
505
|
+
}
|
|
506
|
+
else if (item['field_type'] == "pyro_lang") {
|
|
507
|
+
item['data'] = [
|
|
508
|
+
{ key: 'en', value: "English" },
|
|
509
|
+
{ key: 'th', value: "Thailand" }
|
|
510
|
+
];
|
|
511
|
+
}
|
|
512
|
+
else if (item['field_type'] == "image" || item['field_type'] == "file") {
|
|
513
|
+
if (item['field_value']) {
|
|
514
|
+
if (!/\./.test(item['field_value'])) {
|
|
515
|
+
item['data'] = yield this.fileUtil.findOneFileById(item['field_value']);
|
|
516
|
+
}
|
|
517
|
+
else {
|
|
518
|
+
item['data'] = { path: item['field_value'] };
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
else if (item['field_type'] == "datetime") {
|
|
523
|
+
if (typeof field_data['start_date'] != "undefined" && (0, helper_1.isNumber)(field_data['start_date'], { negative: true })) {
|
|
524
|
+
field_data['start_date'] = field_data['start_date'] + "Y";
|
|
525
|
+
}
|
|
526
|
+
if (typeof field_data['end_date'] != "undefined" && (0, helper_1.isNumber)(field_data['end_date'], { negative: true })) {
|
|
527
|
+
field_data['end_date'] = field_data['end_date'] + "Y";
|
|
528
|
+
}
|
|
529
|
+
if (field_data['range'] && field_data['range']['field_target']) {
|
|
530
|
+
field_data['range']['field_value'] = default_value[field_data['range']['field_target']];
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
else if (item['field_type'] == "time") {
|
|
534
|
+
if (field_data['date'] && field_data['date']['field_target']) {
|
|
535
|
+
item['field_value'] = default_value[field_data['date']['field_target']] || item['field_value'];
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
else if (item['field_type'] == "encrypt") {
|
|
539
|
+
if (item['field_value']) {
|
|
540
|
+
if (item['field_value'] instanceof Buffer)
|
|
541
|
+
item['field_value'] = item['field_value'].toString();
|
|
542
|
+
item['field_value'] = (0, helper_1.xorDecode)(item['field_value']);
|
|
543
|
+
}
|
|
544
|
+
if (field_data['hide_typing'] == "yes") {
|
|
545
|
+
item['field_type'] = "password";
|
|
546
|
+
}
|
|
547
|
+
else {
|
|
548
|
+
item['field_type'] = "text";
|
|
549
|
+
field_data['hide_typing'] = "no";
|
|
550
|
+
}
|
|
551
|
+
field_data['field_type'] = 'encrypt';
|
|
552
|
+
}
|
|
553
|
+
else if (item['field_type'] == "phone") {
|
|
554
|
+
if (!country)
|
|
555
|
+
country = yield (0, user_1.getCountry)(this.ref, { organ_id: this.option.organ_id, lang_code });
|
|
556
|
+
if (country && country['data']) {
|
|
557
|
+
item['data'] = country['data'];
|
|
558
|
+
}
|
|
559
|
+
if (field_data['code'] && field_data['code']['field_target']) {
|
|
560
|
+
if (country && country['default_country']) {
|
|
561
|
+
field_data['code']['field_value'] = country['default_country']['key'];
|
|
562
|
+
}
|
|
563
|
+
field_data['code']['field_value'] = default_value[field_data['code']['field_target']] || field_data['code']['field_value'];
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
fields.push(Object.assign(Object.assign({}, item), { field_data }));
|
|
568
|
+
}
|
|
569
|
+
;
|
|
570
|
+
fields = _.orderBy(fields, ['sort_order', 'field_id'], ['asc', 'asc']);
|
|
571
|
+
if (section) {
|
|
572
|
+
return { sections, fields };
|
|
573
|
+
}
|
|
574
|
+
return fields;
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
getFieldAll(slug, namespace, { where_field = {} } = {}) {
|
|
578
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
579
|
+
let results = [];
|
|
580
|
+
let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({
|
|
581
|
+
raw: true, where: { stream_slug: slug, stream_namespace: namespace }, attributes: ['id']
|
|
582
|
+
});
|
|
583
|
+
if (stream) {
|
|
584
|
+
let fields = yield field_1.FieldModel.schema(this.ref, "_").findAll({
|
|
585
|
+
raw: true, where: Object.assign(Object.assign({}, where_field), { stream_id: stream['id'] }), attributes: ['field_slug']
|
|
586
|
+
});
|
|
587
|
+
if (fields.length) {
|
|
588
|
+
results = fields.map(i => i['field_slug']);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
return results;
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
getDataStreamByField(streams, data, { is_lang = false, option = {}, df_lang = "en" } = {}) {
|
|
595
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
596
|
+
let fields = [];
|
|
597
|
+
let exclude = option['excludeObj'] || {};
|
|
598
|
+
if (is_lang) {
|
|
599
|
+
let result = _.keyBy(data, 'lang_code');
|
|
600
|
+
for (let i of streams) {
|
|
601
|
+
if (!i || exclude[i['field_slug']])
|
|
602
|
+
continue;
|
|
603
|
+
try {
|
|
604
|
+
if (i['is_lang'] && i['is_lang'] == "yes" && i['data'] && i['data'].length) {
|
|
605
|
+
i['data'] = i['data'].map(d => {
|
|
606
|
+
if (result[d['field_slug']]) {
|
|
607
|
+
d['field_value'] = result[d['field_slug']][i['field_slug']];
|
|
608
|
+
}
|
|
609
|
+
return d;
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
else {
|
|
613
|
+
if (i['field_type'] == 'multiple') {
|
|
614
|
+
fields.push(i);
|
|
615
|
+
continue;
|
|
616
|
+
}
|
|
617
|
+
i['field_value'] = result[df_lang][i['field_slug']] ? result[df_lang][i['field_slug']] : i['field_value'];
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
catch (err) { }
|
|
621
|
+
fields.push(i);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
else {
|
|
625
|
+
for (let i of streams) {
|
|
626
|
+
if (!i || exclude[i['field_slug']])
|
|
627
|
+
continue;
|
|
628
|
+
try {
|
|
629
|
+
if (i['is_lang'] && i['is_lang'] == "yes" && i['data'] && i['data'].length) {
|
|
630
|
+
i['data'] = i['data'].map(d => {
|
|
631
|
+
if (data[i['field_slug'] + "_" + d['field_slug']]) {
|
|
632
|
+
d['field_value'] = data[i['field_slug'] + "_" + d['field_slug']];
|
|
633
|
+
}
|
|
634
|
+
return d;
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
else {
|
|
638
|
+
let field_data = null;
|
|
639
|
+
if (i['field_data']) {
|
|
640
|
+
if (typeof i['field_data'] == 'object') {
|
|
641
|
+
field_data = i['field_data'];
|
|
642
|
+
}
|
|
643
|
+
else if (typeof i['field_data'] == 'string') {
|
|
644
|
+
try {
|
|
645
|
+
field_data = JSON.parse(i['field_data']);
|
|
646
|
+
}
|
|
647
|
+
catch (err) { }
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
if (field_data && field_data['default_value'] && !data[i['field_slug']]) {
|
|
651
|
+
i['field_value'] = field_data['default_value'];
|
|
652
|
+
}
|
|
653
|
+
i['field_value'] = data[i['field_slug']] ? data[i['field_slug']] : i['field_value'];
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
catch (err) { }
|
|
657
|
+
fields.push(i);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
return fields;
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
truncate(slug, namespace) {
|
|
664
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
665
|
+
let { id: id_stream, stream_prefix, fields: streams } = yield this.findField(slug, namespace, { field_only: false, multiple: true, convert_option: true });
|
|
666
|
+
let table = stream_prefix ? stream_prefix + slug : slug;
|
|
667
|
+
if (id_stream && (streams && streams.length)) {
|
|
668
|
+
return yield Promise.all(streams.map(({ field_data }) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
669
|
+
let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { id: field_data['choose_stream'] }, raw: true, attributes: ['stream_slug', 'stream_prefix'] });
|
|
670
|
+
let table_f = stream['stream_prefix'] ? stream['stream_prefix'] + stream['stream_slug'] : stream['stream_slug'];
|
|
671
|
+
return yield (0, db_1.truncate)(`${this.ref}_${table}_${table_f}`);
|
|
672
|
+
})));
|
|
673
|
+
}
|
|
674
|
+
return true;
|
|
675
|
+
});
|
|
676
|
+
}
|
|
677
|
+
remove(slug, namespace, index, { field = "id", where = {}, error = true } = {}) {
|
|
678
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
679
|
+
let { id: id_stream, stream_prefix, fields: streams } = yield this.findField(slug, namespace, { field_only: false, field_required: false, convert_option: true });
|
|
680
|
+
let table = stream_prefix ? stream_prefix + slug : slug;
|
|
681
|
+
where = Object.assign(Object.assign({}, where), { [field]: index });
|
|
682
|
+
let query = yield (0, db_1.findAll)(`${this.ref}_${table}`, { where });
|
|
683
|
+
if (query.length) {
|
|
684
|
+
let id = query.map(i => i['id']);
|
|
685
|
+
if (id_stream && (streams && streams.length)) {
|
|
686
|
+
let file_id = [];
|
|
687
|
+
yield Promise.all(streams.map(({ field_type, field_slug, field_data }) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
688
|
+
if (field_type == "multiple" && field_data && field_data['choose_stream']) {
|
|
689
|
+
let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { id: field_data['choose_stream'] }, raw: true, attributes: ['stream_slug', 'stream_prefix'] });
|
|
690
|
+
let table_f = stream['stream_prefix'] ? stream['stream_prefix'] + stream['stream_slug'] : stream['stream_slug'];
|
|
691
|
+
yield (0, db_1.remove)(this.ref, `${table}_${table_f}`, { where: { row_id: id } });
|
|
692
|
+
}
|
|
693
|
+
else if ((field_type == "file" || field_type == "image")) {
|
|
694
|
+
file_id = file_id.concat(query.reduce((total, item) => item[field_slug] ? total.concat(item[field_slug]) : total, []));
|
|
695
|
+
}
|
|
696
|
+
})));
|
|
697
|
+
if (file_id.length) {
|
|
698
|
+
yield this.fileUtil.removeFileById(file_id, { error: false });
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
yield (0, db_1.remove)(this.ref, table, { where });
|
|
702
|
+
}
|
|
703
|
+
else if (error) {
|
|
704
|
+
return Promise.reject(errors_1.generalError.NOT_FOUND);
|
|
705
|
+
}
|
|
706
|
+
return true;
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
upsert(slug, namespace, input = {}, { id = "", req = {}, raw_input = {}, attributes = [], field = "id", where = {}, default_lang = "en", builder = true } = {}) {
|
|
710
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
711
|
+
let files = {}, id_files = {};
|
|
712
|
+
let lang_code = input['lang_code'] ? input['lang_code'] : default_lang;
|
|
713
|
+
let isNew = id ? false : true, query;
|
|
714
|
+
let { stream, fields } = yield this.getBuilder(slug, namespace, { builder });
|
|
715
|
+
stream = stream || {};
|
|
716
|
+
let id_stream = stream['id'], stream_prefix = stream['stream_prefix'];
|
|
717
|
+
if (fields && fields.length) {
|
|
718
|
+
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"));
|
|
719
|
+
}
|
|
720
|
+
if (!Object.keys(raw_input).length) {
|
|
721
|
+
raw_input = input;
|
|
722
|
+
}
|
|
723
|
+
let table = stream_prefix ? stream_prefix + slug : slug;
|
|
724
|
+
let except = fields ? fields.map(i => i['field_slug']) : [];
|
|
725
|
+
if (!attributes.length) {
|
|
726
|
+
attributes = yield (0, db_1.getFieldAll)(this.ref + "_" + table);
|
|
727
|
+
}
|
|
728
|
+
let data = _.pick(_.omit(input, except), attributes);
|
|
729
|
+
if (!Object.keys(data).length) {
|
|
730
|
+
return Promise.reject(errors_1.generalError.BAD_REQUEST);
|
|
731
|
+
}
|
|
732
|
+
if (req.files) {
|
|
733
|
+
files = _.reduce(req.files, (total, item) => {
|
|
734
|
+
total[item['fieldname']] = item;
|
|
735
|
+
return total;
|
|
736
|
+
}, {});
|
|
737
|
+
}
|
|
738
|
+
if (fields && fields.length) {
|
|
739
|
+
let attributes = fields.reduce((total, { field_type, field_slug }) => {
|
|
740
|
+
if ((field_type == "file" || field_type == "image"))
|
|
741
|
+
total.push(field_slug);
|
|
742
|
+
return total;
|
|
743
|
+
}, []);
|
|
744
|
+
if (attributes.length) {
|
|
745
|
+
let query = yield (0, db_1.findOne)(`${this.ref}_${table}`, { where: Object.assign(Object.assign({}, where), { [field]: id }), attributes });
|
|
746
|
+
if (query)
|
|
747
|
+
id_files = query;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
let fields_post = [];
|
|
751
|
+
if (id_stream && fields.length) {
|
|
752
|
+
yield Promise.all(fields.map(({ id: field_id, is_lang, field_type, field_slug, field_data }, index) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
753
|
+
if (is_lang == "yes" && input[field_slug]) {
|
|
754
|
+
data[field_slug] = JSON.stringify(input[field_slug]);
|
|
755
|
+
}
|
|
756
|
+
else if ((field_type == "file" || field_type == "image")) {
|
|
757
|
+
if (files[field_slug] && field_data) {
|
|
758
|
+
let validate = false;
|
|
759
|
+
if (id && id_files[field_slug]) {
|
|
760
|
+
yield this.fileUtil.removeFileById(id_files[field_slug], { error: false });
|
|
761
|
+
}
|
|
762
|
+
if (!field_data['allowed_types'] || field_data['allowed_types'] == "*") {
|
|
763
|
+
validate = true;
|
|
764
|
+
}
|
|
765
|
+
else {
|
|
766
|
+
let allowed_types = (field_data['allowed_types']).split("|");
|
|
767
|
+
if (allowed_types.find(i => files[field_slug]['originalname'].indexOf(i) > -1)) {
|
|
768
|
+
validate = true;
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
if (validate) {
|
|
772
|
+
try {
|
|
773
|
+
let folder_slug = 'field_' + field_id;
|
|
774
|
+
let req = { file: files[field_slug] };
|
|
775
|
+
let res = yield this.fileUtil.uploadByReq(req, folder_slug, this.option.auth_id || 1, { parent_slug: this.folder_parent });
|
|
776
|
+
if (res && res[0] && res[0]['id']) {
|
|
777
|
+
data[field_slug] = res[0]['id'];
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
catch (err) { }
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
else if (raw_input[field_slug]) {
|
|
784
|
+
data[field_slug] = raw_input[field_slug];
|
|
785
|
+
}
|
|
786
|
+
else if ((raw_input[field_slug] === null || raw_input[field_slug] === "")) {
|
|
787
|
+
if (id_files[field_slug])
|
|
788
|
+
yield this.fileUtil.removeFileById(id_files[field_slug], { error: false });
|
|
789
|
+
data[field_slug] = null;
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
else if (field_data['field_type'] == "encrypt" && raw_input[field_slug]) {
|
|
793
|
+
data[field_slug] = (0, helper_1.xorEncode)(raw_input[field_slug]);
|
|
794
|
+
}
|
|
795
|
+
else if (field_type == "multiple") {
|
|
796
|
+
fields_post.push(fields[index]);
|
|
797
|
+
}
|
|
798
|
+
})));
|
|
799
|
+
}
|
|
800
|
+
if (id) {
|
|
801
|
+
yield (0, db_1.update)(this.ref, table, data, { where, default_lang });
|
|
802
|
+
query = { id };
|
|
803
|
+
}
|
|
804
|
+
else {
|
|
805
|
+
query = yield (0, db_1.insert)(this.ref, table, data);
|
|
806
|
+
id = query['id'];
|
|
807
|
+
}
|
|
808
|
+
if (!id_stream || (fields && !fields.length)) {
|
|
809
|
+
return query;
|
|
810
|
+
}
|
|
811
|
+
if (id_stream) {
|
|
812
|
+
yield Promise.all(fields_post.map(({ field_type, field_slug, field_data }) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
813
|
+
if (field_type == "multiple" && lang_code == default_lang && field_data && field_data['choose_stream']) {
|
|
814
|
+
let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { id: field_data['choose_stream'] }, raw: true, attributes: ['stream_slug', 'stream_prefix'] });
|
|
815
|
+
let table_f = stream['stream_prefix'] ? stream['stream_prefix'] + stream['stream_slug'] : stream['stream_slug'];
|
|
816
|
+
if (!isNew) {
|
|
817
|
+
yield (0, db_1.remove)(this.ref, `${table}_${table_f}`, { where: { row_id: id } });
|
|
818
|
+
}
|
|
819
|
+
if (raw_input[field_slug]) {
|
|
820
|
+
let op = [];
|
|
821
|
+
try {
|
|
822
|
+
op = raw_input[field_slug].split(',');
|
|
823
|
+
}
|
|
824
|
+
catch (err) { }
|
|
825
|
+
yield Promise.all(op.map((v) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
826
|
+
v = String(v).replace(/[^0-9]/g, '');
|
|
827
|
+
if (!v)
|
|
828
|
+
return 1;
|
|
829
|
+
let input = { row_id: id, [slug + "_id"]: id, [table_f + "_id"]: v };
|
|
830
|
+
yield (0, db_1.insert)(this.ref, `${table}_${table_f}`, input);
|
|
831
|
+
})));
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
return raw_input[field_slug];
|
|
835
|
+
})));
|
|
836
|
+
return query;
|
|
837
|
+
}
|
|
838
|
+
return Promise.reject(errors_1.generalError.BAD_REQUEST);
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
upsertBySlug(slug, namespace, index, { ref = "", req = {}, parent_slug = "", input = {}, tags = [], group = undefined } = {}) {
|
|
842
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
843
|
+
let fields = yield this.getBuilder(slug, namespace, { index: "fields" }), where_folder = {};
|
|
844
|
+
ref = ref || this.ref;
|
|
845
|
+
let files = (req.file ? [req.file] : (req.files || [])).reduce((total, item) => Object.assign(total, { [item['fieldname']]: item }), {});
|
|
846
|
+
let id_files = {}, fileUtil = new file_2.FileUtil(ref, this.option);
|
|
847
|
+
if (parent_slug) {
|
|
848
|
+
let query = yield fileUtil.createFolder(parent_slug, { root: true, parent_id: where_folder['parent_id'] });
|
|
849
|
+
if (query)
|
|
850
|
+
where_folder['parent_id'] = query['id'];
|
|
851
|
+
}
|
|
852
|
+
let folder = yield fileUtil.createFolder(index, { parent_id: where_folder['parent_id'] });
|
|
853
|
+
let where_file = { folder_id: folder['id'], is_hidden: "yes" };
|
|
854
|
+
where_file = fileUtil.beforeFindFile(where_file, { tags, group });
|
|
855
|
+
let query = yield file_1.FileModel.schema(ref, "_").findAll({ where: where_file, attributes: ['id', 'name'], raw: true });
|
|
856
|
+
id_files = query.reduce((total, item) => Object.assign(total, { [item['name']]: item['id'] }), {});
|
|
857
|
+
let input_files = [];
|
|
858
|
+
yield Promise.all(fields.map(({ field_type, field_slug, field_data }) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
859
|
+
if ((field_type == "file" || field_type == "image")) {
|
|
860
|
+
if (files[field_slug] && field_data) {
|
|
861
|
+
if (id_files[field_slug]) {
|
|
862
|
+
yield fileUtil.removeFileById(id_files[field_slug], { error: false });
|
|
863
|
+
}
|
|
864
|
+
try {
|
|
865
|
+
let req = { file: files[field_slug] };
|
|
866
|
+
req['file']['originalname'] = field_slug;
|
|
867
|
+
let res = yield fileUtil.uploadByReq(req, index, this.option.auth_id || 1, { parent_slug, is_hidden: "yes", tags, group });
|
|
868
|
+
if (res && res[0] && res[0]['id']) {
|
|
869
|
+
input_files.push(res[0]['id']);
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
catch (err) {
|
|
873
|
+
input[field_slug] = null;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
else if ((input[field_slug] === null || input[field_slug] === "")) {
|
|
877
|
+
if (id_files[field_slug])
|
|
878
|
+
yield fileUtil.removeFileById(id_files[field_slug], { error: false });
|
|
879
|
+
input[field_slug] = null;
|
|
880
|
+
}
|
|
881
|
+
else if (id_files[field_slug]) {
|
|
882
|
+
input_files.push(id_files[field_slug]);
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
})));
|
|
886
|
+
if (input_files.length) {
|
|
887
|
+
let query = yield fileUtil.findAllFile({ full: true, where: { id: input_files, is_hidden: "yes" }, attributes: ['name', 'filename', 'path'] });
|
|
888
|
+
let attribute = query.reduce((total, item) => Object.assign(total, { [item['name']]: item['path'] }), {});
|
|
889
|
+
input = Object.assign(Object.assign({}, input), attribute);
|
|
890
|
+
}
|
|
891
|
+
return input;
|
|
892
|
+
});
|
|
893
|
+
}
|
|
894
|
+
findStream(slug, namespace, { result = [], lang_code = "en", field_exclude = [], multiple = true, relationship = true, file = true, image = true, is_lang = true, stream = {}, use_param = false } = {}) {
|
|
895
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
896
|
+
if (!use_param) {
|
|
897
|
+
stream = yield this.findField(slug, namespace, { field_exclude, field_only: false, multiple, relationship, file, image, convert_option: true });
|
|
898
|
+
}
|
|
899
|
+
let { id: id_stream, stream_prefix, fields: streams } = stream;
|
|
900
|
+
let table = stream_prefix ? stream_prefix + slug : slug;
|
|
901
|
+
if (id_stream && result.length) {
|
|
902
|
+
yield Promise.all(streams.map(({ field_type, field_slug, field_data }) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
903
|
+
yield Promise.all(result.map((data) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
904
|
+
if (field_type == "file" || field_type == "image") {
|
|
905
|
+
if (data[field_slug]) {
|
|
906
|
+
data[field_slug] = yield this.fileUtil.findOneFileById(data[field_slug]);
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
else if (field_type == "relationship" && field_data) {
|
|
910
|
+
if (data[field_slug]) {
|
|
911
|
+
let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { id: field_data['choose_stream'] }, raw: true, attributes: ['stream_slug', 'stream_prefix'] });
|
|
912
|
+
let table_f = stream['stream_prefix'] ? stream['stream_prefix'] + stream['stream_slug'] : stream['stream_slug'];
|
|
913
|
+
data[field_slug] = yield (0, db_1.findOne)(`${this.ref}_${table_f}`, { where: { id: data[field_slug] } });
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
else if (field_type == "multiple" && field_data) {
|
|
917
|
+
let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { id: field_data['choose_stream'] }, raw: true, attributes: ['stream_slug', 'stream_prefix'] });
|
|
918
|
+
let table_f = stream['stream_prefix'] ? stream['stream_prefix'] + stream['stream_slug'] : stream['stream_slug'];
|
|
919
|
+
let join = yield (0, db_1.findAll)(`${this.ref}_${table}_${table_f}`, { where: { row_id: data['id'] }, attributes: [`${table_f + "_id"}`] });
|
|
920
|
+
if (join && join.length) {
|
|
921
|
+
let where = join.map(i => i[table_f + "_id"]).join(',');
|
|
922
|
+
let lang_code_exist;
|
|
923
|
+
if (yield (0, db_1.getExistField)(this.ref + "_" + table_f, 'lang_code')) {
|
|
924
|
+
lang_code_exist = true;
|
|
925
|
+
lang_code = data['lang_code'] ? data['lang_code'] : lang_code;
|
|
926
|
+
}
|
|
927
|
+
let query = yield database_1.default.query(`SELECT * FROM ${this.ref}_${table_f} WHERE id IN (${where}) ${lang_code_exist ? `AND lang_code='${lang_code}'` : ''}`);
|
|
928
|
+
data[field_slug] = query && query.length ? query[0] : [];
|
|
929
|
+
}
|
|
930
|
+
else {
|
|
931
|
+
data[field_slug] = [];
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
})));
|
|
935
|
+
})));
|
|
936
|
+
}
|
|
937
|
+
return result;
|
|
938
|
+
});
|
|
939
|
+
}
|
|
940
|
+
cacheIndex(slug, namespace, { result = [], field_filter = [], field_exclude = [], multiple = true, relationship = true, file = true, image = true, stream = {}, use_param = false } = {}) {
|
|
941
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
942
|
+
if (!use_param) {
|
|
943
|
+
stream = yield this.findField(slug, namespace, { field_exclude, field_only: false, multiple, relationship, file, image, convert_option: true });
|
|
944
|
+
}
|
|
945
|
+
let { id: id_stream, stream_prefix, fields: streams } = stream;
|
|
946
|
+
if (id_stream) {
|
|
947
|
+
result = _.cloneDeep(result);
|
|
948
|
+
let table = stream_prefix ? stream_prefix + slug : slug;
|
|
949
|
+
yield Promise.all(streams.map(({ field_type, field_slug, field_data }) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
950
|
+
if (field_type == "relationship" || field_type == "multiple") {
|
|
951
|
+
field_filter.push(field_slug);
|
|
952
|
+
}
|
|
953
|
+
yield Promise.all(result.map((data) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
954
|
+
if (multiple && field_type == "multiple" && field_data) {
|
|
955
|
+
let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { id: field_data['choose_stream'] }, raw: true, attributes: ['stream_slug', 'stream_prefix'] });
|
|
956
|
+
let table_f = stream['stream_prefix'] ? stream['stream_prefix'] + stream['stream_slug'] : stream['stream_slug'];
|
|
957
|
+
let join = yield (0, db_1.findAll)(`${this.ref}_${table}_${table_f}`, { where: { row_id: data['id'] }, attributes: [`${table_f + "_id"}`] });
|
|
958
|
+
join = join.map(i => i[table_f + "_id"]);
|
|
959
|
+
data[field_slug] = join;
|
|
960
|
+
}
|
|
961
|
+
})));
|
|
962
|
+
})));
|
|
963
|
+
}
|
|
964
|
+
return result.map(i => _.pick(i, field_filter));
|
|
965
|
+
});
|
|
966
|
+
}
|
|
967
|
+
createCache(slug, namespace, { result = [], lang_code = "en", field_exclude = [], multiple = true, single = false, relationship = true, file = true, image = true, stream = {}, use_param = false } = {}) {
|
|
968
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
969
|
+
if (!use_param) {
|
|
970
|
+
stream = yield this.findField(slug, namespace, { field_exclude, field_only: false, multiple, relationship, file, image, convert_option: true });
|
|
971
|
+
}
|
|
972
|
+
if (!single) {
|
|
973
|
+
multiple = false;
|
|
974
|
+
relationship = false;
|
|
975
|
+
}
|
|
976
|
+
let { id: id_stream, stream_prefix, fields: streams } = stream;
|
|
977
|
+
if (id_stream) {
|
|
978
|
+
result = _.cloneDeep(result);
|
|
979
|
+
let table = stream_prefix ? stream_prefix + slug : slug;
|
|
980
|
+
let ref = this.ref.toLowerCase();
|
|
981
|
+
let index = ref + "_" + table;
|
|
982
|
+
let multi = redis_1.redisClient.multi();
|
|
983
|
+
yield Promise.all(streams.map(({ field_type, field_slug, field_data }) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
984
|
+
yield Promise.all(result.map((data) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
985
|
+
data['stream'] = data['stream'] ? data['stream'] : {};
|
|
986
|
+
if (file && data[field_slug] && (field_type == "file" || field_type == "image")) {
|
|
987
|
+
data['stream'][field_slug] = { type: field_type, value: data[field_slug] };
|
|
988
|
+
}
|
|
989
|
+
else if (relationship && field_type == "relationship" && field_data) {
|
|
990
|
+
if (data[field_slug] && data[field_slug]['id']) {
|
|
991
|
+
let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { id: field_data['choose_stream'] }, raw: true, attributes: ['stream_slug', 'stream_prefix'] });
|
|
992
|
+
let table_f = stream['stream_prefix'] ? stream['stream_prefix'] + stream['stream_slug'] : stream['stream_slug'];
|
|
993
|
+
let is_lang = false;
|
|
994
|
+
let key = "data";
|
|
995
|
+
if (data[field_slug]['lang_code']) {
|
|
996
|
+
is_lang = true;
|
|
997
|
+
key = key + "_" + data[field_slug]['lang_code'];
|
|
998
|
+
}
|
|
999
|
+
multi.hset(ref + "_" + table_f + "_" + data[field_slug]['id'], key, JSON.stringify(data[field_slug]));
|
|
1000
|
+
data['stream'][field_slug] = { type: field_type, is_lang, table: table_f, value: data[field_slug]['id'] };
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
else if (multiple && field_type == "multiple" && field_data) {
|
|
1004
|
+
if (data[field_slug] && data[field_slug].length) {
|
|
1005
|
+
let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { id: field_data['choose_stream'] }, raw: true, attributes: ['stream_slug', 'stream_prefix'] });
|
|
1006
|
+
let table_f = stream['stream_prefix'] ? stream['stream_prefix'] + stream['stream_slug'] : stream['stream_slug'];
|
|
1007
|
+
let is_lang = false;
|
|
1008
|
+
data[field_slug].forEach(i => {
|
|
1009
|
+
let key = "data";
|
|
1010
|
+
if (i['lang_code']) {
|
|
1011
|
+
is_lang = true;
|
|
1012
|
+
key = key + "_" + i['lang_code'];
|
|
1013
|
+
}
|
|
1014
|
+
multi.hset(ref + "_" + table_f + "_" + i['id'], key, JSON.stringify(i));
|
|
1015
|
+
});
|
|
1016
|
+
data['stream'][field_slug] = { type: field_type, is_lang, table: table_f, value: data[field_slug].map(i => i['id']).join(',') };
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
delete data[field_slug];
|
|
1020
|
+
})));
|
|
1021
|
+
})));
|
|
1022
|
+
let obj = {};
|
|
1023
|
+
result.forEach(i => {
|
|
1024
|
+
if (i['id'] && !obj[i['id']] && _.size(i['stream'])) {
|
|
1025
|
+
obj[i['id']] = true;
|
|
1026
|
+
multi.hset(index + "_" + i['id'], "stream", JSON.stringify(i['stream']));
|
|
1027
|
+
}
|
|
1028
|
+
delete i['stream'];
|
|
1029
|
+
});
|
|
1030
|
+
yield multi.exec();
|
|
1031
|
+
}
|
|
1032
|
+
return result;
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
1035
|
+
cacheQuery(slug, namespace, { result = [], lang_code = "en", single = false, multiple = true, relationship = true }) {
|
|
1036
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1037
|
+
if (!single) {
|
|
1038
|
+
multiple = false;
|
|
1039
|
+
relationship = false;
|
|
1040
|
+
}
|
|
1041
|
+
let arr = {};
|
|
1042
|
+
let ref = this.ref.toLowerCase();
|
|
1043
|
+
result = result.map((i) => {
|
|
1044
|
+
if (i['stream'] && _.size(i['stream'])) {
|
|
1045
|
+
_.map(i['stream'], ((v, k) => {
|
|
1046
|
+
if (multiple && v['type'] == "multiple") {
|
|
1047
|
+
let value = v['value'].split(',');
|
|
1048
|
+
if (arr[k]) {
|
|
1049
|
+
arr[k]['value'] = arr[k]['value'].concat(value);
|
|
1050
|
+
}
|
|
1051
|
+
else {
|
|
1052
|
+
v['value'] = value;
|
|
1053
|
+
arr[k] = v;
|
|
1054
|
+
}
|
|
1055
|
+
i[k] = value;
|
|
1056
|
+
}
|
|
1057
|
+
else if (relationship && v['type'] == "relationship") {
|
|
1058
|
+
arr[k] = v['value'];
|
|
1059
|
+
i[k] = v['value'];
|
|
1060
|
+
}
|
|
1061
|
+
else {
|
|
1062
|
+
i[k] = v['value'];
|
|
1063
|
+
}
|
|
1064
|
+
}));
|
|
1065
|
+
}
|
|
1066
|
+
delete i['stream'];
|
|
1067
|
+
return i;
|
|
1068
|
+
});
|
|
1069
|
+
yield Promise.all(_.map(arr, (v, k) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1070
|
+
if (v) {
|
|
1071
|
+
let value = _.uniq(v['value']);
|
|
1072
|
+
let multi = redis_1.redisClient.multi();
|
|
1073
|
+
let key = "data";
|
|
1074
|
+
if (v['is_lang']) {
|
|
1075
|
+
key = key + "_" + lang_code;
|
|
1076
|
+
}
|
|
1077
|
+
value.forEach(index => {
|
|
1078
|
+
multi.hget(ref + "_" + v['table'] + "_" + index, key);
|
|
1079
|
+
});
|
|
1080
|
+
value = yield (0, helper_1.multiExecArray)(multi, { json_parse: true });
|
|
1081
|
+
let obj = _.keyBy(value, 'id');
|
|
1082
|
+
result = result.map(item => {
|
|
1083
|
+
return Object.assign(Object.assign({}, item), { [k]: item[k] && typeof item[k] == "object" ? item[k].map(val => obj[val]) : obj[item[k]] });
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
1086
|
+
})));
|
|
1087
|
+
return result;
|
|
1088
|
+
});
|
|
1089
|
+
}
|
|
1090
|
+
getStreamFile(slug, namespace, { includeAttrFile = [], items = [], lang_code = "en", field_slug = [], where = {}, relationship = true, image = true, file = false, single = false, limit = 10, offset = 0, order = [], img_kit = false } = {}) {
|
|
1091
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1092
|
+
if (single) {
|
|
1093
|
+
limit = 1;
|
|
1094
|
+
}
|
|
1095
|
+
let sql;
|
|
1096
|
+
let type = {};
|
|
1097
|
+
let include = [];
|
|
1098
|
+
if (!items.length) {
|
|
1099
|
+
relationship = false;
|
|
1100
|
+
}
|
|
1101
|
+
type['image'] = image;
|
|
1102
|
+
type['file'] = file;
|
|
1103
|
+
type['relationship'] = relationship;
|
|
1104
|
+
let { stream_prefix, fields: field } = yield this.findField(slug, namespace, Object.assign(Object.assign({}, type), { field_only: false, field_slug, convert_option: true }));
|
|
1105
|
+
let table = stream_prefix ? stream_prefix + slug : slug;
|
|
1106
|
+
if (field && field.length) {
|
|
1107
|
+
const p = this.ref + "_" + table;
|
|
1108
|
+
const f = this.ref + "_" + file_1.FileModel.getTableName();
|
|
1109
|
+
const attributes = ['id', 'filename', 'path', 'width', 'height'].concat(includeAttrFile);
|
|
1110
|
+
let limitSql, offsetSql, orderSql = "", whereSql = (0, db_1.genWhere)(where, { as: "p" });
|
|
1111
|
+
if (order.length) {
|
|
1112
|
+
order = order[0];
|
|
1113
|
+
orderSql = `ORDER BY p.${order[0]} ${order[1]}`;
|
|
1114
|
+
}
|
|
1115
|
+
limitSql = limit ? `LIMIT ${limit}` : "";
|
|
1116
|
+
offsetSql = offset ? `OFFSET ${offset}` : "";
|
|
1117
|
+
if (relationship && items.length) {
|
|
1118
|
+
let relation = [];
|
|
1119
|
+
relation = yield Promise.all(_.map(field, (fi, index) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1120
|
+
if (fi['field_type'] == "relationship") {
|
|
1121
|
+
try {
|
|
1122
|
+
let lang_code;
|
|
1123
|
+
let field_data = fi['field_data'];
|
|
1124
|
+
let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { id: field_data['choose_stream'] }, raw: true, attributes: ['stream_slug', 'stream_prefix'] });
|
|
1125
|
+
let table_f = stream['stream_slug'];
|
|
1126
|
+
if (stream['stream_prefix']) {
|
|
1127
|
+
table_f = stream['stream_prefix'] + stream['stream_slug'];
|
|
1128
|
+
}
|
|
1129
|
+
table_f = `${this.ref}_${table_f}`;
|
|
1130
|
+
if (yield (0, db_1.getExistField)(table_f, 'lang_code')) {
|
|
1131
|
+
lang_code = true;
|
|
1132
|
+
}
|
|
1133
|
+
else {
|
|
1134
|
+
lang_code = false;
|
|
1135
|
+
}
|
|
1136
|
+
return Object.assign(Object.assign({}, fi), { table: table_f, lang_code });
|
|
1137
|
+
}
|
|
1138
|
+
catch (err) {
|
|
1139
|
+
return null;
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
return null;
|
|
1143
|
+
})));
|
|
1144
|
+
if (relation.length) {
|
|
1145
|
+
relation = _.compact(relation);
|
|
1146
|
+
include = yield Promise.all(_.map(items, (item, index) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1147
|
+
let obj = {};
|
|
1148
|
+
yield Promise.all(_.map(relation, (re) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1149
|
+
try {
|
|
1150
|
+
if (re['lang_code']) {
|
|
1151
|
+
obj[re['field_slug']] = yield (0, db_1.findOne)(re['table'], { where: { id: item[re['field_slug']], lang_code } });
|
|
1152
|
+
}
|
|
1153
|
+
else {
|
|
1154
|
+
obj[re['field_slug']] = yield (0, db_1.findOne)(re['table'], { where: { id: item[re['field_slug']] } });
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
catch (err) { }
|
|
1158
|
+
})));
|
|
1159
|
+
return obj;
|
|
1160
|
+
})));
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
field = _.filter(field, (item) => item['field_type'] != "relationship");
|
|
1164
|
+
if (field.length) {
|
|
1165
|
+
sql = `
|
|
1166
|
+
SELECT
|
|
1167
|
+
` + _.map(field, (stream, index) => {
|
|
1168
|
+
if (stream['field_type'] != "relationship") {
|
|
1169
|
+
return _.map(attributes, (a) => {
|
|
1170
|
+
return `f${index}.${a} AS '${stream['field_slug']}.${a}'`;
|
|
1171
|
+
});
|
|
1172
|
+
}
|
|
1173
|
+
return null;
|
|
1174
|
+
}).filter(i => i != null).join(",") + `
|
|
1175
|
+
|
|
1176
|
+
FROM ${p} as p
|
|
1177
|
+
` + _.map(field, (stream, index) => {
|
|
1178
|
+
if (stream['field_type'] != "relationship") {
|
|
1179
|
+
return `LEFT OUTER JOIN ${f} f${index} ON p.${stream['field_slug']} = f${index}.id `;
|
|
1180
|
+
}
|
|
1181
|
+
return null;
|
|
1182
|
+
}).join("") + `
|
|
1183
|
+
${whereSql}
|
|
1184
|
+
${orderSql}
|
|
1185
|
+
${limitSql}
|
|
1186
|
+
${offsetSql}
|
|
1187
|
+
`;
|
|
1188
|
+
}
|
|
1189
|
+
try {
|
|
1190
|
+
if (!sql) {
|
|
1191
|
+
return null;
|
|
1192
|
+
}
|
|
1193
|
+
let query = yield database_1.default.query(sql, { type: sequelize_1.QueryTypes.SELECT });
|
|
1194
|
+
if (query && query.length) {
|
|
1195
|
+
let setting = {};
|
|
1196
|
+
if (img_kit) {
|
|
1197
|
+
setting = yield this.fileUtil.getSettingImgKit();
|
|
1198
|
+
}
|
|
1199
|
+
query = query.map((item) => {
|
|
1200
|
+
let data = dottie.transform(item);
|
|
1201
|
+
Object.keys(data).forEach((key) => {
|
|
1202
|
+
data[key] = this.fileUtil.afterQueryFile(data[key]);
|
|
1203
|
+
if (setting["url"] && data[key]["path"]) {
|
|
1204
|
+
data[key]["path"] = this.fileUtil.convertImgKit(data[key]["path"], setting);
|
|
1205
|
+
}
|
|
1206
|
+
if (!data[key]["id"])
|
|
1207
|
+
data[key] = null;
|
|
1208
|
+
});
|
|
1209
|
+
return data;
|
|
1210
|
+
});
|
|
1211
|
+
if (query.length) {
|
|
1212
|
+
query = _.merge(query, include);
|
|
1213
|
+
}
|
|
1214
|
+
else if (include.length) {
|
|
1215
|
+
query = include;
|
|
1216
|
+
}
|
|
1217
|
+
if (single) {
|
|
1218
|
+
return query[0];
|
|
1219
|
+
}
|
|
1220
|
+
return query;
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
catch (err) { }
|
|
1224
|
+
}
|
|
1225
|
+
return null;
|
|
1226
|
+
});
|
|
1227
|
+
}
|
|
1228
|
+
getInputByLang(input, { separate = false, attributes_i18n = [], lang = [] } = {}) {
|
|
1229
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1230
|
+
let input_lang = {};
|
|
1231
|
+
let items = [];
|
|
1232
|
+
_.each(input, (item, key) => {
|
|
1233
|
+
if (_.isObject(item)) {
|
|
1234
|
+
input_lang[key] = item;
|
|
1235
|
+
delete input[key];
|
|
1236
|
+
}
|
|
1237
|
+
});
|
|
1238
|
+
_.each(input, (value, key) => {
|
|
1239
|
+
if (value === "") {
|
|
1240
|
+
input[key] = null;
|
|
1241
|
+
}
|
|
1242
|
+
});
|
|
1243
|
+
if (!lang.length) {
|
|
1244
|
+
lang = yield this.languageUtil.getLangsLive();
|
|
1245
|
+
}
|
|
1246
|
+
if (lang.length) {
|
|
1247
|
+
if (separate) {
|
|
1248
|
+
lang.forEach((language, key) => {
|
|
1249
|
+
_.each(input_lang, (v, k) => input[k + "_" + language['lang_code']] = v[language['lang_code']] || v['en']);
|
|
1250
|
+
});
|
|
1251
|
+
return input;
|
|
1252
|
+
}
|
|
1253
|
+
items = lang.map((language, key) => {
|
|
1254
|
+
let item_lang = {};
|
|
1255
|
+
_.each(input_lang, (v, k) => item_lang[k] = v[language['lang_code']] || v['en']);
|
|
1256
|
+
let item = Object.assign(Object.assign({}, item_lang), input);
|
|
1257
|
+
item['lang_code'] = language['lang_code'];
|
|
1258
|
+
return item;
|
|
1259
|
+
});
|
|
1260
|
+
}
|
|
1261
|
+
if (attributes_i18n.length) {
|
|
1262
|
+
let input = _.omit(items[0], [...attributes_i18n, 'lang_code']);
|
|
1263
|
+
items = items.map(item => {
|
|
1264
|
+
return Object.assign(Object.assign({}, _.pick(item, attributes_i18n)), { lang_code: item['lang_code'] });
|
|
1265
|
+
});
|
|
1266
|
+
items = { input, i18n: items };
|
|
1267
|
+
}
|
|
1268
|
+
return items;
|
|
1269
|
+
});
|
|
1270
|
+
}
|
|
1271
|
+
getInputByLangIndex(input, { lang = [] } = {}) {
|
|
1272
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1273
|
+
let input_lang = {};
|
|
1274
|
+
let items = [];
|
|
1275
|
+
_.each(input, (item, key) => {
|
|
1276
|
+
if (_.isArray(item)) {
|
|
1277
|
+
input_lang[key] = item;
|
|
1278
|
+
delete input[key];
|
|
1279
|
+
}
|
|
1280
|
+
});
|
|
1281
|
+
_.each(input, (value, key) => {
|
|
1282
|
+
if (!value) {
|
|
1283
|
+
delete input[key];
|
|
1284
|
+
}
|
|
1285
|
+
});
|
|
1286
|
+
if (!lang.length) {
|
|
1287
|
+
lang = yield this.languageUtil.getLangsLive();
|
|
1288
|
+
}
|
|
1289
|
+
if (lang.length) {
|
|
1290
|
+
items = yield Promise.all(lang.map((language, key) => {
|
|
1291
|
+
let item_lang = {};
|
|
1292
|
+
_.each(input_lang, (v, k) => item_lang[k] = v[key]);
|
|
1293
|
+
let item = Object.assign(Object.assign({}, item_lang), input);
|
|
1294
|
+
item['lang_code'] = language['lang_code'];
|
|
1295
|
+
return item;
|
|
1296
|
+
}));
|
|
1297
|
+
}
|
|
1298
|
+
return items;
|
|
1299
|
+
});
|
|
1300
|
+
}
|
|
1301
|
+
checkStream(slug, namespace) {
|
|
1302
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1303
|
+
return yield stream_1.StreamModel.schema(this.ref, "_").count({ where: { stream_slug: slug, stream_namespace: namespace } });
|
|
1304
|
+
});
|
|
1305
|
+
}
|
|
1306
|
+
addStream(slug, namespace, input = {}, { stream = undefined, column = {}, checkDuplicate = true, fields = [], removePrimary = false } = {}) {
|
|
1307
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1308
|
+
input['stream_slug'] = slug;
|
|
1309
|
+
input['stream_namespace'] = namespace;
|
|
1310
|
+
if (checkDuplicate && !stream) {
|
|
1311
|
+
stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { stream_slug: input['stream_slug'], stream_namespace: input['stream_namespace'] }, attributes: ['id', 'stream_type'], raw: true });
|
|
1312
|
+
}
|
|
1313
|
+
let table = input['stream_prefix'] ? input['stream_prefix'] + input['stream_slug'] : input['stream_slug'];
|
|
1314
|
+
let exist = yield (0, db_1.getExistTable)(`${this.ref}_${table}`);
|
|
1315
|
+
if (checkDuplicate && (stream || exist)) {
|
|
1316
|
+
return Promise.reject(errors_1.generalError.SLUG_DUPLICATE);
|
|
1317
|
+
}
|
|
1318
|
+
input['stream_type'] = input['stream_type'] || (stream || {})['stream_type'] || "auto";
|
|
1319
|
+
if (!stream) {
|
|
1320
|
+
if (!input['view_options'] || !input['view_options']['column']) {
|
|
1321
|
+
input['view_options'] = input['view_options'] || {};
|
|
1322
|
+
input['view_options']['column'] = { column: "id|created" };
|
|
1323
|
+
}
|
|
1324
|
+
if (!input['stream_name']) {
|
|
1325
|
+
input['stream_name'] = _.startCase(_.camelCase(input['stream_slug']));
|
|
1326
|
+
}
|
|
1327
|
+
input['view_options'] = input['view_options'] ? JSON.stringify(input['view_options']) : null;
|
|
1328
|
+
stream = yield (0, db_1.insert)(this.ref, stream_1.StreamModel.getTableName().toString(), input);
|
|
1329
|
+
}
|
|
1330
|
+
else {
|
|
1331
|
+
yield (0, db_1.update)(this.ref, stream_1.StreamModel.getTableName().toString(), input);
|
|
1332
|
+
}
|
|
1333
|
+
input['id'] = stream['id'];
|
|
1334
|
+
if (!exist && input['stream_type'] == "auto")
|
|
1335
|
+
yield (0, db_1.createTable)(`${this.ref}_${table}`, Object.assign(Object.assign({}, this.initColumn), column), { removePrimary });
|
|
1336
|
+
if (fields.length) {
|
|
1337
|
+
yield this.addStreamFieldMany(slug, namespace, fields, { stream: input });
|
|
1338
|
+
}
|
|
1339
|
+
return stream;
|
|
1340
|
+
});
|
|
1341
|
+
}
|
|
1342
|
+
updateStream(slug, namespace, input = {}) {
|
|
1343
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1344
|
+
let query = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { stream_slug: slug, stream_namespace: namespace }, attributes: ['id'], raw: true });
|
|
1345
|
+
if (!query) {
|
|
1346
|
+
return Promise.reject(errors_1.generalError.NOT_FOUND);
|
|
1347
|
+
}
|
|
1348
|
+
delete input['id'];
|
|
1349
|
+
return yield (0, db_1.update)(this.ref, stream_1.StreamModel.getTableName().toString(), input, { where: { id: query['id'] } });
|
|
1350
|
+
});
|
|
1351
|
+
}
|
|
1352
|
+
removeStreamByNamespace(namespace) {
|
|
1353
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1354
|
+
let query = yield stream_1.StreamModel.schema(this.ref, "_").findAll({
|
|
1355
|
+
where: { stream_namespace: namespace }, raw: true, attributes: ['id', 'stream_prefix', 'stream_slug', 'stream_namespace', 'stream_type']
|
|
1356
|
+
});
|
|
1357
|
+
if (query.length) {
|
|
1358
|
+
for (let stream of query) {
|
|
1359
|
+
yield this.removeStream(stream['stream_slug'], stream['stream_namespace'], { stream });
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
return { success: 1 };
|
|
1363
|
+
});
|
|
1364
|
+
}
|
|
1365
|
+
removeStream(slug, namespace, { stream = {}, error = true } = {}) {
|
|
1366
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1367
|
+
if (!Object.keys(stream).length) {
|
|
1368
|
+
stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({
|
|
1369
|
+
where: { stream_slug: slug, stream_namespace: namespace }, raw: true, attributes: ['id', 'stream_prefix', 'stream_slug', 'stream_namespace', 'stream_type']
|
|
1370
|
+
});
|
|
1371
|
+
if (!stream && error)
|
|
1372
|
+
return Promise.reject(errors_1.generalError.NOT_FOUND);
|
|
1373
|
+
stream = stream || {};
|
|
1374
|
+
}
|
|
1375
|
+
let table = stream['stream_prefix'] ? stream['stream_prefix'] + stream['stream_slug'] : stream['stream_slug'];
|
|
1376
|
+
let fields = yield stream_1.StreamAssociation.field.model.schema(this.ref, "_").findAll({
|
|
1377
|
+
where: { stream_id: stream['id'] }, attributes: ['id'], raw: true
|
|
1378
|
+
});
|
|
1379
|
+
if (fields.length) {
|
|
1380
|
+
let field_id = fields.map(i => i['id']);
|
|
1381
|
+
yield stream_1.StreamAssociation.field.model.schema(this.ref, "_").destroy({ where: { id: field_id } });
|
|
1382
|
+
}
|
|
1383
|
+
yield stream_1.StreamModel.schema(this.ref, "_").destroy({ where: { id: stream['id'] } });
|
|
1384
|
+
yield field_section_1.FieldSectionModel.schema(this.ref, "_").destroy({ where: { stream_id: stream['id'] } });
|
|
1385
|
+
if (stream['stream_type'] == "auto")
|
|
1386
|
+
yield (0, db_1.dropTable)(`${this.ref}_${table}`);
|
|
1387
|
+
if (redis_1.redisReady) {
|
|
1388
|
+
yield redis_1.redisClient.del(this.ref + "_stream_" + namespace + "_" + slug);
|
|
1389
|
+
}
|
|
1390
|
+
return true;
|
|
1391
|
+
});
|
|
1392
|
+
}
|
|
1393
|
+
removeStreamManual(slug, { error = true } = {}) {
|
|
1394
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1395
|
+
let query = yield stream_1.StreamModel.schema(this.ref, "_").findAll({
|
|
1396
|
+
where: { stream_slug: slug, stream_namespace: slug }, raw: true, attributes: ['id', 'stream_slug']
|
|
1397
|
+
});
|
|
1398
|
+
if (!query.length && error)
|
|
1399
|
+
return Promise.reject(errors_1.generalError.NOT_FOUND);
|
|
1400
|
+
let multi = redis_1.redisClient.multi();
|
|
1401
|
+
let id = query.map(i => { multi.del(this.ref + "_stream_" + i['stream_slug'] + "_" + i['stream_slug']); return i['id']; });
|
|
1402
|
+
if (query.length) {
|
|
1403
|
+
yield stream_1.StreamModel.schema(this.ref, "_").destroy({ where: { id } });
|
|
1404
|
+
yield stream_1.StreamAssociation.field.model.schema(this.ref, "_").destroy({ where: { stream_id: id } });
|
|
1405
|
+
yield field_section_1.FieldSectionModel.schema(this.ref, "_").destroy({ where: { stream_id: id } });
|
|
1406
|
+
if (redis_1.redisReady) {
|
|
1407
|
+
yield multi.exec();
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
return true;
|
|
1411
|
+
});
|
|
1412
|
+
}
|
|
1413
|
+
addStreamFieldMany(slug, namespace, fields, { stream = {}, checkDuplicate = false } = {}) {
|
|
1414
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1415
|
+
if (!Object.keys(stream).length) {
|
|
1416
|
+
stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({
|
|
1417
|
+
where: { stream_slug: slug, stream_namespace: namespace }, raw: true, attributes: ['id', 'stream_prefix', 'stream_slug', 'stream_namespace', 'stream_type']
|
|
1418
|
+
});
|
|
1419
|
+
if (!stream)
|
|
1420
|
+
return Promise.reject(errors_1.generalError.NOT_FOUND);
|
|
1421
|
+
}
|
|
1422
|
+
for (let field of fields) {
|
|
1423
|
+
yield this.addStreamField(slug, namespace, field['field_slug'], { stream, field, checkDuplicate });
|
|
1424
|
+
}
|
|
1425
|
+
return { success: 1 };
|
|
1426
|
+
});
|
|
1427
|
+
}
|
|
1428
|
+
addStreamField(slug, namespace, field_slug, { stream = {}, field = {}, opt = {}, changeTable = true, checkDuplicate = true, attributes = [] } = {}) {
|
|
1429
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1430
|
+
if (!Object.keys(stream).length) {
|
|
1431
|
+
stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({
|
|
1432
|
+
where: { stream_slug: slug, stream_namespace: namespace }, raw: true, attributes: ['id', 'stream_prefix', 'stream_slug', 'stream_namespace', 'stream_type']
|
|
1433
|
+
});
|
|
1434
|
+
if (!stream)
|
|
1435
|
+
return Promise.reject(errors_1.generalError.NOT_FOUND);
|
|
1436
|
+
}
|
|
1437
|
+
let field_data = {};
|
|
1438
|
+
if (field['field_data']) {
|
|
1439
|
+
if (typeof field['field_data'] == "string") {
|
|
1440
|
+
try {
|
|
1441
|
+
field_data = JSON.parse(field['field_data']);
|
|
1442
|
+
}
|
|
1443
|
+
catch (err) { }
|
|
1444
|
+
}
|
|
1445
|
+
else if (typeof field['field_data'] == "object") {
|
|
1446
|
+
field_data = field['field_data'];
|
|
1447
|
+
field['field_data'] = JSON.stringify(field['field_data']);
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
let table = stream['stream_prefix'] ? stream['stream_prefix'] + stream['stream_slug'] : stream['stream_slug'];
|
|
1451
|
+
if (stream['stream_type'] == "auto" && Object.keys(field).length) {
|
|
1452
|
+
field['field_slug'] = field_slug;
|
|
1453
|
+
if (checkDuplicate) {
|
|
1454
|
+
let exist = yield (0, db_1.getExistField)(`${this.ref}_${table}`, field_slug);
|
|
1455
|
+
if (exist)
|
|
1456
|
+
return Promise.reject(errors_1.generalError.SLUG_DUPLICATE);
|
|
1457
|
+
}
|
|
1458
|
+
if (typeof field_data['default_value'] !== 'undefined') {
|
|
1459
|
+
opt['defaultValue'] = field_data['default_value'];
|
|
1460
|
+
}
|
|
1461
|
+
if (changeTable) {
|
|
1462
|
+
let fields = [];
|
|
1463
|
+
switch (field['field_type']) {
|
|
1464
|
+
case (field['is_lang'] == "yes" ? field['field_type'] : ""):
|
|
1465
|
+
opt['defaultValue'] = undefined;
|
|
1466
|
+
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type: sequelize.TEXT('long') }, opt));
|
|
1467
|
+
break;
|
|
1468
|
+
case "text":
|
|
1469
|
+
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type: sequelize.STRING(255) }, opt));
|
|
1470
|
+
break;
|
|
1471
|
+
case "color":
|
|
1472
|
+
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type: sequelize.STRING(50) }, opt));
|
|
1473
|
+
break;
|
|
1474
|
+
case "bullets":
|
|
1475
|
+
case "textarea":
|
|
1476
|
+
opt['defaultValue'] = undefined;
|
|
1477
|
+
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type: sequelize.TEXT('long') }, opt));
|
|
1478
|
+
break;
|
|
1479
|
+
case "wysiwyg":
|
|
1480
|
+
opt['defaultValue'] = undefined;
|
|
1481
|
+
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type: sequelize.TEXT('long') }, opt));
|
|
1482
|
+
break;
|
|
1483
|
+
case "choice":
|
|
1484
|
+
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type: sequelize.STRING(255) }, opt));
|
|
1485
|
+
break;
|
|
1486
|
+
case "integer":
|
|
1487
|
+
opt['defaultValue'] = _.isNumber(opt['defaultValue']) ? opt['defaultValue'] : undefined;
|
|
1488
|
+
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type: sequelize.INTEGER({ length: 11 }) }, opt));
|
|
1489
|
+
break;
|
|
1490
|
+
case "decimal":
|
|
1491
|
+
opt['defaultValue'] = _.isNumber(opt['defaultValue']) ? opt['defaultValue'] : undefined;
|
|
1492
|
+
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type: sequelize.FLOAT }, opt));
|
|
1493
|
+
break;
|
|
1494
|
+
case "file":
|
|
1495
|
+
case "image":
|
|
1496
|
+
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type: sequelize.CHAR(15) }, opt));
|
|
1497
|
+
break;
|
|
1498
|
+
case "encrypt":
|
|
1499
|
+
opt['defaultValue'] = undefined;
|
|
1500
|
+
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type: sequelize.BLOB }, opt));
|
|
1501
|
+
break;
|
|
1502
|
+
case "datetime":
|
|
1503
|
+
fields = [field_slug];
|
|
1504
|
+
let range = field_data['range'] || {};
|
|
1505
|
+
if (range['field_target']) {
|
|
1506
|
+
fields.push(range['field_target']);
|
|
1507
|
+
}
|
|
1508
|
+
for (let field_slug of fields) {
|
|
1509
|
+
if (field_data['storage'] == "unix") {
|
|
1510
|
+
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type: sequelize.INTEGER({ length: 11 }) }, opt));
|
|
1511
|
+
}
|
|
1512
|
+
else if (field_data['storage'] == "datetime") {
|
|
1513
|
+
let type = sequelize.DATE;
|
|
1514
|
+
if (field_data['use_time'] == "no") {
|
|
1515
|
+
type = sequelize.DATEONLY;
|
|
1516
|
+
}
|
|
1517
|
+
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type }, opt));
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
break;
|
|
1521
|
+
case "time":
|
|
1522
|
+
if (field_data['date'] && field_data['date']['field_target'])
|
|
1523
|
+
break;
|
|
1524
|
+
if (field_data['storage'] == "time") {
|
|
1525
|
+
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type: sequelize.STRING(10) }, opt));
|
|
1526
|
+
}
|
|
1527
|
+
else if (field_data['storage'] == "datetime") {
|
|
1528
|
+
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type: sequelize.DATE }, opt));
|
|
1529
|
+
}
|
|
1530
|
+
break;
|
|
1531
|
+
case "multiple":
|
|
1532
|
+
if (field_data['choose_stream']) {
|
|
1533
|
+
if (field_data['self_table'] != "yes") {
|
|
1534
|
+
let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { id: field_data['choose_stream'] }, raw: true, attributes: ['stream_slug', 'stream_prefix'] });
|
|
1535
|
+
if (stream) {
|
|
1536
|
+
let table_f = stream['stream_prefix'] ? stream['stream_prefix'] + stream['stream_slug'] : stream['stream_slug'];
|
|
1537
|
+
yield (0, db_1.createTable)(`${this.ref}_${table}_${table_f}`, {
|
|
1538
|
+
row_id: { type: sequelize.INTEGER, primaryKey: true },
|
|
1539
|
+
[slug + "_id"]: { type: sequelize.INTEGER, primaryKey: true },
|
|
1540
|
+
[table_f + "_id"]: { type: sequelize.INTEGER, primaryKey: true },
|
|
1541
|
+
organ_id: sequelize.INTEGER
|
|
1542
|
+
});
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
else {
|
|
1546
|
+
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type: sequelize.STRING(255) }, opt));
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
break;
|
|
1550
|
+
case "relationship":
|
|
1551
|
+
opt['defaultValue'] = _.isNumber(opt['defaultValue']) ? opt['defaultValue'] : undefined;
|
|
1552
|
+
yield (0, db_1.addColumn)(`${this.ref}_${table}`, field_slug, Object.assign({ type: sequelize.INTEGER({ length: 11 }) }, opt));
|
|
1553
|
+
break;
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
let sort_order = yield field_1.FieldModel.schema(this.ref, "_").max('sort_order', { where: { stream_id: stream['id'] } });
|
|
1558
|
+
field['sort_order'] = sort_order ? Number(sort_order + 1) : 1;
|
|
1559
|
+
if (!field['stream_id']) {
|
|
1560
|
+
field['stream_id'] = stream['id'];
|
|
1561
|
+
}
|
|
1562
|
+
return yield this.upsert('fields', 'data', field, { attributes });
|
|
1563
|
+
});
|
|
1564
|
+
}
|
|
1565
|
+
removeStreamFieldMany(slug, namespace, field_array) {
|
|
1566
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1567
|
+
let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({
|
|
1568
|
+
where: { stream_slug: slug, stream_namespace: namespace }, raw: true, attributes: ['id', 'stream_prefix', 'stream_slug', 'stream_namespace', 'stream_type']
|
|
1569
|
+
});
|
|
1570
|
+
if (stream && field_array.length) {
|
|
1571
|
+
for (let field_slug of field_array) {
|
|
1572
|
+
yield this.removeStreamField(slug, namespace, field_slug, { stream });
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
return { success: 1 };
|
|
1576
|
+
});
|
|
1577
|
+
}
|
|
1578
|
+
removeStreamField(slug, namespace, field_slug, { field_id = "", stream = {} } = {}) {
|
|
1579
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1580
|
+
if (!Object.keys(stream).length) {
|
|
1581
|
+
stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({
|
|
1582
|
+
where: { stream_slug: slug, stream_namespace: namespace }, raw: true, attributes: ['id', 'stream_prefix', 'stream_slug', 'stream_namespace', 'stream_type']
|
|
1583
|
+
});
|
|
1584
|
+
if (!stream)
|
|
1585
|
+
return Promise.reject(errors_1.generalError.NOT_FOUND);
|
|
1586
|
+
}
|
|
1587
|
+
let table = stream['stream_prefix'] ? stream['stream_prefix'] + stream['stream_slug'] : stream['stream_slug'];
|
|
1588
|
+
let field = {};
|
|
1589
|
+
if (field_id) {
|
|
1590
|
+
let query = yield field_1.FieldModel.schema(this.ref, "_").findOne({ where: { stream_id: stream['id'], id: field_id }, raw: true, attributes: ['id', 'field_slug', 'field_type', 'field_data'] });
|
|
1591
|
+
if (!query) {
|
|
1592
|
+
return Promise.reject(errors_1.generalError.NOT_FOUND);
|
|
1593
|
+
}
|
|
1594
|
+
field_slug = query['field_slug'];
|
|
1595
|
+
field = query;
|
|
1596
|
+
}
|
|
1597
|
+
else {
|
|
1598
|
+
let query = yield field_1.FieldModel.schema(this.ref, "_").findOne({ where: { stream_id: stream['id'], field_slug }, raw: true, attributes: ['id', 'field_type', 'field_data'] });
|
|
1599
|
+
if (!query) {
|
|
1600
|
+
return Promise.reject(errors_1.generalError.NOT_FOUND);
|
|
1601
|
+
}
|
|
1602
|
+
field = query;
|
|
1603
|
+
}
|
|
1604
|
+
let fields = [field_slug], field_data = {};
|
|
1605
|
+
if (field['field_data']) {
|
|
1606
|
+
if (typeof field['field_data'] == 'object') {
|
|
1607
|
+
field_data = field['field_data'];
|
|
1608
|
+
}
|
|
1609
|
+
else if (typeof field['field_data'] == 'string') {
|
|
1610
|
+
try {
|
|
1611
|
+
field_data = JSON.parse(field['field_data']);
|
|
1612
|
+
}
|
|
1613
|
+
catch (err) { }
|
|
1614
|
+
}
|
|
1615
|
+
}
|
|
1616
|
+
switch (field['field_type']) {
|
|
1617
|
+
case "image":
|
|
1618
|
+
case "file":
|
|
1619
|
+
let folder_slug = 'field_' + field['id'];
|
|
1620
|
+
yield this.fileUtil.removeFolder(folder_slug, { parent_folder: this.folder_parent });
|
|
1621
|
+
break;
|
|
1622
|
+
case "datetime":
|
|
1623
|
+
if (field_data['range'] && field_data['range']['field_target']) {
|
|
1624
|
+
fields.push(field_data['range']['field_target']);
|
|
1625
|
+
}
|
|
1626
|
+
break;
|
|
1627
|
+
}
|
|
1628
|
+
yield field_1.FieldModel.schema(this.ref, "_").destroy({ where: { id: field['id'] } });
|
|
1629
|
+
if (stream['stream_type'] == "auto") {
|
|
1630
|
+
for (let field_slug of fields) {
|
|
1631
|
+
if (field['field_type'] == "multiple" && field_data['self_table'] != "yes") {
|
|
1632
|
+
if (field_data['choose_stream']) {
|
|
1633
|
+
let stream = yield stream_1.StreamModel.schema(this.ref, "_").findOne({ where: { id: field_data['choose_stream'] }, raw: true, attributes: ['stream_slug', 'stream_prefix'] });
|
|
1634
|
+
if (stream) {
|
|
1635
|
+
let table_f = stream['stream_prefix'] ? stream['stream_prefix'] + stream['stream_slug'] : stream['stream_slug'];
|
|
1636
|
+
yield (0, db_1.dropTable)(`${this.ref}_${table}_${table_f}`);
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
else {
|
|
1641
|
+
yield (0, db_1.removeColumn)(`${this.ref}_${table}`, field_slug);
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
return { success: 1 };
|
|
1646
|
+
});
|
|
1647
|
+
}
|
|
1648
|
+
inputFieldData(field_type, field_data) {
|
|
1649
|
+
switch (field_type) {
|
|
1650
|
+
case "text":
|
|
1651
|
+
if (field_data['transform.field_target']) {
|
|
1652
|
+
let result = dottie.transform(field_data);
|
|
1653
|
+
field_data['transform'] = result['transform'];
|
|
1654
|
+
}
|
|
1655
|
+
field_data = _.pick(field_data, ['max_length', 'default_value', 'regex', 'transform']);
|
|
1656
|
+
break;
|
|
1657
|
+
case "email":
|
|
1658
|
+
field_data = _.pick(field_data, ['default_value']);
|
|
1659
|
+
break;
|
|
1660
|
+
case "integer":
|
|
1661
|
+
field_data = _.pick(field_data, ['min_value', 'max_value', 'default_value']);
|
|
1662
|
+
break;
|
|
1663
|
+
case "choice":
|
|
1664
|
+
field_data = _.pick(field_data, ['choice_data', 'default_value', 'choice_type', 'min_choices', 'max_choices', 'use_single']);
|
|
1665
|
+
break;
|
|
1666
|
+
case "color":
|
|
1667
|
+
case "textarea":
|
|
1668
|
+
field_data = _.pick(field_data, ['default_value']);
|
|
1669
|
+
break;
|
|
1670
|
+
case "wysiwyg":
|
|
1671
|
+
field_data = _.pick(field_data, ['editor_type']);
|
|
1672
|
+
break;
|
|
1673
|
+
case "decimal":
|
|
1674
|
+
field_data = _.pick(field_data, ['decimal_places', 'min_value', 'max_value', 'default_value']);
|
|
1675
|
+
break;
|
|
1676
|
+
case "image":
|
|
1677
|
+
field_data = _.pick(field_data, ['resize_width', 'resize_hight', 'allowed_types']);
|
|
1678
|
+
break;
|
|
1679
|
+
case "file":
|
|
1680
|
+
field_data = _.pick(field_data, ['allowed_types']);
|
|
1681
|
+
break;
|
|
1682
|
+
case "datetime":
|
|
1683
|
+
if (field_data['range.field_target']) {
|
|
1684
|
+
let result = dottie.transform(field_data);
|
|
1685
|
+
field_data['range'] = result['range'];
|
|
1686
|
+
}
|
|
1687
|
+
field_data = _.pick(field_data, ['storage', 'use_time', 'start_date', 'end_date', 'range']);
|
|
1688
|
+
break;
|
|
1689
|
+
case "time":
|
|
1690
|
+
if (field_data['date.field_target']) {
|
|
1691
|
+
let result = dottie.transform(field_data);
|
|
1692
|
+
field_data['date'] = result['date'];
|
|
1693
|
+
}
|
|
1694
|
+
field_data = _.pick(field_data, ['storage', 'date', 'default_value']);
|
|
1695
|
+
break;
|
|
1696
|
+
case "encrypt":
|
|
1697
|
+
field_data = _.pick(field_data, ['hide_typing']);
|
|
1698
|
+
break;
|
|
1699
|
+
case "relationship":
|
|
1700
|
+
field_data = _.pick(field_data, ['choose_stream', 'skip_data']);
|
|
1701
|
+
break;
|
|
1702
|
+
case "multiple":
|
|
1703
|
+
field_data = _.pick(field_data, ['choose_stream', 'skip_data', 'self_table', 'regex']);
|
|
1704
|
+
break;
|
|
1705
|
+
default:
|
|
1706
|
+
field_data = null;
|
|
1707
|
+
break;
|
|
1708
|
+
}
|
|
1709
|
+
return field_data;
|
|
1710
|
+
}
|
|
1711
|
+
getValidator(streams, { schema = {}, input = {}, req = {}, isNew = false, lang = [], required = true, stripUnknown = false } = {}) {
|
|
1712
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1713
|
+
let field_lang = [];
|
|
1714
|
+
let file = (req.file ? [req.file] : [].concat(req.files || [])).reduce((total, item) => Object.assign(total, { [item['fieldname']]: item }), {});
|
|
1715
|
+
streams.forEach(({ field_type, field_slug, is_required, is_lang, field_data, data }) => {
|
|
1716
|
+
let field;
|
|
1717
|
+
field_data = field_data || {};
|
|
1718
|
+
is_required = (is_required == "yes" && (isNew || (!input[field_slug] && typeof input[field_slug] != "undefined")));
|
|
1719
|
+
if (isNew && (typeof input[field_slug] == "undefined" && field_data['default_value'])) {
|
|
1720
|
+
input[field_slug] = field_data['default_value'];
|
|
1721
|
+
}
|
|
1722
|
+
switch (field_type) {
|
|
1723
|
+
case "text":
|
|
1724
|
+
case "textarea":
|
|
1725
|
+
case "wysiwyg":
|
|
1726
|
+
case "bullets":
|
|
1727
|
+
case "email":
|
|
1728
|
+
field = Joi.string();
|
|
1729
|
+
if (field_type == "email")
|
|
1730
|
+
field = field.email();
|
|
1731
|
+
if (typeof field_data['min_length'] != "undefined" && !Joi.number().validate(field_data['min_length']).error)
|
|
1732
|
+
field = field.min(Number(field_data['min_length']));
|
|
1733
|
+
if (typeof field_data['max_length'] != "undefined" && !Joi.number().validate(field_data['max_length']).error)
|
|
1734
|
+
field = field.max(Number(field_data['max_length']));
|
|
1735
|
+
if (field_data['regex'])
|
|
1736
|
+
field = field.regex(new RegExp(field_data['regex']));
|
|
1737
|
+
if (!is_required)
|
|
1738
|
+
field = field.allow(null, '');
|
|
1739
|
+
break;
|
|
1740
|
+
case "integer":
|
|
1741
|
+
case "decimal":
|
|
1742
|
+
field = Joi.number();
|
|
1743
|
+
if (field_type == "decimal") {
|
|
1744
|
+
let places = (typeof field_data['decimal_places'] != "undefined" && !Joi.number().validate(field_data['decimal_places']).error) ? Number(field_data['decimal_places']) : 2;
|
|
1745
|
+
field = field.precision(places);
|
|
1746
|
+
}
|
|
1747
|
+
if (typeof field_data['min_value'] != "undefined" && !Joi.number().validate(field_data['min_value']).error)
|
|
1748
|
+
field = field.min(Number(field_data['min_value']));
|
|
1749
|
+
if (typeof field_data['max_value'] != "undefined" && !Joi.number().validate(field_data['max_value']).error)
|
|
1750
|
+
field = field.max(Number(field_data['max_value']));
|
|
1751
|
+
if (!is_required)
|
|
1752
|
+
field = field.allow(null, '');
|
|
1753
|
+
break;
|
|
1754
|
+
case "choice":
|
|
1755
|
+
if (typeof input[field_slug] != "undefined") {
|
|
1756
|
+
if (typeof input[field_slug] == "number") {
|
|
1757
|
+
input[field_slug] = String(input[field_slug]);
|
|
1758
|
+
}
|
|
1759
|
+
else {
|
|
1760
|
+
input[field_slug] = String(input[field_slug] || '');
|
|
1761
|
+
}
|
|
1762
|
+
}
|
|
1763
|
+
field = Joi.string();
|
|
1764
|
+
data = (data || []).map(i => i['key']);
|
|
1765
|
+
if (field_data['choice_type'] == "checkboxes") {
|
|
1766
|
+
let regex = new RegExp(`^(${data.join('|')})$`);
|
|
1767
|
+
let invalid = String(input[field_slug] || "").split(",").find((value) => !regex.test(value || ""));
|
|
1768
|
+
if (typeof invalid == "undefined")
|
|
1769
|
+
break;
|
|
1770
|
+
}
|
|
1771
|
+
if (!is_required && field_data['choice_type'] != "dropdown")
|
|
1772
|
+
field = field.allow(null, '');
|
|
1773
|
+
field = field.valid(...data);
|
|
1774
|
+
break;
|
|
1775
|
+
case "datetime":
|
|
1776
|
+
field = helper_1.Joi_date.date().format('YYYY-MM-DD HH:mm:ss').raw();
|
|
1777
|
+
if (field_data['storage'] == "unix") {
|
|
1778
|
+
input[field_slug] = String(input[field_slug] || "");
|
|
1779
|
+
field = Joi.string().length(10).regex(/^[0-9]+$/);
|
|
1780
|
+
}
|
|
1781
|
+
else if (field_data['storage'] == "datetime") {
|
|
1782
|
+
if (field_data['use_time'] == "no") {
|
|
1783
|
+
field = helper_1.Joi_date.date().format('YYYY-MM-DD').raw();
|
|
1784
|
+
}
|
|
1785
|
+
}
|
|
1786
|
+
if (!is_required)
|
|
1787
|
+
field = field.allow(null, '');
|
|
1788
|
+
break;
|
|
1789
|
+
case "time":
|
|
1790
|
+
if (field_data['date'] && field_data['date']['field_target'])
|
|
1791
|
+
break;
|
|
1792
|
+
field = helper_1.Joi_date.date().format('HH:mm:ss').raw();
|
|
1793
|
+
if (!is_required)
|
|
1794
|
+
field = field.allow(null, '');
|
|
1795
|
+
break;
|
|
1796
|
+
case "file":
|
|
1797
|
+
case "image":
|
|
1798
|
+
if (input[field_slug] && String(input[field_slug]).length != 15) {
|
|
1799
|
+
delete input[field_slug];
|
|
1800
|
+
}
|
|
1801
|
+
field = Joi.string();
|
|
1802
|
+
if (!is_required)
|
|
1803
|
+
field = field.allow(null, '');
|
|
1804
|
+
if (file[field_slug]) {
|
|
1805
|
+
is_required = false;
|
|
1806
|
+
let allow_type = false;
|
|
1807
|
+
if (field_type == "image" && !field_data['allowed_types']) {
|
|
1808
|
+
allow_type = new RegExp(/^(image.*)$/).test(file[field_slug]['mimetype']);
|
|
1809
|
+
if (!allow_type) {
|
|
1810
|
+
input[field_slug] = mime.extension(file[field_slug]['mimetype']);
|
|
1811
|
+
field = Joi.string().valid('image/*');
|
|
1812
|
+
}
|
|
1813
|
+
}
|
|
1814
|
+
if (!allow_type && field_data['allowed_types']) {
|
|
1815
|
+
let extension = String(mime.extension(file[field_slug]['mimetype']));
|
|
1816
|
+
if (!new RegExp(`^(${field_data['allowed_types']})$`).test(extension)) {
|
|
1817
|
+
input[field_slug] = extension;
|
|
1818
|
+
field = Joi.string().valid(...(field_data['allowed_types'].split('|') || []));
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
break;
|
|
1823
|
+
case "relationship":
|
|
1824
|
+
case "multiple":
|
|
1825
|
+
let regex = /^\d+$/;
|
|
1826
|
+
field = Joi.string();
|
|
1827
|
+
if (!is_required)
|
|
1828
|
+
field = field.allow(null, '');
|
|
1829
|
+
if (field_type == "multiple") {
|
|
1830
|
+
regex = field_data['regex'] || /^(\d+)(,\s*\d+)*$/;
|
|
1831
|
+
}
|
|
1832
|
+
field = field.regex(new RegExp(regex));
|
|
1833
|
+
if (field_type == "relationship") {
|
|
1834
|
+
field = Joi.alternatives(Joi.number(), field);
|
|
1835
|
+
}
|
|
1836
|
+
break;
|
|
1837
|
+
}
|
|
1838
|
+
field = field || Joi.any();
|
|
1839
|
+
if (is_required)
|
|
1840
|
+
field = field.required();
|
|
1841
|
+
if (is_lang == "yes") {
|
|
1842
|
+
if (!lang.length) {
|
|
1843
|
+
field_lang.push({ field_slug, field });
|
|
1844
|
+
return;
|
|
1845
|
+
}
|
|
1846
|
+
field = Joi.object().keys(lang.reduce((total, i) => Object.assign(total, { [i['lang_code']]: field }), {}));
|
|
1847
|
+
}
|
|
1848
|
+
schema[field_slug] = field;
|
|
1849
|
+
});
|
|
1850
|
+
if (field_lang.length) {
|
|
1851
|
+
lang = yield this.languageUtil.getLangsLive();
|
|
1852
|
+
for (let item of field_lang) {
|
|
1853
|
+
let field = item['field'];
|
|
1854
|
+
if (lang.length)
|
|
1855
|
+
field = Joi.object().keys(lang.reduce((total, i) => Object.assign(total, { [i['lang_code']]: field }), {}));
|
|
1856
|
+
schema[item['field_slug']] = field;
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1859
|
+
let validator = stripUnknown ? Joi.object().keys(schema).options({ stripUnknown: true }) : Joi.object().keys(schema).unknown();
|
|
1860
|
+
if (required)
|
|
1861
|
+
validator = validator.required();
|
|
1862
|
+
return validator;
|
|
1863
|
+
});
|
|
1864
|
+
}
|
|
1865
|
+
getValidatorSchema(slug, namespace, { req = {}, isNew = false, required = true, lang = [], stripUnknown = false, builder = true } = {}) {
|
|
1866
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1867
|
+
let streams = yield this.getBuilder(slug, namespace, { index: "fields", builder });
|
|
1868
|
+
return yield this.getValidator(streams, { req, isNew, required, lang, stripUnknown });
|
|
1869
|
+
});
|
|
1870
|
+
}
|
|
1871
|
+
validate(slug, namespace, input, { req = {}, isNew = false, default_value = {}, lang = [], stripUnknown = false, inputLang = false, inputLangSeparate = true, prefixMessage = "", builder = true, field_exclude = [] } = {}) {
|
|
1872
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1873
|
+
let streams = yield this.getBuilder(slug, namespace, { index: "fields", builder, field_exclude });
|
|
1874
|
+
return this.validateByField(streams, input, { req, isNew, default_value, lang, stripUnknown, inputLang, inputLangSeparate, prefixMessage });
|
|
1875
|
+
});
|
|
1876
|
+
}
|
|
1877
|
+
validateByField(streams, input, { req = {}, isNew = false, default_value = {}, lang = [], stripUnknown = false, inputLang = false, inputLangSeparate = true, prefixMessage = "" } = {}) {
|
|
1878
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
1879
|
+
if (streams && streams.length) {
|
|
1880
|
+
let validator = yield this.getValidator(streams, { input, req, isNew, lang, stripUnknown });
|
|
1881
|
+
input = yield (0, validator_1.validate)(validator, Object.assign(Object.assign({}, input), default_value), { prefixMessage });
|
|
1882
|
+
if (inputLang) {
|
|
1883
|
+
input = yield this.getInputByLang(input, { separate: inputLangSeparate, lang });
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
else if (stripUnknown) {
|
|
1887
|
+
input = {};
|
|
1888
|
+
}
|
|
1889
|
+
return input;
|
|
1890
|
+
});
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
exports.StreamUtil = StreamUtil;
|