Ejemplo n.º 1
0
        private static async Task EnsureProfileExists(TokenValidatedContext context)
        {
            var db = context.HttpContext.RequestServices.GetRequiredService <MakerTrackerContext>();

            if (context.SecurityToken is JwtSecurityToken accessToken)
            {
                var auth0Id = accessToken.Claims.FirstOrDefault(c => c.Type == "sub")?.Value;
                var profile = await db.Profiles.FirstOrDefaultAsync(p => p.Auth0Id == auth0Id);

                if (profile == null)
                {
                    var email     = accessToken.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email)?.Value;
                    var firstName = accessToken.Claims.FirstOrDefault(c => c.Type == ClaimTypes.GivenName)?.Value;
                    var lastName  = accessToken.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Surname)?.Value;

                    profile = new DBModels.Profile()
                    {
                        Email       = email,
                        CreatedDate = DateTime.Now,
                        FirstName   = firstName,
                        LastName    = lastName,
                        Auth0Id     = auth0Id
                    };
                    await db.Profiles.AddAsync(profile);

                    await db.SaveChangesAsync();
                }
            }
        }
Ejemplo n.º 2
0
        protected virtual async Task <IActionResult> UpdateProfile(Profile profile, UpdateProfileDto model, bool updateAdminNotes)
        {
            var adminNotes     = profile.AdminNotes;
            var updatedProfile = _mapper.Map(model, profile);

            if (!updateAdminNotes)
            {
                // restore from original
                updatedProfile.AdminNotes = adminNotes;

                // updating self - set as onboarded
                updatedProfile.HasOnboarded = true;
            }

            if (!updatedProfile.IsDropOffPoint.GetValueOrDefault())
            {
                updatedProfile.Address  = null;
                updatedProfile.Address2 = null;
            }

            try
            {
                var googleAddress = await GeoCodeLocation(updatedProfile);

                updatedProfile.Latitude  = googleAddress?.Coordinates.Latitude ?? 0;
                updatedProfile.Longitude = googleAddress?.Coordinates.Longitude ?? 0;
                updatedProfile.Location  = new Point(googleAddress?.Coordinates.Longitude ?? 0,
                                                     googleAddress?.Coordinates.Latitude ?? 0)
                {
                    SRID = 4326
                };
            }
            catch (Exception exc)
            {
                Logger.LogError(exc, $"Error encoding location via google api for {JsonConvert.SerializeObject(updatedProfile, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })} - Exception: {JsonConvert.SerializeObject(exc, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })}");
            }

            if (updatedProfile.Id > 0)
            {
                _context.Entry(updatedProfile).State = EntityState.Modified;
            }
            else if (!string.IsNullOrWhiteSpace(User.Identity.Name))
            {
                updatedProfile.CreatedDate = DateTime.Now;
                updatedProfile.Auth0Id     = User.Identity.Name;
                _context.Profiles.Add(updatedProfile);
            }
            else
            {
                throw new UnauthorizedAccessException();
            }

            await _context.SaveChangesAsync();

            return(Ok(true));
        }
Ejemplo n.º 3
0
        private async Task <GoogleAddress> GeoCodeLocation(Profile p)
        {
            if (string.IsNullOrWhiteSpace(_configuration["GoogleAPIKey"]))
            {
                return(null);
            }
            var geocoder  = new GoogleGeocoder(_configuration["GoogleAPIKey"]);
            var addresses = await geocoder.GeocodeAsync(string.Join(',', p.Address, p.City, p.State).Trim(' ', ','));

            return(addresses.First());
        }
Ejemplo n.º 4
0
        protected virtual async Task <IActionResult> UpdateProfile(Profile profile, UpdateProfileDto model, bool updateAdminNotes)
        {
            var adminNotes     = profile.AdminNotes;
            var updatedProfile = _mapper.Map(model, profile);

            if (!updateAdminNotes)
            {
                // restore from original
                updatedProfile.AdminNotes = adminNotes;

                // updating self - set as onboarded
                updatedProfile.HasOnboarded = true;
            }

            var googleAddress = await GeoCodeLocation(updatedProfile);

            updatedProfile.Latitude  = googleAddress?.Coordinates.Latitude ?? 0;
            updatedProfile.Longitude = googleAddress?.Coordinates.Longitude ?? 0;
            updatedProfile.Location  = new Point(googleAddress?.Coordinates.Longitude ?? 0,
                                                 googleAddress?.Coordinates.Latitude ?? 0)
            {
                SRID = 4326
            };
            if (updatedProfile.Id > 0)
            {
                _context.Entry(updatedProfile).State = EntityState.Modified;
            }
            else if (!string.IsNullOrWhiteSpace(User.Identity.Name))
            {
                updatedProfile.CreatedDate = DateTime.Now;
                updatedProfile.Auth0Id     = User.Identity.Name;
                _context.Profiles.Add(updatedProfile);
            }
            else
            {
                throw new UnauthorizedAccessException();
            }

            await _context.SaveChangesAsync();

            return(Ok(true));
        }