Retructured solution
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
namespace LiteCharms.Features.Extensions;
|
||||
|
||||
public static class Constants
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
using LiteCharms.Models;
|
||||
|
||||
namespace LiteCharms.Extensions;
|
||||
|
||||
public static class EntityModeMappers
|
||||
{
|
||||
public static ShoppingCartPackage ToModel(this Entities.ShoppingCartPackage entity) =>
|
||||
new()
|
||||
{
|
||||
Id = entity.Id,
|
||||
CreatedAt = entity.CreatedAt,
|
||||
PackageId = entity.PackageId,
|
||||
ShoppingCartId = entity.ShoppingCartId
|
||||
};
|
||||
|
||||
public static PackageItem ToModel(this Entities.PackageItem entity) =>
|
||||
new()
|
||||
{
|
||||
Id = entity.Id,
|
||||
Active = entity.Active,
|
||||
CreatedAt = entity.CreatedAt,
|
||||
PackageId = entity.PackageId,
|
||||
ProductPriceId = entity.ProductPriceId
|
||||
};
|
||||
|
||||
public static Package ToModel(this Entities.Package entity) =>
|
||||
new()
|
||||
{
|
||||
Id = entity.Id,
|
||||
CreatedAt = entity.CreatedAt,
|
||||
Active = entity.Active,
|
||||
Description = entity.Description,
|
||||
Name = entity.Name,
|
||||
UpdatedAt = entity.UpdatedAt
|
||||
};
|
||||
|
||||
public static ShoppingCartItem ToModel(this Entities.ShoppingCartItem entity) =>
|
||||
new()
|
||||
{
|
||||
Id = entity.Id,
|
||||
CreatedAt = entity.CreatedAt,
|
||||
UpdatedAt = entity.UpdatedAt,
|
||||
ProductPriceId = entity.ProductPriceId,
|
||||
Quantity = entity.Quantity,
|
||||
ShoppingCartId = entity.ShoppingCartId
|
||||
};
|
||||
|
||||
public static ShoppingCart ToModel(this Entities.ShoppingCart entity) =>
|
||||
new()
|
||||
{
|
||||
Id = entity.Id,
|
||||
CreatedAt = entity.CreatedAt,
|
||||
UpdatedAt = entity.UpdatedAt,
|
||||
CustomerId = entity.CustomerId,
|
||||
OrderId = entity.OrderId,
|
||||
QuoteId = entity.QuoteId
|
||||
};
|
||||
|
||||
public static Quote ToModel(this Entities.Quote entity) =>
|
||||
new()
|
||||
{
|
||||
Id = entity.Id,
|
||||
CreatedAt = entity.CreatedAt,
|
||||
UpdatedAt = entity.UpdatedAt,
|
||||
CustomerId = entity.CustomerId,
|
||||
ExpiredAt = entity.ExpiredAt,
|
||||
Reason = entity.Reason,
|
||||
ShoppingCartId = entity.ShoppingCartId,
|
||||
Status = entity.Status
|
||||
};
|
||||
|
||||
public static Notification ToModel(this Entities.Notification entity) =>
|
||||
new()
|
||||
{
|
||||
Id = entity.Id,
|
||||
CreatedAt = entity.CreatedAt,
|
||||
Message = entity.Message,
|
||||
Direction = entity.Direction,
|
||||
CorrelationId = entity.CorrelationId,
|
||||
CorrelationIdType = entity.CorrelationIdType,
|
||||
IsInternal = entity.IsInternal,
|
||||
Sender = entity.Sender,
|
||||
Platform = entity.Platform,
|
||||
Recipient = entity.Recipient,
|
||||
Subject = entity.Subject,
|
||||
Processed = entity.Processed,
|
||||
SenderName = entity.SenderName,
|
||||
RecipientAddress = entity.RecipientAddress,
|
||||
Priority = entity.Priority,
|
||||
UpdatedAt = entity?.UpdatedAt,
|
||||
IsHtml = entity!.IsHtml,
|
||||
HasError = entity.HasError,
|
||||
Errors = entity.Errors
|
||||
};
|
||||
|
||||
public static Customer ToModel(this Entities.Customer entity) =>
|
||||
new()
|
||||
{
|
||||
Id = entity.Id,
|
||||
CreatedAt = entity.CreatedAt,
|
||||
UpdatedAt = entity.UpdatedAt,
|
||||
Active = entity.Active,
|
||||
Address = entity.Address,
|
||||
City = entity.City,
|
||||
Company = entity.Company,
|
||||
Country = entity.Country,
|
||||
Discord = entity.Discord,
|
||||
Email = entity.Email,
|
||||
LastName = entity.LastName,
|
||||
LinkedIn = entity.LinkedIn,
|
||||
Name = entity.Name,
|
||||
Phone = entity.Phone,
|
||||
PostalCode = entity.PostalCode,
|
||||
Region = entity.Region,
|
||||
Slack = entity.Slack,
|
||||
Tax = entity.Tax,
|
||||
Website = entity.Website,
|
||||
Whatsapp = entity.Whatsapp
|
||||
};
|
||||
|
||||
public static Lead ToModel(this Entities.Lead entity) =>
|
||||
new()
|
||||
{
|
||||
Id = entity.Id,
|
||||
CreatedAt = entity.CreatedAt,
|
||||
UpdatedAt = entity.UpdatedAt,
|
||||
AdGroupId = entity.AdGroupId,
|
||||
AdName = entity.AdName,
|
||||
AppClickId = entity.AppClickId,
|
||||
AttributionHash = entity.AttributionHash,
|
||||
CampaignId = entity.CampaignId,
|
||||
ClickLocation = entity.ClickLocation,
|
||||
CustomerId = entity.CustomerId,
|
||||
FeedItemId = entity.FeedItemId,
|
||||
Source = entity.Source,
|
||||
ClickId = entity.ClickId,
|
||||
TargetId = entity.TargetId,
|
||||
WebClickId = entity.WebClickId,
|
||||
Status = entity.Status
|
||||
};
|
||||
|
||||
public static Order ToModel(this Entities.Order entity) =>
|
||||
new()
|
||||
{
|
||||
Id = entity.Id,
|
||||
CreatedAt = entity.CreatedAt,
|
||||
UpdatedAt = entity.UpdatedAt,
|
||||
CustomerId = entity.CustomerId,
|
||||
Notes = entity.Notes,
|
||||
RefundId = entity.RefundId,
|
||||
QuoteId = entity.QuoteId,
|
||||
Status = entity.Status,
|
||||
ShoppingCartId = entity.ShoppingCartId,
|
||||
DepositRequired = entity.DepositRequired,
|
||||
Requirements = entity.Requirements,
|
||||
Terms = entity.Terms
|
||||
};
|
||||
|
||||
public static OrderRefund ToModel(this Entities.OrderRefund entity) =>
|
||||
new()
|
||||
{
|
||||
Id = entity.Id,
|
||||
CreatedAt = entity.CreatedAt,
|
||||
OrderId = entity.OrderId,
|
||||
Reason = entity.Reason,
|
||||
Amount = entity.Amount
|
||||
};
|
||||
|
||||
public static Product ToModel(this Entities.Product entity) =>
|
||||
new()
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name,
|
||||
Description = entity.Description,
|
||||
Active = entity.Active
|
||||
};
|
||||
|
||||
public static ProductPrice ToModel(this Entities.ProductPrice entity) =>
|
||||
new()
|
||||
{
|
||||
Id = entity.Id,
|
||||
ProductId = entity.ProductId,
|
||||
Price = entity.Price,
|
||||
Active = entity.Active,
|
||||
CreatedAt = entity.CreatedAt,
|
||||
Discount = entity.Discount,
|
||||
UpdatedAt = entity.UpdatedAt
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace LiteCharms.Features.Extensions;
|
||||
|
||||
public static class Hash
|
||||
{
|
||||
public static Func<string?, string?> GenerateSha256HashString = (input) =>
|
||||
Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(input!)));
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using LiteCharms.Infrastructure.HealthChecks;
|
||||
|
||||
namespace LiteCharms.Extensions;
|
||||
|
||||
public static class HealthChecks
|
||||
{
|
||||
public static IServiceCollection AddQuartzHealtchCheck(this IServiceCollection services)
|
||||
{
|
||||
services.AddHealthChecks().AddCheck<QuartzHealthCheck>("Quartz");
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddPostgresHealtchCheck(this IServiceCollection services)
|
||||
{
|
||||
services.AddHealthChecks().AddCheck<PostgresHealthCheck>("PostgreSQL");
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddHealthChecksSupport(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddHealthChecks()
|
||||
.AddCheck("Self", () => HealthCheckResult.Healthy());
|
||||
|
||||
//services.AddHealthChecksUI(setup =>
|
||||
//{
|
||||
// setup.AddHealthCheckEndpoint("Lead Generator", $"{configuration["ASPNETCORE_URLS"]}/health");
|
||||
// setup.SetEvaluationTimeInSeconds(15);
|
||||
//}).AddInMemoryStorage(databaseName: "healthuidb");
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
namespace LiteCharms.Extensions;
|
||||
|
||||
public static class Monitoring
|
||||
{
|
||||
public static WebApplicationBuilder AddMonitoring(this WebApplicationBuilder builder)
|
||||
{
|
||||
var serviceName = builder.Configuration.GetValue<string>("Monitoring:ServiceName") ?? "LiteCharms";
|
||||
var endpoint = builder.Configuration.GetValue<string>("Monitoring:Address")!;
|
||||
var apiKey = builder.Configuration.GetValue<string>("Monitoring:ApiKey");
|
||||
|
||||
var resourceBuilder = ResourceBuilder.CreateDefault()
|
||||
.AddService(serviceName);
|
||||
|
||||
var otlpHeaders = !string.IsNullOrEmpty(apiKey) ? $"x-otlp-api-key={apiKey}" : null;
|
||||
|
||||
builder.Logging.AddOpenTelemetry(logging =>
|
||||
{
|
||||
logging.SetResourceBuilder(resourceBuilder);
|
||||
logging.AddOtlpExporter(opt =>
|
||||
{
|
||||
opt.Endpoint = new Uri(endpoint);
|
||||
opt.Protocol = OtlpExportProtocol.Grpc;
|
||||
opt.Headers = otlpHeaders;
|
||||
});
|
||||
});
|
||||
|
||||
builder.Services.AddOpenTelemetry()
|
||||
.WithTracing(tracing => tracing
|
||||
.SetResourceBuilder(resourceBuilder)
|
||||
.AddAspNetCoreInstrumentation()
|
||||
.AddHttpClientInstrumentation()
|
||||
.AddOtlpExporter(opt =>
|
||||
{
|
||||
opt.Endpoint = new Uri(endpoint);
|
||||
opt.Protocol = OtlpExportProtocol.Grpc;
|
||||
opt.Headers = otlpHeaders;
|
||||
}))
|
||||
.WithMetrics(metrics => metrics
|
||||
.SetResourceBuilder(resourceBuilder)
|
||||
.AddMeter(serviceName)
|
||||
.AddAspNetCoreInstrumentation()
|
||||
.AddRuntimeInstrumentation()
|
||||
.AddOtlpExporter(opt =>
|
||||
{
|
||||
opt.Endpoint = new Uri(endpoint);
|
||||
opt.Protocol = OtlpExportProtocol.Grpc;
|
||||
opt.Headers = otlpHeaders;
|
||||
}));
|
||||
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using LiteCharms.Infrastructure.Database;
|
||||
|
||||
namespace LiteCharms.Extensions;
|
||||
|
||||
public static class Postgres
|
||||
{
|
||||
public static IServiceCollection AddShopDatabase(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddPooledDbContextFactory<ShopDbContext>(options =>
|
||||
options.UseNpgsql(configuration.GetConnectionString("PostgresShop")));
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
using LiteCharms.Abstractions;
|
||||
using LiteCharms.Infrastructure.Quartz;
|
||||
|
||||
namespace LiteCharms.Extensions;
|
||||
|
||||
public static class Quartz
|
||||
{
|
||||
private const string databaseConfigName = "PostgresScheduler";
|
||||
|
||||
public static IServiceCollection AddQuartzSchedulerClient(this IServiceCollection services, string schedulerName, IConfiguration configuration)
|
||||
{
|
||||
var connectionString = configuration.GetConnectionString(databaseConfigName);
|
||||
|
||||
services.ConfigureCommon();
|
||||
|
||||
services.AddQuartz(config =>
|
||||
{
|
||||
config.SchedulerName = schedulerName;
|
||||
config.SchedulerId = "AUTO";
|
||||
|
||||
config.UseSimpleTypeLoader();
|
||||
config.UseDefaultThreadPool(options => options.MaxConcurrency = 0);
|
||||
config.UseTimeZoneConverter();
|
||||
|
||||
config.UsePersistentStore(storage =>
|
||||
{
|
||||
storage.PerformSchemaValidation = false;
|
||||
|
||||
storage.UseSystemTextJsonSerializer();
|
||||
storage.SetProperty("quartz.jobStore.clustered", "true");
|
||||
storage.SetProperty("quartz.jobStore.tablePrefix", "qrtz_");
|
||||
|
||||
storage.UsePostgres(connectionString!);
|
||||
storage.UseClustering(cluster =>
|
||||
{
|
||||
cluster.CheckinInterval = TimeSpan.FromSeconds(30);
|
||||
cluster.CheckinMisfireThreshold = TimeSpan.FromSeconds(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddQuartzScheduler(this IServiceCollection services, string schedulerName, IConfiguration configuration)
|
||||
{
|
||||
var connectionString = configuration.GetConnectionString(databaseConfigName);
|
||||
|
||||
services.ConfigureCommon();
|
||||
|
||||
services.AddQuartz(config =>
|
||||
{
|
||||
config.SchedulerName = schedulerName;
|
||||
config.SchedulerId = "AUTO";
|
||||
config.InterruptJobsOnShutdown = true;
|
||||
config.InterruptJobsOnShutdownWithWait = true;
|
||||
config.MaxBatchSize = 5;
|
||||
|
||||
config.UseSimpleTypeLoader();
|
||||
config.UseDefaultThreadPool(options => options.MaxConcurrency = 1);
|
||||
config.UseTimeZoneConverter();
|
||||
|
||||
config.UsePersistentStore(storage =>
|
||||
{
|
||||
storage.PerformSchemaValidation = false;
|
||||
|
||||
storage.UseSystemTextJsonSerializer();
|
||||
storage.SetProperty("quartz.jobStore.clustered", "true");
|
||||
storage.SetProperty("quartz.jobStore.tablePrefix", "qrtz_");
|
||||
|
||||
storage.UsePostgres(connectionString!);
|
||||
storage.UseClustering(cluster =>
|
||||
{
|
||||
cluster.CheckinInterval = TimeSpan.FromSeconds(30);
|
||||
cluster.CheckinMisfireThreshold = TimeSpan.FromSeconds(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private static IServiceCollection ConfigureCommon(this IServiceCollection services)
|
||||
{
|
||||
services.Configure<QuartzOptions>(options =>
|
||||
{
|
||||
options.Scheduling.IgnoreDuplicates = true;
|
||||
options.Scheduling.OverWriteExistingData = true;
|
||||
options["quartz.plugin.jobHistory.type"] = "Quartz.Plugin.History.LoggingJobHistoryPlugin, Quartz.Plugins";
|
||||
options["quartz.plugin.triggerHistory.type"] = "Quartz.Plugin.History.LoggingTriggerHistoryPlugin, Quartz.Plugins";
|
||||
});
|
||||
|
||||
services.AddTransient<RetryJobListener>();
|
||||
services.AddTransient<IJobOrchestrator, JobOrchestrator>();
|
||||
services.AddQuartzHostedService(options => options.WaitForJobsToComplete = true);
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using LiteCharms.Abstractions;
|
||||
using LiteCharms.Infrastructure.ServiceBus;
|
||||
using LiteCharms.Infrastructure.ServiceBus.Exchanges;
|
||||
using LiteCharms.Infrastructure.ServiceBus.Queues;
|
||||
|
||||
namespace LiteCharms.Extensions;
|
||||
|
||||
public static class ServiceBus
|
||||
{
|
||||
public static IServiceCollection AddGeneralServiceBus(this IServiceCollection services) => services
|
||||
.AddSingleton<GeneralQueue>()
|
||||
.AddHostedService<GeneralExchange>()
|
||||
.AddKeyedSingleton<IEventBus, GeneralServiceBus>(Constants.GeneralServiceBus)
|
||||
.AddMemoryCache();
|
||||
|
||||
public static IServiceCollection AddEmailServiceBus(this IServiceCollection services) => services
|
||||
.AddSingleton<EmailQueue>()
|
||||
.AddHostedService<EmailExchange>()
|
||||
.AddKeyedTransient<IEventBus, EmailServiceBus>(Constants.EmailServiceBus)
|
||||
.AddMemoryCache();
|
||||
|
||||
public static IServiceCollection AddSalesServiceBus(this IServiceCollection services) => services
|
||||
.AddSingleton<SalesQueue>()
|
||||
.AddHostedService<SalesExchange>()
|
||||
.AddKeyedSingleton<IEventBus, SalesServiceBus>(Constants.SalesServiceBus)
|
||||
.AddMemoryCache();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace LiteCharms.Features.Extensions;
|
||||
|
||||
public static class Timezones
|
||||
{
|
||||
public static TimeZoneInfo SouthAfricanTimeZone => TimeZoneInfo.FindSystemTimeZoneById("South Africa Standard Time");
|
||||
|
||||
public static string? LocaliseDateTime(this DateTime dateTime, TimeSpan offset) => offset.Hours > 0
|
||||
? $"{dateTime:yyyy-MM-ddTHH:mm:ss.fff}+{offset.Hours:00}:{offset.Minutes:00}"
|
||||
: $"{dateTime:yyyy-MM-ddTHH:mm:ss.fff}{offset.Hours:00}:{offset.Minutes:00}";
|
||||
|
||||
public static string? LocaliseDateTimeOffset(this DateTimeOffset dateTime, TimeSpan offset) => LocaliseDateTime(dateTime.DateTime, offset);
|
||||
|
||||
public static DateTimeOffset ToDateTimeWithTimeZone(this DateTime source, TimeZoneInfo? timezone = null)
|
||||
{
|
||||
DateTime sourceDateAdjusted = source.Kind != DateTimeKind.Utc
|
||||
? new(source.Ticks, DateTimeKind.Utc)
|
||||
: source;
|
||||
|
||||
var localised = timezone is null
|
||||
? new DateTimeOffset(sourceDateAdjusted.Ticks, SouthAfricanTimeZone.BaseUtcOffset).LocaliseDateTimeOffset(SouthAfricanTimeZone.BaseUtcOffset)
|
||||
: new DateTimeOffset(sourceDateAdjusted.Ticks, timezone!.BaseUtcOffset).LocaliseDateTimeOffset(timezone.BaseUtcOffset);
|
||||
|
||||
return DateTimeOffset.Parse(localised!);
|
||||
}
|
||||
|
||||
public static DateTimeOffset UtcNow(this TimeZoneInfo timezone) => ToDateTimeWithTimeZone(DateTime.Now, timezone);
|
||||
}
|
||||
Reference in New Issue
Block a user