public UpdateAccountCommand(Guid id, UpdateAccountRequest account) : base(id)
 {
     Account = account;
 }
Beispiel #2
0
        public async Task <GetAccountResponse> UpdateAccount(Guid id, UpdateAccountRequest dto)
        {
            var account = await _unitOfWork.Accounts.Get(id);

            if (account is null)
            {
                throw new KeyNotFoundException("Account does not exist.");
            }

            if (!string.IsNullOrEmpty(dto.Password))
            {
                // validate
                if (dto.Password != dto.RepeatPassword)
                {
                    throw new AppException("Passwords do not match.");
                }

                if (dto.Password.Length < 6)
                {
                    throw new AppException("Password must be at least 6 characters long.");
                }

                // hash password
                account.PasswordHash = Utilities.HashPassword(dto.Password);
            }

            if (!string.IsNullOrEmpty(dto.Email) && account.Email != dto.Email)
            {
                if (await _unitOfWork.Accounts.GetByEmail(dto.Email) is not null)
                {
                    throw new AppException($"Email '{dto.Email}' is already taken");
                }

                account.Email = dto.Email;
            }

            if (dto.Image?.Length > 0)
            {
                int ALLOWED_FILE_SIZE = 3 * 1024 * 1024; // ~= 3 MB
                if (dto.Image.Length > ALLOWED_FILE_SIZE)
                {
                    throw new AppException("Image size is too big.");
                }

                var isValidImage = Utilities.IsValidImage(dto.Image);

                if (!isValidImage)
                {
                    throw new AppException("Uploaded image is invalid.");
                }

                account.Image = dto.Image;
            }

            // update role specific fields
            if (account.Role == Role.Customer)
            {
                var customer = await _unitOfWork.Customers.GetByAccountId(account.Id);

                if (!string.IsNullOrEmpty(dto.CompanyName) && customer.CompanyName != dto.CompanyName)
                {
                    customer.CompanyName = dto.CompanyName;
                }

                if (!string.IsNullOrEmpty(dto.VatNumber) && customer.VatNumber != dto.VatNumber)
                {
                    customer.VatNumber = dto.VatNumber;
                }
            }
            else
            {
                var employee = await _unitOfWork.Employees.GetByAccountId(account.Id);

                if (!string.IsNullOrEmpty(dto.FirstName) && employee.FirstName != dto.FirstName)
                {
                    employee.FirstName = dto.FirstName;
                }

                if (!string.IsNullOrEmpty(dto.LastName) && employee.LastName != dto.LastName)
                {
                    employee.LastName = dto.LastName;
                }
            }

            account.Updated = DateTime.Now;
            _unitOfWork.Commit();

            return(_mapper.Map <GetAccountResponse>(account));
        }
Beispiel #3
0
 public Task <bool> UpdateAccount(UpdateAccountRequest response, CancellationToken token)
 => Put($"accounts/{response.Id}", response, token);
        public async Task <ActionResult <GetAccountResponse> > UpdateAccount(Guid id, [FromBody] UpdateAccountRequest dto)
        {
            // users can update their own account and Managers can update any account
            if (id != Account.Id && Account.Role != Role.Manager)
            {
                return(Unauthorized(new { message = "Unauthorized" }));
            }

            var account = await _accountService.UpdateAccount(id, dto);

            return(Ok(account));
        }
 public UpdateAccountResponse UpdateAccount(UpdateAccountRequest request)
 {
     return(ExecuteAction <UpdateAccountResponse>(request));
 }
Beispiel #6
0
 public void Update(UpdateAccountRequest request)
 {
     _merchantRepository.Update(request);
 }
Beispiel #7
0
 public void UpdateAccount(UpdateAccountRequest request)
 {
     ApplyChange(new AccountUpdatedEvent(Id, request));
 }
 public ActionResult UpdateAccount(UpdateAccountRequest request)
 {
     return(this.Execute(
                () => this.accountService.UpdateAccount(request.ContactId, request.FirstName, request.LastName)));
 }
Beispiel #9
0
        public async Task <ActionResult <UpdateAccountResponse> > Put([FromHeader] string client, [FromRoute] string id, [FromBody] UpdateAccountRequest request)
        {
            UpdateAccountResponse response = new UpdateAccountResponse();
            string responseCode            = $"UPDATE_{client}_{id}";

            try
            {
                var factory = WalletFactory.Instance.GetAccount(_configuration, true);
                var account = await factory.UpdateGiftcard(client, id, request.CPF);

                var dto = new UpdateAccountDTO
                {
                    AccountID = account.AccountID
                };
                response.StatusCode = "200";
                response.Data       = dto;
                return(Ok(response));
            }
            catch (Exception ex)
            {
                response.StatusCode = "500";
                response.Messages.Add(ResponseMessage.Create(ex, responseCode));
                return(StatusCode(500, response));
            }
        }
Beispiel #10
0
        public BaseResponse <GetAccountResponse, ErrorsResponse> UpdateAccount(string accountId, UpdateAccountRequest request)
        {
            var method   = HttpMethod.Put;
            var endpoint = $"/accounts/{accountId}";

            return(this.SendRequest <GetAccountResponse>(method, endpoint, request, authMode: "amk"));
        }
Beispiel #11
0
 public async Task UpdateAccountById(int id, UpdateAccountRequest request)
 {
     await _accountsRepository.UpdateAccountById(id, request.Name, request.StartingBalance, request.AccountTypeId);
 }
Beispiel #12
0
 public UpdateAccountResponse UpdateAccount(UpdateAccountRequest request)
 {
     return(Execute(() => InternalService.UpdateAccount(request)));
 }
Beispiel #13
0
        public async Task <IActionResult> Update([FromBody] UpdateAccountRequest request)
        {
            UpdateAccountResponse response = await _accountService.UpdateAccountAsync(request);

            return(Result(response));
        }
Beispiel #14
0
 public Task <UpdateAccountResponse> UpdateAccountAsync(UpdateAccountRequest request)
 {
     throw new System.NotImplementedException();
 }
Beispiel #15
0
        /// <summary>
        /// Sửa thông tin tài khoản, thêm xóa quyền
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="request"></param>
        /// <returns>Thông tin tài khoản</returns>
        public async Task <Response <AccountResponse> > UpdateAccountAsync(Guid userId, UpdateAccountRequest request)
        {
            var appUser = await _userManager.FindByIdAsync(userId.ToString());

            if (appUser == null)
            {
                _logger.LogError(Constant.USER_NOT_EXIST);
                return(new Response <AccountResponse>(Constant.STATUS_ERROR, new List <string> {
                    Constant.USER_NOT_EXIST
                }));
            }
            var validUser = await _userValidator.ValidateAsync(_userManager, appUser);

            if (!validUser.Succeeded || validUser == null)
            {
                _logger.LogError("User is not validate");
                return(new Response <AccountResponse>(Constant.STATUS_ERROR, validUser.Errors.Select(x => x.Description)));
            }
            if (request.Age > 0)
            {
                appUser.Age = request.Age;
            }

            if (!string.IsNullOrEmpty(request.Address))
            {
                appUser.Address = request.Address;
            }

            if (!string.IsNullOrEmpty(request.LastName))
            {
                appUser.LastName = request.LastName;
            }

            if (!string.IsNullOrEmpty(request.FirstName))
            {
                appUser.FirstName = request.FirstName;
            }

            appUser.UserName = appUser.FirstName + appUser.LastName;

            if (!string.IsNullOrEmpty(request.Email))
            {
                appUser.Email = request.Email;
            }

            if (!string.IsNullOrEmpty(request.Password))
            {
                var validPass = await _passwordValidator.ValidateAsync(_userManager, appUser, request.Password);

                if (validPass.Succeeded)
                {
                    appUser.PasswordHash = _passwordHasher.HashPassword(appUser, request.Password);
                }
                else
                {
                    _logger.LogError("Password is not validate");
                    return(new Response <AccountResponse>(Constant.STATUS_ERROR, validPass.Errors.Select(x => x.Description)));
                }
            }

            var result = new IdentityResult();

            //Add account to role
            foreach (var item in request.AddRoles ?? new List <string> {
            })
            {
                var role = await _roleManager.FindByNameAsync(item);

                var isInRole = await _userManager.IsInRoleAsync(await _userManager.FindByIdAsync(userId.ToString()), role.Name);

                if (role != null && !isInRole)
                {
                    result = await _userManager.AddToRoleAsync(appUser, role.Name);

                    if (!result.Succeeded)
                    {
                        _logger.LogError("Add user to role failed");
                        return(new Response <AccountResponse>(Constant.STATUS_ERROR, result.Errors.Select(x => x.Description)));
                    }
                }
            }

            // Xóa tài khoản khỏi quyền
            foreach (var item in request.RemoveRoles ?? new List <string> {
            })
            {
                var role = await _roleManager.FindByNameAsync(item);

                var isInRole = await _userManager.IsInRoleAsync(await _userManager.FindByIdAsync(userId.ToString()), role.Name);

                if (role != null && isInRole)
                {
                    result = await _userManager.RemoveFromRoleAsync(appUser, role.Name);

                    if (!result.Succeeded)
                    {
                        _logger.LogError("Remove user from role failed");
                        return(new Response <AccountResponse>(Constant.STATUS_ERROR, result.Errors.Select(x => x.Description)));
                    }
                }
            }

            result = await _userManager.UpdateAsync(appUser);

            var dataResponse = new AccountResponse
            {
                Id        = appUser.Id,
                Address   = appUser.Address,
                Age       = appUser.Age,
                Email     = appUser.Email,
                UserName  = appUser.UserName,
                FirstName = appUser.FirstName,
                LastName  = appUser.LastName,
                Role      = (await _userManager.GetRolesAsync(appUser)).ToList()
            };

            if (result.Succeeded)
            {
                _logger.LogInformation("Update account success");
                return(new Response <AccountResponse>(Constant.STATUS_SUCESS, null, dataResponse, 1));
            }

            _logger.LogError("Update account error");
            return(new Response <AccountResponse>(Constant.STATUS_ERROR, result.Errors.Select(x => x.Description)));
        }
        /// <summary>
        /// Updates the account.
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        private async Task<UpdateAccountResponse> UpdateAccountAsync(Account account)
        {
            var request = new UpdateAccountRequest
            {
                Account = account
            };

            return (await Service.CallAsync((s, r) => s.UpdateAccountAsync(r), request));
        }
Beispiel #17
0
 public async Task <Response <AccountResponse> > UpdateAccount([FromRoute] Guid userId, [FromBody] UpdateAccountRequest request)
 {
     return(await _accountService.UpdateAccountAsync(userId, request));
 }