public AuthModule(IUserManagementApiClient client, ITokenizer tokenizer)
        {
            Get["/login"] = parameters =>
            {
                return View["Index"];
            };

            Get["/logout"] = parameters =>
            {
                return this.Logout("/");
            };

            Post["/login"] = parameters =>
            {
                var model = this.Bind<AuthModel>();

                var token = client.Post("", "/login", null, null, new[] { new KeyValuePair<string, string>("Username", model.Username), new KeyValuePair<string, string>("Password", model.Password) });

                var userIdentity = tokenizer.Detokenize(token, Context, new DefaultUserIdentityResolver());

                Context.CurrentUser = userIdentity;

                return token;
            };

            Get["/forgotPassword/{username}"] = _ =>
            {
                ViewBag.Message = client.Put("", UserManagementApiRoute.User.RequestResetPassword, new[] { new KeyValuePair<string, string>("username", _.username + "") }, null, null);
                return View["Login"];
            };
        }
 public RequestInfoModule(IUserManagementApiClient userManagementApi)
 {
     Get["/RequestInfo"] = _ =>
     {
         var token = Context.Request.Headers.Authorization.Split(' ')[1];
         var user = userManagementApi.Get<RequestInfoDto>(token, "/RequestInfo/{username}", new[] { new KeyValuePair<string, string>("username", Context.CurrentUser.UserName) });
         return user;
     };
 }
        public AuthorizationModule(IUserManagementApiClient userManagementApi)
        {
            Get[LiveAutoApiRoute.Authorization.GetChangePassword] = _ =>
            {
                var dto = new ChangePasswordDto(_.token, "");
                return View["index", dto];
            };

            Post[LiveAutoApiRoute.Authorization.PutChangePassword] = _ =>
            {
                var dto = this.Bind<ChangePasswordDto>();
                if ((dto.Password + "").Length < 5)
                {
                    dto.Message = "Password length must be at least 5 characters long";
                    return View["index", dto];                    
                }

                dto.Message = userManagementApi.Put("", "Users/ResetPassword/{token}", new[] { new KeyValuePair<string, string>("token", dto.Token + "") }, dto, null);
                if (!dto.Message.Contains("Password changed"))
                    dto.Message = "Error updating password";

                return View["index", dto];
            };
        }
 public GetDataPlatformClients(IUserManagementApiClient api, string token)
 {
     Api = api;
     Token = token;
 }
        public PackageModule(IPublishStorableCommands publisher,
            IRepository<Domain.Entities.Packages.Read.Package> readRepo,
            INEventStoreRepository<Package> writeRepo, IRepository<State> stateRepo, IEntryPoint entryPoint, IAdvancedBus eBus,
            IUserManagementApiClient userManagementApi, IBillingApiClient billingApiClient, IPublishIntegrationMessages integration)
        {

            Get[PackageBuilderApi.PackageRoutes.RequestIndex.ApiRoute] = _ =>
            {
                return _.showAll
                    ? Response.AsJson(
                        from p1 in readRepo
                        where p1.Version == (from p2 in readRepo where p2.PackageId == p1.PackageId && !p2.IsDeleted select p2.Version).Max()
                        select p1)
                    : Response.AsJson(readRepo.Where(x => !x.IsDeleted));
            };

            Get[PackageBuilderApi.PackageRoutes.RequestLookup.ApiRoute] = parameters =>
            {
                var filter = ((string)Context.Request.Query["q_word[]"].Value + "").Replace(",", " ");
                var pageIndex = 0;
                var pageSize = 0;
                int.TryParse(Context.Request.Query["page_num"].Value, out pageIndex);
                int.TryParse(Context.Request.Query["per_page"].Value, out pageSize);

                var industries = ((string)parameters.industryIds.Value + "").Split(',').Select(x => !string.IsNullOrEmpty(x) ? new Guid(x) : new Guid());

                var publishedPackages = from p1 in readRepo
                    where p1.Version == (from p2 in readRepo where p2.PackageId == p1.PackageId select p2.Version).Max()
                          && p1.State.Name == StateName.Published &&
                          p1.Industries.Any(ind => industries.Contains(ind.Id))
                          && (p1.Name + "").Trim().ToLower().StartsWith(filter)
                    select p1;

                var packages = new PagedList<Domain.Entities.Packages.Read.Package>(publishedPackages, pageIndex - 1, pageSize, x => !x.IsDeleted);

                return Response.AsJson(
                        new
                        {
                            result = packages,
                            cnt_whole = packages.RecordsFiltered
                        });
            };

            Get[PackageBuilderApi.PackageRoutes.RequestUpdate.ApiRoute] = parameters => Response.AsJson(
                new
                {
                    Response = new[] { Mapper.Map<IPackage, PackageDto>(writeRepo.GetById(parameters.id)) }
                });


            Post[PackageBuilderApi.PackageRoutes.Execute.ApiRoute] = parameters =>
            {
                var apiRequest = this.Bind<ApiRequestDto>();
                this.Info(() => StringExtensions.FormatWith("Package Execute Initialized for {0}, TimeStamp: {1}", apiRequest.RequestId, DateTime.UtcNow));

                this.Info(() => StringExtensions.FormatWith("Package Read Initialized, TimeStamp: {0}", DateTime.UtcNow));
                var package = writeRepo.GetById(apiRequest.PackageId, true);
                this.Info(() => StringExtensions.FormatWith("Package Read Completed, TimeStamp: {0}", DateTime.UtcNow));

                if (package == null)
                {
                    this.Error(() => StringExtensions.FormatWith("Package not found:", apiRequest.PackageId));
                    throw new LightstoneAutoException("Package could not be found");
                }

                this.Info(() => StringExtensions.FormatWith("PackageBuilder Auth to UserManagement Initialized for {0}, TimeStamp: {1}", apiRequest.RequestId, DateTime.UtcNow));
                var token = Context.Request.Headers.Authorization.Split(' ')[1];
                var accountNumber = userManagementApi.Get(token, "/CustomerClient/{id}", new[] { new KeyValuePair<string, string>("id", apiRequest.CustomerClientId.ToString()) }, null);

                this.Info(() => StringExtensions.FormatWith("PackageBuilder Auth to UserManagement Completed for {0}, TimeStamp: {1}", apiRequest.RequestId, DateTime.UtcNow));

                var responses = ((Package)package).Execute(entryPoint, apiRequest.UserId, Context.CurrentUser.UserName,
                    Context.CurrentUser.UserName, apiRequest.RequestId, accountNumber, apiRequest.ContractId, apiRequest.ContractVersion,
                    apiRequest.DeviceType, apiRequest.FromIpAddress, "", apiRequest.SystemType, apiRequest.RequestFields, (double)package.CostOfSale, (double)package.RecommendedSalePrice, apiRequest.HasConsent);

                // Filter responses for cleaner api payload
                this.Info(() => StringExtensions.FormatWith("Package Response Filter Cleanup Initialized for {0}, TimeStamp: {1}", apiRequest.RequestId, DateTime.UtcNow));
                var filteredResponse = new List<IProvideResponseDataProvider>
                {
                    new ResponseMeta(apiRequest.RequestId, responses.ResponseState())
                };

                filteredResponse.AddRange(Mapper.Map<IEnumerable<IDataProvider>, IEnumerable<IProvideResponseDataProvider>>(responses));
                this.Info(() => StringExtensions.FormatWith("Package Response Filter Cleanup Completed for {0}, TimeStamp: {1}", apiRequest.RequestId, DateTime.UtcNow));

                integration.SendToBus(new PackageResponseMessage(package.Id, apiRequest.UserId, apiRequest.ContractId, accountNumber,
                    filteredResponse.Any() ? filteredResponse.AsJsonString() : string.Empty, apiRequest.RequestId, Context != null ? Context.CurrentUser.UserName : "******"));

                this.Info(() => StringExtensions.FormatWith("Package Execute Completed for {0}, TimeStamp: {1}", apiRequest.RequestId, DateTime.UtcNow));

                return filteredResponse;
            };

            Post[PackageBuilderApi.PackageRoutes.CommitRequest.ApiRoute] = _ =>
            {
                var apiRequest = this.Bind<ApiCommitRequestDto>();

                var token = Context.Request.Headers.Authorization.Split(' ')[1];
                var request = billingApiClient.Get(token, "/Transactions/Request/{requestId}", new[]
                {
                    new KeyValuePair<string, string>("requestId", apiRequest.RequestId.ToString())
                }
                ,null);

                if (request.Contains("error")) return request;

                // RabbitMQ
                new TransactionBus(eBus).SendDynamic(Mapper.Map(apiRequest, new TransactionRequestMessage()));

                this.Info(() => StringExtensions.FormatWith("Updated TransactionRequest UserState: {0}", apiRequest.UserState));
                if (apiRequest.UserState == ApiCommitRequestUserState.Cancelled) return Response.AsJson(new { Success = "Request successfully cancelled by user" });
                if (apiRequest.UserState == ApiCommitRequestUserState.VehicleNotProvided) return Response.AsJson(new { Success = "Request successfully marked as VehicleNotProvided by user" });

                this.Info(() => StringExtensions.FormatWith("Package ExecuteWithCarId Initialized for {0}, TimeStamp: {1}", apiRequest.RequestId, DateTime.UtcNow));

                this.Info(() => StringExtensions.FormatWith("Package Read Initialized, TimeStamp: {0}", DateTime.UtcNow));
                var package = writeRepo.GetById(apiRequest.PackageId, true);
                this.Info(() => StringExtensions.FormatWith("Package Read Completed, TimeStamp: {0}", DateTime.UtcNow));

                if (package == null)
                {
                    this.Error(() => StringExtensions.FormatWith("Package not found:", apiRequest.PackageId));
                    throw new LightstoneAutoException("Package could not be found");
                }

                this.Info(() => StringExtensions.FormatWith("PackageBuilder Auth to UserManagement Initialized for {0}, TimeStamp: {1}", apiRequest.RequestId, DateTime.UtcNow));

                var accountNumber = userManagementApi.Get(token, "/CustomerClient/{id}", new[] { new KeyValuePair<string, string>("id", apiRequest.CustomerClientId.ToString()) }, null);

                this.Info(() => StringExtensions.FormatWith("PackageBuilder Auth to UserManagement Completed for {0}, TimeStamp: {1}", apiRequest.RequestId, DateTime.UtcNow));

                var responses = ((Package)package).ExecuteWithCarId(entryPoint, apiRequest.UserId, Context.CurrentUser.UserName,
                    Context.CurrentUser.UserName, apiRequest.RequestId, accountNumber, apiRequest.ContractId, apiRequest.ContractVersion,
                    apiRequest.DeviceType, apiRequest.FromIpAddress, "", apiRequest.SystemType, apiRequest.RequestFields, (double)package.CostOfSale, (double)package.RecommendedSalePrice, apiRequest.HasConsent);

                // Filter responses for cleaner api payload
                this.Info(() => StringExtensions.FormatWith("Package Response Filter Cleanup Initialized for {0}, TimeStamp: {1}", apiRequest.RequestId, DateTime.UtcNow));
                var filteredResponse = new List<IProvideResponseDataProvider>
                {
                    new ResponseMeta(apiRequest.RequestId, responses.ResponseState())
                };

                filteredResponse.AddRange(Mapper.Map<IEnumerable<IDataProvider>, IEnumerable<IProvideResponseDataProvider>>(responses));
                this.Info(() => StringExtensions.FormatWith("Package Response Filter Cleanup Completed for {0}, TimeStamp: {1}", apiRequest.RequestId, DateTime.UtcNow));

                integration.SendToBus(new PackageResponseMessage(package.Id, apiRequest.UserId, apiRequest.ContractId, accountNumber,
                    filteredResponse.Any() ? filteredResponse.AsJsonString() : string.Empty, apiRequest.RequestId, Context != null ? Context.CurrentUser.UserName : "******"));

                this.Info(() => StringExtensions.FormatWith("Package ExecuteWithCarId Completed for {0}, TimeStamp: {1}", apiRequest.RequestId, DateTime.UtcNow));

                return filteredResponse;
            };

            Post[PackageBuilderApi.PackageRoutes.ProcessCreate.ApiRoute] = parameters =>
            {
                var dto = this.Bind<PackageDto>();
                dto.Id = dto.Id == new Guid() ? Guid.NewGuid() : dto.Id; // Required for acceptance tests where we specify the Id

                var dProviders = Mapper.Map<IEnumerable<DataProviderDto>, IEnumerable<DataProviderOverride>>(dto.DataProviders);

                publisher.Publish(new CreatePackage(dto.Id, dto.Name, dto.Description, dto.CostOfSale,
                    dto.RecommendedSalePrice, dto.Notes, dto.PackageEventType, Mapper.Map<PackageDto, IEnumerable<Industry>>(dto), dto.State, dto.Owner, DateTime.UtcNow, null,
                    dProviders));

                ////RabbitMQ
                var metaEntity = Mapper.Map(dto, new PackageMessage());
                var advancedBus = new TransactionBus(eBus);
                advancedBus.SendDynamic(metaEntity);

                return Response.AsJson(new { msg = "Success" });
            };

            Put[PackageBuilderApi.PackageRoutes.ProcessUpdate.ApiRoute] = parameters =>
            {
                var dto = this.Bind<PackageDto>();
                var dProviders =
                    Mapper.Map<IEnumerable<DataProviderDto>, IEnumerable<DataProviderOverride>>(dto.DataProviders);

                publisher.Publish(new UpdatePackage(parameters.id, dto.Name, dto.Description, dto.CostOfSale,
                    dto.RecommendedSalePrice, dto.Notes, dto.PackageEventType, Mapper.Map<PackageDto, IEnumerable<Industry>>(dto), dto.State, dto.Version, dto.Owner,
                    dto.CreatedDate, DateTime.UtcNow, dProviders));

                ////RabbitMQ
                var metaEntity = Mapper.Map(dto, new PackageMessage());
                var advancedBus = new TransactionBus(eBus);
                advancedBus.SendDynamic(metaEntity);

                return Response.AsJson(new { msg = "Success, " + parameters.id + " edited" });
            };

            Put["/Packages/Clone/{id}/{cloneName}"] = parameters =>
            {
                var packageToClone = Mapper.Map<IPackage, PackageDto>(writeRepo.GetById(parameters.id));
                var dataProvidersToClone =
                    Mapper.Map<IEnumerable<DataProviderDto>, IEnumerable<DataProviderOverride>>(
                        packageToClone.DataProviders);
                var stateResolve = stateRepo.Where(x => x.Alias == "Draft")
                    .Select(y => new State(y.Id, y.Name, y.Alias));

                publisher.Publish(new CreatePackage(Guid.NewGuid(),
                    parameters.cloneName,
                    packageToClone.Description,
                    packageToClone.CostOfSale,
                    packageToClone.RecommendedSalePrice,
                    packageToClone.Notes,
                    packageToClone.PackageEventType,
                    packageToClone.Industries,
                    stateResolve.FirstOrDefault(),
                    packageToClone.Owner, DateTime.UtcNow, null,
                    dataProvidersToClone));

                return
                    Response.AsJson(
                        new
                        {
                            msg =
                                "Success, Package with ID: " + parameters.id + " has been cloned to package '" +
                                parameters.cloneName + "'"
                        });
            };

            Delete[PackageBuilderApi.PackageRoutes.ProcessDelete.ApiRoute] = parameters =>
            {
                this.RequiresAnyClaim(new[] { RoleType.Admin.ToString(), RoleType.ProductManager.ToString(), RoleType.Support.ToString() });

                publisher.Publish(new DeletePackage(new Guid(parameters.id)));

                return Response.AsJson(new { msg = "Success, " + parameters.id + " deleted" });
            };
        }
        public ApiModule(IHandleGettingConfiguration setup, IHandleSavingConfiguration save, IHandleGettingIntegrationClient client,
            IUserManagementApiClient api, IHandleGettingDataPlatformClient dataPlatform, IHandleGettingMetadata metadata)
        {
            this.RequiresAnyClaim(new[] { RoleType.Admin.ToString(), RoleType.ProductManager.ToString(), RoleType.Support.ToString() });

            Get["/integrations/for/api/push"] = _ =>
            {
                var model = PushConfiguration.Create();
                var token = Context.Request.Headers.Authorization.Any() ? Context.Request.Headers.Authorization.Split(' ')[1] : string.Empty;
                model.SetFrequency(setup, new GetFrequencyTypes());
                model.SetAuthentication(setup, new GetAuthenticationTypes());
                model.SetDataPlatformClients(dataPlatform, new GetDataPlatformClients(api, token));
                model.SetWeekdays(setup, new GetWeekdays());
                model.SetIntegrationClients(client, new GetIntegrationClients());
                return View["integrations/api/push", model];
            };

            Get["/integrations/for/api/push/edit/{id}/{clientId}"] = _ =>
                {
                    int id;
                    int clientId;

                    int.TryParse(_.Id, out id);
                    int.TryParse(_.clientId, out clientId);

                    if (id == 0 || clientId == 0)
                        return HttpStatusCode.NoResponse;

                    var token = Context.Request.Headers.Authorization.Any() ? Context.Request.Headers.Authorization.Split(' ')[1] : string.Empty;
                    var model = PushConfiguration.Existing(setup, new GetApiPushConfiguration(id, clientId));
                    model.SetFrequency(setup, new GetFrequencyTypes());
                    model.SetAuthentication(setup, new GetAuthenticationTypes());
                    model.SetDataPlatformClients(dataPlatform, new GetDataPlatformClients(api, token));
                    model.SetWeekdays(setup, new GetWeekdays());
                    model.SetIntegrationClients(client, new GetIntegrationClients());
                    return View["integrations/api/push", model];
                };

            Post["/integrations/for/api/push/save"] = _ =>
            {
                var configuration = this.Bind<PushConfiguration>();
                var token = Context.Request.Headers.Authorization.Any() ? Context.Request.Headers.Authorization.Split(' ')[1] : string.Empty;
                configuration.SetDataPlatformClients(dataPlatform, new GetDataPlatformClients(api, token));
                var command = new AddApiPushConfiguration(configuration);
                save.Handle(command);
                return Response.AsRedirect("/integrations/for/api/configurations");
            };

            Get["/integrations/for/api/pull"] = _ =>
            {
                var model = PullConfiguration.Create();
                model.SetAuthentication(setup);
                model.SetFrequency(setup);
                model.SetWeekdays(setup, new GetWeekdays());
                model.SetIntegrationClients(client, new GetIntegrationClients());
                return View["integrations/api/pull", model];
            };

            Get["/integrations/for/api/configurations"] = _ => View["integrations/api/configurations", ApiConfiguration.Get(setup, client)];

            Get["/integrations/for/api/metadata/push"] = _ =>
            {
                var command = new GetApiResponseMetadataCommand();
                metadata.Handle(command);
                return View["integrations/api/pushmetadata", command.Metadata];
            };
        }