using AutoMapper; using Hangfire; using Lada.Framework.Data.Domains.Identity; using Lada.Framework.Data.Mappings.DbContexts; using Ladaer.BackEnd.Infrastructures.AppSettings; using Ladaer.BackEnd.Infrastructures.DTO; using Ladaer.BackEnd.Infrastructures.Services; using Microsoft.AspNetCore.Identity; using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System.Data; namespace Ladaer.BackEnd.Infrastructures.Startup { /// /// L>p mY rTng ` `ng kA cAc d public static class GeneralRegister { private static readonly string policyName = "default"; /// /// ?ng kA cAc d /// ?`i tng IServiceCollection. /// ?`i tng IConfiguration. public static void RegisterGeneralServices(this IServiceCollection services, IConfiguration Configuration) { services.AddControllers() .AddNewtonsoftJson(x => { x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; x.SerializerSettings.ContractResolver = new DefaultContractResolver(); }); string connectionString = Configuration.GetConnectionString("LadaConnection"); services.AddDbContext(options => options.UseSqlServer(connectionString, b => b.MigrationsAssembly("Ladaer.BackEnd")), ServiceLifetime.Scoped); // Khai bAo Dapper services.AddTransient((sp) => new SqlConnection(connectionString)); services.AddMemoryCache(); services.AddCors(options => { options.AddPolicy(policyName, builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); }); services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("LadaConnection"))); services.AddHangfireServer(); } /// /// Cu hAnh vA chy cng dng. /// /// ?`i tng WebApplication. public static void ConfigAndRunApp(this WebApplication app) { // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler(appBuilder => { appBuilder.Run(async context => { context.Response.StatusCode = 500; context.Response.ContentType = "application/json"; var error = context.Features.Get(); if (error != null) { var response = new { error = "Internal Server Error", message = error.Error.Message }; await context.Response.WriteAsync(System.Text.Json.JsonSerializer.Serialize(response)); } }); }); app.UseHsts(); } app.UseSwagger(); app.UseSwaggerUI(); app.UseHangfireDashboard("/hangfire"); app.UseCors(policyName); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); AutoMapperConfig.Init(); // ?ng kA Hangfire recurring job cho VAT Invoice sync RecurringJob.AddOrUpdate( "vat-invoice-sync", job => job.Execute(), "0 3 * * *", // Cron: 3:00 AM hAng ngAy new RecurringJobOptions { TimeZone = TimeZoneInfo.FindSystemTimeZoneById("SE Asia Standard Time") }); // ?ng kA Hangfire recurring job cho Auto Penalty RecurringJob.AddOrUpdate( "auto-penalty-check", job => job.Execute(), "30 10 * * *", // Cron: 10:30 AM hAng ngAy (sau CutoffTime mc `