示例#1
0
        public async Task Import()
        {
            var backupData = await ReadFile();

            using var tx = db.Database.BeginTransaction();

            var teamMap = await ImportTeams(backupData);

            var phoneNumberMap = await ImportUsers(backupData, teamMap);

            await ImportHistory(backupData, teamMap, phoneNumberMap);
            await ImportSchedule(backupData, teamMap, phoneNumberMap);

            await db.SaveChangesAsync();

            tx.Commit();
        }
示例#2
0
            public async Task Handle(Command message)
            {
                var original = db.ScheduledForwards.Find(message.Id);

                if (original.ScheduleId != null)
                {
                    BackgroundJob.Delete(original.ScheduleId);
                    original.ScheduleId = null;
                    await db.SaveChangesAsync();
                }

                original.PhoneNumber = message.PhoneNumber;
                original.When        = message.When;
                await db.SaveChangesAsync();

                original.ScheduleId = BackgroundJob.Schedule <IForwarder>(f => f.ApplyScheduledForward(original.Id, null), message.When);
                await db.SaveChangesAsync();
            }
示例#3
0
            public async Task Handle(Command message)
            {
                var scheduledForward =
                    new ScheduledForward {
                    TeamId = message.TeamId, PhoneNumber = message.PhoneNumber, When = message.When
                };

                db.BeginTransaction();
                db.ScheduledForwards.Add(scheduledForward);
                await db.SaveChangesAsync();

                scheduledForward.ScheduleId =
                    BackgroundJob.Schedule <IForwarder>(f => f.ApplyScheduledForward(scheduledForward.Id, null), message.When);

                await db.SaveChangesAsync();

                await db.CommitTransactionAsync();
            }
示例#4
0
            public async Task Handle(Command message)
            {
                var scheduled = db.ScheduledForwards.Find(message.Id);

                if (scheduled.ScheduleId != null)
                {
                    BackgroundJob.Delete(scheduled.ScheduleId);
                }
                db.ScheduledForwards.Remove(scheduled);
                await db.SaveChangesAsync();
            }
示例#5
0
        protected override async Task Handle(AddPhoneNumberCommand message, CancellationToken cancellationToken)
        {
            var user = await db.Users.WhereUserLoginIs(message.UserName).SingleAsync();

            var phoneNumber = mapper.Map <UserPhoneNumber>(message.PhoneNumber);
            var code        = VerificationCodeManager.GenerateCode();

            phoneNumber.VerificationToken = VerificationCodeManager.GetHash(phoneNumber.Value + code);

            user.PhoneNumbers.Add(mapper.Map <UserPhoneNumber>(message.PhoneNumber));
            await db.SaveChangesAsync();
        }
示例#6
0
        private async Task ReadTeamStatus(SupportTeam team, PerformContext context)
        {
            var number = await TaskQueue.Enqueue(async() =>
            {
                using (var helper = new ATHelper(team.ConnectionString))
                {
                    await helper.OpenAsync();
                    return(await helper.GetForwardedPhoneNumber());
                }
            });

            context.WriteLine($"Team {team.Name} is forwarding to '{number}'.");

            var state = await db.ForwardingStates.Where(s => s.TeamId == team.Id).OrderByDescending(s => s.When)
                        .FirstOrDefaultAsync();

            if (state?.RawPhoneNumber == number)
            {
                context.WriteLine("No state change, completed check.");
                return;
            }

            context.WriteLine($"Change detected from '{state?.RawPhoneNumber}' to '{number}'.");
            var newState = new ForwardingState
            {
                Team                = team,
                When                = DateTimeOffset.Now,
                RawPhoneNumber      = number,
                DetectedPhoneNumber = db.UserPhoneNumbers.FirstOrDefault(p => p.Value == number)
            };

            db.ForwardingStates.Add(newState);
            await db.SaveChangesAsync();

            var prevStateId = state?.Id;
            await db.Teams.Where(t => t.Id == team.Id).SelectMany(t => t.Members).Select(m => m.User)
            .SelectMany(u => u.ApiKeys).Where(a => a.CallbackUrl != null).ForEachAsync(apiKey =>
            {
                context.WriteLine($"Scheduling notification to {apiKey.CallbackUrl}.");
                BackgroundJob.Enqueue <IPublisher>(p =>
                                                   p.NotifyStateChange(prevStateId, newState.Id, apiKey.CallbackUrl, null));
            });
        }
示例#7
0
            protected override async Task Handle(Command request, CancellationToken cancellationToken)
            {
                var apiKey = await
                             db.Users.WhereUserLoginIs(request.UserName).SelectMany(u => u.ApiKeys)
                             .Where(a => a.Value == request.ApiKey).SingleOrDefaultAsync() ??
                             throw new Exception($"Failed to find configuration for ApiKey {request.ApiKey}.");

                var callback = RestService.For <ICallback>(request.CallbackUrl);

                try
                {
                    await callback.Ping();
                }
                catch (Exception)
                {
                    throw new Exception($"Failed to ping to callback URL {request.CallbackUrl}.");
                }

                apiKey.CallbackUrl = request.CallbackUrl;
                await db.SaveChangesAsync();
            }
        protected override async Task Handle(AddEmailAddressCommand message, CancellationToken cancellationToken)
        {
            var user = await db.Users.WhereUserLoginIs(message.UserName).SingleAsync();

            var emailAddress = new UserEmailAddress {
                Value = message.EmailAddress
            };
            var code = VerificationCodeManager.GenerateCode();

            emailAddress.VerificationToken = VerificationCodeManager.GetHash(emailAddress.Value + code);

            user.EmailAddresses.Add(emailAddress);
            await db.SaveChangesAsync();

            var urlCommand = new VerifyEmailAddressCommand
            {
                Id = emailAddress.Id,
                VerificationCode = code
            };

            var url = message.VerificationUrlBuilder(urlCommand);

            mailer.EmailVerificationCode(emailAddress.Value, user.Login, url);
        }
示例#9
0
 protected override async Task Handle(PhoneNumberCreateCommand message, CancellationToken cancellationToken)
 {
     var phoneNumberEntity = mapper.Map <UserPhoneNumber>(message);
     //db.PhoneNumbers.Add(phoneNumberEntity);
     await db.SaveChangesAsync();
 }