Esempio n. 1
0
            public async Task <Unit> Handle(UpdateCurrencyCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.Currencies.FindAsync(request.Id);

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

                if (request.CountryId.HasValue)
                {
                    var countryHasCurrency = await _context.Currencies.AnyAsync(x => x.CountryId == request.CountryId.Value);

                    if (countryHasCurrency)
                    {
                        throw new BadRequestException("The country reference used already has a currency associated to it. The opreation cannot be completed.");
                    }
                }

                entity.Name         = request.Name ?? entity.Name;
                entity.ExchangeRate = request.ExchangeRate ?? entity.ExchangeRate;
                entity.BaseCurrency = request.BaseCurrency ?? entity.BaseCurrency;
                entity.CountryId    = request.CountryId ?? entity.CountryId;

                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
Esempio n. 2
0
            public async Task <Guid> Handle(CreateCustomerCommand request, CancellationToken cancellationToken)
            {
                if (request.CountryId.HasValue)
                {
                    var hasCountry = await _context.Countries.AnyAsync(x => x.Id == request.CountryId.Value);

                    if (!hasCountry)
                    {
                        throw new BadRequestException("A country with referenced Id does not exist. The operation cannot be completed.");
                    }
                }

                var entity = new Customer {
                    Name      = request.Name,
                    Phone     = request.Phone,
                    CountryId = request.CountryId,
                    Category  = request.Category.GetHashCode(),
                    IsActive  = false
                };

                await _context.Customers.AddAsync(entity, cancellationToken);

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
Esempio n. 3
0
            public async Task <Unit> Handle(UpdatePriceListCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.PriceLists.FindAsync(request.Id);

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

                if (request.CurrencyId.HasValue)
                {
                    var hasPriceListWithCurrency = await _context.PriceLists.AnyAsync(x => x.CurrencyId == request.CurrencyId.Value);

                    if (hasPriceListWithCurrency)
                    {
                        throw new BadRequestException("There is already a price list with updated currency reference. The operation cannot be completed.");
                    }
                }

                entity.Name       = request.Name ?? entity.Name;
                entity.CurrencyId = request.CurrencyId.HasValue ? request.CurrencyId.Value : entity.CurrencyId;

                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
            public async Task <Unit> Handle(DeleteCustomerCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.Customers.FindAsync(request.Id);

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

                var hasServiceContract = await _context.ServiceContracts.AnyAsync(x => x.CustomerId == request.Id);

                if (hasServiceContract)
                {
                    throw new DeleteFailureException(nameof(Customer), request.Id, "There are service contracts associated to this customer. The operation cannot be completed.");
                }

                var hasCases = await _context.Cases.AnyAsync(x => x.CustomerId == request.Id);

                if (hasCases)
                {
                    throw new DeleteFailureException(nameof(Customer), request.Id, "There are cases associated to this customer. The operation cannot be completed.");
                }

                var hasContacts = await _context.Contacts.AnyAsync(x => x.CustomerId == request.Id);

                if (hasContacts)
                {
                    throw new DeleteFailureException(nameof(Customer), request.Id, "There are contacts associated to this customer. The opration cannot be completed.");
                }

                _context.Customers.Remove(entity);
                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
            public async Task <Unit> Handle(UpdateCustomerCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.Customers.FindAsync(request.Id);

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

                if (request.CountryId.HasValue)
                {
                    var hasCountry = await _context.Countries.AnyAsync(x => x.Id == request.CountryId.Value);

                    if (!hasCountry)
                    {
                        throw new BadRequestException("The referenced country does not exist. The operation cannot be completed.");
                    }
                }

                entity.Name     = request.Name;
                entity.Category = request.Category.GetHashCode();
                entity.Phone    = request.Phone;
                entity.IsActive = request.IsActive;

                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
            public async Task <Unit> Handle(DeletePriceListCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.PriceLists.FindAsync(request.Id);

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

                var hasServiceContracts = await _context.ServiceContracts.AnyAsync(x => x.PriceListId == request.Id);

                if (hasServiceContracts)
                {
                    throw new DeleteFailureException(nameof(PriceList), request.Id, "There are service contracts associated to this price list. The operation cannot be completed.");
                }

                var hasServiceContractLines = await _context.ServiceContractLines.AnyAsync(x => x.PriceListId == request.Id);

                if (hasServiceContractLines)
                {
                    throw new DeleteFailureException(nameof(PriceList), request.Id, "There are service contract items associated to this price list. The operation cannot be completed.");
                }

                var hasProducts = await _context.Products.AnyAsync(x => x.DefaultPriceListId == request.Id);

                if (hasProducts)
                {
                    throw new DeleteFailureException(nameof(PriceList), request.Id, "There are products associated to this price list. The operation cannot be completed.");
                }

                _context.PriceLists.Remove(entity);
                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
            public async Task <Guid> Handle(CreateCaseCommand request, CancellationToken cancellationToken)
            {
                // Case entity;
                // if(request.Id.HasValue) {
                //     entity = await _context.Cases.FindAsync(request.Id.Value, cancellationToken);
                // } else {
                //     entity = new Case();
                //     await _context.Cases.AddAsync(entity, cancellationToken);
                // }
                var entity = new Case {
                    Title           = request.Title,
                    Description     = request.Description,
                    Status          = request.Status.GetHashCode(),
                    Priority        = request.Priority.GetHashCode(),
                    ExpectedEndDate = _slaService.CalculateExpectedDate(request.Priority),
                    ActualEndDate   = _slaService.CalculateActualEndDate(request.Status),
                    ContactId       = request.ContactId,
                    CustomerId      = request.CustomerId
                };

                await _context.Cases.AddAsync(entity, cancellationToken);

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
Esempio n. 8
0
            public async Task <Unit> Handle(DeleteServiceContractCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.ServiceContracts.FindAsync(request.Id);

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

                var hasCases = await _context.Cases.AnyAsync(x => x.ServiceContractId == request.Id);

                if (hasCases)
                {
                    throw new DeleteFailureException(nameof(ServiceContract), request.Id, "This serivce contract has cases assocaited to it. The operation cannot be completed.");
                }

                var serviceContractLineCollection = await _context.ServiceContractLines.ToListAsync(cancellationToken);

                _context.ServiceContractLines.RemoveRange(serviceContractLineCollection);
                _context.ServiceContracts.Remove(entity);

                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
            public async Task <Guid> Handle(CreateServiceContractLineCommand request, CancellationToken cancellationToken)
            {
                var hasServiceContract = await _context.ServiceContracts.AnyAsync(x => x.Id == request.ServiceContractId);

                if (!hasServiceContract)
                {
                    throw new BadRequestException("The referenced service contract does not exist. The operation cannot be completed.");
                }

                // var hasProduct = await _context.Products.AnyAsync(x => x.Id == request.ProductId);
                // if(!hasProduct) {
                //     throw new BadRequestException("The referenced product does not exist or is not active. The operaiton cannot be completed.");
                // }

                var hasPriceList = await _context.PriceLists.AnyAsync(x => x.Id == request.PriceListId);

                if (!hasPriceList)
                {
                    throw new BadRequestException("The referenced price list does not exist. The operation cannot be completed.");
                }

                var productPriceListItem = await _context.PriceListItems.AnyAsync(x => x.Id == request.PriceListItemId);

                //.Where(x => x.PriceListId == request.PriceListId && x.ProductId == request.ProductId).FirstOrDefaultAsync();

                if (!productPriceListItem)
                {
                    throw new BadRequestException("The referenced product does not belong to the referenced price list. The operation cannot be completed.");
                }

                if (request.TaxProfileId.HasValue)
                {
                    var hastaxProfile = await _context.TaxProfiles.AnyAsync(x => x.Id == request.TaxProfileId.Value);

                    if (!hastaxProfile)
                    {
                        throw new BadRequestException("The referenced tax profile does not exist. The operation cannot be completed.");
                    }
                }

                var entity = new ServiceContractLine {
                    Name              = request.Name,
                    UnitPrice         = 0.00,
                    NetPrice          = 0.00,
                    Discount          = request.Discount,
                    DiscountType      = request.DiscountType.GetHashCode(),
                    Quantity          = request.Quantity,
                    PriceListItemId   = request.PriceListItemId,
                    TaxProfileId      = request.TaxProfileId,
                    PriceListId       = request.PriceListId,
                    ServiceContractId = request.ServiceContractId
                };

                await _context.ServiceContractLines.AddAsync(entity);

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
            public async Task <Unit> Handle(UpdateSystemUserCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.SystemUsers.FindAsync(request.Id);

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

                if (request.UserManagerId.HasValue)
                {
                    var userManager = await _context.SystemUsers.AnyAsync(x => x.Id == request.UserManagerId.Value);

                    if (!userManager)
                    {
                        throw new BadRequestException("Referenced user manager does not exist. The operation cannot be completed.");
                    }

                    entity.UserManagerId = request.UserManagerId.Value;
                }

                if (!string.IsNullOrEmpty(request.FirstName))
                {
                    entity.FirstName = request.FirstName;
                }
                if (!string.IsNullOrEmpty(request.LastName))
                {
                    entity.LastName = request.LastName;
                }

                if (!string.IsNullOrEmpty(request.EmailAddress) && !entity.EmailAddress.Equals(request.EmailAddress))
                {
                    entity.EmailAddress = request.EmailAddress;
                }

                if (request.IsVerified.HasValue)
                {
                    entity.IsVerified = request.IsVerified.Value;
                }

                if (request.IsActive.HasValue)
                {
                    entity.IsActive = request.IsActive.Value;
                }

                if (request.IsLocked.HasValue)
                {
                    entity.IsLocked = request.IsLocked.Value;
                }

                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
            public async Task <Guid> Handle(CreateCountryCommand request, CancellationToken cancellationToken)
            {
                var entity = new Country {
                    Name            = request.Name,
                    TwoDigitISOCode = request.TwoDigitISOCode
                };
                await _context.Countries.AddAsync(entity);

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
Esempio n. 12
0
            public async Task <Unit> Handle(DeleteContactCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.Contacts.FindAsync(request.Id);

                if (entity == null)
                {
                    // throw 404
                }
                _context.Contacts.Remove(entity);
                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
Esempio n. 13
0
            public async Task <Unit> Handle(DeleteSystemUserCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.SystemUsers.FindAsync(request.Id);

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

                _context.SystemUsers.Remove(entity);
                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
            public async Task <Guid> Handle(CreateCurrencyCommand request, CancellationToken cancellationToken)
            {
                var entity = new Currency {
                    Name         = request.Name,
                    ExchangeRate = request.ExchangeRate,
                    BaseCurrency = request.BaseCurrency,
                    CountryId    = request.CountryId
                };

                await _context.Currencies.AddAsync(entity, cancellationToken);

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
Esempio n. 15
0
            public async Task <Unit> Handle(UpdateCountryCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.Countries.FindAsync(request.Id);

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

                entity.Name            = request.Name ?? entity.Name;
                entity.TwoDigitISOCode = request.TwoDigitISOCode ?? entity.TwoDigitISOCode;

                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
            public async Task <Guid> Handle(CreateContactCommand request, CancellationToken cancellationToken)
            {
                var entity = new Contact {
                    FirstName    = request.FirstName,
                    LastName     = request.LastName,
                    MobilePhone  = request.MobilePhone,
                    EmailAddress = request.EmailAddress,
                    CustomerId   = request.CustomerId
                };

                await _context.Contacts.AddAsync(entity, cancellationToken);

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
            public async Task <Unit> Handle(DeleteCaseCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.Cases.FindAsync(request.Id);

                if (entity == null)
                {
                    throw new Exception("Case with Id does not exixt.");
                    // throw custom notfound exception;
                }
                else
                {
                    _context.Cases.Remove(entity);
                    await _context.SaveChangesAsync(cancellationToken);

                    return(Unit.Value);
                }
            }
Esempio n. 18
0
            public async Task <Guid> Handle(CreateServiceContractCommand request, CancellationToken cancellationToken)
            {
                if (request.TaxProfileId.HasValue)
                {
                    var hasTaxProfile = await _context.TaxProfiles.AnyAsync(x => x.Id == request.TaxProfileId.Value);

                    if (!hasTaxProfile)
                    {
                        throw new BadRequestException("The referenced tax profile does not exist. The opration cannot be completed.");
                    }
                }

                var hasPriceList = await _context.PriceLists.AnyAsync(x => x.Id == request.PriceListId);

                if (!hasPriceList)
                {
                    throw new BadRequestException("The referenced price list does not eixst. The operation cannot be completed.");
                }

                var hasCustomer = await _context.Customers.AnyAsync(x => x.Id == request.CustomerId);

                if (!hasCustomer)
                {
                    throw new BadRequestException("The referenced customer does not exist. The operation cannot be completed.");
                }

                var entity = new ServiceContract {
                    Name         = request.Name,
                    Amount       = request.Amount,
                    StartDate    = request.StartDate,
                    EndDate      = request.EndDate,
                    PriceListId  = request.PriceListId,
                    TaxProfileId = request.TaxProfileId,
                    CustomerId   = request.CustomerId,
                    Status       = request.Status.GetHashCode()
                };

                await _context.ServiceContracts.AddAsync(entity);

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
Esempio n. 19
0
            public async Task <Unit> Handle(UpdateContactCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.Contacts.FindAsync(request.Id);

                if (entity == null)
                {
                    throw new EntityNotFoundException(nameof(Contact), request.Id);
                }
                entity.FirstName    = request.FirstName;
                entity.LastName     = request.LastName;
                entity.EmailAddress = request.EmailAddress;
                entity.MobilePhone  = request.MobilePhone;
                entity.CustomerId   = request.CustomerId;
                entity.IsActive     = request.IsActive;

                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
Esempio n. 20
0
            public async Task <Unit> Handle(UpdateTaxProfileCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.TaxProfiles
                             .Include(x => x.ServiceContractLines)
                             .SingleOrDefaultAsync(x => x.Id == request.Id);

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

                if (request.CurrencyId.HasValue && request.CurrencyId.Value != entity.CurrencyId)
                {
                    var currencyExists = await _context.Currencies.AnyAsync(x => x.Id == request.CurrencyId.Value);

                    if (!currencyExists)
                    {
                        throw new BadRequestException("The referenced currency does not exist. The operation cannot be completed.");
                    }

                    entity.CurrencyId = request.CurrencyId.Value;
                }

                if (!string.IsNullOrEmpty(request.Name))
                {
                    entity.Name = request.Name;
                }

                if (request.Amount.HasValue)
                {
                    entity.Amount = request.Amount.Value;
                }

                if (request.TaxProfileType.HasValue)
                {
                    entity.TaxProfileType = request.TaxProfileType.Value.GetHashCode();
                }

                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
Esempio n. 21
0
            public async Task <Guid> Handle(CreatePriceListCommand request, CancellationToken cancellationToken)
            {
                var hasPriceListForCurrency = await _context.PriceLists.AnyAsync(x => x.CurrencyId == request.CurrencyId);

                if (hasPriceListForCurrency)
                {
                    throw new BadRequestException("There is already a price list for the referenced currency.");
                }

                var entity = new PriceList {
                    Name       = request.Name,
                    CurrencyId = request.CurrencyId
                };

                await _context.PriceLists.AddAsync(entity);

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
Esempio n. 22
0
            public async Task <Unit> Handle(DeleteCountryCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.Countries.FindAsync(request.Id);

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

                var hasCurrencies = await _context.Currencies.AnyAsync(x => x.CountryId == request.Id);

                if (hasCurrencies)
                {
                    throw new DeleteFailureException(nameof(Country), request.Id, "There is a currency associated to this country. The request cannot be completed.");
                }

                _context.Countries.Remove(entity);
                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
Esempio n. 23
0
            public async Task <Unit> Handle(UpdateProductCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.Products.FindAsync(request.Id);

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

                if (request.DefaultPriceListId.HasValue)
                {
                    var priceListExists = await _context.PriceLists.AnyAsync(x => x.Id == request.DefaultPriceListId.Value);

                    if (!priceListExists)
                    {
                        throw new BadRequestException("The referenced price list does not exist. The opration cannot be completed.");
                    }
                }

                entity.Name = request.Name ?? entity.Name;

                if (request.ProductType.HasValue)
                {
                    entity.ProductType = request.ProductType.GetHashCode();
                }

                if (request.ListPrice.HasValue)
                {
                    entity.ListPrice = request.ListPrice.Value;
                }

                if (request.DefaultPriceListId.HasValue)
                {
                    entity.DefaultPriceListId = request.DefaultPriceListId.Value;
                }

                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
            public async Task <Guid> Handle(CreateTaxProfileCommand request, CancellationToken cancellationToken)
            {
                var hasCurrency = await _context.Currencies.AnyAsync(x => x.Id == request.CurrencyId);

                if (!hasCurrency)
                {
                    throw new BadRequestException("The referenced currency does not exist. The operation cannot be completed.");
                }

                var entity = new TaxProfile {
                    Name           = request.Name,
                    Amount         = request.Amount,
                    CurrencyId     = request.CurrencyId,
                    TaxProfileType = request.TaxProfileType.GetHashCode()
                };

                await _context.TaxProfiles.AddAsync(entity);

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
Esempio n. 25
0
            public async Task <Guid> Handle(CreateProductCommand request, CancellationToken cancellationToken)
            {
                var priceListExists = await _context.PriceLists.AnyAsync(x => x.Id == request.DefaultPriceListId);

                if (!priceListExists)
                {
                    throw new BadRequestException("The referenced price list does not eixst. The opration cannot be completed.");
                }

                var entity = new Product {
                    Name               = request.Name,
                    ProductType        = request.ProductType.GetHashCode(),
                    ListPrice          = request.ListPrice,
                    DefaultPriceListId = request.DefaultPriceListId
                };

                await _context.Products.AddAsync(entity);

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
Esempio n. 26
0
            public async Task <Guid> Handle(RegisterSystemUserCommand request, CancellationToken cancellationToken)
            {
                var userExists = await _context.SystemUsers.Where(x => x.EmailAddress == request.EmailAddress.ToLower()).AnyAsync();

                if (userExists)
                {
                    throw new DuplicateUserException(nameof(SystemUser), request.EmailAddress);
                }

                if (request.UserManagerId.HasValue)
                {
                    var managerExists = await _context.SystemUsers.AnyAsync(x => x.Id == request.UserManagerId.Value);

                    if (!managerExists)
                    {
                        throw new EntityNotFoundException(nameof(SystemUser), request.UserManagerId.Value);
                    }
                }

                _passwordService.GeneratePassword(request.Password, out byte[] passwordHash, out byte[] passwordSalt);

                var entity = new SystemUser {
                    FirstName     = request.FirstName,
                    LastName      = request.LastName,
                    EmailAddress  = request.EmailAddress.ToLower(),
                    PasswordHash  = passwordHash,
                    PasswordSalt  = passwordSalt,
                    UserManagerId = request.UserManagerId
                };

                await _context.SystemUsers.AddAsync(entity);

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
            public async Task <Unit> Handle(UpdateServiceContractLineCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.ServiceContractLines.FindAsync(request.Id);

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

                if (request.TaxProfileId.HasValue)
                {
                    var hasTaxProfile = await _context.TaxProfiles.AnyAsync(x => x.Id == request.TaxProfileId.Value);

                    if (!hasTaxProfile)
                    {
                        throw new BadRequestException("The referenced tax profile id does not exist. The operation cannot be completed.");
                    }
                }

                if (request.PriceListItemId.HasValue)
                {
                    var priceListItem = await _context.PriceListItems.Include(x => x.PriceList).SingleOrDefaultAsync(x => x.Id == request.PriceListItemId.Value);

                    if (priceListItem == null)
                    {
                        throw new BadRequestException("The referenced price list item does not exist. The operation cannot be completed.");
                    }
                    if (request.PriceListId.HasValue && request.PriceListId.Value != priceListItem.PriceListId)
                    {
                        throw new BadRequestException("The referenced price list item does not exist in the referenced price list. The operation cannot be completed.");
                    }
                }

                if (request.PriceListId.HasValue)
                {
                    var hsaPriceList = await _context.PriceLists.AnyAsync(x => x.Id == request.PriceListId.Value);

                    if (!hsaPriceList)
                    {
                        throw new BadRequestException("The referenced price list does not exist. The operation cannot be completed.");
                    }
                }

                entity.Name = request.Name ?? entity.Name;
                if (request.Quantity.HasValue)
                {
                    entity.Quantity = request.Quantity.Value;
                }
                if (request.DiscountType.HasValue)
                {
                    entity.DiscountType = request.DiscountType.Value.GetHashCode();
                }
                if (request.Discount.HasValue)
                {
                    entity.Discount = request.Discount.Value;
                }

                if (request.TaxProfileId.HasValue)
                {
                    entity.TaxProfileId = request.TaxProfileId.Value;
                }

                if (request.PriceListId.HasValue)
                {
                    entity.PriceListId = request.PriceListId.Value;
                }

                if (request.PriceListItemId.HasValue)
                {
                    entity.PriceListItemId = request.PriceListItemId.Value;
                }

                // if(request.ServiceContractId.HasValue) {
                //     entity.ServiceContractId = request.ServiceContractId.Value;
                // }

                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
            public async Task <Unit> Handle(UpdateServiceContractCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.ServiceContracts.FindAsync(request.Id);

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

                if (request.TaxProfileId.HasValue)
                {
                    var hasTaxProfile = await _context.TaxProfiles.AnyAsync(x => x.Id == request.TaxProfileId.Value);

                    if (!hasTaxProfile)
                    {
                        throw new BadRequestException("The referenced tax profile does not exist. The opration cannot be completed.");
                    }
                }

                if (request.PriceListId.HasValue)
                {
                    var hasPriceList = await _context.PriceLists.AnyAsync(x => x.Id == request.PriceListId);

                    if (!hasPriceList)
                    {
                        throw new BadRequestException("The referenced price list does not eixst. The operation cannot be completed.");
                    }
                }

                if (request.CustomerId.HasValue)
                {
                    var hasCustomer = await _context.Customers.AnyAsync(x => x.Id == request.CustomerId);

                    if (!hasCustomer)
                    {
                        throw new BadRequestException("The referenced customer does not exist. The operation cannot be completed.");
                    }
                }

                if (!string.IsNullOrEmpty(request.Name))
                {
                    entity.Name = request.Name;
                }
                if (request.Amount.HasValue)
                {
                    entity.Amount = request.Amount.Value;
                }

                if (request.StartDate.HasValue)
                {
                    entity.StartDate = request.StartDate.Value;
                }

                if (request.EndDate.HasValue)
                {
                    entity.EndDate = request.EndDate.Value;
                }

                if (request.PriceListId.HasValue)
                {
                    entity.PriceListId = request.PriceListId.Value;
                }

                if (request.CustomerId.HasValue)
                {
                    entity.CustomerId = request.CustomerId.Value;
                }

                if (request.TaxProfileId.HasValue)
                {
                    entity.TaxProfileId = request.TaxProfileId.Value;
                }

                if (request.Status.HasValue)
                {
                    entity.Status = request.Status.GetHashCode();
                }

                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }