Example #1
0
        }         // ValidateAccount

        private int SaveMarketplace(AddAccountState oState, AccountModel model)
        {
            int nResult = -1;

            try {
                new Transactional(() => {
                    model.id          = _mpTypes.GetAll().First(a => a.InternalId == oState.VendorInfo.Guid()).Id;
                    model.displayName = model.displayName ?? model.name;

                    IDatabaseCustomerMarketPlace mp = _helper.SaveOrUpdateEncryptedCustomerMarketplace(
                        model.name,
                        oState.Marketplace,
                        model,
                        _context.Customer
                        );

                    oState.Model = Json(AccountModel.ToModel(mp), JsonRequestBehavior.AllowGet);
                    oState.CustomerMarketPlace = mp;
                    nResult = mp.Id;
                }).Execute();
            }
            catch (Exception e) {
                ms_oLog.Error(e, "Failed to save marketplace.");
                oState.Error = CreateError(e);
            }             // try

            return(nResult);
        }         // SaveMarketplace
Example #2
0
        public JsonResult Accounts(AccountModel model)
        {
            AddAccountState oState = ValidateModel(model);

            if (oState.Error != null)
            {
                return(oState.Error);
            }

            ValidateAccount(oState, model);

            if (oState.Error != null)
            {
                return(oState.Error);
            }

            int mpId = SaveMarketplace(oState, model);

            if (mpId != -1)
            {
                try {
                    m_oServiceClient.Instance.UpdateMarketplace(_context.Customer.Id, mpId, true, _context.UserId);
                }
                catch (Exception e) {
                    ms_oLog.Warn(e, "Something not so excellent while updating CG marketplace with id {0}.", mpId);
                }         // try
            }             // if

            if (oState.Error != null)
            {
                return(oState.Error);
            }

            return(oState.Model);
        }         // Accounts
Example #3
0
        public bool Execute()
        {
            Console.Clear();
            Render();

            switch(_state)
            {
                case AddAccountState.Name:
                    _accountName = Console.ReadLine();

                    if (ValidateInput())
                        _state = AddAccountState.AccountNumber;

                    break;

                case AddAccountState.AccountNumber:
                    _accountNumber = Console.ReadLine();

                    if (ValidateInput())
                        _state = AddAccountState.Summary;

                    break;

                case AddAccountState.Summary:
                    BudgeteerData.Accounts.Add(new Account(_accountName, _accountNumber));
                    Console.ReadLine();
                    Program.ChangeController("Menu", true);
                    break;
            }

            return true;
        }
Example #4
0
        }         // class AddAccountState

        private AddAccountState ValidateMpUniqueness(AccountModel model, Customer oCustomer)
        {
            log.Debug("ValidateMpUniqueness started...");

            var oResult = new AddAccountState();

            log.Debug("ValidateMpUniqueness: result holder created.");

            try {
                log.Debug("ValidateMpUniqueness: going for it...");

                oResult.Marketplace = new DatabaseMarketPlace(model.accountTypeName);

                string uniqueID = model.Fill().UniqueID();

                log.Debug("ValidateMpUniqueness: result marketplace is set, unique id is '{0}'.", uniqueID);

                this.uniquenessChecker.Check(oResult.Marketplace.InternalId, oCustomer, uniqueID);

                log.Debug("ValidateMpUniqueness: congrats! It's unique.");
            } catch (MarketPlaceAddedByThisCustomerException) {
                log.Error("ValidateMpUniqueness: oops! The same customer already has this.");
                oResult.Error = new HmrcManualAccountManagerException(DbStrings.StoreAddedByYou);
            } catch (MarketPlaceIsAlreadyAddedException) {
                log.Error("ValidateMpUniqueness: oops! Someone else already has this.");
                oResult.Error = new HmrcManualAccountManagerException(DbStrings.StoreAlreadyExistsInDb);
            } catch (Exception e) {
                log.Error(e, "ValidateMpUniqueness: something went wrong while checking for uniqueness.");
                oResult.Error = new HmrcManualAccountManagerException(e);
            }             // try

            log.Debug("ValidateMpUniqueness complete with{0} error.", oResult.Error == null ? "out" : string.Empty);

            return(oResult);
        }         // ValidateMpUniqueness
Example #5
0
        }         // ValidateModel

        private void ValidateAccount(AddAccountState oState, AccountModel model)
        {
            try {
                var ctr = new Connector(
                    oState.AccountData,
                    ms_oLog,
                    _context.Customer.Id,
                    _context.Customer.Name
                    );

                if (ctr.Init())
                {
                    ctr.Run(true);
                    ctr.Done();
                }                 // if
            }
            catch (ConnectionFailException cge) {
                if (CurrentValues.Instance.ChannelGrabberRejectPolicy == ChannelGrabberRejectPolicy.ConnectionFail)
                {
                    ms_oLog.Error(cge, "Connection failure.");
                    oState.Error = CreateError(cge);
                }                 // if

                ms_oLog.Error(cge, "Failed to validate {0} account, continuing with registration.", model.accountTypeName);

                // Error is logged but not written into state.
            } catch (ConfigException cex) {
                if (cex.IsWarn)
                {
                    ms_oLog.Warn(cex, "Failed to validate {0} account.", model.accountTypeName);
                }
                else
                {
                    ms_oLog.Error(cex, "Failed to validate {0} account.", model.accountTypeName);
                }
                oState.Error = CreateError(cex);
            }
            catch (ApiException cge) {
                ms_oLog.Error(cge, "Failed to validate {0} account.", model.accountTypeName);
                oState.Error = CreateError(cge);
            }
            catch (InvalidCredentialsException ice) {
                ms_oLog.Info(ice, "Invalid credentials.");
                oState.Error = CreateError(ice);
            }
            catch (Exception e) {
                ms_oLog.Error(e, "Unexpected exception.");
                oState.Error = CreateError(e);
            }     // try
        }         // ValidateAccount
Example #6
0
        }         // class AddAccountState

        private AddAccountState ValidateModel(AccountModel model)
        {
            var oResult = new AddAccountState();

            oResult.VendorInfo = Configuration.Instance.GetVendorInfo(model.accountTypeName);

            if (oResult.VendorInfo == null)
            {
                var sError = "Unsupported account type: " + model.accountTypeName;
                ms_oLog.Error(sError);
                oResult.Error = CreateError(sError);
                return(oResult);
            }             // try

            try {
                oResult.AccountData = model.Fill();

                oResult.Marketplace = new DatabaseMarketPlace(model.accountTypeName);

                _mpChecker.Check(oResult.Marketplace.InternalId, _context.Customer, oResult.AccountData.UniqueID());
            }
            catch (MarketPlaceAddedByThisCustomerException) {
                oResult.Error = CreateError(DbStrings.StoreAddedByYou);
                return(oResult);
            }
            catch (MarketPlaceIsAlreadyAddedException) {
                oResult.Error = CreateError(DbStrings.StoreAlreadyExistsInDb);
                return(oResult);
            }
            catch (Exception e) {
                ms_oLog.Error(e);
                oResult.Error = CreateError(e);
                return(oResult);
            }             // try

            return(oResult);
        }         // ValidateModel
Example #7
0
 public void Reset()
 {
     _state = AddAccountState.Name;
     _accountName = "";
     _accountNumber = "";
 }
Example #8
0
        }         // ValidateMpUniqueness

        private void SaveMarketplace(AddAccountState oState, AccountModel model, Customer oCustomer)
        {
            try {
                model.id          = this.mpTypes.GetAll().First(a => a.InternalId == this.vendorInfo.Guid()).Id;
                model.displayName = model.displayName ?? model.name;

                SafeReader sr = DbConnectionGenerator.Get(log).GetFirst(
                    "CreateOrLoadUploadedHmrcMarketplace",
                    CommandSpecies.StoredProcedure,
                    new QueryParameter("@CustomerID", oCustomer.Id),
                    new QueryParameter("@SecurityData", new Encrypted(new Serialized(model))),
                    new QueryParameter("@Now", DateTime.UtcNow)
                    );

                int spExitCode = sr["ExitCode"];
                oState.CustomerMarketPlaceID = 0;

                switch (spExitCode)
                {
                case 0:
                    oState.CustomerMarketPlaceID = sr["MpID"];
                    log.Info(
                        "Successfully created uploaded/manual HMRC marketplace {0} for customer {1}.",
                        oState.CustomerMarketPlaceID,
                        oCustomer.Stringify()
                        );
                    break;

                case -1:
                    log.Alert(
                        "Failed to create uploaded/manual HMRC marketplace for customer {0}.",
                        oCustomer.Stringify()
                        );
                    break;

                case -2:
                    log.Alert(
                        "Uploaded/manual HMRC marketplace with email '{0}' that should belong to customer {1}" +
                        "already exists with id {2} and belongs to customer {3}.",
                        oCustomer.Name,
                        oCustomer.Stringify(),
                        sr["MpID"],
                        sr["OtherCustomerID"]
                        );
                    break;

                default:
                    log.Alert(
                        "Other error while creating uploaded/manual HMRC marketplace for customer {0}.",
                        oCustomer.Stringify()
                        );
                    break;
                }                 // switch

                if (spExitCode < 0)
                {
                    throw new Exception("Failed to save VAT return data.");
                }
            } catch (Exception e) {
                log.Error(e);
                oState.Error = new HmrcManualAccountManagerException(e);
            }             // try

            if (oState.Error != null)
            {
                return;
            }

            try {
                // This is done to for two reasons:
                // 1. update Customer.WizardStep to WizardStepType.Marketplace
                // 2. insert entries into EzServiceActionHistory
                this.serviceClient.Instance.UpdateMarketplace(
                    oCustomer.Id,
                    oState.CustomerMarketPlaceID,
                    true,
                    this.context.UserId
                    );
            }
            catch (Exception e) {
                log.Warn(e,
                         "Failed to start UpdateMarketplace strategy for customer [{0}: {1}] with marketplace id {2}," +
                         " if this is the only customer marketplace underwriter should run this strategy manually" +
                         " (otherwise Main strategy will be stuck).",
                         oCustomer.Id,
                         oCustomer.Name,
                         oState.CustomerMarketPlaceID
                         );
            }     // try
        }         // SaveMarketplace
Example #9
0
        }         // FindMarketplace

        private int FindOrCreateMarketplace(Customer oCustomer)
        {
            log.Debug("FindOrCreateMarketplace for customer {0} started...", oCustomer.Stringify());

            int mpID = FindMarketplace(oCustomer);

            if (mpID > 0)
            {
                log.Debug(
                    "FindOrCreateMarketplace for customer {0} complete: found {1} on the first check.",
                    oCustomer.Stringify(),
                    mpID
                    );

                return(mpID);
            }             // if

            log.Debug(
                "FindOrCreateMarketplace for customer {0}: not found on the first check, locking...",
                oCustomer.Stringify()
                );

            lock (lockCreateMarketplace) {
                log.Debug(
                    "FindOrCreateMarketplace for customer {0}: locked.",
                    oCustomer.Stringify()
                    );

                mpID = FindMarketplace(oCustomer);

                if (mpID > 0)
                {
                    log.Debug(
                        "FindOrCreateMarketplace for customer {0} complete: found {1} on the second check.",
                        oCustomer.Stringify(),
                        mpID
                        );

                    return(mpID);
                }                 // if

                log.Debug(
                    "FindOrCreateMarketplace for customer {0}: not found on the second check, creating model...",
                    oCustomer.Stringify()
                    );

                var model = new AccountModel {
                    accountTypeName = this.vendorInfo.Name,
                    displayName     = oCustomer.Name,
                    name            = oCustomer.Name,
                    login           = oCustomer.Name,
                    password        = VendorInfo.TopSecret,
                };

                log.Debug(
                    "FindOrCreateMarketplace for customer {0}: model is ready for uniqueness check.",
                    oCustomer.Stringify()
                    );

                AddAccountState oState = ValidateMpUniqueness(model, oCustomer);

                if (oState.Error != null)
                {
                    log.Debug(
                        "FindOrCreateMarketplace for customer {0} complete: model is not unique.",
                        oCustomer.Stringify()
                        );

                    return(0);
                }                 // if

                log.Debug(
                    "FindOrCreateMarketplace for customer {0}: creating a new marketplace.",
                    oCustomer.Stringify()
                    );

                SaveMarketplace(oState, model, oCustomer);

                log.Debug(
                    "FindOrCreateMarketplace for customer {0} complete: created {1}.",
                    oCustomer.Stringify(),
                    oState.CustomerMarketPlaceID
                    );

                return(oState.CustomerMarketPlaceID);
            }     // lock
        }         // FindOrCreateMarketplace