public async Task <UserActorError> RentVehicleAsync(RentInfo rentInfo, CancellationToken cancellationToken) { if (rentInfo == null) { throw new ArgumentNullException(nameof(rentInfo)); } if (!await IsValidInternalAsync(cancellationToken)) { return(UserActorError.UserNotValid); } UserActorError result = UserActorError.Ok; var currentInvoice = await this.GetInvoiceDataFromStateAsync(cancellationToken); if (currentInvoice == null) { var currentRentVehicle = await GetRentDataFromStateAsync(cancellationToken); if (currentRentVehicle == null) { currentRentVehicle = rentInfo.ToRentData(); await SetRentDataIntoStateAsync(currentRentVehicle, cancellationToken); await SetMailSendedIntoStateAsync(false, cancellationToken); var minutesForReminder = rentInfo.EndRent - DateTime.Now; await this.RegisterReminderAsync(EndTimeExpiredReminderName, null, minutesForReminder, TimeSpan.FromMinutes(1)); } else { result = UserActorError.VehicleAlreadyRented; } } else { result = UserActorError.InvoiceNotPaid; } return(result); }
public async Task <UserActorError> ReleaseVehicleAsync(CancellationToken cancellationToken) { if (!await IsValidInternalAsync(cancellationToken)) { return(UserActorError.UserNotValid); } UserActorError result = UserActorError.Ok; var currentRentVehicle = await GetRentDataFromStateAsync(cancellationToken); if (currentRentVehicle != null) { currentRentVehicle.EndRent = DateTime.Now; var amount = currentRentVehicle.CalculateCost(DateTime.Now); var invoice = await this.invoicesServiceProxy.GenerateInvoiceAsync(this.Id.ToString(), amount.Value, DateTime.Now, UriConstants.UserActorUri, cancellationToken); var localInvoice = new InvoiceData() { Amount = invoice.Amount, Number = invoice.InvoiceNumber, ReleaseDate = invoice.ReleaseDate, State = invoice.State == InvoicesServiceInterfaces.InvoiceState.Paid ? InvoiceState.Paid : InvoiceState.NotPaid, }; await SetInvoiceDataIntoStateAsync(localInvoice, cancellationToken); await SetRentDataIntoStateAsync(null, cancellationToken); } else { result = UserActorError.VehicleNotRented; } return(result); }