/// <summary>
        /// Refresh the contracts of the sales agents with the oldest refresh time and update contract counts.
        /// </summary>
        /// <param name="force">Indicates that all sales agents should be refreshed, ignoring their LastContractRefresh values.</param>
        /// <param name="batchSize">The number of sales agents to refresh at a time.</param>
        public void RefreshContracts(bool force, int batchSize)
        {
            // Refresh the contracts.
            ContractManager.RefreshContracts(force, batchSize);

            // Refresh the number of valid contracts available for each ship fit.
            ShipFitManager.RefreshShipFitContractCounts();

            // Refresh the number of contracts available for each sales agent.
            SalesAgentManager.RefreshSalesAgentContractCounts();
        }
        /// <summary>
        /// Forces a contract refresh for a single sales agent. This operation is only permitted once every 30 minutes.
        /// </summary>
        /// <param name="accountId">The account Id of the requestor. The account Id should own the sales agent being refreshed.</param>
        /// <param name="salesAgentId">The id of the sales agent for which a contract refresh is to be forced.</param>
        /// <returns>Returns true if the force was successful or false if not.</returns>
        public bool ForceContractRefresh(int accountId, int salesAgentId)
        {
            // Force a contract refresh for this sales agent and store the result.
            var result = ContractManager.ForceContractRefresh(accountId, salesAgentId);

            // Refresh the number of valid contracts available for each ship fit.
            ShipFitManager.RefreshShipFitContractCounts();

            // Refresh the number of contracts available for each sales agent.
            SalesAgentManager.RefreshSalesAgentContractCounts();

            return(result);
        }
        /// <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>
        public async Task <IValidationResult> AddSalesAgent(int apiId, string apiKey, int accountId)
        {
            IValidationResult validationResult;

            validationResult = SalesAgentManager.AddSalesAgent(apiId, apiKey, accountId);

            // If the sales agent addition was successful, notify the account twitter account.
            if (validationResult.IsValid)
            {
                SettingProfile settingProfile = AccountManager.GetAccountSettingProfile(accountId);

                await TaskManager.SendDirectMessage(this.doctrineShipsSettings.TwitterContext,
                                                    settingProfile.TwitterHandle,
                                                    "A Sales Agent Was Added To Account: " + accountId);
            }

            return(validationResult);
        }
        /// <summary>
        /// Perform daily maintenance tasks.
        /// </summary>
        public async Task DailyMaintenance()
        {
            // Delete any contracts where the expired date has passed.
            ContractManager.DeleteExpiredContracts();

            // Deletes log entries older than 7 days.
            TaskManager.DeleteOldLogs();

            // Deletes expired access codes.
            AccountManager.DeleteExpiredAccessCodes();

            // Debit any accounts with subscription payments that are due.
            AccountManager.DebitDueAccounts();

            // Deletes active sales agents that have not had a successful contract refresh in the last 7 days.
            SalesAgentManager.DeleteStaleSalesAgents();

            // Send out daily ship fit availability summaries for all accounts.
            await TaskManager.SendDailySummary(doctrineShipsSettings.TwitterContext);
        }
 /// <summary>
 /// Updates the state of a sales agent.
 /// </summary>
 /// <param name="accountId">The account Id of the requestor. The account Id should own the sales agent being changed.</param>
 /// <param name="salesAgentId">The id of the sales agent to be changed.</param>
 /// <param name="isActive">The required boolean state.</param>
 /// <returns>Returns true if the change was successful or false if not.</returns>
 public bool UpdateSalesAgentState(int accountId, int salesAgentId, bool isActive)
 {
     return(SalesAgentManager.UpdateSalesAgentState(accountId, salesAgentId, isActive));
 }
 /// <summary>
 /// <para>Deletes a sales agent from an accountId and a salesAgentId.</para>
 /// </summary>
 /// <param name="accountId">The account Id of the requestor. The account Id should own the sales agent being deleted.</param>
 /// <param name="salesAgent">The Id of the sales agent to be deleted.</param>
 /// <returns>Returns true if the deletion was successful or false if not.</returns>
 public bool DeleteSalesAgent(int accountId, int salesAgentId)
 {
     return(SalesAgentManager.DeleteSalesAgent(accountId, salesAgentId));
 }
        /// <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 doctrines.
            var accountDoctrines = ShipFitManager.GetDoctrineList(accountId);

            foreach (var doctrine in accountDoctrines)
            {
                if (ShipFitManager.DeleteDoctrine(accountId, doctrine.DoctrineId) == false)
                {
                    validationResult.AddError(doctrine.DoctrineId.ToString(), "Error while deleting doctrine: " + doctrine.DoctrineId.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>
 /// Fetches and returns a list of all Doctrine Ships sales agents for a particular account.
 /// </summary>
 /// <param name="accountId">The account for which sales agents should be returned.</param>
 /// <returns>A list of sales agents objects.</returns>
 public IEnumerable <SalesAgent> GetSalesAgents(int accountId)
 {
     return(SalesAgentManager.GetSalesAgents(accountId));
 }
 /// <summary>
 /// Fetches and returns a Doctrine Ships sales agent.
 /// </summary>
 /// <param name="salesAgentId">The id of the sales agent for which a sales agent object should be returned.</param>
 /// <returns>A sales agents object.</returns>
 public SalesAgent GetSalesAgent(int salesAgentId)
 {
     return(SalesAgentManager.GetSalesAgent(salesAgentId));
 }
 /// <summary>
 /// Returns a list of contracts for a given sales agent.
 /// </summary>
 /// <param name="salesAgentId">The id of the sales agent for which contracts should be returned.</param>
 /// <returns>A list of sales agent contract objects.</returns>
 public IEnumerable <Contract> GetSalesAgentContracts(int salesAgentId)
 {
     return(SalesAgentManager.GetSalesAgentContracts(salesAgentId));
 }