using System; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using eprintServer.Data; using eprintServer.Entities; using eprintServer.DTOs; namespace eprintServer.Services; public class OAuthSettingsService { private readonly DataContext _context; public OAuthSettingsService(DataContext context) { _context = context; } public async Task GetOrCreateSettingsAsync() { var settings = await _context.OAuthSettings.FirstOrDefaultAsync(s => s.Id == 1); if (settings == null) { settings = new OAuthSettings { Id = 1, // Set the default template here TwilioShippmentTemplate = "Hi {{1}}! Great news: your order #{{2}} has been shipped and is on its way. You can track your package here: {{3}}. Use your tracking key {{4}} for details." }; _context.OAuthSettings.Add(settings); await _context.SaveChangesAsync(); } else if (string.IsNullOrEmpty(settings.TwilioShippmentTemplate)) { // If record exists but template is empty, update it settings.TwilioShippmentTemplate = "Hi {{1}}! Great news: your order #{{2}} has been shipped and is on its way. You can track your package here: {{3}}. Use your tracking key {{4}} for details."; await _context.SaveChangesAsync(); } return settings; } public async Task UpdateSettingsAsync(OAuthSettingsDto dto) { var settings = await GetOrCreateSettingsAsync(); bool changed = false; if (dto.GoogleClientId != null) { settings.GoogleClientId = dto.GoogleClientId; changed = true; } if (dto.GoogleClientSecret != null) { settings.GoogleClientSecret = dto.GoogleClientSecret; changed = true; } if (dto.FacebookAppId != null) { settings.FacebookAppId = dto.FacebookAppId; changed = true; } if (dto.FacebookAppSecret != null) { settings.FacebookAppSecret = dto.FacebookAppSecret; changed = true; } if (dto.InstagramAppId != null) { settings.InstagramAppId = dto.InstagramAppId; changed = true; } if (dto.InstagramAppSecret != null) { settings.InstagramAppSecret = dto.InstagramAppSecret; changed = true; } if (dto.LinkedInClientId != null) { settings.LinkedInClientId = dto.LinkedInClientId; changed = true; } if (dto.LinkedInClientSecret != null) { settings.LinkedInClientSecret = dto.LinkedInClientSecret; changed = true; } if (dto.AIApiKey != null) { settings.AIApiKey = dto.AIApiKey; changed = true; } if (dto.CanvaKey != null) { settings.CanvaKey = dto.CanvaKey; changed = true; } if (dto.ThawaniRoot != null) { settings.ThawaniRoot = dto.ThawaniRoot; changed = true; } if (dto.ThawaniCheckoutModelUrl != null) { settings.ThawaniCheckoutModelUrl = dto.ThawaniCheckoutModelUrl; changed = true; } if (dto.ThawaniApiKey != null) { settings.ThawaniApiKey = dto.ThawaniApiKey; changed = true; } if (dto.ThawaniPublicKey != null) { settings.ThawaniPublicKey = dto.ThawaniPublicKey; changed = true; } if (dto.AssiadExpressUserName != null) { settings.AssiadExpressUserName = dto.AssiadExpressUserName; changed = true; } if (dto.AssiadExpressPassword != null) { settings.AssiadExpressPassword = dto.AssiadExpressPassword; changed = true; } if (dto.TwilioAccountSid != null) { settings.TwilioAccountSid = dto.TwilioAccountSid; changed = true; } if (dto.TwilioAuthToken != null) { settings.TwilioAuthToken = dto.TwilioAuthToken; changed = true; } if (dto.TwilioPhoneNumber != null) { settings.TwilioPhoneNumber = dto.TwilioPhoneNumber; changed = true; } if (changed) { settings.UpdatedAt = DateTime.UtcNow; _context.OAuthSettings.Update(settings); await _context.SaveChangesAsync(); } return settings; } }