public async Task ExecuteAsync(CancelBookingCommand command, IExecutionContext executionContext)
        {
            PermissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(BookingCustomEntityDefinition.DefinitionCode, executionContext.UserContext);

            using (var scope = DomainRepository.Transactions().CreateScope())
            {
                var booking = await BookingProvider.GetBookingById(command.Id);

                booking.BookingState = BookingDataModel.BookingStateType.Closed;
                booking.IsCancelled  = true;

                var user = await CurrentUserProvider.GetAsync();

                booking.AddLogEntry(new BookingLogEntry
                {
                    Text      = "Reservationen blev aflyst.",
                    Username  = user.User.GetFullName(),
                    UserId    = user.User.UserId,
                    Timestamp = DateTime.Now
                });

                UpdateCustomEntityDraftVersionCommand updateCmd = new UpdateCustomEntityDraftVersionCommand
                {
                    CustomEntityDefinitionCode = BookingCustomEntityDefinition.DefinitionCode,
                    CustomEntityId             = command.Id,
                    Title   = booking.MakeTitle(),
                    Publish = true,
                    Model   = booking
                };

                await DomainRepository.CustomEntities().Versions().UpdateDraftAsync(updateCmd);

                await scope.CompleteAsync();
            }
        }
        public async Task ExecuteAsync(ApproveBookingCommand command, IExecutionContext executionContext)
        {
            PermissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(BookingCustomEntityDefinition.DefinitionCode, executionContext.UserContext);

            using (var scope = DomainRepository.Transactions().CreateScope())
            {
                var booking = await BookingProvider.GetBookingById(command.Id);

                booking.BookingState = BookingDataModel.BookingStateType.Approved;
                booking.IsApproved   = true;
                booking.IsCancelled  = false;

                await booking.AddLogEntry(CurrentUserProvider, "Reservationen blev godkendt.");

                UpdateCustomEntityDraftVersionCommand updateCmd = new UpdateCustomEntityDraftVersionCommand
                {
                    CustomEntityDefinitionCode = BookingCustomEntityDefinition.DefinitionCode,
                    CustomEntityId             = command.Id,
                    Title   = booking.MakeTitle(),
                    Publish = true,
                    Model   = booking
                };

                await DomainRepository.CustomEntities().Versions().UpdateDraftAsync(updateCmd);

                await scope.CompleteAsync();
            }
        }
Exemplo n.º 3
0
        public async Task ExecuteAsync(BookingRequestCommand command, IExecutionContext executionContext)
        {
            using (var scope = DomainRepository.Transactions().CreateScope())
            {
                var tenantCategory = await TenantCategoryProvider.GetTenantCategoryById(command.TenantCategoryId.Value);

                DateTime lastAllowedArrivalDate = DateTime.Now.AddMonths(tenantCategory.AllowedBookingFutureMonths);
                if (command.ArrivalDate.Value >= lastAllowedArrivalDate || command.DepartureDate.Value >= lastAllowedArrivalDate)
                {
                    throw new ValidationErrorException(new ValidationError($"Den valgte lejertype kan ikke reservere mere end {tenantCategory.AllowedBookingFutureMonths} måneder ud i fremtiden. Dvs. senest {lastAllowedArrivalDate.ToShortDateString()}.", nameof(command.ArrivalDate)));
                }

                int bookingNumber = await SequenceNumberGenerator.NextNumber("BookingNumber");

                var booking = new BookingDataModel
                {
                    BookingNumber        = bookingNumber,
                    ArrivalDate          = command.ArrivalDate.Value,
                    DepartureDate        = command.DepartureDate.Value,
                    OnlySelectedWeekdays = command.OnlySelectedWeekdays,
                    SelectedWeekdays     = command.SelectedWeekdays,
                    TenantCategoryId     = command.TenantCategoryId.Value,
                    TenantName           = command.TenantName,
                    Purpose        = command.Purpose,
                    ContactName    = command.ContactName,
                    ContactPhone   = command.ContactPhone,
                    ContactAddress = command.ContactAddress,
                    ContactCity    = command.ContactCity,
                    ContactEMail   = command.ContactEMail,
                    Comments       = command.Comments,
                    RentalPrice    = null, // To be set later
                    Deposit        = BookingSettings.StandardDeposit,
                    BookingState   = BookingDataModel.BookingStateType.Requested
                };

                await booking.AddLogEntry(CurrentUserProvider, "Reservationen blev indsendt af lejer.");

                await SendConfirmationMail(booking);
                await SendNotificationMail(booking);

                var addCommand = new AddCustomEntityCommand
                {
                    CustomEntityDefinitionCode = BookingCustomEntityDefinition.DefinitionCode,
                    Model   = booking,
                    Title   = booking.MakeTitle(),
                    Publish = true,
                };

                await DomainRepository.WithElevatedPermissions().CustomEntities().AddAsync(addCommand);

                await scope.CompleteAsync();
            }
        }
Exemplo n.º 4
0
        public async Task ExecuteAsync(CheckoutBookingCommand command, IExecutionContext executionContext)
        {
            using (var scope = DomainRepository.Transactions().CreateScope())
            {
                BookingDataModel booking = await BookingProvider.GetBookingById(command.Id);

                if (command.Token != booking.TenantSelfServiceToken)
                {
                    throw new AuthenticationFailedException("Ugyldigt eller manglende adgangsnøgle");
                }

                if (booking.BookingState == BookingDataModel.BookingStateType.Requested)
                {
                    throw new ValidationErrorException("Det er ikke muligt at indberette slutafregning, da reservationen endnu ikke er godkendt.");
                }
                else if (booking.BookingState == BookingDataModel.BookingStateType.Closed)
                {
                    throw new ValidationErrorException("Det er ikke muligt at indberette slutafregning, da reservationen allerede er afsluttet.");
                }

                booking.IsCheckedOut            = true;
                booking.ElectricityReadingStart = command.StartReading;
                booking.ElectricityReadingEnd   = command.EndReading;
                booking.ElectricityPriceUnit    = BookingSettings.ElectricityPrice;

                if (!string.IsNullOrEmpty(command.Comments))
                {
                    booking.Comments += $"\n\n=== Kommentarer til slutregnskab [{DateTime.Now}] ===\n{command.Comments}";
                }

                await booking.AddLogEntry(CurrentUserProvider, "Elforbrug blev indmeldt af lejer.");

                // Do not use BookingSummary for mails as it will be the old version of the booking, from before readings were updated.
                // Also make sure mails are sent before updating the entity, as sent mail will be refered by the model.
                await SendCheckoutConfirmationMail(booking);
                await SendAdminNotificationMail(booking);

                UpdateCustomEntityDraftVersionCommand updateCmd = new UpdateCustomEntityDraftVersionCommand
                {
                    CustomEntityDefinitionCode = BookingCustomEntityDefinition.DefinitionCode,
                    CustomEntityId             = command.Id,
                    Title   = booking.MakeTitle(),
                    Publish = true,
                    Model   = booking
                };

                await DomainRepository.WithElevatedPermissions().CustomEntities().Versions().UpdateDraftAsync(updateCmd);

                await scope.CompleteAsync();
            }
        }
Exemplo n.º 5
0
        public async Task ExecuteAsync(UpdateBookingCommand command, IExecutionContext executionContext)
        {
            PermissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(BookingCustomEntityDefinition.DefinitionCode, executionContext.UserContext);

            using (var scope = DomainRepository.Transactions().CreateScope())
            {
                // Verify teneant category
                await TenantCategoryProvider.GetTenantCategoryById(command.TenantCategoryId.Value);

                BookingDataModel booking = await BookingProvider.GetBookingById(command.BookingId);

                booking.ArrivalDate          = command.ArrivalDate.Value;
                booking.DepartureDate        = command.DepartureDate.Value;
                booking.OnlySelectedWeekdays = command.OnlySelectedWeekdays;
                booking.SelectedWeekdays     = command.SelectedWeekdays;
                booking.TenantCategoryId     = command.TenantCategoryId;
                booking.TenantName           = command.TenantName;
                booking.Purpose                 = command.Purpose;
                booking.ContactName             = command.ContactName;
                booking.ContactPhone            = command.ContactPhone;
                booking.ContactAddress          = command.ContactAddress;
                booking.ContactCity             = command.ContactCity;
                booking.ContactEMail            = command.ContactEMail;
                booking.Comments                = command.Comments;
                booking.RentalPrice             = command.RentalPrice;
                booking.BookingState            = command.BookingState;
                booking.IsApproved              = command.IsApproved;
                booking.IsCancelled             = command.IsCancelled;
                booking.IsCheckedOut            = command.IsCheckedOut;
                booking.IsArchived              = command.IsArchived;
                booking.WelcomeLetterIsSent     = command.WelcomeLetterIsSent;
                booking.Deposit                 = command.Deposit;
                booking.DepositReceived         = command.DepositReceived;
                booking.ElectricityReadingStart = command.ElectricityReadingStart;
                booking.ElectricityReadingEnd   = command.ElectricityReadingEnd;
                booking.ElectricityPriceUnit    = command.ElectricityPriceUnit;

                UpdateCustomEntityDraftVersionCommand updateCmd = new UpdateCustomEntityDraftVersionCommand
                {
                    CustomEntityDefinitionCode = BookingCustomEntityDefinition.DefinitionCode,
                    CustomEntityId             = command.BookingId,
                    Title   = booking.MakeTitle(),
                    Publish = true,
                    Model   = booking
                };

                await DomainRepository.CustomEntities().Versions().UpdateDraftAsync(updateCmd);

                await scope.CompleteAsync();
            }
        }
        public async Task ExecuteAsync(SendBookingMailCommand command, IExecutionContext executionContext)
        {
            PermissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(BookingCustomEntityDefinition.DefinitionCode, executionContext.UserContext);

            using (var scope = DomainRepository.Transactions().CreateScope())
            {
                BookingDataModel booking = await BookingProvider.GetBookingById(command.BookingId);

                await booking.AddLogEntry(CurrentUserProvider, $"Sendt: {command.Subject}.");

                byte[] mailBody           = System.Text.Encoding.UTF8.GetBytes(command.Message);
                var    addDcoumentCommand = new AddDocumentCommand
                {
                    Title    = command.Subject,
                    MimeType = "text/html",
                    Body     = mailBody
                };

                await CommandExecutor.ExecuteAsync(addDcoumentCommand);

                booking.AddDocument(command.Subject, addDcoumentCommand.OutputDocumentId);

                UpdateCustomEntityDraftVersionCommand updateCmd = new UpdateCustomEntityDraftVersionCommand
                {
                    CustomEntityDefinitionCode = BookingCustomEntityDefinition.DefinitionCode,
                    CustomEntityId             = command.BookingId,
                    Title   = booking.MakeTitle(),
                    Publish = true,
                    Model   = booking
                };

                await DomainRepository.CustomEntities().Versions().UpdateDraftAsync(updateCmd);

                MailAddress to      = new MailAddress(booking.ContactEMail, booking.ContactName);
                MailMessage message = new MailMessage
                {
                    To       = to,
                    Subject  = command.Subject,
                    HtmlBody = command.Message
                };

                // It is not really a good idea to contact a mail server during a transaction, but ...
                // 1) I really don't want the mail being registered in the database as "sent" if the mail sending fails.
                // 2) One can hope that the dispatcher simply adds the message to an outgoing mail queue.
                // (and, yes, sending mails may fail much later with an "unknown recipient" or similar)

                await MailDispatchService.DispatchAsync(message);

                await scope.CompleteAsync();
            }
        }
Exemplo n.º 7
0
        public async Task ExecuteAsync(DeleteBookingCommand command, IExecutionContext executionContext)
        {
            PermissionValidationService.EnforceCustomEntityPermission <CustomEntityDeletePermission>(BookingCustomEntityDefinition.DefinitionCode, executionContext.UserContext);

            using (var scope = DomainRepository.Transactions().CreateScope())
            {
                var booking = await BookingProvider.GetBookingById(command.Id);

                foreach (var document in booking.Documents)
                {
                    var deleteDocumentCommand = new DeleteDocumentCommand {
                        Id = document.DocumentId
                    };
                    await CommandExecutor.ExecuteAsync(deleteDocumentCommand);
                }

                await DomainRepository.CustomEntities().DeleteAsync(command.Id);

                await scope.CompleteAsync();
            }
        }
        public async Task ExecuteAsync(SendWelcomeLetterCommand command, IExecutionContext executionContext)
        {
            PermissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(BookingCustomEntityDefinition.DefinitionCode, executionContext.UserContext);

            using (var scope = DomainRepository.Transactions().CreateScope())
            {
                var booking = await BookingProvider.GetBookingById(command.Id);

                booking.WelcomeLetterIsSent = true;

                // Welcome is not really sent here, we only redirect to the mail editing-and-sending page afterwards.

                var user = await CurrentUserProvider.GetAsync();

                booking.AddLogEntry(new BookingLogEntry
                {
                    Text      = "Velkomstbrev er udsendt.",
                    Username  = user.User.GetFullName(),
                    UserId    = user.User.UserId,
                    Timestamp = DateTime.Now
                });

                UpdateCustomEntityDraftVersionCommand updateCmd = new UpdateCustomEntityDraftVersionCommand
                {
                    CustomEntityDefinitionCode = BookingCustomEntityDefinition.DefinitionCode,
                    CustomEntityId             = command.Id,
                    Title   = booking.MakeTitle(),
                    Publish = true,
                    Model   = booking
                };

                await DomainRepository.CustomEntities().Versions().UpdateDraftAsync(updateCmd);

                await scope.CompleteAsync();
            }
        }
Exemplo n.º 9
0
        public async Task<IActionResult> Test()
        {
            var user = await _contentRepository
                .WithElevatedPermissions()
                .Users()
                .GetById(2)
                .AsMicroSummary()
                .ExecuteAsync();

            var image = await _contentRepository
                .ImageAssets()
                .GetById(2)
                .AsRenderDetails()
                .ExecuteAsync();

            var page = await _contentRepository
                .Pages()
                .Search()
                .AsRenderSummaries(new SearchPageRenderSummariesQuery()
                {
                    PageSize = 20,
                    PageDirectoryId = 2
                })
                .ExecuteAsync();

            var page2 = await _contentRepository
                .Pages()
                .GetById(1)
                .AsRenderSummary()
                .ExecuteAsync();

            // Perhaps NotFound should look like this:
            await _contentRepository
                .Pages()
                .NotFound()
                .GetByPath(new GetNotFoundPageRouteByPathQuery())
                .ExecuteAsync();

            var regions = await _contentRepository
                .Pages()
                .Versions()
                .Regions()
                .Blocks()
                .GetById(2)
                .AsRenderDetails(PublishStatusQuery.Latest)
                .ExecuteAsync();

            await _contentRepository
                .Pages()
                .GetByPath()
                .AsRoutingInfo(new GetPageRoutingInfoByPathQuery())
                .ExecuteAsync();
            //.PublishAsync(new PublishPageCommand() { PageId = 2 });

            var isUnique = await _contentRepository
                .Users()
                .IsUsernameUnique(new IsUsernameUniqueQuery()
                {
                    UserAreaCode = CofoundryAdminUserArea.AreaCode,
                    Username = "******"
                })
                .ExecuteAsync();

            int userId;

            using (var scope = _contentRepository
                .Transactions()
                .CreateScope()
            {
                var adminRole = await _contentRepository
                    .Roles()
                    .GetByCode(SuperAdminRole.SuperAdminRoleCode)
                    .AsDetails()
                    .ExecuteAsync();

                userId = await _contentRepository
                    .WithElevatedPermissions()
                    .Users()
                    .AddAsync(new AddUserCommand()
                    {
                        Email = Guid.NewGuid().ToString() + "@cofoundry.org",
                        Password = "******",
                        UserAreaCode = CofoundryAdminUserArea.AreaCode,
                        RoleId = adminRole.RoleId
                    });

                //await _contentRepository
                //    .WithElevatedPermissions()
                //    .ImageAssets()
                //    .DeleteAsync(2);

                await _contentRepository
                    .CustomEntities()
                    .Definitions()
                    .GetByCode(BlogPostCustomEntityDefinition.DefinitionCode)
                    .AsSummary()
                    .ExecuteAsync();

                await _contentRepository
                    .CustomEntities()
                    .DataModelSchemas()
                    .GetByCustomEntityDefinitionCode(BlogPostCustomEntityDefinition.DefinitionCode)
                    .AsDetails()
                    .ExecuteAsync();

                await _contentRepository
                    .CustomEntities()
                    .GetById(1)
                    .AsRenderSummary()
                    .ExecuteAsync();

                var permissions = await _contentRepository
                    .Roles()
                    .Permissions()
                    .GetAll()
                    .AsIPermission()
                    .ExecuteAsync();

                var directoryTree = await _contentRepository
                    .PageDirectories()
                    .GetAll()
                    .AsTree()
                    .ExecuteAsync();

                var rewriteRules = await _contentRepository
                    .RewriteRules()
                    .GetAll()
                    .AsSummaries()
                    .ExecuteAsync();

                var blockTypes = await _contentRepository
                    .PageBlockTypes()
                    .GetAll()
                    .AsSummaries()
                    .ExecuteAsync();

                await scope.CompleteAsync();
            }

            await _contentRepository
                .WithElevatedPermissions()
                .Users()
                .DeleteUserAsync(userId);

            return View();
        }