Skip to content

Commit

Permalink
Fix lints for integrations_builder.ts
Browse files Browse the repository at this point in the history
Signed-off-by: Simeon Widdis <sawiddis@amazon.com>
  • Loading branch information
Swiddis committed Jan 11, 2024
1 parent 8e109c7 commit 6546a73
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions server/adaptors/integrations/integrations_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/

import { v4 as uuidv4 } from 'uuid';
import { uuidRx } from 'public/components/custom_panels/redux/panel_slice';
import { SavedObjectsClientContract } from '../../../../../src/core/server';
import { IntegrationReader } from './repository/integration_reader';
import { SavedObjectsBulkCreateObject } from '../../../../../src/core/public';
Expand All @@ -14,6 +13,13 @@ interface BuilderOptions {
dataSource: string;
}

interface SavedObject {
id: string;
type: string;
attributes: { title: string };
references: Array<{ id: string }>;
}

export class IntegrationInstanceBuilder {
client: SavedObjectsClientContract;

Expand All @@ -36,14 +42,17 @@ export class IntegrationInstanceBuilder {
}
return assets.value;
})
.then((assets) => this.remapIDs(assets.savedObjects!))
.then((assets) => this.remapIDs(assets.savedObjects! as SavedObject[]))
.then((assets) => this.remapDataSource(assets, options.dataSource))
.then((assets) => this.postAssets(assets))
.then((refs) => this.buildInstance(integration, refs, options));
return instance;
}

remapDataSource(assets: any[], dataSource: string | undefined): any[] {
remapDataSource(
assets: SavedObject[],
dataSource: string | undefined
): Array<{ type: string; attributes: { title: string } }> {
if (!dataSource) return assets;
assets = assets.map((asset) => {
if (asset.type === 'index-pattern') {
Expand All @@ -54,7 +63,7 @@ export class IntegrationInstanceBuilder {
return assets;
}

remapIDs(assets: any[]): any[] {
remapIDs(assets: SavedObject[]): SavedObject[] {
const toRemap = assets.filter((asset) => asset.id);
const idMap = new Map<string, string>();
return toRemap.map((item) => {
Expand All @@ -73,20 +82,22 @@ export class IntegrationInstanceBuilder {
});
}

async postAssets(assets: any[]): Promise<AssetReference[]> {
async postAssets(assets: SavedObjectsBulkCreateObject[]): Promise<AssetReference[]> {
try {
const response = await this.client.bulkCreate(assets as SavedObjectsBulkCreateObject[]);
const refs: AssetReference[] = response.saved_objects.map((obj: any) => {
return {
assetType: obj.type,
assetId: obj.id,
status: 'available', // Assuming a successfully created object is available
isDefaultAsset: obj.type === 'dashboard', // Assuming for now that dashboards are default
description: obj.attributes?.title,
};
});
const response = await this.client.bulkCreate(assets);
const refs: AssetReference[] = (response.saved_objects as SavedObject[]).map(
(obj: SavedObject) => {
return {
assetType: obj.type,
assetId: obj.id,
status: 'available', // Assuming a successfully created object is available
isDefaultAsset: obj.type === 'dashboard', // Assuming for now that dashboards are default
description: obj.attributes?.title,
};
}
);
return Promise.resolve(refs);
} catch (err: any) {
} catch (err) {
return Promise.reject(err);
}
}
Expand Down

0 comments on commit 6546a73

Please sign in to comment.