Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating new customer for each session. #1044

Open
ejazmusavi opened this issue Dec 3, 2022 · 2 comments
Open

Creating new customer for each session. #1044

ejazmusavi opened this issue Dec 3, 2022 · 2 comments

Comments

@ejazmusavi
Copy link

Hi
after deployment the application is creating a new customer record for each session and 10s of customer for each unique user.
is there any easy solution for this.

@fetullahyldz
Copy link

Hello, there is a solution for this, you need to write a service and delete visitors at certain intervals.

@fetullahyldz
Copy link

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using SimplCommerce.Infrastructure.Data;
using SimplCommerce.Module.Core.Models;
using SimplCommerce.Module.ProductComparison.Models;
using SimplCommerce.Module.ShoppingCart.Models;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NCrontab;

namespace SimplCommerce.Module.ShoppingCart.Services
{
public class DeleteguestsBackground : BackgroundService
{

    private readonly long SystemUserId = 2;
    private readonly IServiceProvider _serviceProvider;
    private readonly ILogger _logger;

    public DeleteguestsBackground(IServiceProvider serviceProvider, ILogger<DeleteguestsBackground> logger)
    {
        _serviceProvider = serviceProvider;
        _logger = logger;

    }


    protected async override Task ExecuteAsync(CancellationToken stoppingToken)
    {
        _logger.LogInformation("DeleteguestsBackground is starting.");
        while (!stoppingToken.IsCancellationRequested)
        {
            _logger.LogInformation("DeleteguestsBackground is working.");
            await Task.Delay(TimeSpan.FromSeconds(1800), stoppingToken);

            using (var scope = _serviceProvider.CreateScope())
            {
                var userRepository = scope.ServiceProvider.GetRequiredService<IRepository<User>>();
                var _cartRepository = scope.ServiceProvider.GetRequiredService<IRepository<Cart>>();
                var _cartItemRepository = scope.ServiceProvider.GetRequiredService<IRepository<CartItem>>();
                var _compareRepository = scope.ServiceProvider.GetRequiredService<IRepository<ComparingProduct>>();
                var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
                await DeleteFailedUsers(userRepository, _cartRepository, _cartItemRepository, _compareRepository);
            }
        }
    }



    private async Task DeleteFailedUsers(IRepository<User> userRepository, IRepository<Cart> _cartRepository, IRepository<CartItem> _cartItemRepository,
        IRepository<ComparingProduct> _compareRepository)
    {


        var user = await userRepository.Query().Include(x => x.Roles).ThenInclude(x => x.Role)
         .Where(x => x.FullName == "Guest").ToListAsync();



        foreach (var guest in user)
        {
            guest.Roles.Clear();

            var GetCartCompare = _compareRepository.Query().Where(x => x.UserId == guest.Id).ToList();

            if (GetCartCompare.Count > 0)
            {
                foreach (var compares in GetCartCompare)
                {
                    _compareRepository.Remove(compares);
                }
            }

            var GetCartUser = _cartRepository.Query().Where(x => x.CustomerId == guest.Id).ToList();
            if (GetCartUser.Count > 0)
            {
                foreach (var item in GetCartUser)
                {
                    var GetCartItem = _cartItemRepository.Query().Where(x => x.CartId == item.Id).ToList();
                    foreach (var Deleteıtem in GetCartItem)
                    {
                        _cartItemRepository.Remove(Deleteıtem);

                    }

                    _cartRepository.Remove(item);

                }
            }

            userRepository.Remove(guest);
            await userRepository.SaveChangesAsync();
        }

    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants