Esempio n. 1
0
 public UserInvitationData(UserDescriptionInfo userRegistrationInfo,
                           // Think about removing such a special property from there
                           RegistrationAgencyInfo childAgencyRegistrationInfo,
                           int[] roleIds)
 {
     UserRegistrationInfo        = userRegistrationInfo;
     ChildAgencyRegistrationInfo = childAgencyRegistrationInfo;
     RoleIds = roleIds;
 }
        public Task <Result> RegisterWithAgency(UserDescriptionInfo agentData, RegistrationAgencyInfo registrationAgencyInfo, string externalIdentity,
                                                string email)
        {
            return(Result.Success()
                   .Ensure(IsIdentityPresent, "User should have identity")
                   .Bind(Validate)
                   .BindWithTransaction(_context, () => Result.Success()
                                        .Bind(CreateRootAgency)
                                        .Bind(CreateAgent)
                                        .Tap(AddMasterAgentAgencyRelation))
                   .Bind(LogSuccess)
                   .Bind(SendRegistrationMailToAdmins)
                   .OnFailure(LogFailure));


            bool IsIdentityPresent() => !string.IsNullOrWhiteSpace(externalIdentity);


            Result Validate()
            => AgencyValidator.Validate(registrationAgencyInfo);


            Task <Result <AgencyInfo> > CreateRootAgency()
            => _agencyService.Create(registrationAgencyInfo, parentAgencyId: null);


            async Task <Result <(AgencyInfo, Agent)> > CreateAgent(AgencyInfo agency)
            {
                var(_, isFailure, agent, error) = await _agentService.Add(agentData, externalIdentity, email);

                return(isFailure
                    ? Result.Failure <(AgencyInfo, Agent)>(error)
                    : Result.Success((agency, agent)));
            }

            async Task AddMasterAgentAgencyRelation((AgencyInfo agency, Agent agent) agencyAgentInfo)
            {
                var(agency, agent) = agencyAgentInfo;

                // assign all roles to master agent
                var roleIds = await _context.AgentRoles.Select(x => x.Id).ToArrayAsync();

                await AddAgentAgencyRelation(agent,
                                             AgentAgencyRelationTypes.Master,
                                             agency.Id.Value,
                                             roleIds);
            }

            async Task <Result> SendRegistrationMailToAdmins(AgencyInfo agency)
            {
                var agent = $"{agentData.Title} {agentData.FirstName} {agentData.LastName}";

                if (!string.IsNullOrWhiteSpace(agentData.Position))
                {
                    agent += $" ({agentData.Position})";
                }

                var messageData = new RegistrationDataForAdmin
                {
                    Agency = new RegistrationDataForAdmin.RootAgencyRegistrationMailData
                    {
                        Id                     = agency.Id.ToString(),
                        Name                   = agency.Name,
                        CountryCode            = agency.CountryCode,
                        City                   = agency.City,
                        Address                = agency.Address,
                        Phone                  = agency.Phone,
                        PostalCode             = agency.PostalCode,
                        Fax                    = agency.Fax,
                        PreferredCurrency      = EnumFormatters.FromDescription(agency.PreferredCurrency),
                        PreferredPaymentMethod = EnumFormatters.FromDescription(agency.PreferredPaymentMethod),
                        Website                = agency.Website
                    },
                    AgentEmail = email,
                    AgentName  = agent
                };

                return(await _notificationService.Send(messageData : messageData,
                                                       notificationType : NotificationTypes.MasterAgentSuccessfulRegistration,
                                                       emails : _notificationOptions.AdministratorsEmails));
            }

            Result <AgencyInfo> LogSuccess((AgencyInfo, Agent) registrationData)
            {
                var(agency, agent) = registrationData;
                _logger.LogAgentRegistrationSuccess(agent.Email);
                return(Result.Success(agency));
            }

            void LogFailure(string error)
            {
                _logger.LogAgentRegistrationFailed(error);
            }
        }
Esempio n. 3
0
 public RegisterInvitedAgencyRequest(UserDescriptionInfo registrationInfo, RegistrationAgencyInfo childAgencyRegistrationInfo, string invitationCode)
 {
     RegistrationInfo            = registrationInfo;
     ChildAgencyRegistrationInfo = childAgencyRegistrationInfo;
     InvitationCode = invitationCode;
 }
 public RegisterAgentWithAgencyRequest(UserDescriptionInfo agent, RegistrationAgencyInfo agency)
 {
     Agent  = agent;
     Agency = agency;
 }
Esempio n. 5
0
 public async Task <Result <AgencyInfo> > Create(RegistrationAgencyInfo agencyInfo, int?parentAgencyId)
 => await Create(agencyInfo.Name, agencyInfo.Address, agencyInfo.BillingEmail, agencyInfo.Fax,
                 agencyInfo.Phone, agencyInfo.PostalCode, agencyInfo.Website, agencyInfo.VatNumber,
                 parentAgencyId, agencyInfo.LegalAddress, agencyInfo.PreferredPaymentMethod, agencyInfo.LocalityHtId);