public async Task <Unit> Handle(DeleteEmployeeCommand request, CancellationToken cancellationToken)
        {
            var employees = _context.Set <Employee>();
            var entity    = await employees.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Employee), request.Id);
            }

            if (entity.UserId == _currentUser.UserId)
            {
                throw new BadRequestException("Employees cannot delete their own account.");
            }

            if (entity.UserId > 0)
            {
                await _userManager.DeleteUserAsync(entity.UserId);
            }

            // TODO: Update this logic, this will only work if the employee has no associated territories or orders.Emp

            employees.Remove(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Example #2
0
            public async Task <bool> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
            {
                if (request.UserName == _user)
                {
                    var deleted = await _userManager.DeleteUserAsync(request.UserName);

                    if (deleted.Succeeded)
                    {
                        return(true);
                    }

                    throw new CustomException($"DeleteUserCommand >> UserManager.DeleteUserAsync({request.UserName}) failed.");
                }

                throw new CustomException($"DeleteUserCommand >> Unauthorized!");
            }