public void InvitesAdd(InviteRequestDTO item)
 {
     if (Database.Persons.GetAll().FirstOrDefault(x => x.Login == item.Login).InvitesList.FirstOrDefault(x => x.Inviter_Login == item.InviterLogin) == null)
     {
         Database.Persons.GetAll().FirstOrDefault(x => x.Login == item.Login).InvitesList.Add(new Invite()
         {
             Invited_Login = item.Login, Inviter_Login = item.InviterLogin
         });
     }
     Database.Save();
 }
        private static void SendInvitationRequest(IOrganizationService service, ITracingService trace, InviteRequestDTO request)
        {
            //Get the application settings
            var xFunctionKey      = SettingHelper.RetrieveSetting(trace, SettingHelper.SecureConfigKeys.InviteFunctionKey);
            var xFunctionEndPoint = SettingHelper.RetrieveSetting(trace, SettingHelper.SecureConfigKeys.InviteEndPoint);

            trace.Trace($"Sending request to Invite Azure Function: {xFunctionEndPoint}");
            trace.Trace($"Function key - {xFunctionKey}");

            // Call mail API via APIM.
            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Add("x-functions-key", xFunctionKey);

                var content = new StringContent(GlobalHelper.JsonSerializer <InviteRequestDTO>(request), Encoding.UTF8, "application/json");

                var response = httpClient.PostAsync(
                    xFunctionEndPoint, content).GetAwaiter().GetResult();

                if (response.IsSuccessStatusCode)
                {
                    trace.Trace($"Success: invitation request is successfully send.");
                }
                else
                {
                    var errorContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();

                    trace.Trace($"Error: invitation request failed - {errorContent}");
                }
            }
        }
        public static void SendInvitation(IOrganizationService service, ITracingService trace, Entity entity,
                                          Entity image)
        {
            try
            {
                // Check if the entity contains status and email attributes
                if (!image.Contains(CrmContext.Contact.EmailAddress1) ||
                    !image.Contains(CrmContext.Base.StateCode))
                {
                    return;
                }

                trace.Trace("Start processing contact invitation");

                var status = entity.GetAttributeValue <OptionSetValue>(CrmContext.Base.StateCode);

                // Check if status is active
                if (status.Value == CrmContext.StateCodes.Active)
                {
                    trace.Trace("Status of contact is active");

                    trace.Trace("Retrieve contact information");
                    var emailAddress = image.GetAttributeValue <string>(CrmContext.Contact.EmailAddress1);
                    var firstName    = image.GetAttributeValue <string>(CrmContext.Contact.FirstName);
                    var lastName     = image.GetAttributeValue <string>(CrmContext.Contact.LastName);
                    var accountId    = image.GetAttributeValue <EntityReference>(CrmContext.Contact.AccountId);

                    if (accountId == null || accountId.Id == Guid.Empty)
                    {
                        trace.Trace("No account information found for this contact.");
                        return;
                    }

                    var account     = AccountHelper.RetrieveAccount(service, trace, accountId);
                    var accountName = account.GetAttributeValue <string>(CrmContext.Account.NameNl);

                    trace.Trace("Sending invite to the API");

                    // Create invite
                    var invite = new InviteRequestDTO
                    {
                        FirstName    = firstName,
                        LastName     = lastName,
                        EmailAddress = emailAddress,
                        RequestDate  = DateTime.Now,
                        AccountId    = accountId.Id.ToString(),
                        AccountName  = accountName
                    };

                    SendInvitationRequest(service, trace, invite);
                }
            }
            catch (FaultException <OrganizationServiceFault> ex)
            {
                throw new InvalidPluginExecutionException(
                          $"An error occurrd in the organizationservice. {ex.Detail.Message}");
            }
            catch (TimeoutException ex)
            {
                throw new InvalidPluginExecutionException($"A timeout occurred. {ex.Message}");
            }
            catch (FaultException ex)
            {
                throw new InvalidPluginExecutionException($"An error occurrd. {ex.Message}");
            }
        }