Base solution for your next web application
Open Closed

Background Job permission #11816


User avatar
0
alliance225 created

Hello, I configured Hangfire and I have this job running: RecurringJob.AddOrUpdate<IMyDomainServices>( service => service.UpdateVentesAsync(), "*/2 * * * *"); // Cron expression for every 2 minutes

I have this in my module: public override void PreInitialize() { //Adding authorization providers Configuration.Authorization.Providers.Add<AppAuthorizationProvider>();

   //Adding custom AutoMapper configuration
   Configuration.Modules.AbpAutoMapper().Configurators.Add(CustomDtoMapper.CreateMappings);

   IocManager.Register&lt;IMyDomainServices, MyDomainServices&gt;();

}

Here is my interface: namespace DocuPro.DomainServices { public interface IMyDomainServices: IDomainService { Task UpdateVentesAsync(); } }

And this is my Service:

using Abp.Authorization; using Abp.Domain.Repositories; using Abp.Domain.Uow; using Abp.Runtime.Session; using DocuPro; using DocuPro.Authorization.Users; using DocuPro.CommonBases; using DocuPro.DomainServices; using DocuPro.SortiesLocalesBases; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System; using System.Linq; using System.Threading.Tasks;

public class MyDomainServices : DocuProAppServiceBase, IMyDomainServices { private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IServiceScopeFactory _serviceScopeFactory;

private readonly IRepository&lt;StatsVente&gt; _statsVenteRepository;
private readonly IRepository&lt;SortiesLocales&gt; _sortiesLocalesRepository;

public MyDomainServices(
    IUnitOfWorkManager unitOfWorkManager,
    IServiceScopeFactory serviceScopeFactory,
           IRepository&lt;StatsVente&gt; statsVenteRepository,
    IRepository&lt;SortiesLocales&gt; sortiesLocalesRepository)
{
    _unitOfWorkManager = unitOfWorkManager;
    _serviceScopeFactory = serviceScopeFactory;
          _statsVenteRepository = statsVenteRepository;
    _sortiesLocalesRepository = sortiesLocalesRepository;
}

[AbpAllowAnonymous]
public async Task UpdateVentesAsync()
{
    using (var scope = _serviceScopeFactory.CreateScope())
    {
        var userManager = scope.ServiceProvider.GetRequiredService&lt;UserManager&gt;();
        var abpSession = scope.ServiceProvider.GetRequiredService&lt;IAbpSession&gt;();
        var unitOfWorkManager = scope.ServiceProvider.GetRequiredService&lt;IUnitOfWorkManager&gt;();
        var sortiesLocalesesAppService = scope.ServiceProvider.GetRequiredService&lt;ISortiesLocalesesAppService&gt;();
        var periodesAppService = scope.ServiceProvider.GetRequiredService&lt;IPeriodesAppService&gt;();

        var adminUser = await userManager.FindByNameAsync("admin");
        if (adminUser == null)
        {
            throw new InvalidOperationException("Admin user not found.");
        }

        using (var unitOfWork = unitOfWorkManager.Begin())
        {
            using (abpSession.Use(adminUser.TenantId, adminUser.Id))
            {
                var periodeId = await GetLatestPeriodeId();

                await sortiesLocalesesAppService.UpdateStatsVentesMois(periodeId);

           
            }
            await unitOfWork.CompleteAsync();
        }
    }


}
public async Task&lt;int&gt; GetLatestPeriodeId()
{
    var usedPeriodeIds = await _statsVenteRepository
        .GetAll()
        .Select(sv => sv.PeriodeId)
        .Distinct()
        .ToListAsync();

    var latestPeriodeId = await _sortiesLocalesRepository
        .GetAll()
        .Where(p => !usedPeriodeIds.Contains(p.PeriodeId)) // Exclude PeriodeIds that are in _statsVenteRepository
        .OrderByDescending(p => p.PeriodeId) // Order by PeriodeId descending
        .Select(p => p.PeriodeId)
        .FirstOrDefaultAsync(); // Get the first (latest) PeriodeId that is not used

    return latestPeriodeId; // This will be the latest PeriodeId not used in _statsVenteRepository
}

}

My problem is that any database call returns 0 rows even though there are data there.

As you can see I struggled with authorization, unitofwork, etc. to make it work.

What is the best way to do this?


1 Answer(s)