Exemplo n.º 1
0
        public override async Task <AddLibraryRequest> HandleAsync(AddLibraryRequest command, CancellationToken cancellationToken = new CancellationToken())
        {
            command.Result = await _libraryRepository.AddLibrary(command.Library, cancellationToken);

            var account = await _accountRepository.GetAccountByEmail(command.Library.OwnerEmail, cancellationToken);

            if (account != null)
            {
                await _libraryRepository.AddAccountToLibrary(command.Result.Id, account.Id, Role.LibraryAdmin, cancellationToken);

                return(await base.HandleAsync(command, cancellationToken));
            }

            var invitationCode = Guid.NewGuid().ToString("N");

            await _accountRepository.AddInvitedAccount(
                command.Library.Name,
                command.Library.OwnerEmail,
                Role.LibraryAdmin,
                invitationCode,
                DateTime.Today.AddDays(7),
                command.Result.Id,
                cancellationToken);

            await _emailService.SendAsync(command.Library.OwnerEmail,
                                          $"Welcome to {command.Result.Name}",
                                          EmailTemplateProvider.GetLibraryAdminInvitationEmail(command.Result.Name, new Uri(new Uri(_settings.FrontEndUrl), _settings.RegisterPagePath + invitationCode).ToString()),
                                          _settings.EmailFrom,
                                          cancellationToken);

            return(await base.HandleAsync(command, cancellationToken));
        }
Exemplo n.º 2
0
        public override async Task <InviteUserCommand> HandleAsync(InviteUserCommand command, CancellationToken cancellationToken = default)
        {
            var library = await _libraryRepository.GetLibraryById(command.LibraryId, cancellationToken);

            if (command.Role != Role.Admin && library == null)
            {
                throw new UnauthorizedException();
            }

            var account = await _accountRepository.GetAccountByEmail(command.Email, cancellationToken);

            if (account != null)
            {
                var accountLibraries = await _libraryRepository.GetLibrariesByAccountId(account.Id, cancellationToken);

                if (string.IsNullOrWhiteSpace(account.InvitationCode) &&
                    accountLibraries.Any(t => t.Id == command.LibraryId))
                {
                    return(await base.HandleAsync(command, cancellationToken));
                }
                else
                {
                    await _libraryRepository.AddAccountToLibrary(command.LibraryId, account.Id, command.Role, cancellationToken);

                    return(await base.HandleAsync(command, cancellationToken));
                }
            }

            var invitationCode = Guid.NewGuid();

            await _accountRepository.AddInvitedAccount(
                command.Name,
                command.Email,
                command.Role,
                invitationCode.ToString("N"),
                DateTime.Today.AddDays(7),
                library?.Id,
                cancellationToken);

            if (command.Role == Role.Admin)
            {
                await _emailService.SendAsync(command.Email,
                                              $"Welcome to Dashboards",
                                              EmailTemplateProvider.GetSuperAdminInvitationEmail(command.Name, new Uri(new Uri(_settings.FrontEndUrl), _settings.ResetPasswordPagePath).ToString()),
                                              _settings.EmailFrom,
                                              cancellationToken);
            }
            else
            {
                await _emailService.SendAsync(command.Email,
                                              $"Welcome to {library.Name}",
                                              EmailTemplateProvider.GetLibraryUserInvitationEmail(command.Name, library.Name, new Uri(new Uri(_settings.FrontEndUrl), _settings.ResetPasswordPagePath).ToString()),
                                              _settings.EmailFrom,
                                              cancellationToken);
            }

            return(await base.HandleAsync(command, cancellationToken));
        }
Exemplo n.º 3
0
        public override async Task <AddLibraryToAccountRequest> HandleAsync(AddLibraryToAccountRequest command, CancellationToken cancellationToken = new CancellationToken())
        {
            await _libraryRepository.AddAccountToLibrary(command.LibraryId, command.AccountId, command.Role, cancellationToken);

            return(await base.HandleAsync(command, cancellationToken));
        }