@opengis/fastify-table 1.4.46 → 1.4.48
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/index.js +3 -0
- package/package.json +1 -1
- package/server/plugins/grpc/grpc.js +74 -249
- package/server/plugins/grpc/office2pdf.js +38 -19
- package/server/plugins/migration/exec.migrations.js +43 -19
- package/server/plugins/migration/exec.sql.js +6 -4
- package/server/plugins/pg/funcs/init.js +1 -0
- package/server/plugins/redis/funcs/getRedis.js +1 -1
- package/server/routes/widget/controllers/file.edit.js +55 -0
- package/server/routes/widget/controllers/widget.del.js +89 -0
- package/server/routes/widget/controllers/widget.get.js +137 -0
- package/server/routes/widget/controllers/widget.set.js +106 -0
- package/server/routes/widget/hook/onWidgetSet.js +13 -0
- package/server/routes/widget/index.mjs +38 -0
package/index.js
CHANGED
|
@@ -42,6 +42,8 @@ import dblistRoutes from './server/routes/dblist/index.mjs';
|
|
|
42
42
|
import menuRoutes from './server/routes/menu/index.mjs';
|
|
43
43
|
import templatesRoutes from './server/routes/templates/index.mjs';
|
|
44
44
|
|
|
45
|
+
import widgetRoutes from './server/routes/widget/index.mjs';
|
|
46
|
+
|
|
45
47
|
// core templates && cls
|
|
46
48
|
const filename = fileURLToPath(import.meta.url);
|
|
47
49
|
const cwd = path.dirname(filename);
|
|
@@ -110,6 +112,7 @@ async function plugin(fastify, opt) {
|
|
|
110
112
|
propertiesRoutes(fastify, opt);
|
|
111
113
|
tableRoutes(fastify, opt);
|
|
112
114
|
utilRoutes(fastify, opt);
|
|
115
|
+
widgetRoutes(fastify, opt);
|
|
113
116
|
|
|
114
117
|
menuRoutes(fastify, opt);
|
|
115
118
|
templatesRoutes(fastify, opt);
|
package/package.json
CHANGED
|
@@ -1,26 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { fileURLToPath } from 'url';
|
|
3
|
+
|
|
2
4
|
import grpc from '@grpc/grpc-js';
|
|
3
5
|
import protoLoader from '@grpc/proto-loader';
|
|
4
6
|
|
|
5
7
|
import config from '../../../config.js';
|
|
6
8
|
import logger from '../logger/getLogger.js';
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
const filename = fileURLToPath(import.meta.url);
|
|
11
|
+
const dirname = path.dirname(filename);
|
|
9
12
|
|
|
10
|
-
|
|
13
|
+
config.ready = config.ready || {};
|
|
11
14
|
|
|
12
15
|
// external ip not-accessible from internal network
|
|
13
|
-
const defaultConvertServerAddress = config.pg?.host?.startsWith('192.168.3')
|
|
16
|
+
const defaultConvertServerAddress = config.pg?.host?.startsWith?.('192.168.3')
|
|
17
|
+
? '192.168.1.96:4003'
|
|
18
|
+
: '193.239.152.181:44003';
|
|
14
19
|
|
|
15
|
-
const convertServerAddress = config.convertServerAddress
|
|
20
|
+
const convertServerAddress = config.convertServerAddress === true
|
|
21
|
+
? defaultConvertServerAddress
|
|
22
|
+
: config.convertServerAddress;
|
|
16
23
|
|
|
17
|
-
console.log('convertServerAddress: ', convertServerAddress, !!config.convertServerAddress);
|
|
24
|
+
if (config.local || config.debug) console.log('convertServerAddress: ', convertServerAddress, !!config.convertServerAddress);
|
|
18
25
|
|
|
19
|
-
const relpath = 'server/plugins/grpc/utils/convertp.proto';
|
|
20
|
-
const protoLocation = process.cwd().includes('fastify-table') ? relpath : `node_modules/@opengis/fastify-table/${relpath}`;
|
|
26
|
+
// const relpath = 'server/plugins/grpc/utils/convertp.proto';
|
|
27
|
+
// const protoLocation = process.cwd().includes('fastify-table') ? relpath : `node_modules/@opengis/fastify-table/${relpath}`;
|
|
21
28
|
|
|
22
29
|
const proto = grpc.loadPackageDefinition(
|
|
23
|
-
protoLoader.loadSync(
|
|
30
|
+
protoLoader.loadSync(`${dirname}/utils/convertp.proto`, {
|
|
24
31
|
keepCase: true,
|
|
25
32
|
longs: String,
|
|
26
33
|
enums: String,
|
|
@@ -29,267 +36,85 @@ const proto = grpc.loadPackageDefinition(
|
|
|
29
36
|
}),
|
|
30
37
|
);
|
|
31
38
|
|
|
32
|
-
const convertClient = new proto.Convert(
|
|
39
|
+
const convertClient = convertServerAddress ? new proto.Convert(
|
|
33
40
|
convertServerAddress,
|
|
34
41
|
grpc.credentials.createInsecure(),
|
|
35
42
|
{
|
|
36
43
|
'grpc.max_send_message_length': 512 * 1024 * 1024,
|
|
37
44
|
'grpc.max_receive_message_length': 512 * 1024 * 1024,
|
|
38
45
|
},
|
|
39
|
-
);
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
) : {};
|
|
47
|
+
|
|
48
|
+
if (convertServerAddress) {
|
|
49
|
+
convertClient.waitForReady(Date.now() + 5000, (err) => {
|
|
50
|
+
if (err) {
|
|
51
|
+
config.ready.convert = false;
|
|
52
|
+
console.error('Client connection timeout or failure:', err);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
config.ready.convert = true;
|
|
56
|
+
console.log('Client connected successfully.');
|
|
57
|
+
// You can now make RPC calls safely
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const methodNames = [
|
|
63
|
+
'pdfMerge',
|
|
64
|
+
'htmlToPdf',
|
|
65
|
+
'csvToXls',
|
|
66
|
+
'jsonToXls',
|
|
67
|
+
'excelToJson',
|
|
68
|
+
'xmlToJson',
|
|
69
|
+
'htmlToDoc',
|
|
70
|
+
'htmlToImage',
|
|
71
|
+
'shpToGeojson',
|
|
72
|
+
'geojsonToShp',
|
|
73
|
+
'geojsonToGpkg',
|
|
74
|
+
'docToPDF',
|
|
75
|
+
'mergeImages',
|
|
76
|
+
'resizeImage',
|
|
77
|
+
'jsonToYaml',
|
|
78
|
+
'yamlToJson',
|
|
79
|
+
'log',
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
const wrapGrpcCall = (methodName) => async (data) => new Promise((res, rej) => {
|
|
83
|
+
if (!convertServerAddress) {
|
|
84
|
+
logger.file('grpc/convert', {
|
|
85
|
+
method: methodName,
|
|
86
|
+
error: 'grpc convert not set',
|
|
87
|
+
stack: new Error().stack,
|
|
88
|
+
});
|
|
89
|
+
return rej(new Error('grpc convert not set'));
|
|
45
90
|
}
|
|
46
|
-
|
|
47
|
-
config.ready.convert = true;
|
|
48
|
-
console.log('Client connected successfully.');
|
|
49
|
-
// You can now make RPC calls safely
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
// rewrite, merge??
|
|
54
|
-
const pdfMerge = async (data) => new Promise((res, rej) => {
|
|
55
|
-
convertClient.pdfMerge(data, (err, data1) => {
|
|
56
|
-
if (err) {
|
|
57
|
-
logger.file('grpc/convert', { error: err.toString(), stack: err.stack, ready: config.ready.convert });
|
|
58
|
-
if (!config.ready.convert) {
|
|
59
|
-
return rej(new Error('no grpc convert connection'));
|
|
60
|
-
}
|
|
61
|
-
return rej(err);
|
|
62
|
-
}
|
|
63
|
-
res(data1);
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
const htmlToPdf = async (data) => new Promise((res, rej) => {
|
|
68
|
-
convertClient.htmlToPdf(data, (err, data1) => {
|
|
69
|
-
if (err) {
|
|
70
|
-
logger.file('grpc/convert', { error: err.toString(), stack: err.stack, ready: config.ready.convert });
|
|
71
|
-
if (!config.ready.convert) {
|
|
72
|
-
return rej(new Error('no grpc convert connection'));
|
|
73
|
-
}
|
|
74
|
-
return rej(err);
|
|
75
|
-
}
|
|
76
|
-
res(data1);
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
const csvToXls = async (data) => new Promise((res, rej) => {
|
|
81
|
-
convertClient.csvToXls(data, (err, data1) => {
|
|
82
|
-
if (err) {
|
|
83
|
-
logger.file('grpc/convert', { error: err.toString(), stack: err.stack, ready: config.ready.convert });
|
|
84
|
-
if (!config.ready.convert) {
|
|
85
|
-
return rej(new Error('no grpc convert connection'));
|
|
86
|
-
}
|
|
87
|
-
return rej(err);
|
|
88
|
-
}
|
|
89
|
-
res(data1);
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
const jsonToXls = async (data) => new Promise((res, rej) => {
|
|
94
|
-
convertClient.jsonToXls(data, (err, data1) => {
|
|
95
|
-
if (err) {
|
|
96
|
-
logger.file('grpc/convert', { error: err.toString(), stack: err.stack, ready: config.ready.convert });
|
|
97
|
-
if (!config.ready.convert) {
|
|
98
|
-
return rej(new Error('no grpc convert connection'));
|
|
99
|
-
}
|
|
100
|
-
return rej(err);
|
|
101
|
-
}
|
|
102
|
-
res(data1);
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
const excelToJson = async (data) => new Promise((res, rej) => {
|
|
107
|
-
convertClient.excelToJson(data, (err, data1) => {
|
|
108
|
-
if (err) {
|
|
109
|
-
logger.file('grpc/convert', { error: err.toString(), stack: err.stack, ready: config.ready.convert });
|
|
110
|
-
if (!config.ready.convert) {
|
|
111
|
-
return rej(new Error('no grpc convert connection'));
|
|
112
|
-
}
|
|
113
|
-
return rej(err);
|
|
114
|
-
}
|
|
115
|
-
res(data1);
|
|
116
|
-
});
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
const xmlToJson = async (data) => new Promise((res, rej) => {
|
|
120
|
-
convertClient.xmlToJson(data, (err, data1) => {
|
|
121
|
-
if (err) {
|
|
122
|
-
logger.file('grpc/convert', { error: err.toString(), stack: err.stack, ready: config.ready.convert });
|
|
123
|
-
if (!config.ready.convert) {
|
|
124
|
-
return rej(new Error('no grpc convert connection'));
|
|
125
|
-
}
|
|
126
|
-
return rej(err);
|
|
127
|
-
}
|
|
128
|
-
res(data1);
|
|
129
|
-
});
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
const html2Doc = async (data) => new Promise((res, rej) => {
|
|
133
|
-
convertClient.htmlToDoc(data, (err, data1) => {
|
|
134
|
-
if (err) {
|
|
135
|
-
logger.file('grpc/convert', { error: err.toString(), stack: err.stack, ready: config.ready.convert });
|
|
136
|
-
if (!config.ready.convert) {
|
|
137
|
-
return rej(new Error('no grpc convert connection'));
|
|
138
|
-
}
|
|
139
|
-
return rej(err);
|
|
140
|
-
}
|
|
141
|
-
res(data1);
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
const htmlToImg = async (data) => new Promise((res, rej) => {
|
|
146
|
-
convertClient.htmlToImage(data, (err, data1) => {
|
|
147
|
-
if (err) {
|
|
148
|
-
logger.file('grpc/convert', { error: err.toString(), stack: err.stack, ready: config.ready.convert });
|
|
149
|
-
if (!config.ready.convert) {
|
|
150
|
-
return rej(new Error('no grpc convert connection'));
|
|
151
|
-
}
|
|
152
|
-
return rej(err);
|
|
153
|
-
}
|
|
154
|
-
res(data1);
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
const shpToGeojson = async (data) => new Promise((res, rej) => {
|
|
159
|
-
convertClient.shpToGeojson(data, (err, data1) => {
|
|
160
|
-
if (err) {
|
|
161
|
-
logger.file('grpc/convert', { error: err.toString(), stack: err.stack, ready: config.ready.convert });
|
|
162
|
-
if (!config.ready.convert) {
|
|
163
|
-
return rej(new Error('no grpc convert connection'));
|
|
164
|
-
}
|
|
165
|
-
return rej(err);
|
|
166
|
-
}
|
|
167
|
-
res(data1);
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
const geojsonToShp = async (data) => new Promise((res, rej) => {
|
|
172
|
-
convertClient.geojsonToShp(data, (err, data1) => {
|
|
173
|
-
if (err) {
|
|
174
|
-
logger.file('grpc/convert', { error: err.toString(), stack: err.stack, ready: config.ready.convert });
|
|
175
|
-
if (!config.ready.convert) {
|
|
176
|
-
return rej(new Error('no grpc convert connection'));
|
|
177
|
-
}
|
|
178
|
-
return rej(err);
|
|
179
|
-
}
|
|
180
|
-
res(data1);
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
const geojsonToGpkg = async (data) => new Promise((res, rej) => {
|
|
185
|
-
convertClient.geojsonToGpkg(data, (err, data1) => {
|
|
186
|
-
if (err) {
|
|
187
|
-
logger.file('grpc/convert', { error: err.toString(), stack: err.stack, ready: config.ready.convert });
|
|
188
|
-
if (!config.ready.convert) {
|
|
189
|
-
return rej(new Error('no grpc convert connection'));
|
|
190
|
-
}
|
|
191
|
-
return rej(err);
|
|
192
|
-
}
|
|
193
|
-
res(data1);
|
|
194
|
-
});
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
const docToPDF = async (data) => new Promise((res, rej) => {
|
|
198
|
-
convertClient.docToPDF(data, (err, data1) => {
|
|
199
|
-
if (err) {
|
|
200
|
-
logger.file('grpc/convert', { error: err.toString(), stack: err.stack, ready: config.ready.convert });
|
|
201
|
-
if (!config.ready.convert) {
|
|
202
|
-
return rej(new Error('no grpc convert connection'));
|
|
203
|
-
}
|
|
204
|
-
return rej(err);
|
|
205
|
-
}
|
|
206
|
-
res(data1);
|
|
207
|
-
});
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
const mergeImages = async (data) => new Promise((res, rej) => {
|
|
211
|
-
convertClient.mergeImages(data, (err, data1) => {
|
|
91
|
+
convertClient[methodName](data, (err, response) => {
|
|
212
92
|
if (err) {
|
|
213
|
-
logger.file('grpc/convert', {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
res(data1);
|
|
220
|
-
});
|
|
221
|
-
});
|
|
93
|
+
logger.file('grpc/convert', {
|
|
94
|
+
method: methodName,
|
|
95
|
+
error: err.toString(),
|
|
96
|
+
stack: err.stack,
|
|
97
|
+
ready: config.ready.convert,
|
|
98
|
+
});
|
|
222
99
|
|
|
223
|
-
const resizeImage = async (data) => new Promise((res, rej) => {
|
|
224
|
-
convertClient.resizeImage(data, (err, data1) => {
|
|
225
|
-
if (err) {
|
|
226
|
-
logger.file('grpc/convert', { error: err.toString(), stack: err.stack, ready: config.ready.convert });
|
|
227
100
|
if (!config.ready.convert) {
|
|
228
101
|
return rej(new Error('no grpc convert connection'));
|
|
229
102
|
}
|
|
230
|
-
return rej(err);
|
|
231
|
-
}
|
|
232
|
-
res(data1);
|
|
233
|
-
});
|
|
234
|
-
});
|
|
235
103
|
|
|
236
|
-
const jsonToYaml = async (data) => new Promise((res, rej) => {
|
|
237
|
-
convertClient.jsonToYaml(data, (err, data1) => {
|
|
238
|
-
if (err) {
|
|
239
|
-
logger.file('grpc/convert', { error: err.toString(), stack: err.stack, ready: config.ready.convert });
|
|
240
|
-
if (!config.ready.convert) {
|
|
241
|
-
return rej(new Error('no grpc convert connection'));
|
|
242
|
-
}
|
|
243
104
|
return rej(err);
|
|
244
105
|
}
|
|
245
|
-
res(data1);
|
|
246
|
-
});
|
|
247
|
-
});
|
|
248
106
|
|
|
249
|
-
|
|
250
|
-
convertClient.yamlToJson(data, (err, data1) => {
|
|
251
|
-
if (err) {
|
|
252
|
-
logger.file('grpc/convert', { error: err.toString(), stack: err.stack, ready: config.ready.convert });
|
|
253
|
-
if (!config.ready.convert) {
|
|
254
|
-
return rej(new Error('no grpc convert connection'));
|
|
255
|
-
}
|
|
256
|
-
return rej(err);
|
|
257
|
-
}
|
|
258
|
-
res(data1);
|
|
107
|
+
return res(response);
|
|
259
108
|
});
|
|
260
109
|
});
|
|
261
110
|
|
|
262
|
-
const
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
if (!config.ready.convert) {
|
|
267
|
-
return rej(new Error('no grpc convert connection'));
|
|
268
|
-
}
|
|
269
|
-
return rej(err);
|
|
270
|
-
}
|
|
271
|
-
res(data1);
|
|
272
|
-
});
|
|
273
|
-
});
|
|
111
|
+
const grpcMethods = methodNames.reduce((acc, name) => {
|
|
112
|
+
acc[name] = wrapGrpcCall(name);
|
|
113
|
+
return acc;
|
|
114
|
+
}, {});
|
|
274
115
|
|
|
275
116
|
const getGRPC = () => ({
|
|
276
|
-
|
|
277
|
-
htmlToPdf,
|
|
278
|
-
csvToXls,
|
|
279
|
-
jsonToXls,
|
|
280
|
-
excelToJson,
|
|
281
|
-
xmlToJson,
|
|
282
|
-
html2Doc,
|
|
283
|
-
htmlToImg,
|
|
284
|
-
shpToGeojson,
|
|
285
|
-
geojsonToShp,
|
|
286
|
-
geojsonToGpkg,
|
|
287
|
-
docToPDF,
|
|
288
|
-
mergeImages,
|
|
289
|
-
resizeImage,
|
|
290
|
-
jsonToYaml,
|
|
291
|
-
yamlToJson,
|
|
292
|
-
log,
|
|
117
|
+
...grpcMethods,
|
|
293
118
|
convertServerAddress,
|
|
294
119
|
});
|
|
295
120
|
|
|
@@ -1,24 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { fileURLToPath } from 'url';
|
|
3
|
+
|
|
2
4
|
import grpc from '@grpc/grpc-js';
|
|
3
5
|
import protoLoader from '@grpc/proto-loader';
|
|
4
6
|
|
|
5
7
|
import config from '../../../config.js';
|
|
6
8
|
import logger from '../logger/getLogger.js';
|
|
7
9
|
|
|
10
|
+
const filename = fileURLToPath(import.meta.url);
|
|
11
|
+
const dirname = path.dirname(filename);
|
|
12
|
+
|
|
8
13
|
config.ready = config.ready || {};
|
|
9
14
|
|
|
10
15
|
// external ip not-accessible from internal network
|
|
11
|
-
const defaultOfficeConverterServerAddress = config.pg?.host?.startsWith('192.168.3')
|
|
16
|
+
const defaultOfficeConverterServerAddress = config.pg?.host?.startsWith?.('192.168.3')
|
|
17
|
+
? '192.168.1.96:4011'
|
|
18
|
+
: '193.239.152.181:44011';
|
|
12
19
|
|
|
13
|
-
const officeConverterServerAddress = config.officeConverterServerAddress
|
|
20
|
+
const officeConverterServerAddress = config.officeConverterServerAddress === true
|
|
21
|
+
? defaultOfficeConverterServerAddress
|
|
22
|
+
: config.officeConverterServerAddress;
|
|
14
23
|
|
|
15
24
|
console.log('officeConverterServerAddress: ', officeConverterServerAddress, !!config.officeConverterServerAddress);
|
|
16
25
|
|
|
17
|
-
const relpath = 'server/plugins/grpc/utils/office2pdf.proto';
|
|
18
|
-
const protoLocation = process.cwd().includes('fastify-table') ? relpath : `node_modules/@opengis/fastify-table/${relpath}`;
|
|
26
|
+
// const relpath = 'server/plugins/grpc/utils/office2pdf.proto';
|
|
27
|
+
// const protoLocation = process.cwd().includes('fastify-table') ? relpath : `node_modules/@opengis/fastify-table/${relpath}`;
|
|
19
28
|
|
|
20
29
|
const proto = grpc.loadPackageDefinition(
|
|
21
|
-
protoLoader.loadSync(
|
|
30
|
+
protoLoader.loadSync(`${dirname}/utils/office2pdf.proto`, {
|
|
22
31
|
keepCase: true,
|
|
23
32
|
longs: String,
|
|
24
33
|
enums: String,
|
|
@@ -27,28 +36,38 @@ const proto = grpc.loadPackageDefinition(
|
|
|
27
36
|
}),
|
|
28
37
|
);
|
|
29
38
|
|
|
30
|
-
const officeClient = new proto.OfficeConverterService(
|
|
39
|
+
const officeClient = officeConverterServerAddress ? new proto.OfficeConverterService(
|
|
31
40
|
officeConverterServerAddress,
|
|
32
41
|
grpc.credentials.createInsecure(),
|
|
33
42
|
{
|
|
34
43
|
'grpc.max_send_message_length': 512 * 1024 * 1024,
|
|
35
44
|
'grpc.max_receive_message_length': 512 * 1024 * 1024,
|
|
36
45
|
},
|
|
37
|
-
);
|
|
46
|
+
) : {};
|
|
38
47
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
48
|
+
if (officeConverterServerAddress) {
|
|
49
|
+
officeClient.waitForReady(Date.now() + 5000, (err) => {
|
|
50
|
+
if (err) {
|
|
51
|
+
config.ready.office2pdf = false;
|
|
52
|
+
console.error('Client connection timeout or failure:', err);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
config.ready.office2pdf = true;
|
|
56
|
+
console.log('Client connected successfully.');
|
|
57
|
+
// You can now make RPC calls safely
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
50
61
|
|
|
51
62
|
const officeToPdf = async (data) => new Promise((res, rej) => {
|
|
63
|
+
if (!officeConverterServerAddress) {
|
|
64
|
+
logger.file('grpc/convert', {
|
|
65
|
+
method: 'officeToPdf',
|
|
66
|
+
error: 'grpc office2pdf not set',
|
|
67
|
+
stack: new Error().stack,
|
|
68
|
+
});
|
|
69
|
+
return rej(new Error('grpc office2pdf not set'));
|
|
70
|
+
}
|
|
52
71
|
officeClient.OfficeToPdf(data, (err, data1) => {
|
|
53
72
|
if (err) {
|
|
54
73
|
logger.file('grpc/office2pdf', { error: err.toString(), stack: err.stack, ready: config.ready.office2pdf });
|
|
@@ -7,33 +7,57 @@ import pgClients from '../pg/pgClients.js';
|
|
|
7
7
|
// import getCallerDir from './get.caller.dir.js';
|
|
8
8
|
|
|
9
9
|
const time = Date.now();
|
|
10
|
+
const debug = config.debug || config.local;
|
|
10
11
|
|
|
11
12
|
export default async function execMigrations(dirPath, pg = pgClients.client, iscore = false) {
|
|
12
|
-
if (
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|| process.env.NODE_ENV === 'unit test'
|
|
17
|
-
) {
|
|
18
|
-
console.log('migrations skip', 'core: ', !!iscore, 'dir: ', dirPath || 'not specified');
|
|
19
|
-
return;
|
|
13
|
+
if (!dirPath) {
|
|
14
|
+
const txt = 'migrations skip: path not specified';
|
|
15
|
+
if (debug) console.warn(txt);
|
|
16
|
+
return txt;
|
|
20
17
|
}
|
|
21
18
|
|
|
22
|
-
|
|
19
|
+
if (config.migrationsCore === false && iscore) {
|
|
20
|
+
const txt = `migrations skip: core - ${dirPath}`;
|
|
21
|
+
if (debug) console.log(txt);
|
|
22
|
+
return txt;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (config.migrations === false && !iscore) {
|
|
26
|
+
const txt = `migrations skip: path - ${dirPath}`;
|
|
27
|
+
if (debug) console.log(txt);
|
|
28
|
+
return txt;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (process.env.NODE_ENV !== 'production' && (!config.migrations || !(config.migrationsCore && iscore))) {
|
|
32
|
+
const txt = `migrations skip: not a production environment - ${iscore ? 'core' : 'path'} : ${dirPath}`;
|
|
33
|
+
if (debug) console.log(txt);
|
|
34
|
+
return txt;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (debug) console.log('migrations start', dirPath, Date.now() - time);
|
|
23
38
|
|
|
24
39
|
const exists = existsSync(dirPath);
|
|
25
40
|
|
|
26
|
-
if (exists) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
41
|
+
if (!exists) {
|
|
42
|
+
const txt = `migrations skip: directory not found - ${dirPath}`;
|
|
43
|
+
if (debug) console.warn(txt);
|
|
44
|
+
return txt;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// get directory sql file list
|
|
48
|
+
const content = readdirSync(dirPath, { withFileTypes: true })
|
|
49
|
+
?.filter((el) => el.isFile() && path.extname(el.name) === '.sql')
|
|
50
|
+
?.map((el) => el.name) || [];
|
|
31
51
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
52
|
+
// execute sql files
|
|
53
|
+
if (!content?.length) {
|
|
54
|
+
const txt = `migrations skip: no sql in specified directory - ${dirPath}`;
|
|
55
|
+
if (debug) console.warn(txt);
|
|
56
|
+
return txt;
|
|
36
57
|
}
|
|
37
58
|
|
|
38
|
-
|
|
59
|
+
await content.reduce((promise, filename) => promise.then(() => execSql(path.join(dirPath, filename), pg)), Promise.resolve());
|
|
60
|
+
if (debug) console.log('migrations success', dirPath, exists, Date.now() - time);
|
|
61
|
+
|
|
62
|
+
return 'success';
|
|
39
63
|
}
|
|
@@ -12,6 +12,8 @@ import getRedis from '../redis/funcs/getRedis.js';
|
|
|
12
12
|
|
|
13
13
|
const rclient = getRedis();
|
|
14
14
|
|
|
15
|
+
const debug = config.debug || config.local;
|
|
16
|
+
|
|
15
17
|
export default async function execSql(filepath, pg = pgClients.client) {
|
|
16
18
|
const start = Date.now();
|
|
17
19
|
|
|
@@ -33,16 +35,16 @@ export default async function execSql(filepath, pg = pgClients.client) {
|
|
|
33
35
|
const hash = createHash('md5').update(sql).digest('hex');
|
|
34
36
|
const hashes = config.redis ? await rclient.hgetall(`${pg?.options?.database}:migration-hashes`).then(obj => Object.keys(obj)) : [];
|
|
35
37
|
|
|
36
|
-
if (hashes.includes(hash) && !config.disableCache) {
|
|
38
|
+
if (hashes.includes(hash) && !config.disableCache && debug) {
|
|
37
39
|
console.log(filename, 'skip equal hash', Date.now() - start);
|
|
38
40
|
return null;
|
|
39
41
|
}
|
|
40
42
|
|
|
41
43
|
try {
|
|
42
|
-
console.log(filename, 'start', Date.now() - start);
|
|
44
|
+
if (debug) console.log(filename, 'start', Date.now() - start);
|
|
43
45
|
await pg.query(sql);
|
|
44
46
|
if (!config.disableCache && config.redis) await rclient.hset(`${pg?.options?.database}:migration-hashes`, hash, 1);
|
|
45
|
-
console.log(filename, 'finish', Date.now() - start);
|
|
47
|
+
if (debug) console.log(filename, 'finish', Date.now() - start);
|
|
46
48
|
logger.file('migration/success', {
|
|
47
49
|
filepath,
|
|
48
50
|
result: 'success',
|
|
@@ -51,7 +53,7 @@ export default async function execSql(filepath, pg = pgClients.client) {
|
|
|
51
53
|
return 'ok';
|
|
52
54
|
}
|
|
53
55
|
catch (err) {
|
|
54
|
-
console.
|
|
56
|
+
console.error(filename, 'error', err.toString(), Date.now() - start);
|
|
55
57
|
logger.file('migration/error', {
|
|
56
58
|
filepath,
|
|
57
59
|
result: 'error',
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import dataUpdate from '../../../plugins/crud/funcs/dataUpdate.js';
|
|
4
|
+
import pgClients from '../../../plugins/pg/pgClients.js';
|
|
5
|
+
import uploadMultiPart from '../../../plugins/file/uploadMultiPart.js';
|
|
6
|
+
|
|
7
|
+
export default async function widgetSet(req, reply) {
|
|
8
|
+
const {
|
|
9
|
+
pg = pgClients.client, headers = {}, user = {}, params = {},
|
|
10
|
+
} = req;
|
|
11
|
+
|
|
12
|
+
if (!params?.id) {
|
|
13
|
+
return reply.status(400).send('not enough params: id');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (!pg.pk?.['crm.files']) {
|
|
17
|
+
return reply.status(404).send('table not found');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (headers['content-type']?.split?.(';')?.shift?.() !== 'multipart/form-data') {
|
|
21
|
+
return reply.status(400).send('invalid payload content type');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const file = await uploadMultiPart(req);
|
|
25
|
+
const extName = path.extname(file.filepath).slice(1).toLowerCase();
|
|
26
|
+
|
|
27
|
+
const data = {
|
|
28
|
+
uploaded_name: file?.originalFilename?.toLocaleLowerCase()?.replace(/'/g, '\'\''),
|
|
29
|
+
file_path: file?.relativeFilepath?.replace(/\\/g, '/'),
|
|
30
|
+
ext: extName,
|
|
31
|
+
size: file?.size,
|
|
32
|
+
file_status: 1,
|
|
33
|
+
uid: user?.uid || 1,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const result = await dataUpdate({
|
|
37
|
+
pg,
|
|
38
|
+
table: 'crm.files',
|
|
39
|
+
id: params.id,
|
|
40
|
+
data,
|
|
41
|
+
uid: user?.uid,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
if (!result?.file_id) {
|
|
45
|
+
return reply.status(404).send('file not found');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return reply.status(200).send({
|
|
49
|
+
rowCount: 1,
|
|
50
|
+
data: result,
|
|
51
|
+
command: 'UPLOAD',
|
|
52
|
+
id: result?.file_id,
|
|
53
|
+
entity_id: result?.entity_id,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import config from '../../../../config.js';
|
|
2
|
+
import isFileExists from '../../../plugins/file/isFileExists.js';
|
|
3
|
+
import logChanges from '../../../plugins/crud/funcs/utils/logChanges.js';
|
|
4
|
+
import pgClients from '../../../plugins/pg/pgClients.js';
|
|
5
|
+
|
|
6
|
+
const isAdmin = (req) => process.env.NODE_ENV === 'admin'
|
|
7
|
+
|| config.admin
|
|
8
|
+
|| req?.hostname?.split?.(':')?.shift?.() === config.adminDomain
|
|
9
|
+
|| req?.hostname?.startsWith?.('admin');
|
|
10
|
+
|
|
11
|
+
async function checkAccess(pg, objectid, id) {
|
|
12
|
+
const { uid, filepath } = await pg.query('select uid, file_path as filepath from crm.files where entity_id=$1 and file_id=$2', [objectid, id])
|
|
13
|
+
.then(el => el.rows?.[0] || {});
|
|
14
|
+
return { uid, exists: filepath ? await isFileExists(filepath) : null };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Дістає CRM дані для vue хешує ідентифікатори, підтягує селекти
|
|
19
|
+
*
|
|
20
|
+
* @method DELETE
|
|
21
|
+
* @summary CRM дані для обраного віджета.
|
|
22
|
+
* @priority 2
|
|
23
|
+
* @tag table
|
|
24
|
+
* @type api
|
|
25
|
+
* @requires setTokenById
|
|
26
|
+
* @requires getSelect
|
|
27
|
+
* @param {String} id Ідентифікатор для хешування
|
|
28
|
+
* @param {Any} sql Використовується для повернення sql запиту
|
|
29
|
+
* @param {String} type Тип для хешування даних
|
|
30
|
+
* @errors 400, 500
|
|
31
|
+
* @returns {Number} status Номер помилки
|
|
32
|
+
* @returns {String|Object} error Опис помилки
|
|
33
|
+
* @returns {String|Object} message Повідомлення про успішне виконання або об'єкт з параметрами
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
export default async function widgetDel(req, reply) {
|
|
37
|
+
const {
|
|
38
|
+
pg = pgClients.client, params = {}, user = {},
|
|
39
|
+
} = req;
|
|
40
|
+
|
|
41
|
+
if (!user?.uid) {
|
|
42
|
+
return reply.status(401).send('access restricted: user not authorized');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const { type, objectid, id } = params;
|
|
46
|
+
|
|
47
|
+
if (!objectid) {
|
|
48
|
+
return reply.status(400).send('not enough params: id');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// force delete db entry if file not exists
|
|
52
|
+
const { exists, uid } = ['file', 'gallery'].includes(type) ? await checkAccess(pg, objectid, id) : {};
|
|
53
|
+
|
|
54
|
+
if (exists && !isAdmin(req) && uid && user?.uid !== uid) {
|
|
55
|
+
return reply.status(403).send('access restricted: file exists, not an author');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const sqls = {
|
|
59
|
+
comment: `delete from crm.communications where entity_id=$1 and ${isAdmin(req) ? '$2=$2' : 'uid=$2'} and communication_id=$3`,
|
|
60
|
+
checklist: `delete from crm.checklists where entity_id=$1 and ${isAdmin(req) ? '$2=$2' : 'uid=$2'} and checklist_id=$3`,
|
|
61
|
+
file: `update crm.files set file_status=3 where entity_id=$1 and ${!exists || isAdmin(req) ? '$2=$2' : 'uid=$2'} and file_id=$3 returning uploaded_name`,
|
|
62
|
+
gallery: `update crm.files set file_status=3 where entity_id=$1 and ${!exists || isAdmin(req) ? '$2=$2' : 'uid=$2'} and file_id=$3 returning uploaded_name`,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const sql = sqls[type];
|
|
66
|
+
const table = {
|
|
67
|
+
comment: 'crm.communications',
|
|
68
|
+
checklist: 'crm.checklists',
|
|
69
|
+
file: 'crm.files',
|
|
70
|
+
gallery: 'crm.files',
|
|
71
|
+
}[type];
|
|
72
|
+
|
|
73
|
+
if (!sql) {
|
|
74
|
+
return reply.status(400).send('invalid widget type');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const { rows = [] } = await pg.query(sql, [objectid, user.uid, id]);
|
|
78
|
+
|
|
79
|
+
await logChanges({
|
|
80
|
+
pg,
|
|
81
|
+
table,
|
|
82
|
+
id,
|
|
83
|
+
data: rows[0],
|
|
84
|
+
uid: user?.uid,
|
|
85
|
+
type: 'DELETE',
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
return reply.status(200).send({ data: { id }, user: { uid: user.uid, name: user.user_name } });
|
|
89
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import getMeta from '../../../plugins/pg/funcs/getMeta.js';
|
|
2
|
+
import getToken from '../../../plugins/crud/funcs/getToken.js';
|
|
3
|
+
import pgClients from '../../../plugins/pg/pgClients.js';
|
|
4
|
+
|
|
5
|
+
const galleryExtList = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'mp4', 'mov', 'avi'];
|
|
6
|
+
const username = 'coalesce(u.sur_name,\'\')||coalesce(\' \'||u.user_name,\'\') ||coalesce(\' \'||u.father_name,\'\')';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Дістає CRM для widget
|
|
10
|
+
*
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export default async function widgetGet({
|
|
14
|
+
pg = pgClients.client, user = {}, params = {}, query = {}, unittest,
|
|
15
|
+
}, reply) {
|
|
16
|
+
const param = user?.uid ? await getToken({
|
|
17
|
+
token: params.objectid, mode: 'w', uid: user.uid,
|
|
18
|
+
}) : null;
|
|
19
|
+
|
|
20
|
+
const objectid = param ? JSON.parse(param)?.id : params.objectid;
|
|
21
|
+
|
|
22
|
+
if (!objectid) {
|
|
23
|
+
return reply.status(400).send('not enough params: id');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const sqls = {
|
|
27
|
+
comment: pg.pk['admin.users']
|
|
28
|
+
? `select communication_id, entity_id, body, subject, c.cdate, c.uid,
|
|
29
|
+
${username} as username, u.login, u.avatar
|
|
30
|
+
from crm.communications c
|
|
31
|
+
left join admin.users u on u.uid=c.uid
|
|
32
|
+
where entity_id=$1 order by cdate desc`
|
|
33
|
+
: 'select communication_id, entity_id, body, subject, cdate, uid from crm.communications where entity_id=$1 order by cdate desc',
|
|
34
|
+
|
|
35
|
+
history: `select * from (
|
|
36
|
+
SELECT change_id, entity_id, entity_type, change_type, change_date, a.change_user_id, a.uid, a.cdate, b.json_agg as changes,
|
|
37
|
+
${username} as username, u.login, u.avatar
|
|
38
|
+
FROM log.table_changes a
|
|
39
|
+
left join admin.users u on a.change_user_id = u.uid
|
|
40
|
+
left join lateral(
|
|
41
|
+
select json_agg(row_to_json(q)) from (
|
|
42
|
+
select change_data_id, entity_key, value_new, value_old from log.table_changes_data
|
|
43
|
+
where change_id=a.change_id
|
|
44
|
+
)q
|
|
45
|
+
)b on 1=1
|
|
46
|
+
where b.json_agg is not null and (entity_id=$1 or entity_id in (
|
|
47
|
+
select communication_id as comments from crm.communications where entity_id=$1
|
|
48
|
+
union all select checklist_id from crm.checklists where entity_id=$1)
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
union all
|
|
52
|
+
select change_id, entity_id, entity_type, change_type, change_date, a.change_user_id, a.uid, a.cdate, b.json_agg as changes,
|
|
53
|
+
${username} as username, u.login, u.avatar
|
|
54
|
+
FROM log.table_changes a
|
|
55
|
+
left join admin.users u on a.change_user_id = u.uid
|
|
56
|
+
left join lateral(
|
|
57
|
+
select json_agg(o) from (
|
|
58
|
+
select json_object_agg(entity_key, coalesce(value_new, value_old)) as o from log.table_changes_data
|
|
59
|
+
where change_id=a.change_id and entity_key not in ('uid', 'file_status', 'editor_id', 'cdate', 'editor_date', 'entity_id')
|
|
60
|
+
)q
|
|
61
|
+
)b on 1=1
|
|
62
|
+
where a.change_type in ('INSERT', 'DELETE') and a.entity_id in (select file_id from crm.files where entity_id=$1)
|
|
63
|
+
limit 100
|
|
64
|
+
|
|
65
|
+
)q order by cdate desc limit 100`,
|
|
66
|
+
|
|
67
|
+
checklist: pg.pk['admin.users']
|
|
68
|
+
? `SELECT checklist_id, entity_id, subject, is_done, done_date, c.uid, c.cdate, ${username} as username, u.login, u.avatar
|
|
69
|
+
FROM crm.checklists c
|
|
70
|
+
left join admin.users u on u.uid=c.uid
|
|
71
|
+
where entity_id=$1 order by cdate desc`
|
|
72
|
+
: 'SELECT checklist_id, entity_id, subject, is_done, done_date, uid, cdate FROM crm.checklists where entity_id=$1 order by cdate desc',
|
|
73
|
+
|
|
74
|
+
file: pg.pk['admin.users']
|
|
75
|
+
? `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, c.uid, c.cdate, file_type, c.ismain,
|
|
76
|
+
${username} as username, u.login, isverified, u.avatar, u.uid as author, file_status
|
|
77
|
+
FROM crm.files c
|
|
78
|
+
left join admin.users u on u.uid=c.uid
|
|
79
|
+
where entity_id=$1 and file_status<>3 order by cdate desc`
|
|
80
|
+
: `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, uid, cdate, file_type, ismain,
|
|
81
|
+
isverified, uid as author, file_status FROM crm.files c where entity_id=$1 and file_status<>3 order by cdate desc`,
|
|
82
|
+
gallery: pg.pk['admin.users']
|
|
83
|
+
? `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, c.uid, c.cdate, file_type, c.ismain,
|
|
84
|
+
${username} as username, u.login, u.avatar, isverified, u.avatar, c.uid as author, file_status
|
|
85
|
+
FROM crm.files c
|
|
86
|
+
left join admin.users u on u.uid=c.uid
|
|
87
|
+
where entity_id=$1 and file_status<>3 and ext = any($2) order by cdate desc`
|
|
88
|
+
: `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, c.uid, c.cdate, file_type, ismain,
|
|
89
|
+
isverified, uid as author, file_status FROM crm.files c where entity_id=$1 and file_status<>3 and ext = any($2) order by cdate desc`,
|
|
90
|
+
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const q = sqls[params.type];
|
|
94
|
+
|
|
95
|
+
if (!q) {
|
|
96
|
+
return reply.status(400).send('invalid widget type');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/* data */
|
|
100
|
+
const time = [Date.now()];
|
|
101
|
+
const { rows = [] } = await pg.query(q, [objectid, params.type === 'gallery' ? galleryExtList : null].filter((el) => el));
|
|
102
|
+
rows.forEach(row => Object.assign(row, { username: row.username?.trim?.() || row.login }));
|
|
103
|
+
time.push(Date.now());
|
|
104
|
+
|
|
105
|
+
/* Object info */
|
|
106
|
+
const { tableName } = pg.pk['log.table_changes'] ? await pg.query(
|
|
107
|
+
'select entity_type as "tableName" from log.table_changes where entity_id=$1 limit 1',
|
|
108
|
+
[objectid],
|
|
109
|
+
).then(el => el.rows?.[0] || {}) : {};
|
|
110
|
+
|
|
111
|
+
const { pk, columns = [] } = await getMeta({ pg, table: tableName });
|
|
112
|
+
|
|
113
|
+
const authorIdColumn = columns.find(col => ['uid', 'created_by'].includes(col.name))?.name;
|
|
114
|
+
|
|
115
|
+
if (!pk && params.type === 'history' && !unittest) {
|
|
116
|
+
return reply.status(404).send('log table not found');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const q1 = `select ${username} as author, u.login, a.cdate, a.editor_date from ${tableName} a
|
|
120
|
+
left join admin.users u on a.${authorIdColumn}=u.uid where a.${pk}=$1 limit 1`;
|
|
121
|
+
|
|
122
|
+
const data = pg.pk['admin.users'] && pk && tableName ? await pg.query(q, [objectid, params.type === 'gallery' ? galleryExtList : null].filter((el) => el)).then(el => el.rows?.[0] || {}) : {};
|
|
123
|
+
|
|
124
|
+
if (query.debug && user?.user_type === 'admin') {
|
|
125
|
+
return {
|
|
126
|
+
q, type: params.type, q1, id: objectid, data,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return reply.status(200).send({
|
|
131
|
+
time: { data: time[1] - time[0] },
|
|
132
|
+
rows,
|
|
133
|
+
user: { uid: user?.uid, name: user?.user_name },
|
|
134
|
+
data: { author: data?.author, cdate: data?.cdate, edate: data?.editor_date },
|
|
135
|
+
objectid: params.objectid,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import getMeta from '../../../plugins/pg/funcs/getMeta.js';
|
|
4
|
+
import dataInsert from '../../../plugins/crud/funcs/dataInsert.js';
|
|
5
|
+
import dataUpdate from '../../../plugins/crud/funcs/dataUpdate.js';
|
|
6
|
+
import applyHook from '../../../plugins/hook/funcs/applyHook.js';
|
|
7
|
+
import uploadMultiPart from '../../../plugins/file/uploadMultiPart.js';
|
|
8
|
+
|
|
9
|
+
const tableList = {
|
|
10
|
+
comment: 'crm.communications',
|
|
11
|
+
gallery: 'crm.files',
|
|
12
|
+
file: 'crm.files',
|
|
13
|
+
checklist: 'crm.checklists',
|
|
14
|
+
};
|
|
15
|
+
const pkList = {
|
|
16
|
+
comment: 'communication_id',
|
|
17
|
+
checklist: 'checklist_id',
|
|
18
|
+
file: 'file_id',
|
|
19
|
+
gallery: 'file_id',
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const galleryExtList = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'mp4', 'mov', 'avi'];
|
|
23
|
+
|
|
24
|
+
export default async function widgetSet(req, reply) {
|
|
25
|
+
const {
|
|
26
|
+
pg, params = {}, session = {}, headers = {}, body = {}, user = {},
|
|
27
|
+
} = req;
|
|
28
|
+
const { type, id, objectid } = params;
|
|
29
|
+
|
|
30
|
+
if (!['comment', 'checklist', 'file', 'gallery'].includes(type)) {
|
|
31
|
+
return reply.status(400).send('param type not valid');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!objectid) {
|
|
35
|
+
return reply.status(400).send('not enough params: id');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const table = tableList[type];
|
|
39
|
+
|
|
40
|
+
// dsadasdad
|
|
41
|
+
if (['gallery', 'file'].includes(type) && headers['content-type']?.split?.(';')?.shift?.() === 'multipart/form-data') {
|
|
42
|
+
const file = await uploadMultiPart(req);
|
|
43
|
+
const extName = path.extname(file.filepath).slice(1).toLowerCase();
|
|
44
|
+
|
|
45
|
+
const data = {
|
|
46
|
+
uploaded_name: file?.originalFilename?.toLocaleLowerCase()?.replace(/'/g, '\'\''),
|
|
47
|
+
file_path: file?.relativeFilepath?.replace(/\\/g, '/'),
|
|
48
|
+
ext: extName,
|
|
49
|
+
size: file?.size,
|
|
50
|
+
file_status: 1,
|
|
51
|
+
uid: user?.uid || 1,
|
|
52
|
+
entity_id: objectid,
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
if (type === 'gallery' && !galleryExtList.includes(extName.toLowerCase())) {
|
|
56
|
+
return reply.status(400).send('invalid file extension');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const { rows = [] } = await dataInsert({
|
|
60
|
+
pg, table: 'crm.files', data, uid: user?.uid,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
if (type === 'gallery') {
|
|
64
|
+
await pg.query(`update crm.files set ismain=true
|
|
65
|
+
where entity_id=$1
|
|
66
|
+
and file_id=$2
|
|
67
|
+
and (select count(*) = 0 from crm.files where entity_id=$1 and ismain)`, [objectid, rows[0]?.file_id]);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
rowCount: 1, data: 'ok', command: 'UPLOAD', id: rows[0]?.file_id, entity_id: rows[0]?.entity_id,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const { pk } = await getMeta({ pg, table });
|
|
75
|
+
|
|
76
|
+
if (!pk) {
|
|
77
|
+
return reply.status(404).send('table not found');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const data = { ...body, uid: user?.uid, entity_id: objectid };
|
|
81
|
+
|
|
82
|
+
await applyHook('onWidgetSet', {
|
|
83
|
+
pg,
|
|
84
|
+
link: req.path,
|
|
85
|
+
id,
|
|
86
|
+
objectid,
|
|
87
|
+
session,
|
|
88
|
+
type,
|
|
89
|
+
payload: data,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const result = id
|
|
93
|
+
? await dataUpdate({
|
|
94
|
+
pg, table, data, id, uid: user?.uid,
|
|
95
|
+
})
|
|
96
|
+
: await dataInsert({
|
|
97
|
+
pg, table, data, uid: user?.uid,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
return reply.status(200).send({
|
|
101
|
+
rowCount: result.rowCount,
|
|
102
|
+
data: 'ok',
|
|
103
|
+
command: result.command,
|
|
104
|
+
id: result.rows?.[0]?.[pkList[type]] || result?.[pkList[type]],
|
|
105
|
+
});
|
|
106
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import pgClients from '../../../plugins/pg/pgClients.js';
|
|
2
|
+
|
|
3
|
+
export default async function onWidgetSet({
|
|
4
|
+
pg = pgClients.client, id, objectid, type, payload = {},
|
|
5
|
+
}) {
|
|
6
|
+
if (!id || !objectid || type !== 'gallery') {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
if (payload?.ismain) {
|
|
10
|
+
await pg.query('update crm.files set ismain=false where entity_id=$1 and file_id<>$2', [objectid, id]);
|
|
11
|
+
}
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import addHook from '../../plugins/hook/funcs/addHook.js';
|
|
2
|
+
|
|
3
|
+
import widgetDel from './controllers/widget.del.js';
|
|
4
|
+
import widgetSet from './controllers/widget.set.js';
|
|
5
|
+
import widgetGet from './controllers/widget.get.js';
|
|
6
|
+
import fileEdit from './controllers/file.edit.js';
|
|
7
|
+
|
|
8
|
+
import onWidgetSet from './hook/onWidgetSet.js';
|
|
9
|
+
|
|
10
|
+
const tableSchema = {
|
|
11
|
+
params: {
|
|
12
|
+
type: 'object',
|
|
13
|
+
properties: {
|
|
14
|
+
// type: { type: 'string', pattern: '^([\\d\\w]+)$' },
|
|
15
|
+
objectid: { type: 'string', pattern: '^([\\d\\w]+)$' },
|
|
16
|
+
id: { type: 'string', pattern: '^([\\d\\w]+)$' },
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
querystring: {
|
|
20
|
+
type: 'object',
|
|
21
|
+
properties: {
|
|
22
|
+
debug: { type: 'string', pattern: '^(\\d+)$' },
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
addHook('onWidgetSet', onWidgetSet);
|
|
28
|
+
|
|
29
|
+
const policy = ['site'];
|
|
30
|
+
const params = { config: { policy }, schema: tableSchema };
|
|
31
|
+
|
|
32
|
+
export default async function route(app, config = {}) {
|
|
33
|
+
const { prefix = '/api' } = config;
|
|
34
|
+
app.delete(`${prefix}/widget/:type/:objectid/:id`, params, widgetDel);
|
|
35
|
+
app.post(`${prefix}/widget/:type/:objectid/:id?`, params, widgetSet);
|
|
36
|
+
app.put(`${prefix}/file-edit/:id`, params, fileEdit);
|
|
37
|
+
app.get(`${prefix}/widget/:type/:objectid`, { config: { policy: ['public'] }, schema: tableSchema }, widgetGet);
|
|
38
|
+
}
|