Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.9 kB
1
Indexable
Never
using Ezrx.AccountIdentity.Application.Contract.Repositories;
using Ezrx.AccountIdentity.Common.Constants;
using Ezrx.AccountIdentity.Common.Exceptions;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Ezrx.AccountIdentity.Application.Features.Accounts.Commands
{
    public class LogoutCommand : IRequest<Unit>
    {
        public bool? isMobile { get; set; }

        public Guid AccountId { get; set; }

        public LogoutCommand(bool? _isMobile)
        {
            isMobile = _isMobile;
        }

        public class LogoutCommandHandler : IRequestHandler<LogoutCommand, Unit>
        {
            private readonly IApplicationDbContext _dbContext;

            public LogoutCommandHandler(IApplicationDbContext dbContext)
            {
                _dbContext = dbContext;
            }

            public async Task<Unit> Handle(LogoutCommand request, CancellationToken cancellationToken)
            {
                var accountSetting = await _dbContext.AccountSettings
                                           .Where(x => x.AccountId == request.AccountId)
                                           .Where(x => x.LimitActive == true)
                                           .FirstOrDefaultAsync();

                if (accountSetting == null)
                    throw new BadRequestException(ApplicationConstant.DataNotFoundMessage);

                if (request.isMobile.Value)
                {
                    accountSetting.FCMToken = null;
                }
                else
                {
                    accountSetting.FCMTokenWeb = null;
                }

                _dbContext.AccountSettings.Update(entity: accountSetting);
                
                await _dbContext.SaveChangesAsync(cancellationToken);

                return Unit.Value;
            }
        }
    }
}