Пример #1
0
        public async Task <IActionResult> ProfileUpdate([FromBody] VendorInputModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            return(Ok(await _userService.UpdateVendorAsync(model)));
        }
Пример #2
0
        /// <summary>
        /// This Method Update Vendor's Information
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public async Task <ResponseViewModel> UpdateVendorAsync(VendorInputModel model)
        {
            try
            {
                AppUser currentUser = await _userManager.FindByIdAsync(_httpContextAccessor.HttpContext.User.GetLoggedInUserId <int>().ToString());

                if (!currentUser.EmailConfirmed)
                {
                    return(Failed(ResponseMessageViewModel.EMAIL_NOT_CONFIRMED, ResponseErrorCodeStatus.EMAIL_NOT_CONFIRMED));
                }

                if (currentUser != null)
                {
                    currentUser.FirstName          = model.FirstName;
                    currentUser.LastName           = model.LastName;
                    currentUser.MiddleName         = model.MiddleName;
                    currentUser.PhoneNumber        = model.PhoneNumber;
                    currentUser.ResidentialAddress = model.ResidentialAddress;
                    currentUser.MailingAddress     = model.MailingAddress;

                    if (!string.IsNullOrEmpty(model.ProfilePhoto) && model.IsProfilePhotoChanged)
                    {
                        FileDocument uploadResult = FileDocument.Create();

                        try
                        {
                            uploadResult = await
                                           BaseContentServer
                                           .Build(ContentServerTypeEnum.FIREBASE, _settings)
                                           .UploadDocumentAsync(FileDocument.Create(model.ProfilePhoto, $"photo-{currentUser.Id}", currentUser.GUID, FileDocumentType.GetDocumentType(MIMETYPE.IMAGE)));
                        }
                        catch (Exception e)
                        {
                            return(Failed(ResponseMessageViewModel.ERROR_UPLOADING_FILE, ResponseErrorCodeStatus.ERROR_UPLOADING_FILE));
                        }

                        currentUser.ProfilePhoto = uploadResult.Path;

                        currentUser.HasUploadedProfilePhoto = !string.IsNullOrEmpty(currentUser.ProfilePhoto);
                    }

                    if (!string.IsNullOrEmpty(model.IdentityDocument) && model.IsIdentityDocumentChanged)
                    {
                        FileDocument uploadResult = FileDocument.Create();

                        try
                        {
                            uploadResult = await
                                           BaseContentServer
                                           .Build(ContentServerTypeEnum.FIREBASE, _settings)
                                           .UploadDocumentAsync(FileDocument.Create(model.IdentityDocument, $"document-{currentUser.Id}", currentUser.GUID, FileDocumentType.GetDocumentType(MIMETYPE.IMAGE)));
                        }
                        catch (Exception e)
                        {
                            return(Failed(ResponseMessageViewModel.ERROR_UPLOADING_FILE, ResponseErrorCodeStatus.ERROR_UPLOADING_FILE));
                        }

                        currentUser.IdentityDocument = uploadResult.Path;

                        currentUser.HasUploadedDocument = !string.IsNullOrEmpty(currentUser.IdentityDocument);
                    }

                    var gender = _utilityRepository.GetGenderByName(model.Gender);

                    if (gender == null)
                    {
                        return(Failed(ResponseMessageViewModel.INVALID_GENDER, ResponseErrorCodeStatus.INVALID_GENDER));
                    }

                    currentUser.GenderId = gender.Id;

                    var state = _stateService.GetState(model.StateOfOriginId);

                    if (state == null)
                    {
                        return(Failed(ResponseMessageViewModel.INVALID_STATE, ResponseErrorCodeStatus.INVALID_STATE));
                    }

                    currentUser.StateOfOriginId = state.Id;

                    if (model.NextOfKin != null)
                    {
                        var nextOfKin = _mapper.Map <VendorNextOfKinInputModel, NextOfKin>(model.NextOfKin);

                        nextOfKin.AppUserId = currentUser.Id;

                        var nextOfKinGender = _utilityRepository.GetGenderByName(model.NextOfKin.Gender);

                        if (nextOfKinGender == null)
                        {
                            return(Failed(ResponseMessageViewModel.INVALID_NEXT_OF_KIN_GENDER, ResponseErrorCodeStatus.INVALID_NEXT_OF_KIN_GENDER));
                        }

                        nextOfKin.GenderId = nextOfKinGender.Id;

                        _utilityRepository.AddNextOfKin(nextOfKin);
                    }

                    var userType = _utilityRepository.GetOrganizationType(model.UserTypeId);

                    if (userType == null)
                    {
                        return(Failed(ResponseMessageViewModel.INVALID_ORGANIZATION_TYPE, ResponseErrorCodeStatus.INVALID_ORGANIZATION_TYPE));
                    }

                    currentUser.OrganizationTypeId = model.UserTypeId;

                    if (model.UserTypeId == (int)OrganizationEnumType.CORPORATE)
                    {
                        if (string.IsNullOrEmpty(model.RCNumber))
                        {
                            return(Failed(ResponseMessageViewModel.INVALID_RC_NUMBER, ResponseErrorCodeStatus.INVALID_RC_NUMBER));
                        }

                        currentUser.RCNumber = model.RCNumber;

                        if (string.IsNullOrEmpty(model.OfficeAddress))
                        {
                            return(Failed(ResponseMessageViewModel.INVALID_OFFICE_ADDRESS, ResponseErrorCodeStatus.INVALID_OFFICE_ADDRESS));
                        }

                        currentUser.OfficeAddress = model.OfficeAddress;

                        if (string.IsNullOrEmpty(model.NameOfEntry))
                        {
                            return(Failed(ResponseMessageViewModel.INVALID_NAME_NAME_OF_ENTRY, ResponseErrorCodeStatus.INVALID_NAME_NAME_OF_ENTRY));
                        }

                        currentUser.EntryName = model.NameOfEntry;

                        currentUser.WebsiteUrl = model.WebSiteUrl;
                    }

                    await _userManager.UpdateAsync(currentUser);

                    var mappedResult = _mapper.Map <AppUser, VendorViewModel>(currentUser);

                    return(Ok().AddStatusMessage(ResponseMessageViewModel.SUCCESSFUL).AddData(mappedResult));
                }
                else
                {
                    return(Failed());
                }
            }
            catch (Exception e)
            {
                return(Create(false, e.Message).AddStatusCode(ResponseErrorCodeStatus.FAIL));
            }
        }