-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathvalidate-create-widgets-ds.ts
More file actions
135 lines (126 loc) · 5.26 KB
/
Copy pathvalidate-create-widgets-ds.ts
File metadata and controls
135 lines (126 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import JSON5 from 'json5';
import { EncryptionAlgorithmEnum } from '../../../enums/encryption-algorithm.enum.js';
import { WidgetTypeEnum } from '../../../enums/widget-type.enum.js';
import { Messages } from '../../../exceptions/text/messages.js';
import { Constants } from '../../../helpers/constants/constants.js';
import { getPropertyValueByDescriptor } from '../../../helpers/get-property-value-by-descriptor.js';
import { ConnectionEntity } from '../../connection/connection.entity.js';
import { BucketProviderEnum } from '../../s3-widget/application/data-structures/bucket-provider.enum.js';
import { ForeignKeyDSInfo } from '../../table/table-datastructures.js';
import { findTableFieldsUtil } from '../../table/utils/find-table-fields.util.js';
import { findTablesInConnectionUtil } from '../../table/utils/find-tables-in-connection.util.js';
import { CreateTableWidgetDs } from '../application/data-sctructures/create-table-widgets.ds.js';
export async function validateCreateWidgetsDs(
widgetsDS: Array<CreateTableWidgetDs>,
userId: string,
connection: ConnectionEntity,
tableName: string,
userEmail: string,
): Promise<Array<string>> {
const errors = [];
const availableTablesInConnection = await findTablesInConnectionUtil(connection, userId, userEmail);
if (!availableTablesInConnection.includes(tableName)) {
errors.push(Messages.TABLE_NOT_FOUND);
return errors;
}
const availableTableFields = await findTableFieldsUtil(connection, tableName, userId, userEmail);
if (!widgetsDS || !Array.isArray(widgetsDS)) {
errors.push(Messages.WIDGETS_PROPERTY_MISSING);
return errors;
}
for (const widgetDS of widgetsDS) {
if (!widgetDS.field_name) {
errors.push(Messages.WIDGET_FIELD_NAME_MISSING);
} else {
const fieldIndex = availableTableFields.indexOf(widgetDS.field_name);
if (fieldIndex < 0) {
errors.push(Messages.EXCLUDED_OR_NOT_EXISTS(widgetDS.field_name));
}
}
const { widget_type } = widgetDS;
// if (widget_type) {
// if (!Object.keys(WidgetTypeEnum).find((key) => key === widget_type)) {
// errors.push(Messages.WIDGET_TYPE_INCORRECT);
// }
// }
if (widget_type && widget_type === WidgetTypeEnum.Password) {
let widget_params = widgetDS.widget_params as string | Record<string, any>;
if (typeof widget_params === 'string') {
widget_params = JSON5.parse<ForeignKeyDSInfo>(widget_params);
}
if (
widget_params.algorithm &&
!Object.keys(EncryptionAlgorithmEnum).find((key) => key === widget_params.algorithm)
) {
errors.push(Messages.ENCRYPTION_ALGORITHM_INCORRECT(widget_params.algorithm));
}
if (widget_params.encrypt === undefined) {
errors.push(Messages.WIDGET_REQUIRED_PARAMETER_MISSING('encrypt'));
}
}
if (widget_type && widget_type === WidgetTypeEnum.Foreign_key) {
const widget_params: ForeignKeyDSInfo = JSON5.parse(widgetDS.widget_params);
for (const key in widget_params) {
if (!Constants.FOREIGN_KEY_FIELDS.includes(key)) {
errors.push(Messages.WIDGET_PARAMETER_UNSUPPORTED(key, widgetDS.widget_type));
continue;
}
if (!getPropertyValueByDescriptor(widget_params, key) && key !== 'constraint_name') {
errors.push(Messages.WIDGET_REQUIRED_PARAMETER_MISSING(key));
}
}
if (errors.length > 0) {
return errors;
}
if (!availableTablesInConnection.includes(widget_params.referenced_table_name)) {
errors.push(Messages.TABLE_WITH_NAME_NOT_EXISTS(widget_params.referenced_table_name));
return errors;
}
const foreignTableFields = await findTableFieldsUtil(
connection,
widget_params.referenced_table_name,
userId,
userEmail,
);
if (!foreignTableFields.includes(widget_params.referenced_column_name)) {
errors.push(
Messages.NO_SUCH_FIELD_IN_TABLE(widget_params.referenced_column_name, widget_params.referenced_table_name),
);
}
}
if (widget_type && widget_type === WidgetTypeEnum.S3) {
const rawParams = widgetDS.widget_params;
const widget_params: Record<string, any> =
typeof rawParams === 'string' ? JSON5.parse(rawParams) : (rawParams as Record<string, any>);
if (!widget_params.bucket) {
errors.push(Messages.WIDGET_REQUIRED_PARAMETER_MISSING('bucket'));
}
if (!widget_params.access_key_id) {
errors.push(Messages.WIDGET_REQUIRED_PARAMETER_MISSING('access_key_id'));
}
if (!widget_params.access_key) {
errors.push(Messages.WIDGET_REQUIRED_PARAMETER_MISSING('access_key'));
}
if (
widget_params.provider &&
!Object.values(BucketProviderEnum).includes(widget_params.provider as BucketProviderEnum)
) {
errors.push(Messages.WIDGET_PARAMETER_UNSUPPORTED('provider', widget_type));
}
if (widget_params.provider === BucketProviderEnum.CloudflareR2 && !widget_params.account_id) {
errors.push(Messages.WIDGET_REQUIRED_PARAMETER_MISSING('account_id'));
}
}
if (widget_type && widget_type === WidgetTypeEnum.Binary) {
const rawParams = widgetDS.widget_params;
if (rawParams) {
const widget_params: Record<string, any> =
typeof rawParams === 'string' ? JSON5.parse(rawParams) : (rawParams as Record<string, any>);
if (widget_params.encoding !== undefined && !['hex', 'base64', 'ascii'].includes(widget_params.encoding)) {
errors.push(Messages.WIDGET_PARAMETER_UNSUPPORTED('encoding', WidgetTypeEnum.Binary));
}
}
}
}
return errors;
}