public Task <Result <string> > Send(UserInvitationData prefilledData, UserInvitationTypes invitationType,
                                            int inviterUserId, int?inviterAgencyId = null)
        {
            return(Validate()
                   .Bind(CreateInvitation)
                   .Check(SendInvitationMailPipe));

            Task <Result> SendInvitationMailPipe(string invitationCode)
            => SendInvitationMail(invitationCode, prefilledData, invitationType, inviterAgencyId);


            Result Validate()
            => GenericValidator <UserInvitationData> .Validate(v =>
            {
                v.RuleFor(x => x.UserRegistrationInfo.FirstName).NotEmpty()
                .WithMessage("FirstName is required");
                v.RuleFor(x => x.UserRegistrationInfo.LastName).NotEmpty()
                .WithMessage("LastName is required");
                v.RuleFor(x => x.UserRegistrationInfo.Title).NotEmpty()
                .WithMessage("Title is required");
                v.RuleFor(e => e.UserRegistrationInfo.Email).NotEmpty().EmailAddress()
                .WithMessage("Valid email is required");
            }, prefilledData);


            Task <Result <string> > CreateInvitation()
            => Create(prefilledData, invitationType, inviterUserId, inviterAgencyId);
        }
Пример #2
0
 public Task <Result> Send <TInvitationData>(string email, TInvitationData invitationInfo,
                                             Func <TInvitationData, string, DataWithCompanyInfo> messagePayloadGenerator, string mailTemplateId,
                                             UserInvitationTypes invitationType)
 {
     SentInvitationInfo = invitationInfo;
     return(Task.FromResult(Result.Success()));
 }
Пример #3
0
        public async Task <Result> Send <TInvitationData>(string email,
                                                          TInvitationData invitationInfo,
                                                          Func <TInvitationData, string, DataWithCompanyInfo> messagePayloadGenerator,
                                                          string mailTemplateId,
                                                          UserInvitationTypes invitationType)
        {
            var invitationCode = GenerateRandomCode();

            return(await SendInvitationMail()
                   .Tap(SaveInvitation)
                   .Tap(LogInvitationCreated));

            Task <Result> SendInvitationMail()
            {
                var messagePayload = messagePayloadGenerator(invitationInfo, invitationCode);

                return(_mailSender.Send(mailTemplateId,
                                        email,
                                        messagePayload));
            }

            Task SaveInvitation() => SaveInvitationData(email, invitationInfo, invitationType, invitationCode);

            void LogInvitationCreated() => this.LogInvitationCreated(email);
        }
Пример #4
0
 public AgentInvitationInfo(UserDescriptionInfo userRegistrationInfo, RegistrationAgencyInfo?childAgencyRegistrationInfo,
                            UserInvitationTypes userInvitationType, int agencyId, int agentId, string email)
 {
     UserRegistrationInfo        = userRegistrationInfo;
     ChildAgencyRegistrationInfo = childAgencyRegistrationInfo;
     UserInvitationType          = userInvitationType;
     AgencyId = agencyId;
     Email    = email;
     AgentId  = agentId;
 }
        public Task <Result <string> > Create(UserInvitationData prefilledData, UserInvitationTypes invitationType,
                                              int inviterUserId, int?inviterAgencyId = null)
        {
            var invitationCode = GenerateRandomCode();
            var now            = _dateTimeProvider.UtcNow();

            return(Result.Success()
                   .Ensure(AllProvidedRolesExist, "All roles should exist")
                   .Bind(SaveInvitation)
                   .Tap(LogInvitationCreated)
                   .Map(_ => invitationCode));


            string GenerateRandomCode()
            {
                using var provider = new RNGCryptoServiceProvider();

                var byteArray = new byte[64];

                provider.GetBytes(byteArray);

                return(Base64UrlEncoder.Encode(byteArray));
            }

            async Task <bool> AllProvidedRolesExist()
            {
                var allRoleIds = await _context.AgentRoles.Select(r => r.Id).ToListAsync();

                return(prefilledData.RoleIds.All(x => allRoleIds.Contains(x)));
            }

            async Task <Result <UserInvitation> > SaveInvitation()
            {
                var newInvitation = new UserInvitation
                {
                    CodeHash         = HashGenerator.ComputeSha256(invitationCode),
                    Email            = prefilledData.UserRegistrationInfo.Email,
                    Created          = now,
                    InviterUserId    = inviterUserId,
                    InviterAgencyId  = inviterAgencyId,
                    InvitationType   = invitationType,
                    InvitationStatus = UserInvitationStatuses.Active,
                    Data             = JsonConvert.SerializeObject(prefilledData)
                };

                _context.UserInvitations.Add(newInvitation);

                await _context.SaveChangesAsync();

                return(newInvitation);
            }

            void LogInvitationCreated()
            => _logger.LogInvitationCreated(invitationType, prefilledData.UserRegistrationInfo.Email);
        }
        private async Task <Result> SendInvitationMail(string invitationCode, UserInvitationData prefilledData,
                                                       UserInvitationTypes invitationType, int?inviterAgencyId)
        {
            string agencyName = null;

            if (inviterAgencyId.HasValue)
            {
                var getAgencyResult = await _agencyManagementService.Get(inviterAgencyId.Value);

                if (getAgencyResult.IsFailure)
                {
                    return(Result.Failure("Could not find inviter agency"));
                }

                agencyName = getAgencyResult.Value.Name;
            }

            var messagePayload = new InvitationData
            {
                AgencyName       = agencyName,
                InvitationCode   = invitationCode,
                UserEmailAddress = prefilledData.UserRegistrationInfo.Email,
                UserName         = $"{prefilledData.UserRegistrationInfo.FirstName} {prefilledData.UserRegistrationInfo.LastName}"
            };

            var notificationType = invitationType switch
            {
                UserInvitationTypes.Agent => NotificationTypes.AgentInvitation,
                UserInvitationTypes.ChildAgency => NotificationTypes.ChildAgencyInvitation,
                _ => NotificationTypes.None
            };

            return(await _notificationService.Send(messageData : messagePayload,
                                                   notificationType : notificationType,
                                                   email : prefilledData.UserRegistrationInfo.Email));
        }
Пример #7
0
 public Task <Result <TInvitationData> > GetPendingInvitation <TInvitationData>(string invitationCode, UserInvitationTypes invitationType)
 => throw new NotImplementedException();
Пример #8
0
 public Task <Result <string> > Create <TInvitationData>(string email, TInvitationData invitationInfo, UserInvitationTypes invitationType)
 {
     CreatedInvitationInfo = invitationInfo;
     return(Task.FromResult(Result.Success(string.Empty)));
 }
Пример #9
0
 public Task <Result <TInvitationData> > GetPendingInvitation <TInvitationData>(string invitationCode, UserInvitationTypes invitationType)
 {
     return(GetInvitation(invitationCode).ToResult("Could not find invitation")
            .Ensure(IsNotAccepted, "Already accepted")
            .Ensure(IsNotResent, "Already resent")
            .Ensure(HasCorrectType, "Invitation type mismatch")
            .Ensure(InvitationIsActual, "Invitation expired")
            .Map(GetInvitationData <TInvitationData>));
Пример #10
0
        public Task <Result <string> > Create <TInvitationData>(string email, TInvitationData invitationInfo, UserInvitationTypes invitationType)
        {
            var invitationCode = GenerateRandomCode();

            return(SaveInvitation()
                   .Tap(LogInvitationCreated)
                   .Map(ReturnCode));

            async Task <Result> SaveInvitation()
            {
                await SaveInvitationData(email, invitationInfo, invitationType, invitationCode);

                return(Result.Success());
            }

            void LogInvitationCreated() => this.LogInvitationCreated(email);

            string ReturnCode() => invitationCode;
        }