Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gp
diff

Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,42 @@ export class AzCognitiveSearch implements CognitiveSearchBase {

constructor(endpoint: string) {
let credentials: TokenCredential;
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
credentials = new DefaultAzureCredential();
} else if (process.env.MANAGED_IDENTITY_CLIENT_ID !== undefined) {
credentials = new DefaultAzureCredential({ ManangedIdentityClientId: process.env.MANAGED_IDENTITY_CLIENT_ID } as DefaultAzureCredentialOptions);
} else {
credentials = new DefaultAzureCredential();
try {
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
credentials = new DefaultAzureCredential();
} else if (process.env.MANAGED_IDENTITY_CLIENT_ID !== undefined) {
credentials = new DefaultAzureCredential({ managedIdentityClientId: process.env.MANAGED_IDENTITY_CLIENT_ID } as DefaultAzureCredentialOptions);
} else {
credentials = new DefaultAzureCredential();
}
this.client = new SearchIndexClient(endpoint, credentials);
} catch (error) {
const errorMessage =
'Failed to initialize DefaultAzureCredential. ' +
'Please ensure you have proper Azure credentials configured. ' +
'This could be due to missing environment variables, invalid credentials, or network issues.';
throw new Error(`${errorMessage}\nOriginal error: ${error instanceof Error ? error.message : String(error)}`);
}
this.client = new SearchIndexClient(endpoint, credentials);
}

private getSearchClient(indexName: string): SearchClient<unknown> {
let client = this.searchClients.get(indexName);
if (!client) {
client = this.client.getSearchClient(indexName);
this.searchClients.set(indexName, client);
async initializeSearchClients(): Promise<void> {
try {
const indexNames = this.client.listIndexesNames();
for await (const indexName of indexNames) {
this.searchClients.set(indexName, this.client.getSearchClient(indexName));
}
} catch (error) {
throw new Error(
`Failed to initialize search clients while listing indexes. This could be due to network issues or insufficient permissions.\nOriginal error: ${
error instanceof Error ? error.message : String(error)
}`
);
}
return client;
}


// check if index exists
async indexExists(indexName: string): Promise<boolean> {
indexExists(indexName: string): boolean {
return this.searchClients.has(indexName);
}

Expand All @@ -48,7 +63,7 @@ export class AzCognitiveSearch implements CognitiveSearchBase {
this.searchClients.set(indexDefinition.name, this.client.getSearchClient(indexDefinition.name));
console.log(`Index ${indexDefinition.name} created`);
} catch (error) {
throw new Error(`Failed to create index ${indexDefinition.name}: ${error.message}`);
throw new Error(`Failed to create index ${indexDefinition.name}: ${error.message}\nCause: ${error}`);
}
}
}
Expand All @@ -64,13 +79,13 @@ export class AzCognitiveSearch implements CognitiveSearchBase {
console.log(`Index ${indexName} updated`);
}
} catch (error) {
throw new Error(`Failed to create or update index ${indexName}: ${error.message}`);
throw new Error(`Failed to create or update index ${indexName}: ${error.message}\nCause: ${error}`);
}
}

async search(indexName: string, searchText: string, options?: any): Promise<SearchDocumentsResult<Pick<unknown, never>>> {
const startTime = new Date();
const result = await this.getSearchClient(indexName).search(searchText, options);
const result = await this.searchClients.get(indexName).search(searchText, options);
console.log(`SearchLibrary took ${new Date().getTime() - startTime.getTime()}ms`);
return result;
}
Expand All @@ -79,7 +94,7 @@ export class AzCognitiveSearch implements CognitiveSearchBase {
try {
await this.searchClients.get(indexName).deleteDocuments([document]);
} catch (error) {
throw new Error(`Failed to delete document from index ${indexName}: ${error.message}`);
throw new Error(`Failed to delete document from index ${indexName}: ${error.message}\nCause: ${error}`);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export interface IMemoryCognitiveSearch {
deleteDocument(indexName: string, document: any): Promise<void>;
indexDocument(indexName: string, document: any): Promise<void>;
search(indexName: string, searchText: string, options?: any): Promise<any>;
indexExists(indexName: string): Promise<boolean>;
indexExists(indexName: string): boolean;
logSearchCollectionIndexMap(): void;

initializeSearchClients(): Promise<void>
}


Expand Down Expand Up @@ -47,9 +47,9 @@ export class MemoryCognitiveSearch implements IMemoryCognitiveSearch, CognitiveS
this.searchCollectionIndexDefinitionMap = new Map<string, SearchIndex>();
}
initializeSearchClients(): Promise<void> {
throw new Error("Method not implemented.");
throw new Error('Method not implemented.');
}

deleteIndex(indexName: string): Promise<void> {
throw new Error("Method not implemented.");
}
Expand Down Expand Up @@ -92,7 +92,7 @@ export class MemoryCognitiveSearch implements IMemoryCognitiveSearch, CognitiveS
}
}

async indexExists(indexName: string): Promise<boolean> {
indexExists(indexName: string): boolean {
return this.searchCollectionIndexMap.has(indexName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export interface CognitiveSearchBase {
deleteDocument(indexName: string, document: any): Promise<void>;
indexDocument(indexName: string, document: any): Promise<void>;
deleteIndex(indexName: string): Promise<void>;
indexExists(indexName: string): Promise<boolean>;
indexExists(indexName: string): boolean;
initializeSearchClients(): Promise<void>
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function initializeFunctionApp() {
await cosmosDbConnection.connect();

// Initialize the infrastructure services
InfrastructureServicesBuilder.initialize();
await InfrastructureServicesBuilder.initialize();
const infrastructureServices = InfrastructureServicesBuilder.getInstance();

// Initialize the domain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import { tryGetEnvVar } from '../../seedwork/utils/get-env-var';
import { PaymentInfrastructureService } from '../app/infrastructure-services/payment';
import { CybersourceImpl } from './payment/cybersource/impl';

export class InfrastructureServicesBuilder implements InfrastructureServices{
export class InfrastructureServicesBuilder implements InfrastructureServices {
private _vercel: VercelInfrastructureService;
private _contentModerator: ContentModeratorInfrastructureService;
private _cognitiveSearch: CognitiveSearchInfrastructureService;
private _blobStorage: BlobStorageInfrastructureService;
private _payment: PaymentInfrastructureService;
private _datastore: DatastoreInfrastructureService;
private _maps: MapsInfrastructureService;

private constructor() {
this._vercel = this.InitVercel();
this._contentModerator = this.InitContentModerator();
Expand All @@ -49,7 +49,7 @@ export class InfrastructureServicesBuilder implements InfrastructureServices{

public get payment(): PaymentInfrastructureService {
return this._payment;
}
}

public get datastore(): DatastoreInfrastructureService {
return this._datastore;
Expand All @@ -59,7 +59,6 @@ export class InfrastructureServicesBuilder implements InfrastructureServices{
return this._maps;
}


private InitContentModerator(): ContentModeratorInfrastructureService {
const subscriptionKey = tryGetEnvVar('CONTENT_MODERATOR_SUBSCRIPTION_KEY');
const endpoint = tryGetEnvVar('CONTENT_MODERATOR_ENDPOINT');
Expand Down Expand Up @@ -96,18 +95,17 @@ export class InfrastructureServicesBuilder implements InfrastructureServices{
}

private static _instance: InfrastructureServicesBuilder;
public static initialize(): void {
public static async initialize(): Promise<void> {
if (InfrastructureServicesBuilder._instance) {
throw new Error('InfrastructureServicesBuilder is already initialized');
}
InfrastructureServicesBuilder._instance = new InfrastructureServicesBuilder();


await InfrastructureServicesBuilder._instance._cognitiveSearch.initializeSearchClients();
}
public static getInstance(): InfrastructureServicesBuilder {
if (!InfrastructureServicesBuilder._instance) {
throw new Error('InfrastructureServicesBuilder is not initialized');
}
return InfrastructureServicesBuilder._instance;
}
}
}
Loading