Ejemplo n.º 1
0
        public async Task <CreateShopCommandResponse> Handle(CreateShopCommand command)
        {
            var shopNumber = _seqRepository.GetNextSequenceValue(SqNames.ShopNumberSequence);
            var marketer   = await _marketerRepository.AsQuery().SingleOrDefaultAsync(p => p.BarcodeId == command.BarcodeId);

            if (marketer == null)
            {
                throw new DomainException("بازاریاب یافت نشد");
            }
            _marketerDomainService.CheckMarketerActive(marketer);
            _marketerDomainService.CheckMaxMarketerAllowedIsEnough(marketer);
            _personDomainService.CheckShopIsExist(command.UserId);
            var city = _cityRepository.Find(command.Address.CityId);

            if (city == null)
            {
                throw new DomainException("شهر وارد شده یافت نشد");
            }
            var appSetting = await _applicationSettingRepository.AsQuery().SingleOrDefaultAsync();

            if (appSetting == null)
            {
                throw new DomainException("تنظیمات برنامه یافت نشد");
            }
            if (command.DefaultDiscount < appSetting.MinimumDiscount || command.DefaultDiscount > appSetting.MaximumDiscount)
            {
                throw new DomainException("تخفیف پیش فرض در بازه تخفیفات معتبر نمی باشد");
            }
            var address = new ShopAddress(city.Id, city.Code, city.CityName, command.Address.AddressText,
                                          command.Address.PhoneNumber, command.Address.Position.ToDbGeography(),
                                          command.Address.ShopMobileNumber, command.Address.ZoneId,
                                          city.Province.Id, city.Province.Code, city.Province.Name);
            var accountBank   = new BankAccount(command.BankAccount.Iban, command.BankAccount.AccountOwnerName, command.BankAccount.AccountNumber);
            var imageDocument = new ImageDocuments(command.ImageDocuments.FaceImage, command.ImageDocuments.NationalCardImage);
            var shop          = new Shop(Guid.NewGuid(), command.Name, command.FirstName, command.LastName, command.EmailAddress,
                                         command.UserId, command.Description, command.NationalCode, address, accountBank, imageDocument,
                                         command.MobileNumber, command.AreaRadius, command.Metrage, command.DefaultDiscount, marketer.Id, shopNumber)
            {
                AppInfos        = new List <AppInfo>(),
                CustomerSubsets = new List <ShopCustomerSubset>()
            };

            _repository.Add(shop);
            DomainEventDispatcher.Raise(new AssignmentShopMarketersHistoryEvent(shop, marketer,
                                                                                new UserInfo(command.UserId, command.MobileNumber, "کاربر موبایل")));
            DomainEventDispatcher.Raise(new AddPanelNotificationEvent(Guid.NewGuid(), command.Name, "فروشگاه ایجاد شد",
                                                                      PanelNotificationType.ShopCreated, shop.Id.ToString()));
            return(new CreateShopCommandResponse());
        }