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(); } }
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(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(); } }
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(); } }
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(); } }