Ejemplo n.º 1
0
        /// <summary>
        /// <para>Adds a sales agent from an api key and account id.</para>
        /// </summary>
        /// <param name="apiId">A valid eve api id (keyID).</param>
        /// <param name="apiKey">A valid eve api key (vCode).</param>
        /// <param name="accountId">The id of the account for which a sales agent should be added.</param>
        /// <returns>Returns a validation result object.</returns>
        internal IValidationResult AddSalesAgent(int apiId, string apiKey, int accountId)
        {
            IValidationResult validationResult = new ValidationResult();

            // Check the remaining sales agent licences for the account.
            if (this.GetRemainingSalesAgentLicences(accountId) <= 0)
            {
                validationResult.AddError("SalesAgent.Licences", "You have exceeded the number of sales agent licences available with your account subscription plan.");
            }
            else
            {
                // Fetch details about the Api Key from Eve Data.
                IEveDataApiKey apiKeyInfo = this.eveDataSource.GetApiKeyInfo(apiId, apiKey);

                if (apiKeyInfo != null)
                {
                    // Validate the api key.
                    validationResult = this.doctrineShipsValidation.ApiKey(apiKeyInfo);
                    if (validationResult.IsValid == true)
                    {
                        // Use the api key info to populate a new sales agent object.
                        SalesAgent newSalesAgent = new SalesAgent();

                        // If this is a character or account key use the character details, if a corp key use the corp details.
                        if (apiKeyInfo.Type == EveDataApiKeyType.Character || apiKeyInfo.Type == EveDataApiKeyType.Account)
                        {
                            // If this is an account key, the first character in the list will be used.
                            newSalesAgent.SalesAgentId = apiKeyInfo.Characters.FirstOrDefault().CharacterId;
                            newSalesAgent.Name = apiKeyInfo.Characters.FirstOrDefault().CharacterName;
                            newSalesAgent.ImageUrl = eveDataSource.GetCharacterPortraitUrl(newSalesAgent.SalesAgentId);
                            newSalesAgent.IsCorp = false;
                        }
                        else if (apiKeyInfo.Type == EveDataApiKeyType.Corporation)
                        {
                            newSalesAgent.SalesAgentId = apiKeyInfo.Characters.FirstOrDefault().CorporationId;
                            newSalesAgent.Name = apiKeyInfo.Characters.FirstOrDefault().CorporationName;
                            newSalesAgent.ImageUrl = eveDataSource.GetCorporationLogoUrl(newSalesAgent.SalesAgentId);
                            newSalesAgent.IsCorp = true;
                        }

                        // Populate the remaining properties.
                        newSalesAgent.AccountId = accountId;
                        newSalesAgent.ApiId = apiId;
                        newSalesAgent.ApiKey = apiKey;
                        newSalesAgent.IsActive = true;
                        newSalesAgent.LastForce = DateTime.UtcNow;
                        newSalesAgent.LastContractRefresh = DateTime.UtcNow;

                        // Validate the new sales agent.
                        validationResult = this.doctrineShipsValidation.SalesAgent(newSalesAgent);
                        if (validationResult.IsValid == true)
                        {
                            // Add the new sales agent and log the event.
                            this.doctrineShipsRepository.CreateSalesAgent(newSalesAgent);
                            this.doctrineShipsRepository.Save();
                            logger.LogMessage("Sales Agent '" + newSalesAgent.Name + "' Successfully Added For Account Id: " + newSalesAgent.AccountId, 2, "Message", MethodBase.GetCurrentMethod().Name);
                        }
                    }
                }
                else
                {
                    validationResult.AddError("ApiKey.Valid", "An invalid api key was entered or the eve api is currently unavailable.");
                }
            }

            return validationResult;
        }
Ejemplo n.º 2
0
        public ActionResult DeleteAccount(SiteAccountsViewModel viewModel)
        {
            if (viewModel.RemoveList != null)
            {
                IValidationResult validationResults = new ValidationResult();

                foreach (var accountId in viewModel.RemoveList)
                {
                    // Delete the current account and merge any validation errors in to a validation result object.
                    validationResults.Merge(this.doctrineShipsServices.DeleteAccount(accountId));
                }

                // If the validationResult is not valid, something did not validate in the service layer.
                if (validationResults.IsValid)
                {
                    TempData["Status"] = "All selected accounts were successfully removed.";
                }
                else
                {
                    TempData["Status"] = "Error: One or more errors were detected while removing the selected accounts.<br /><b>Error Details: </b>";

                    foreach (var error in validationResults.Errors)
                    {
                        TempData["Status"] += error.Value + "<br />";
                    }
                }
            }

            return RedirectToAction("Accounts");
        }
        /// <summary>
        /// Deletes an account and all access codes, ship fits, sales agents and their contracts.
        /// </summary>
        /// <param name="accountId">The account Id being deleted.</param>
        /// <returns>Returns a validation result object.</returns>
        public IValidationResult DeleteAccount(int accountId)
        {
            IValidationResult validationResult = new ValidationResult();

            // Delete all account ship fits, components and related contracts.
            var accountShipFits = ShipFitManager.GetShipFitList(accountId);
            foreach (var shipFit in accountShipFits)
            {
                if (ShipFitManager.DeleteShipFit(accountId, shipFit.ShipFitId) == false)
                {
                    validationResult.AddError(shipFit.ShipFitId.ToString(), "Error while deleting ship fit: " + shipFit.ShipFitId.ToString());
                }
            }

            // Delete all account sales agents.
            var accountSalesAgents = SalesAgentManager.GetSalesAgents(accountId);
            foreach (var salesAgent in accountSalesAgents)
            {
                if (SalesAgentManager.DeleteSalesAgent(accountId, salesAgent.SalesAgentId) == false)
                {
                    validationResult.AddError(salesAgent.SalesAgentId.ToString(), "Error while deleting sales agent: " + salesAgent.SalesAgentId.ToString());
                }
            }
                
            // Delete all account access codes.
            var accountAccessCodes = AccountManager.GetAccessCodes(accountId);
            foreach (var accessCode in accountAccessCodes)
            {
                if (AccountManager.DeleteAccessCode(accountId, accessCode.AccessCodeId) == false)
                {
                    validationResult.AddError(accessCode.AccessCodeId.ToString(), "Error while deleting access code: " + accessCode.AccessCodeId.ToString());
                }
            }

            // Delete all notification recipients.
            var accountNotificationRecipients = AccountManager.GetNotificationRecipients(accountId);
            foreach (var notificationRecipient in accountNotificationRecipients)
            {
                if (AccountManager.DeleteNotificationRecipient(accountId, notificationRecipient.NotificationRecipientId) == false)
                {
                    validationResult.AddError(notificationRecipient.NotificationRecipientId.ToString(), "Error while deleting notification recipient: " + notificationRecipient.NotificationRecipientId.ToString());
                }
            }

            // Delete the account.
            if (AccountManager.DeleteAccount(accountId) == false)
            {
                validationResult.AddError(accountId.ToString(), "Error while deleting account: " + accountId.ToString());
            }

            try
            {
                // Delete the account setting profile.
                var settingProfile = this.GetAccountSettingProfile(accountId);
                if (AccountManager.DeleteSettingProfile(accountId, settingProfile.SettingProfileId) == false)
                {
                    validationResult.AddError(settingProfile.SettingProfileId.ToString(), "Error while deleting setting profile: " + settingProfile.SettingProfileId.ToString());
                }
            }
            catch (System.ArgumentException e)
            {
                // The setting profile did not exist. Add an error to the validation result object.
                validationResult.AddError("SettingProfile.Exists" + accountId.ToString(), "The setting profile did not exist for account id: " + accountId.ToString());
            }
            catch (Exception)
            {
                throw;
            }

            return validationResult;
        }
        /// <summary>
        /// Updates a ship fit for a particular account.
        /// </summary>
        /// <param name="shipFit">A partially populated ship fit object to be updated.</param>
        /// <returns>Returns a validation result object.</returns>
        internal IValidationResult UpdateShipFit(ShipFit shipFit)
        {
            IValidationResult validationResult = new ValidationResult();

            var existingShipFit = this.doctrineShipsRepository.GetShipFit(shipFit.ShipFitId);

            if (existingShipFit != null)
            {
                if (existingShipFit.AccountId != shipFit.AccountId)
                {
                    validationResult.AddError("ShipFit.Permission", "The ship fit being modified does not belong to the requesting account.");
                }
                else
                {
                    // Map the updates to the existing ship fit.
                    existingShipFit.Name = shipFit.Name;
                    existingShipFit.Role = shipFit.Role;
                    existingShipFit.Notes = shipFit.Notes;

                    // Validate the ship fit updates.
                    validationResult = this.doctrineShipsValidation.ShipFit(existingShipFit);
                    if (validationResult.IsValid == true)
                    {
                        // Update the existing record, save and log.
                        this.doctrineShipsRepository.UpdateShipFit(existingShipFit);
                        this.doctrineShipsRepository.Save();
                        logger.LogMessage("Ship Fit '" + existingShipFit.Name + "' Successfully Updated For Account Id: " + existingShipFit.AccountId, 2, "Message", MethodBase.GetCurrentMethod().Name);
                    }
                }
            }

            return validationResult;
        }
        /// <summary>
        /// Updates a notification recipient for a particular account.
        /// </summary>
        /// <param name="notificationRecipient">A partially populated notification recipient object to be updated.</param>
        /// <returns>Returns a validation result object.</returns>
        internal IValidationResult UpdateNotificationRecipient(NotificationRecipient notificationRecipient)
        {
            IValidationResult validationResult = new ValidationResult();

            var existingNotificationRecipient = this.doctrineShipsRepository.GetNotificationRecipient(notificationRecipient.NotificationRecipientId);

            if (existingNotificationRecipient != null)
            {
                if (existingNotificationRecipient.AccountId != notificationRecipient.AccountId)
                {
                    validationResult.AddError("NotificationRecipient.Permission", "The notification recipient being modified does not belong to the requesting account.");
                }
                else
                {
                    // Map the updates to the existing notification recipient.
                    existingNotificationRecipient.AlertIntervalHours = notificationRecipient.AlertIntervalHours;
                    existingNotificationRecipient.ReceivesAlerts = notificationRecipient.ReceivesAlerts;
                    existingNotificationRecipient.ReceivesDailySummary = notificationRecipient.ReceivesDailySummary;

                    // Validate the notification recipient updates.
                    validationResult = this.doctrineShipsValidation.NotificationRecipient(existingNotificationRecipient);
                    if (validationResult.IsValid == true)
                    {
                        // Update the existing record, save and log.
                        this.doctrineShipsRepository.UpdateNotificationRecipient(existingNotificationRecipient);
                        this.doctrineShipsRepository.Save();
                        logger.LogMessage("Notification Recipient '" + existingNotificationRecipient.Description + "' Successfully Updated For Account Id: " + existingNotificationRecipient.AccountId, 2, "Message", MethodBase.GetCurrentMethod().Name);
                    }
                }
            }

            return validationResult;
        }
        /// <summary>
        /// Updates the subscription plan for an account.
        /// </summary>
        /// <param name="accountId">The account Id being changed.</param>
        /// <param name="subscriptionPlanId">The id of the new subscription plan.</param>
        /// <returns>Returns a validation result object.</returns>
        internal IValidationResult UpdateAccountSubscriptionPlan(int accountId, int subscriptionPlanId)
        {
            IValidationResult validationResult = new ValidationResult();

            Account account = this.doctrineShipsRepository.GetAccount(accountId);

            if (account != null)
            {
                // Change the subscription plan of the account and validate.
                account.SubscriptionPlanId = subscriptionPlanId;

                validationResult = this.doctrineShipsValidation.Account(account);
                if (validationResult.IsValid == true)
                {
                    // Update the account and log the event.
                    this.doctrineShipsRepository.UpdateAccount(account);
                    this.doctrineShipsRepository.Save();
                    logger.LogMessage("Subscription Plan Successfully Changed For Account: '" + account.Description + "'.", 2, "Message", MethodBase.GetCurrentMethod().Name);
                }
            }
            else
            {
                validationResult.AddError("AccountId.Null", "The account does not exist in the database.");
            }

            return validationResult;
        }
        /// <summary>
        /// <para>Adds a subscription plan.</para>
        /// </summary>
        /// <param name="subscriptionPlan">A populated subscription plan object.</param>
        /// <returns>Returns a validation result object.</returns>
        internal IValidationResult AddSubscriptionPlan(SubscriptionPlan subscriptionPlan)
        {
            IValidationResult validationResult = new ValidationResult();

            // Validate the new subscription plan.
            validationResult = this.doctrineShipsValidation.SubscriptionPlan(subscriptionPlan);
            if (validationResult.IsValid == true)
            {
                // Add the new subscription plan.
                this.doctrineShipsRepository.CreateSubscriptionPlan(subscriptionPlan);
                this.doctrineShipsRepository.Save();

                // Log the addition.
                logger.LogMessage("Subscription Plan '" + subscriptionPlan.Name + "' Successfully Added.", 2, "Message", MethodBase.GetCurrentMethod().Name);
            }

            return validationResult;
        }
        /// <summary>
        /// <para>Adds a notification recipient for an account.</para>
        /// </summary>
        /// <param name="accountId">The id of the account for which a recipient should be added.</param>
        /// <param name="twitterHandle">The twitter handle for the recipient.</param>
        /// <param name="description">A short description for the recipient.</param>
        /// <returns>Returns a validation result object.</returns>
        internal IValidationResult AddNotificationRecipient(int accountId, string twitterHandle, string description)
        {
            IValidationResult validationResult = new ValidationResult();

            // Populate a new account object.
            NotificationRecipient newNotificationRecipient = new NotificationRecipient();

            // Populate the remaining properties.
            newNotificationRecipient.AccountId = accountId;
            newNotificationRecipient.TwitterHandle = twitterHandle;
            newNotificationRecipient.Description = description;
            newNotificationRecipient.ReceivesDailySummary = true;
            newNotificationRecipient.ReceivesAlerts = true;
            newNotificationRecipient.AlertIntervalHours = 12;
            newNotificationRecipient.Method = NotificationMethod.DirectMessage;
            newNotificationRecipient.LastAlert = DateTime.UtcNow;

            // Validate the new notification recipient.
            validationResult = this.doctrineShipsValidation.NotificationRecipient(newNotificationRecipient);
            if (validationResult.IsValid == true)
            {
                // Add the new notification recipient.
                this.doctrineShipsRepository.CreateNotificationRecipient(newNotificationRecipient);
                this.doctrineShipsRepository.Save();

                // Log the addition.
                logger.LogMessage("Notification Recipient '" + newNotificationRecipient.Description + "' Successfully Added For Account Id: " + newNotificationRecipient.AccountId, 2, "Message", MethodBase.GetCurrentMethod().Name);
            }

            return validationResult;
        }
        /// <summary>
        /// <para>Adds an account with a default setting profile and an account admin access code.</para>
        /// </summary>
        /// <param name="description">A short description for the new account.</param>
        /// <param name="subscriptionPlanId">The subscription plan for the new account.</param>
        /// <param name="generatedKey">An out string parameter containing the account admin key or an emptry string on failure.</param>
        /// <param name="newAccountId">An out int parameter containing the new account id or 0 on failure.</param>
        /// <returns>Returns a validation result object.</returns>
        internal IValidationResult AddAccount(string description, int subscriptionPlanId, out string generatedKey, out int newAccountId)
        {
            generatedKey = string.Empty;
            newAccountId = 0;
            IValidationResult validationResult = new ValidationResult();

            // Populate a new account object.
            Account newAccount = new Account();

            // Populate the remaining properties.
            newAccount.Description = description;
            newAccount.DateCreated = DateTime.UtcNow;
            newAccount.LastCredit = DateTime.UtcNow;
            newAccount.LastDebit = DateTime.UtcNow;
            newAccount.Balance = 0;
            newAccount.IsActive = true;
            newAccount.SettingProfileId = 0;
            newAccount.SubscriptionPlanId = subscriptionPlanId;

            // Validate the new account.
            validationResult = this.doctrineShipsValidation.Account(newAccount);
            if (validationResult.IsValid == true)
            {
                // Add the new account and read it back to get the account id.
                newAccount = this.doctrineShipsRepository.CreateAccount(newAccount);
                this.doctrineShipsRepository.Save();

                // Assign the new account id to the passed out parameter.
                newAccountId = newAccount.AccountId;

                // Add a default account admin access code for the account and capture the generated key.
                generatedKey = this.AddAccessCode(newAccount.AccountId, "Default Account Admin", Role.Admin);

                // Create a default setting profile with the new account Id and assign it to the account.
                newAccount.SettingProfileId = this.AddDefaultSettingProfile(newAccount.AccountId);
                this.doctrineShipsRepository.UpdateAccount(newAccount);
                this.doctrineShipsRepository.Save();

                // Log the addition.
                logger.LogMessage("Account '" + newAccount.Description + "' Successfully Added.", 2, "Message", MethodBase.GetCurrentMethod().Name);
            }

            return validationResult;
        }