Skip to content

Commit c131f92

Browse files
committed
These changes are in response to latest PR comments and suggestions made by Simeon
Signed-off-by: Aaron Alvarez <[email protected]>
1 parent 0234433 commit c131f92

File tree

2 files changed

+90
-75
lines changed

2 files changed

+90
-75
lines changed

public/components/integrations/components/__tests__/__snapshots__/setup_integration.test.tsx.snap

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ exports[`Integration Setup Page Renders integration setup page as expected 1`] =
3838
"connectionLocation": "",
3939
"connectionTableName": "sample",
4040
"connectionType": "index",
41+
"databaseName": "default",
4142
"displayName": "sample Integration",
4243
"enabledWorkflows": Array [],
4344
}
@@ -114,6 +115,7 @@ exports[`Integration Setup Page Renders integration setup page as expected 1`] =
114115
"connectionLocation": "",
115116
"connectionTableName": "sample",
116117
"connectionType": "index",
118+
"databaseName": "default",
117119
"displayName": "sample Integration",
118120
"enabledWorkflows": Array [],
119121
}
@@ -262,6 +264,7 @@ exports[`Integration Setup Page Renders integration setup page as expected 1`] =
262264
"connectionLocation": "",
263265
"connectionTableName": "sample",
264266
"connectionType": "index",
267+
"databaseName": "default",
265268
"displayName": "sample Integration",
266269
"enabledWorkflows": Array [],
267270
}
@@ -943,6 +946,7 @@ exports[`Integration Setup Page Renders integration setup page as expected 1`] =
943946
"connectionLocation": "",
944947
"connectionTableName": "sample",
945948
"connectionType": "index",
949+
"databaseName": "default",
946950
"displayName": "sample Integration",
947951
"enabledWorkflows": Array [],
948952
}

public/components/integrations/components/setup_integration.tsx

+86-75
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,32 @@ export interface IntegrationConfigProps {
5656
handleSelectedDataSourceChange: (dataSourceMDSId?: string, dataSourceMDSLabel?: string) => void;
5757
}
5858

59+
/**
60+
* Interface for the parameters used in the addIntegration function
61+
*/
62+
interface AddIntegrationParams {
63+
/** Configuration settings for the integration setup */
64+
config: IntegrationSetupInputs;
65+
66+
/** Integration configuration details */
67+
integration: IntegrationConfig;
68+
69+
/** Callback function to set loading state */
70+
setLoading: (loading: boolean) => void;
71+
72+
/** Callback function to display toast notifications */
73+
setCalloutLikeToast: (title: string, color?: Color, text?: string) => void;
74+
75+
/** Optional MDS ID for the data source */
76+
dataSourceMDSId?: string;
77+
78+
/** Optional MDS label for the data source */
79+
dataSourceMDSLabel?: string;
80+
81+
/** Optional callback to set installation status */
82+
setIsInstalling?: (isInstalling: boolean, success?: boolean) => void;
83+
}
84+
5985
type SetupCallout = { show: true; title: string; color?: Color; text?: string } | { show: false };
6086

6187
const sqlService = new SQLService(coreRefs.http!);
@@ -149,15 +175,6 @@ const prepareQuery = (query: string, config: IntegrationSetupInputs): string =>
149175
/**
150176
* Handles the integration setup process based on the connection type.
151177
*
152-
* @param {Object} params - The parameters object
153-
* @param {IntegrationSetupInputs} params.config - Configuration settings for the integration setup
154-
* @param {IntegrationConfig} params.integration - Integration configuration details
155-
* @param {Function} params.setLoading - Callback function to set loading state
156-
* @param {Function} params.setCalloutLikeToast - Callback function to display toast notifications
157-
* @param {string} [params.dataSourceMDSId] - Optional MDS ID for the data source
158-
* @param {string} [params.dataSourceMDSLabel] - Optional MDS label for the data source
159-
* @param {Function} [params.setIsInstalling] - Optional callback to set installation status
160-
*
161178
* @throws {Error} Throws an error if the connection type is invalid
162179
* @returns {Promise<void>} A promise that resolves when the integration is added
163180
*/
@@ -169,15 +186,7 @@ const addIntegration = async ({
169186
dataSourceMDSId,
170187
dataSourceMDSLabel,
171188
setIsInstalling,
172-
}: {
173-
config: IntegrationSetupInputs;
174-
integration: IntegrationConfig;
175-
setLoading: (loading: boolean) => void;
176-
setCalloutLikeToast: (title: string, color?: Color, text?: string) => void;
177-
dataSourceMDSId?: string;
178-
dataSourceMDSLabel?: string;
179-
setIsInstalling?: (isInstalling: boolean, success?: boolean) => void;
180-
}): Promise<void> => {
189+
}: AddIntegrationParams): Promise<void> => {
181190
setLoading(true);
182191

183192
if (config.connectionType === 'index') {
@@ -209,24 +218,8 @@ const addIntegration = async ({
209218
/**
210219
* Handles the installation of an integration index by processing the configuration and making the integration request.
211220
*
212-
* @param {Object} params - The parameters object
213-
* @param {IntegrationSetupInputs} params.config - Configuration inputs for the integration setup
214-
* @param {IntegrationConfig} params.integration - Integration configuration object
215-
* @param {Function} params.setLoading - Function to set the loading state
216-
* @param {Function} params.setCalloutLikeToast - Function to display toast notifications
217-
* @param {string} [params.dataSourceMDSId] - Optional MDS ID for the data source
218-
* @param {string} [params.dataSourceMDSLabel] - Optional MDS label for the data source
219-
* @param {Function} [params.setIsInstalling] - Optional function to set installation status
220-
*
221221
* @returns {Promise<void>} A promise that resolves when the installation is complete
222222
*
223-
* @example
224-
* await addNativeIntegration({
225-
* config: setupInputs,
226-
* integration: integrationConfig,
227-
* setLoading: (loading) => setLoadingState(loading),
228-
* setCalloutLikeToast: (title, color, text) => showToast(title, color, text)
229-
* });
230223
*/
231224
const addNativeIntegration = async ({
232225
config,
@@ -236,15 +229,7 @@ const addNativeIntegration = async ({
236229
dataSourceMDSId,
237230
dataSourceMDSLabel,
238231
setIsInstalling,
239-
}: {
240-
config: IntegrationSetupInputs;
241-
integration: IntegrationConfig;
242-
setLoading: (loading: boolean) => void;
243-
setCalloutLikeToast: (title: string, color?: Color, text?: string) => void;
244-
dataSourceMDSId?: string;
245-
dataSourceMDSLabel?: string;
246-
setIsInstalling?: (isInstalling: boolean, success?: boolean) => void;
247-
}): Promise<void> => {
232+
}: AddIntegrationParams): Promise<void> => {
248233
let enabledWorkflows: string[] | undefined;
249234
if (integration.workflows) {
250235
enabledWorkflows = integration.workflows
@@ -279,15 +264,6 @@ const addNativeIntegration = async ({
279264
* Handles the installation process for S3 integration by creating a database (if specified),
280265
* processing integration assets, and executing necessary queries.
281266
*
282-
* @param {Object} params - The parameters object
283-
* @param {IntegrationSetupInputs} params.config - Configuration settings for the integration setup
284-
* @param {IntegrationConfig} params.integration - Integration configuration details
285-
* @param {Function} params.setLoading - Callback function to set loading state
286-
* @param {Function} params.setCalloutLikeToast - Callback function to display toast notifications
287-
* @param {string} [params.dataSourceMDSId] - Optional MDS ID for the data source
288-
* @param {string} [params.dataSourceMDSLabel] - Optional MDS label for the data source
289-
* @param {Function} [params.setIsInstalling] - Optional callback to set installation status
290-
*
291267
* @returns {Promise<void>} A promise that resolves when the installation is complete
292268
*
293269
* @throws Will set error toast if database creation fails or integration addition fails
@@ -300,34 +276,22 @@ const addFlintIntegration = async ({
300276
dataSourceMDSId,
301277
dataSourceMDSLabel,
302278
setIsInstalling,
303-
}: {
304-
config: IntegrationSetupInputs;
305-
integration: IntegrationConfig;
306-
setLoading: (loading: boolean) => void;
307-
setCalloutLikeToast: (title: string, color?: Color, text?: string) => void;
308-
dataSourceMDSId?: string;
309-
dataSourceMDSLabel?: string;
310-
setIsInstalling?: (isInstalling: boolean, success?: boolean) => void;
311-
}): Promise<void> => {
279+
}: AddIntegrationParams): Promise<void> => {
312280
let sessionId: string | undefined;
313281

314282
// Create database if specified
315-
if (config.databaseName) {
316-
const createDbQuery = `CREATE DATABASE IF NOT EXISTS ${config.databaseName}`;
317-
const result = await runQuery(
318-
createDbQuery,
319-
config.connectionDataSource,
320-
sessionId,
321-
dataSourceMDSId
322-
);
283+
const dbResult = await createDatabase(
284+
config,
285+
sessionId,
286+
dataSourceMDSId,
287+
setLoading,
288+
setCalloutLikeToast
289+
);
323290

324-
if (!result.ok) {
325-
setLoading(false);
326-
setCalloutLikeToast('Failed to create database', 'danger', result.error.message);
327-
return;
328-
}
329-
sessionId = result.value.sessionId;
291+
if (!dbResult.success) {
292+
return;
330293
}
294+
sessionId = dbResult.sessionId;
331295

332296
// Process integration assets
333297
const http = coreRefs.http!;
@@ -386,6 +350,53 @@ const addFlintIntegration = async ({
386350
}
387351
};
388352

353+
/**
354+
* Creates a database if it doesn't already exist using the provided configuration.
355+
*
356+
* @param config - Configuration object containing database details
357+
* @param config.databaseName - Name of the database to create
358+
* @param config.connectionDataSource - Data source connection string
359+
* @param sessionId - Current session identifier
360+
* @param dataSourceMDSId - Data source MDS identifier
361+
* @param setLoading - Callback function to update loading state
362+
* @param setCalloutLikeToast - Callback function to display toast notifications
363+
* @param setCalloutLikeToast.message - Message to display in the toast
364+
* @param setCalloutLikeToast.type - Type of toast notification (e.g., 'danger')
365+
* @param setCalloutLikeToast.details - Optional details for the toast message
366+
*
367+
* @returns Promise resolving to an object containing:
368+
* - success: boolean indicating if the operation was successful
369+
* - sessionId: the current or updated session identifier
370+
*
371+
*/
372+
const createDatabase = async (
373+
config: { databaseName: string; connectionDataSource: string },
374+
sessionId: string,
375+
dataSourceMDSId: string,
376+
setLoading: (loading: boolean) => void,
377+
setCalloutLikeToast: (message: string, type: string, details?: string) => void
378+
): Promise<{ success: boolean; sessionId: string }> => {
379+
if (!config.databaseName) {
380+
return { success: true, sessionId };
381+
}
382+
383+
const createDbQuery = `CREATE DATABASE IF NOT EXISTS ${config.databaseName}`;
384+
const result = await runQuery(
385+
createDbQuery,
386+
config.connectionDataSource,
387+
sessionId,
388+
dataSourceMDSId
389+
);
390+
391+
if (!result.ok) {
392+
setLoading(false);
393+
setCalloutLikeToast('Failed to create database', 'danger', result.error.message);
394+
return { success: false, sessionId };
395+
}
396+
397+
return { success: true, sessionId: result.value.sessionId };
398+
};
399+
389400
const isConfigValid = (config: IntegrationSetupInputs, integration: IntegrationConfig): boolean => {
390401
if (config.displayName.length < 1 || config.connectionDataSource.length < 1) {
391402
return false;

0 commit comments

Comments
 (0)