Exemple #1
0
        public async Task <ServiceResponseResult> UpdateAdminProfile(AdminProfileUpdateReqModel model, long userId, long locationId)
        {
            Logger.WriteInformation("Updating admin profile data.");
            var user = await _context.User.FirstOrDefaultAsync(x => x.Id == userId);

            var token = Guid.NewGuid().ToString();
            var now   = DateTime.UtcNow;

            if (!string.IsNullOrEmpty(model.Email))
            {
                if (await _context.User.AnyAsync(x => x.Email == model.Email.ToLower()) || await _context.ChangeEmailRequest.AnyAsync(x => x.Email == model.Email.ToLower()))
                {
                    return(new ServiceResponseResult
                    {
                        StatusCode = System.Net.HttpStatusCode.BadRequest,
                        Result = new { Message = "An user with the email already exists." },
                    });
                }


                var emailChange = new ChangeEmailRequest
                {
                    Email                   = model.Email,
                    RequestedOn             = DateTime.UtcNow,
                    UserId                  = userId,
                    VerificationToken       = token,
                    VerificationTokenExpiry = now.AddHours(24)
                };

                _context.ChangeEmailRequest.Add(emailChange);

                var emailData = await _emailService.ConstructEmailVerification(user.VerificationToken);

                await _emailSender.SendMailViaSmtpClientAsync(new string[] { model.Email }, new string[] { }, new string[] { }, emailData);
            }

            if (!string.IsNullOrEmpty(model.Name))
            {
                user = user.UpdateName(model.Name);
            }

            user = user.UpdateVerificationToken(token)
                   .UpdateVerificationTokenExpiry(now.AddHours(24))
                   .UpdateLastUpdatedOn(DateTime.UtcNow)
                   .UpdateLastUpdatedBy(userId);

            _context.User.Update(user);
            await _context.SaveChangesAsync();

            Logger.WriteInformation("Updating admin profile data completed.");
            return(await GetAdminProfile(locationId, userId));
        }
Exemple #2
0
        public async Task <ActionResult> UpdateProfile([FromRoute] long locationId, [FromBody] AdminProfileUpdateReqModel model)
        {
            var res = await _accountService.UpdateAdminProfile(model, _authHelpers.GetCurrentUserId().Value, locationId);

            return(StatusCode(res.GetStatusCode(), res.Result));
        }