// USERS
        private static void SeedNotificationSettings(ModelBuilder b, IEnumerable <Guid> userSettingIds)
        {
            // missing last character
            // we'll use index of setting to fill that in
            // Other characters will be removed by counter
            var startGuid = "71691ddc-039f-4606-b614-ff4a19516cd";
            var counter   = 0;

            foreach (var userSettingId in userSettingIds)
            {
                if (counter % 10 == 0)
                {
                    startGuid = startGuid.Remove(startGuid.Length - 1);
                }
                var values = EnumFactory.SeedEnum <NotificationType, NotificationSetting>((value, index) => new NotificationSetting()
                {
                    Id = Guid.Parse(startGuid + counter + index),
                    NotificationType = value,
                    UserSettingId    = userSettingId
                }).ToList();

                counter++;
                b.Entity <NotificationSetting>().HasData(values);
            }
        }
Exemple #2
0
        public async Task <ExternalLoginResponse> Handle(ExternalLoginRequest request, CancellationToken cancellationToken)
        {
            try
            {
                var user = await _context.Users.FirstOrDefaultAsync(x => x.Email == request.Email, cancellationToken);

                // TODO: Extract this
                if (user == null)
                {
                    var notificationSettings = EnumFactory.SeedEnum <NotificationType, NotificationSetting>((value, index) => new NotificationSetting()
                    {
                        Id = Guid.NewGuid(),
                        NotificationType = value,
                    }).ToList();

                    // call create user request..
                    var userId  = Guid.NewGuid();
                    var newUser = new ApplicationUser()
                    {
                        Id                   = userId,
                        AccountType          = request.AccountType,
                        Email                = request.Email,
                        FirstName            = request.FirstName,
                        LastName             = request.LastName,
                        ExternalLoginAccount = true,
                        UserSetting          = new UserSetting()
                        {
                            ApplicationUserId    = userId,
                            NotificationSettings = notificationSettings,
                        },
                    };

                    if (request.Avatar != null)
                    {
                        newUser.Avatar = request.Avatar;
                    }

                    newUser.CustomerId = await StripeConfiguration.AddCustomer(newUser.FullName, newUser.Email); // add to stripe

                    newUser = ExerciseTagGroupsFactory.ApplyProperties <ApplicationUser>(newUser);

                    await _context.Users.AddAsync(newUser, cancellationToken);

                    await _context.SaveChangesAsync(cancellationToken);

                    user = newUser;
                }
                else if (user.ExternalLoginAccount == false)
                {
                    throw new Exception("Can't external login user who exists but doesn't have external login enabled");
                }

                var token = _tokenGenerator.GenerateToken(user.Id);

                var userInfo = await _mediator.Send(new CurrentUserRequest(user.Id), cancellationToken);

                return(new ExternalLoginResponse(token, userInfo));
            }
            catch (Exception e)
            {
                throw new Exception(nameof(ExternalLoginRequest), e);
            }
        }