private async Task <List <BuilderParticipant> > AddPersonParticipantsWithOidsAsync(
            Invitation invitation,
            List <BuilderParticipant> meetingParticipants,
            List <ParticipantsForCommand> personParticipantsWithOids)
        {
            var personsAdded = new List <ParticipantsForCommand>();

            foreach (var participant in personParticipantsWithOids)
            {
                if (InvitationHelper.ParticipantIsSigningParticipant(participant))
                {
                    meetingParticipants = await AddSigner(
                        invitation,
                        meetingParticipants,
                        participant.InvitedPerson,
                        participant.SortKey,
                        participant.Organization);

                    personsAdded.Add(participant);
                }
            }

            personParticipantsWithOids.RemoveAll(p => personsAdded.Contains(p));

            var oids    = personParticipantsWithOids.Where(p => p.SortKey > 1).Select(p => p.InvitedPerson.AzureOid.ToString()).ToList();
            var persons = oids.Count > 0
                ? await _personApiService.GetPersonsByOidsAsync(_plantProvider.Plant, oids)
                : new List <ProCoSysPerson>();

            if (persons.Any())
            {
                foreach (var participant in personParticipantsWithOids)
                {
                    var person = persons.SingleOrDefault(p => p.AzureOid == participant.InvitedPerson.AzureOid.ToString());
                    if (person != null)
                    {
                        invitation.AddParticipant(new Participant(
                                                      _plantProvider.Plant,
                                                      participant.Organization,
                                                      IpoParticipantType.Person,
                                                      null,
                                                      person.FirstName,
                                                      person.LastName,
                                                      person.UserName,
                                                      person.Email,
                                                      new Guid(person.AzureOid),
                                                      participant.SortKey));
                        meetingParticipants = InvitationHelper.AddPersonToOutlookParticipantList(person, meetingParticipants);
                    }
                }
            }

            return(meetingParticipants);
        }
        private async Task <List <BuilderParticipant> > AddSigner(
            Invitation invitation,
            List <BuilderParticipant> meetingParticipants,
            IList <Participant> existingParticipants,
            InvitedPersonForEditCommand person,
            int sortKey,
            Organization organization)
        {
            var personFromMain = await _personApiService.GetPersonByOidWithPrivilegesAsync(_plantProvider.Plant,
                                                                                           person.AzureOid.ToString(), _objectName, _signerPrivileges);

            if (personFromMain != null)
            {
                var existingParticipant = existingParticipants.SingleOrDefault(p => p.Id == person.Id);
                if (existingParticipant != null)
                {
                    invitation.UpdateParticipant(
                        existingParticipant.Id,
                        organization,
                        IpoParticipantType.Person,
                        null,
                        personFromMain.FirstName,
                        personFromMain.LastName,
                        personFromMain.Email,
                        new Guid(personFromMain.AzureOid),
                        sortKey,
                        person.RowVersion);
                }
                else
                {
                    invitation.AddParticipant(new Participant(
                                                  _plantProvider.Plant,
                                                  organization,
                                                  IpoParticipantType.Person,
                                                  null,
                                                  personFromMain.FirstName,
                                                  personFromMain.LastName,
                                                  personFromMain.UserName,
                                                  personFromMain.Email,
                                                  new Guid(personFromMain.AzureOid),
                                                  sortKey));
                }
                meetingParticipants = InvitationHelper.AddPersonToOutlookParticipantList(personFromMain, meetingParticipants);
            }
            else
            {
                throw new IpoValidationException($"Person does not have required privileges to be the {organization} participant.");
            }
            return(meetingParticipants);
        }
        private async Task <List <BuilderParticipant> > AddSigner(
            Invitation invitation,
            List <BuilderParticipant> meetingParticipants,
            IInvitedPersonForCommand invitedSigner,
            int sortKey,
            Organization organization)
        {
            var person = await _personApiService.GetPersonByOidWithPrivilegesAsync(
                _plantProvider.Plant,
                invitedSigner.AzureOid.ToString(),
                _objectName,
                _signerPrivileges);

            if (person != null)
            {
                invitation.AddParticipant(new Participant(
                                              _plantProvider.Plant,
                                              organization,
                                              IpoParticipantType.Person,
                                              null,
                                              person.FirstName,
                                              person.LastName,
                                              person.UserName,
                                              person.Email,
                                              new Guid(person.AzureOid),
                                              sortKey));
                meetingParticipants = InvitationHelper.AddPersonToOutlookParticipantList(person, meetingParticipants);
            }
            else
            {
                throw new IpoValidationException(
                          $"Person does not have required privileges to be the {organization} participant.");
            }

            return(meetingParticipants);
        }
        private async Task <List <BuilderParticipant> > AddFunctionalRoleParticipantsAsync(
            Invitation invitation,
            List <BuilderParticipant> meetingParticipants,
            List <ParticipantsForCommand> functionalRoleParticipants)
        {
            var codes           = functionalRoleParticipants.Select(p => p.InvitedFunctionalRole.Code).ToList();
            var functionalRoles =
                await _functionalRoleApiService.GetFunctionalRolesByCodeAsync(_plantProvider.Plant, codes);

            foreach (var participant in functionalRoleParticipants)
            {
                var fr = functionalRoles.SingleOrDefault(p => p.Code == participant.InvitedFunctionalRole.Code);
                if (fr != null)
                {
                    invitation.AddParticipant(new Participant(
                                                  _plantProvider.Plant,
                                                  participant.Organization,
                                                  IpoParticipantType.FunctionalRole,
                                                  fr.Code,
                                                  null,
                                                  null,
                                                  null,
                                                  fr.Email,
                                                  null,
                                                  participant.SortKey));
                    if (fr.UsePersonalEmail != null && fr.UsePersonalEmail == false && fr.Email != null)
                    {
                        meetingParticipants.AddRange(InvitationHelper.SplitAndCreateOutlookParticipantsFromEmailList(fr.Email));
                    }
                    if (fr.InformationEmail != null)
                    {
                        meetingParticipants.AddRange(InvitationHelper.SplitAndCreateOutlookParticipantsFromEmailList(fr.InformationEmail));
                    }
                    foreach (var person in participant.InvitedFunctionalRole.InvitedPersons)
                    {
                        var frPerson = fr.Persons.SingleOrDefault(p => p.AzureOid == person.AzureOid.ToString());
                        if (frPerson != null)
                        {
                            invitation.AddParticipant(new Participant(
                                                          _plantProvider.Plant,
                                                          participant.Organization,
                                                          IpoParticipantType.Person,
                                                          fr.Code,
                                                          frPerson.FirstName,
                                                          frPerson.LastName,
                                                          frPerson.UserName,
                                                          frPerson.Email,
                                                          new Guid(frPerson.AzureOid),
                                                          participant.SortKey));
                            meetingParticipants = InvitationHelper.AddPersonToOutlookParticipantList(frPerson, meetingParticipants, person.Required);
                        }
                    }
                }
                else
                {
                    throw new IpoValidationException(
                              $"Could not find functional role with functional role code '{participant.InvitedFunctionalRole.Code}' on participant {participant.Organization}.");
                }
            }
            return(meetingParticipants);
        }