From 63a82336c6ce44b10bf0661b82cfbb2a7ea427ae Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 14 Aug 2017 22:05:37 -0400 Subject: [PATCH] billing response for org --- .../Controllers/OrganizationsController.cs | 27 ++++++++++++------- .../Api/Response/OrganizationResponseModel.cs | 16 ++++++++++- src/Core/Services/IOrganizationService.cs | 1 + 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/Api/Controllers/OrganizationsController.cs b/src/Api/Controllers/OrganizationsController.cs index c485434d7..86ea51a6c 100644 --- a/src/Api/Controllers/OrganizationsController.cs +++ b/src/Api/Controllers/OrganizationsController.cs @@ -25,6 +25,7 @@ namespace Bit.Api.Controllers private readonly IOrganizationService _organizationService; private readonly IUserService _userService; private readonly CurrentContext _currentContext; + private readonly GlobalSettings _globalSettings; private readonly UserManager _userManager; public OrganizationsController( @@ -33,6 +34,7 @@ namespace Bit.Api.Controllers IOrganizationService organizationService, IUserService userService, CurrentContext currentContext, + GlobalSettings globalSettings, UserManager userManager) { _organizationRepository = organizationRepository; @@ -41,6 +43,7 @@ namespace Bit.Api.Controllers _userService = userService; _currentContext = currentContext; _userManager = userManager; + _globalSettings = globalSettings; } [HttpGet("{id}")] @@ -76,14 +79,20 @@ namespace Bit.Api.Controllers throw new NotFoundException(); } - var paymentService = new StripePaymentService(); - var billingInfo = await paymentService.GetBillingAsync(organization); - if(billingInfo == null) + if(!_globalSettings.SelfHosted && organization.Gateway != null) { - throw new NotFoundException(); + var paymentService = new StripePaymentService(); + var billingInfo = await paymentService.GetBillingAsync(organization); + if(billingInfo == null) + { + throw new NotFoundException(); + } + return new OrganizationBillingResponseModel(organization, billingInfo); + } + else + { + return new OrganizationBillingResponseModel(organization); } - - return new OrganizationBillingResponseModel(organization, billingInfo); } [HttpGet("")] @@ -208,7 +217,7 @@ namespace Bit.Api.Controllers await _organizationService.AdjustStorageAsync(orgIdGuid, model.StorageGbAdjustment.Value); } - + [HttpPost("{id}/verify-bank")] [SelfHosted(NotSelfHostedOnly = true)] public async Task PostVerifyBank(string id, [FromBody]OrganizationVerifyBankRequestModel model) @@ -307,13 +316,13 @@ namespace Bit.Api.Controllers throw new NotFoundException(); } - var license = await ApiHelpers.ReadJsonFileFromBody(HttpContext, model.License); + var license = await ApiHelpers.ReadJsonFileFromBody(HttpContext, model.License); if(license == null) { throw new BadRequestException("Invalid license"); } - await _organizationService.UpdateLicenseAsync(id, license); + await _organizationService.UpdateLicenseAsync(new Guid(id), license); } [HttpPost("{id}/import")] diff --git a/src/Core/Models/Api/Response/OrganizationResponseModel.cs b/src/Core/Models/Api/Response/OrganizationResponseModel.cs index 533fd2d5d..9d43d522b 100644 --- a/src/Core/Models/Api/Response/OrganizationResponseModel.cs +++ b/src/Core/Models/Api/Response/OrganizationResponseModel.cs @@ -51,10 +51,22 @@ namespace Bit.Core.Models.Api Subscription = billing.Subscription != null ? new BillingSubscription(billing.Subscription) : null; Charges = billing.Charges.Select(c => new BillingCharge(c)); UpcomingInvoice = billing.UpcomingInvoice != null ? new BillingInvoice(billing.UpcomingInvoice) : null; - StorageName = organization.Storage.HasValue ? + StorageName = organization.Storage.HasValue ? Utilities.CoreHelpers.ReadableBytesSize(organization.Storage.Value) : null; StorageGb = organization.Storage.HasValue ? Math.Round(organization.Storage.Value / 1073741824D) : 0; // 1 GB MaxStorageGb = organization.MaxStorageGb; + // License = ... + Expiration = DateTime.UtcNow.AddYears(1); + } + + public OrganizationBillingResponseModel(Organization organization) + : base(organization, "organizationBilling") + { + StorageName = organization.Storage.HasValue ? + Utilities.CoreHelpers.ReadableBytesSize(organization.Storage.Value) : null; + StorageGb = organization.Storage.HasValue ? Math.Round(organization.Storage.Value / 1073741824D, 2) : 0; // 1 GB + MaxStorageGb = organization.MaxStorageGb; + Expiration = organization.ExpirationDate; } public string StorageName { get; set; } @@ -64,5 +76,7 @@ namespace Bit.Core.Models.Api public BillingSubscription Subscription { get; set; } public BillingInvoice UpcomingInvoice { get; set; } public IEnumerable Charges { get; set; } + public OrganizationLicense License { get; set; } + public DateTime? Expiration { get; set; } } } diff --git a/src/Core/Services/IOrganizationService.cs b/src/Core/Services/IOrganizationService.cs index aede51690..68b78252d 100644 --- a/src/Core/Services/IOrganizationService.cs +++ b/src/Core/Services/IOrganizationService.cs @@ -19,6 +19,7 @@ namespace Bit.Core.Services Task VerifyBankAsync(Guid organizationId, int amount1, int amount2); Task> SignUpAsync(OrganizationSignup organizationSignup); Task> SignUpAsync(OrganizationLicense license, User owner, string ownerKey); + Task UpdateLicenseAsync(Guid organizationId, OrganizationLicense license); Task DeleteAsync(Organization organization); Task DisableAsync(Guid organizationId, DateTime? expirationDate); Task UpdateExpirationDateAsync(Guid organizationId, DateTime? expirationDate);