|
1 | | -import { EncryptionAlgorithmEnum, WidgetTypeEnum } from '../../../enums/index.js'; |
2 | | -import { Messages } from '../../../exceptions/text/messages.js'; |
3 | | -import { getPropertyValueByDescriptor } from '../../../helpers/index.js'; |
4 | | -import { Constants } from '../../../helpers/constants/constants.js'; |
5 | | -import { ConnectionEntity } from '../../connection/connection.entity.js'; |
6 | | -import { ForeignKeyDSInfo } from '../../table/table-datastructures.js'; |
7 | | -import { findTableFieldsUtil } from '../../table/utils/find-table-fields.util.js'; |
8 | | -import { findTablesInConnectionUtil } from '../../table/utils/find-tables-in-connection.util.js'; |
9 | | -import { CreateTableWidgetDs } from '../application/data-sctructures/create-table-widgets.ds.js'; |
10 | | -import JSON5 from 'json5'; |
| 1 | +import JSON5 from "json5"; |
| 2 | +import { |
| 3 | + EncryptionAlgorithmEnum, |
| 4 | + WidgetTypeEnum, |
| 5 | +} from "../../../enums/index.js"; |
| 6 | +import { Messages } from "../../../exceptions/text/messages.js"; |
| 7 | +import { Constants } from "../../../helpers/constants/constants.js"; |
| 8 | +import { getPropertyValueByDescriptor } from "../../../helpers/index.js"; |
| 9 | +import { ConnectionEntity } from "../../connection/connection.entity.js"; |
| 10 | +import { ForeignKeyDSInfo } from "../../table/table-datastructures.js"; |
| 11 | +import { findTableFieldsUtil } from "../../table/utils/find-table-fields.util.js"; |
| 12 | +import { findTablesInConnectionUtil } from "../../table/utils/find-tables-in-connection.util.js"; |
| 13 | +import { CreateTableWidgetDs } from "../application/data-sctructures/create-table-widgets.ds.js"; |
11 | 14 |
|
12 | 15 | export async function validateCreateWidgetsDs( |
13 | | - widgetsDS: Array<CreateTableWidgetDs>, |
14 | | - userId: string, |
15 | | - connection: ConnectionEntity, |
16 | | - tableName: string, |
17 | | - userEmail: string, |
| 16 | + widgetsDS: Array<CreateTableWidgetDs>, |
| 17 | + userId: string, |
| 18 | + connection: ConnectionEntity, |
| 19 | + tableName: string, |
| 20 | + userEmail: string, |
18 | 21 | ): Promise<Array<string>> { |
19 | | - const errors = []; |
20 | | - const availableTablesInConnection = await findTablesInConnectionUtil(connection, userId, userEmail); |
21 | | - if (!availableTablesInConnection.includes(tableName)) { |
22 | | - errors.push(Messages.TABLE_NOT_FOUND); |
23 | | - return errors; |
24 | | - } |
25 | | - const availableTableFields = await findTableFieldsUtil(connection, tableName, userId, userEmail); |
26 | | - if (!widgetsDS || !Array.isArray(widgetsDS)) { |
27 | | - errors.push(Messages.WIDGETS_PROPERTY_MISSING); |
28 | | - return errors; |
29 | | - } |
| 22 | + const errors = []; |
| 23 | + const availableTablesInConnection = await findTablesInConnectionUtil( |
| 24 | + connection, |
| 25 | + userId, |
| 26 | + userEmail, |
| 27 | + ); |
| 28 | + if (!availableTablesInConnection.includes(tableName)) { |
| 29 | + errors.push(Messages.TABLE_NOT_FOUND); |
| 30 | + return errors; |
| 31 | + } |
| 32 | + const availableTableFields = await findTableFieldsUtil( |
| 33 | + connection, |
| 34 | + tableName, |
| 35 | + userId, |
| 36 | + userEmail, |
| 37 | + ); |
| 38 | + if (!widgetsDS || !Array.isArray(widgetsDS)) { |
| 39 | + errors.push(Messages.WIDGETS_PROPERTY_MISSING); |
| 40 | + return errors; |
| 41 | + } |
30 | 42 |
|
31 | | - for (const widgetDS of widgetsDS) { |
32 | | - if (!widgetDS.field_name) { |
33 | | - errors.push(Messages.WIDGET_FIELD_NAME_MISSING); |
34 | | - } else { |
35 | | - const fieldIndex = availableTableFields.indexOf(widgetDS.field_name); |
36 | | - if (fieldIndex < 0) { |
37 | | - errors.push(Messages.EXCLUDED_OR_NOT_EXISTS(widgetDS.field_name)); |
38 | | - } |
39 | | - } |
40 | | - const { widget_type } = widgetDS; |
| 43 | + for (const widgetDS of widgetsDS) { |
| 44 | + if (!widgetDS.field_name) { |
| 45 | + errors.push(Messages.WIDGET_FIELD_NAME_MISSING); |
| 46 | + } else { |
| 47 | + const fieldIndex = availableTableFields.indexOf(widgetDS.field_name); |
| 48 | + if (fieldIndex < 0) { |
| 49 | + errors.push(Messages.EXCLUDED_OR_NOT_EXISTS(widgetDS.field_name)); |
| 50 | + } |
| 51 | + } |
| 52 | + const { widget_type } = widgetDS; |
41 | 53 |
|
42 | | - // if (widget_type) { |
43 | | - // if (!Object.keys(WidgetTypeEnum).find((key) => key === widget_type)) { |
44 | | - // errors.push(Messages.WIDGET_TYPE_INCORRECT); |
45 | | - // } |
46 | | - // } |
47 | | - if (widget_type && widget_type === WidgetTypeEnum.Password) { |
48 | | - let widget_params = widgetDS.widget_params as string | Record<string, any>; |
49 | | - if (typeof widget_params === 'string') { |
50 | | - widget_params = JSON5.parse<ForeignKeyDSInfo>(widget_params); |
51 | | - } |
| 54 | + // if (widget_type) { |
| 55 | + // if (!Object.keys(WidgetTypeEnum).find((key) => key === widget_type)) { |
| 56 | + // errors.push(Messages.WIDGET_TYPE_INCORRECT); |
| 57 | + // } |
| 58 | + // } |
| 59 | + if (widget_type && widget_type === WidgetTypeEnum.Password) { |
| 60 | + let widget_params = widgetDS.widget_params as |
| 61 | + | string |
| 62 | + | Record<string, any>; |
| 63 | + if (typeof widget_params === "string") { |
| 64 | + widget_params = JSON5.parse<ForeignKeyDSInfo>(widget_params); |
| 65 | + } |
52 | 66 |
|
53 | | - if ( |
54 | | - widget_params.algorithm && |
55 | | - !Object.keys(EncryptionAlgorithmEnum).find((key) => key === widget_params.algorithm) |
56 | | - ) { |
57 | | - errors.push(Messages.ENCRYPTION_ALGORITHM_INCORRECT(widget_params.algorithm)); |
58 | | - } |
59 | | - if (widget_params.encrypt === undefined) { |
60 | | - errors.push(Messages.WIDGET_REQUIRED_PARAMETER_MISSING('encrypt')); |
61 | | - } |
62 | | - } |
| 67 | + if ( |
| 68 | + widget_params.algorithm && |
| 69 | + !Object.keys(EncryptionAlgorithmEnum).find( |
| 70 | + (key) => key === widget_params.algorithm, |
| 71 | + ) |
| 72 | + ) { |
| 73 | + errors.push( |
| 74 | + Messages.ENCRYPTION_ALGORITHM_INCORRECT(widget_params.algorithm), |
| 75 | + ); |
| 76 | + } |
| 77 | + if (widget_params.encrypt === undefined) { |
| 78 | + errors.push(Messages.WIDGET_REQUIRED_PARAMETER_MISSING("encrypt")); |
| 79 | + } |
| 80 | + } |
63 | 81 |
|
64 | | - if (widget_type && widget_type === WidgetTypeEnum.Foreign_key) { |
65 | | - const widget_params: ForeignKeyDSInfo = JSON5.parse(widgetDS.widget_params); |
| 82 | + if (widget_type && widget_type === WidgetTypeEnum.Foreign_key) { |
| 83 | + const widget_params: ForeignKeyDSInfo = JSON5.parse( |
| 84 | + widgetDS.widget_params, |
| 85 | + ); |
66 | 86 |
|
67 | | - for (const key in widget_params) { |
68 | | - if (!Constants.FOREIGN_KEY_FIELDS.includes(key)) { |
69 | | - errors.push(Messages.WIDGET_PARAMETER_UNSUPPORTED(key, widgetDS.widget_type)); |
70 | | - continue; |
71 | | - } |
72 | | - if (!getPropertyValueByDescriptor(widget_params, key) && key !== 'constraint_name') { |
73 | | - errors.push(Messages.WIDGET_REQUIRED_PARAMETER_MISSING(key)); |
74 | | - } |
75 | | - } |
76 | | - if (errors.length > 0) { |
77 | | - return errors; |
78 | | - } |
79 | | - if (!availableTablesInConnection.includes(widget_params.referenced_table_name)) { |
80 | | - errors.push(Messages.TABLE_WITH_NAME_NOT_EXISTS(widget_params.referenced_table_name)); |
81 | | - return errors; |
82 | | - } |
83 | | - const foreignTableFields = await findTableFieldsUtil( |
84 | | - connection, |
85 | | - widget_params.referenced_table_name, |
86 | | - userId, |
87 | | - userEmail, |
88 | | - ); |
89 | | - if (!foreignTableFields.includes(widget_params.referenced_column_name)) { |
90 | | - errors.push( |
91 | | - Messages.NO_SUCH_FIELD_IN_TABLE(widget_params.referenced_column_name, widget_params.referenced_table_name), |
92 | | - ); |
93 | | - } |
94 | | - } |
| 87 | + for (const key in widget_params) { |
| 88 | + if (!Constants.FOREIGN_KEY_FIELDS.includes(key)) { |
| 89 | + errors.push( |
| 90 | + Messages.WIDGET_PARAMETER_UNSUPPORTED(key, widgetDS.widget_type), |
| 91 | + ); |
| 92 | + continue; |
| 93 | + } |
| 94 | + if ( |
| 95 | + !getPropertyValueByDescriptor(widget_params, key) && |
| 96 | + key !== "constraint_name" |
| 97 | + ) { |
| 98 | + errors.push(Messages.WIDGET_REQUIRED_PARAMETER_MISSING(key)); |
| 99 | + } |
| 100 | + } |
| 101 | + if (errors.length > 0) { |
| 102 | + return errors; |
| 103 | + } |
| 104 | + if ( |
| 105 | + !availableTablesInConnection.includes( |
| 106 | + widget_params.referenced_table_name, |
| 107 | + ) |
| 108 | + ) { |
| 109 | + errors.push( |
| 110 | + Messages.TABLE_WITH_NAME_NOT_EXISTS( |
| 111 | + widget_params.referenced_table_name, |
| 112 | + ), |
| 113 | + ); |
| 114 | + return errors; |
| 115 | + } |
| 116 | + const foreignTableFields = await findTableFieldsUtil( |
| 117 | + connection, |
| 118 | + widget_params.referenced_table_name, |
| 119 | + userId, |
| 120 | + userEmail, |
| 121 | + ); |
| 122 | + if (!foreignTableFields.includes(widget_params.referenced_column_name)) { |
| 123 | + errors.push( |
| 124 | + Messages.NO_SUCH_FIELD_IN_TABLE( |
| 125 | + widget_params.referenced_column_name, |
| 126 | + widget_params.referenced_table_name, |
| 127 | + ), |
| 128 | + ); |
| 129 | + } |
| 130 | + } |
95 | 131 |
|
96 | | - if (widget_type && widget_type === WidgetTypeEnum.S3) { |
97 | | - let widget_params = widgetDS.widget_params as string | Record<string, any>; |
98 | | - if (typeof widget_params === 'string') { |
99 | | - widget_params = JSON5.parse(widget_params); |
100 | | - } |
| 132 | + if (widget_type && widget_type === WidgetTypeEnum.S3) { |
| 133 | + const rawParams = widgetDS.widget_params; |
| 134 | + const widget_params: Record<string, any> = |
| 135 | + typeof rawParams === "string" |
| 136 | + ? JSON5.parse(rawParams) |
| 137 | + : (rawParams as Record<string, any>); |
101 | 138 |
|
102 | | - if (!widget_params.bucket) { |
103 | | - errors.push(Messages.WIDGET_REQUIRED_PARAMETER_MISSING('bucket')); |
104 | | - } |
105 | | - if (!widget_params.aws_access_key_id_secret_name) { |
106 | | - errors.push(Messages.WIDGET_REQUIRED_PARAMETER_MISSING('aws_access_key_id_secret_name')); |
107 | | - } |
108 | | - if (!widget_params.aws_secret_access_key_secret_name) { |
109 | | - errors.push(Messages.WIDGET_REQUIRED_PARAMETER_MISSING('aws_secret_access_key_secret_name')); |
110 | | - } |
111 | | - } |
112 | | - } |
113 | | - return errors; |
| 139 | + if (!widget_params.bucket) { |
| 140 | + errors.push(Messages.WIDGET_REQUIRED_PARAMETER_MISSING("bucket")); |
| 141 | + } |
| 142 | + if (!widget_params.aws_access_key_id_secret_name) { |
| 143 | + errors.push( |
| 144 | + Messages.WIDGET_REQUIRED_PARAMETER_MISSING( |
| 145 | + "aws_access_key_id_secret_name", |
| 146 | + ), |
| 147 | + ); |
| 148 | + } |
| 149 | + if (!widget_params.aws_secret_access_key_secret_name) { |
| 150 | + errors.push( |
| 151 | + Messages.WIDGET_REQUIRED_PARAMETER_MISSING( |
| 152 | + "aws_secret_access_key_secret_name", |
| 153 | + ), |
| 154 | + ); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + return errors; |
114 | 159 | } |
0 commit comments