Skip to content

Commit

Permalink
Code(API::Mongo): Seed default Integrations (OpenAI, Ollama) if missi…
Browse files Browse the repository at this point in the history
…ng in AppSettingsDocument. Improved performance checks to avoid overwriting user settings
  • Loading branch information
ktutak1337 committed Jul 24, 2024
1 parent f319b5c commit befeba3
Showing 1 changed file with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@ public async Task SeedAsync(IMongoDatabase database)
var cursor = await settingsCollection.FindAsync(FilterDefinition<AppSettingsDocument>.Empty);
var documents = await cursor.ToListAsync();

if (documents.Any())
if (documents.Count == 0)
{
await SeedNewSettingsAsync(settingsCollection);
return;
}

var appSettingsDocument = documents.First();

if (appSettingsDocument.Integrations is null || appSettingsDocument.Integrations.Count == 0)
{
await UpdateSettingsWithIntegrationsAsync(settingsCollection, appSettingsDocument);
}
}

private async Task SeedNewSettingsAsync(IMongoCollection<AppSettingsDocument> settingsCollection)
{
var now = _clock.GetLocalNow();

var appSettingsDocument = new AppSettingsDocument
Expand Down Expand Up @@ -53,4 +64,30 @@ public async Task SeedAsync(IMongoDatabase database)
_logger.LogInformation($"Added a settings document to the database with 'ID': {appSettingsDocument.Id}, and 'Key': {appSettingsDocument.Key}.");
_logger.LogInformation("Finished seeding 'settings' collection.");
}

private async Task UpdateSettingsWithIntegrationsAsync(IMongoCollection<AppSettingsDocument> settingsCollection, AppSettingsDocument appSettingsDocument)
{
appSettingsDocument.Integrations = new List<IntegrationDocument>
{
new()
{
Name = "OpenAI",
ApiKey = string.Empty,
Endpoint = string.Empty,
IsEnabled = true,
},
new()
{
Name = "Ollama",
ApiKey = string.Empty,
Endpoint = string.Empty,
IsEnabled = true,
},
};
appSettingsDocument.UpdatedAt = _clock.GetLocalNow();
_logger.LogInformation("Started updating 'settings' collection with integrations.");
await settingsCollection.ReplaceOneAsync(doc => doc.Id == appSettingsDocument.Id, appSettingsDocument);
_logger.LogInformation($"Updated the settings document in the database with 'ID': {appSettingsDocument.Id}.");
_logger.LogInformation("Finished updating 'settings' collection.");
}
}

0 comments on commit befeba3

Please sign in to comment.