public async Task <ActionResult> AddOwner([FromBody] AddOwnerDto addOwnerDto)
        {
            try
            {
                using (_ownerLogger.BeginScope($"API-AddOwner-started {DateTime.UtcNow}"))
                {
                    var result = await _ownerService.RegisterOwner(addOwnerDto).ConfigureAwait(false);

                    if (!string.IsNullOrEmpty(result.ownerId) && result.confirmationToken != Guid.Empty)
                    {
                        string confirmationLink = Url.Action("ConfirmEmail",
                                                             "Account",
                                                             new
                        {
                            userid = result.ownerId,
                            token  = result.confirmationToken
                        },
                                                             protocol: HttpContext.Request.Scheme);

                        var message = _mailHelper.GetEmailMessage("AddOwner",
                                                                  new { FullName = result.fullName, UserName = result.ownerId, ConfirmationLink = confirmationLink, EmailId = result.ownerEmailId });

                        await _mail.SendMailAsync(message);
                    }

                    _ownerLogger.LogInformation($"API-AddOwner-completed {DateTime.UtcNow}");

                    return(StatusCode((int)result.statusCode, result.ownerId));
                }
            }
            catch (Exception ex)
            {
                _ownerLogger.LogError
                    (ex,
                    $"API-AddOwner-Exception {DateTime.UtcNow}"
                    );

                return(StatusCode((int)HttpStatusCode.BadRequest,
                                  _apiSettings.IsSecuredEnvironment ? "An error occured while processing AddOwner" : ex.ToString()));
            }
        }
Beispiel #2
0
        public async Task <(HttpStatusCode statusCode, string ownerId, Guid confirmationToken, string ownerEmailId, string fullName)> RegisterOwner(AddOwnerDto addOwnerDto)
        {
            _logger.LogInformation($"Service-RegisterOwner-Executing RegisterOwner started at {DateTime.UtcNow}");

            addOwnerDto.CheckArgumentIsNull(nameof(addOwnerDto));

            var    statusCode        = HttpStatusCode.Created;
            string ownerId           = string.Empty;
            string emailId           = string.Empty;
            string fullName          = string.Empty;
            Guid   confirmationToken = Guid.Empty;

            var ownerSpec = new OwnerWithAreaSpecification(addOwnerDto.Email);

            var owner = (await _ownerRepository.ListAsync(ownerSpec).ConfigureAwait(false)).FirstOrDefault();

            if (owner != null)
            {
                _logger.LogInformation($"owner with email {addOwnerDto.Email} already exists!!!");
                statusCode = HttpStatusCode.BadRequest;
            }
            else
            {
                Owner ownerEntity = await _ownerRepository.GetMaxOfPrimaryKey();

                string newOwnerDisplayId        = ownerEntity.GetNextPrimaryKey();
                Dictionary <string, Guid> roles = _dictionaryRepository.RolesDictionary;

                Guid roleId = roles.Where(s => s.Key == addOwnerDto.RoleName).First().Value;

                var ownerDto = new SaveOwnerDto(roleId, newOwnerDisplayId)
                {
                    FirstName = addOwnerDto.FirstName,
                    LastName  = addOwnerDto.LastName,
                    Email     = addOwnerDto.Email
                };

                var ownerToAddToDb = ownerDto.ToEntity(true);
                ownerToAddToDb.Password = _securityService.GetSha256Hash(addOwnerDto.Password);
                await _ownerRepository.AddAsync(ownerToAddToDb).ConfigureAwait(false);

                //Add entry in ownerconfirmation table
                var ownerConfirmation = new OwnerConfirmation();
                ownerConfirmation.GenerateLinkAndToken(ownerToAddToDb.OwnerId);
                await _ownerConfirmationRepository.AddAsync(ownerConfirmation);

                await _ownerRepository.SaveAllwithAudit();

                ownerId           = newOwnerDisplayId;
                confirmationToken = ownerConfirmation.confirmationToken;
                emailId           = ownerToAddToDb.EmailId;
                fullName          = ownerToAddToDb.FullName;
            }

            _logger.LogInformation($"Service-RegisterOwner-Executing RegisterOwner completed at {DateTime.UtcNow}");

            return(statusCode, ownerId, confirmationToken, emailId, fullName);
        }